[PATCH] KToggleToolbarAction, KWidgetAction

Tobias Koenig tokoe82 at yahoo.de
Mon Apr 15 06:32:41 BST 2002


On Mon, Apr 08, 2002 at 11:46:02PM -0700, John Firebaugh wrote:
> On Monday 08 April 2002 2:10, Tobias Koenig wrote:
> > On Mon, Apr 08, 2002 at 10:24:59PM +0200, Wilco Greven wrote:
> > > Maybe we should add a KToggleStatusbarAction too for completeness. It
> > > could be used in at least KMail and KGhostView.
> >
> > What is about KToggleMenuBarAction for konsole and maybe some other
> > applications?
> 
> Unfortunately KMenuBar and KStatusBar do not have a visibilityChanged() signal 
> like KToolBar, which is necessary for the action to update itself 
> automatically. I don't know why QWidget doesn't have a visibilityChanged() 
> signal... seems like an oversight to me.

Since QMenuBar and QStatusBar do not have this signal the attached patch add
it to KMenuBar and KStatusBar and provide the two new classes
KToggleMenuBarAction and KToggleStatusBarAction. Maybe thet TTs could add
the signals to QMenuBar and QStatusBar in the future so we can ommit this
workaround. Is it ok to commit?


Ciao,
Tobias
-- 
In a world without walls and fences who
needs Windows and Gates???
-------------- next part --------------
Index: kaction.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kaction.h,v
retrieving revision 1.147
diff -u -b -p -r1.147 kaction.h
--- kaction.h	2002/04/13 22:21:27	1.147
+++ kaction.h	2002/04/15 05:10:38
@@ -49,6 +49,8 @@ class KConfigBase;
 class KURL;
 class KInstance;
 class KToolBar;
+class KMenuBar;
+class KStatusBar;
 class KActionCollection;
 class KPopupMenu;
 
@@ -1631,7 +1633,76 @@ private:
     KToggleToolBarActionPrivate *d;
 };
 
+
+/**
+ * An action that takes care of everything associated with
+ * showing or hiding the menubar by a menu action. It will
+ * show or hide the menubar when activated, and check or
+ * uncheck itself if the menubar is manually shown or hidden.
+ */
+class KToggleMenuBarAction : public KToggleAction
+{
+    Q_OBJECT
+public:
+    /**
+     * Create a KToggleMenuBarAction that manages the menubar.
+     */
+    KToggleMenuBarAction(const QString& text, KActionCollection* parent = 0,
+		const char* name = 0);
+    virtual ~KToggleMenuBarAction();
+
+    virtual int plug( QWidget*, int index = -1 );
+
+public slots:
+    virtual void setChecked( bool );
+
+private:
+    QGuardedPtr<KMenuBar>   m_menuBar;
+
+protected:
+    virtual void virtual_hook( int id, void* data );
+
+private:
+    class KToggleMenuBarActionPrivate;
+    KToggleMenuBarActionPrivate *d;
+};
+
+
 /**
+ * An action that takes care of everything associated with
+ * showing or hiding the statusbar by a menu action. It will
+ * show or hide the statusbar when activated, and check or
+ * uncheck itself if the statusbar is manually shown or hidden.
+ */
+class KToggleStatusBarAction : public KToggleAction
+{
+    Q_OBJECT
+public:
+    /**
+     * Create a KToggleStatusBarAction that manages the statusbar.
+     */
+    KToggleStatusBarAction(const QString& text, KActionCollection* parent = 0,
+		const char* name = 0);
+    virtual ~KToggleStatusBarAction();
+
+    virtual int plug( QWidget*, int index = -1 );
+
+public slots:
+    virtual void setChecked( bool );
+
+private:
+    QGuardedPtr<KStatusBar>   m_statusBar;
+
+protected:
+    virtual void virtual_hook( int id, void* data );
+
+private:
+    class KToggleStatusBarActionPrivate;
+    KToggleStatusBarActionPrivate *d;
+};
+
+
+/**
  * An action that automatically embeds a widget into a
  * toolbar.
  */
@@ -1676,6 +1747,7 @@ private:
     class KWidgetActionPrivate;
     KWidgetActionPrivate *d;
 };
+
 
 class KActionSeparator : public KAction
 {
Index: kaction.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kaction.cpp,v
retrieving revision 1.256
diff -u -b -p -r1.256 kaction.cpp
--- kaction.cpp	2002/04/13 22:21:27	1.256
+++ kaction.cpp	2002/04/15 05:10:43
@@ -55,6 +55,7 @@
 #include <kmainwindow.h>
 #include <kmenubar.h>
 #include <kpopupmenu.h>
+#include <kstatusbar.h>
 #include <kstdaccel.h>
 #include <ktoolbar.h>
 #include <ktoolbarbutton.h>
@@ -123,6 +124,16 @@ static QValueList<int> get_standard_font
     return fontDataBase->standardSizes();
 }
 
+static KMainWindow* findKMainWindow(QWidget* wdg)
+{
+	QWidget * tl = wdg;
+	QWidget * n;
+	while ( !tl->isDialog() && ( n = tl->parentWidget() ) ) // lookup parent and store
+		tl = n;
+
+	return dynamic_cast<KMainWindow *>(tl);
+}
+
 int KAction::getToolButtonID()
 {
     static int toolbutton_no = -2;
@@ -673,17 +684,11 @@ void KAction::unplugAccel()
 
 void KAction::plugMainWindowAccel( QWidget *w )
 {
-  // Note: topLevelWidget() stops too early, we can't use it.
-  QWidget * tl = w;
-  QWidget * n;
-  while ( !tl->isDialog() && ( n = tl->parentWidget() ) ) // lookup parent and store
-    tl = n;
-
-  KMainWindow * mw = dynamic_cast<KMainWindow *>(tl); // try to see if it's a kmainwindow
+  KMainWindow * mw = findKMainWindow(w); // try to see if it's a kmainwindow
   if (mw)
     plugAccel( mw->accel() );
   else
-    kdDebug(129) << "KAction::plugMainWindowAccel: Toplevel widget isn't a KMainWindow, can't plug accel. " << tl << endl;
+    kdDebug(129) << "KAction::plugMainWindowAccel: Toplevel widget isn't a KMainWindow, can't plug accel. " << w << endl;
 }
 
 void KAction::setEnabled(bool enable)
@@ -2624,13 +2629,7 @@ KToggleToolBarAction::~KToggleToolBarAct
 
 int KToggleToolBarAction::plug( QWidget* w, int index )
 {
-  // Note: topLevelWidget() stops too early, we can't use it.
-  QWidget * tl = w;
-  QWidget * n;
-  while ( !tl->isDialog() && ( n = tl->parentWidget() ) ) // lookup parent and store
-    tl = n;
-
-  KMainWindow * mw = dynamic_cast<KMainWindow *>(tl); // try to see if it's a kmainwindow
+  KMainWindow * mw = findKMainWindow(w);
 
   if( mw && (m_toolBar = mw->toolBar( m_toolBarName )) ) {
     setChecked( m_toolBar->isVisible() );
@@ -2658,6 +2657,88 @@ void KToggleToolBarAction::setChecked( b
 
 ////////
 
+KToggleMenuBarAction::KToggleMenuBarAction(const QString& text,
+	KActionCollection* parent, const char* name)
+    : KToggleAction( text, KShortcut(), parent, name )
+    , m_menuBar( 0L )
+{
+}
+
+KToggleMenuBarAction::~KToggleMenuBarAction()
+{
+}
+
+int KToggleMenuBarAction::plug( QWidget* w, int index )
+{
+    KMainWindow * mw = findKMainWindow(w);
+
+    if( mw && (m_menuBar = mw->menuBar()) ) {
+	setChecked( m_menuBar->isVisible() );
+	connect( m_menuBar, SIGNAL(visibilityChanged(bool)), this, SLOT(setChecked(bool)) );
+	connect( m_menuBar, SIGNAL(visibilityChanged(bool)), this, SIGNAL(toggled(bool)) );
+    } else {
+	setEnabled( false );
+    }
+
+    return KToggleAction::plug( w, index );
+}
+
+void KToggleMenuBarAction::setChecked( bool c )
+{
+    if( m_menuBar && c != m_menuBar->isVisible() ) {
+	if( c ) {
+	    m_menuBar->show();
+	} else {
+	    m_menuBar->hide();
+	}
+    }
+
+    KToggleAction::setChecked( c );
+}
+
+////////
+
+KToggleStatusBarAction::KToggleStatusBarAction(const QString& text,
+	KActionCollection* parent, const char* name)
+    : KToggleAction( text, KShortcut(), parent, name )
+    , m_statusBar( 0L )
+{
+}
+
+KToggleStatusBarAction::~KToggleStatusBarAction()
+{
+}
+
+int KToggleStatusBarAction::plug( QWidget* w, int index )
+{
+    KMainWindow * mw = findKMainWindow(w);
+
+    if( mw && (m_statusBar = mw->statusBar()) ) {
+	setChecked( m_statusBar->isVisible() );
+	connect( m_statusBar, SIGNAL(visibilityChanged(bool)), this, SLOT(setChecked(bool)) );
+	connect( m_statusBar, SIGNAL(visibilityChanged(bool)), this, SIGNAL(toggled(bool)) );
+    } else {
+	setEnabled( false );
+    }
+
+    return KToggleAction::plug( w, index );
+}
+
+void KToggleStatusBarAction::setChecked( bool c )
+{
+    if( m_statusBar && c != m_statusBar->isVisible() ) {
+	if( c ) {
+	    m_statusBar->show();
+	} else {
+	    m_statusBar->hide();
+	}
+    }
+
+    KToggleAction::setChecked( c );
+}
+
+////////
+
 KWidgetAction::KWidgetAction( QWidget* widget,
     const QString& text, const KShortcut& cut,
     const QObject* receiver, const char* slot,
@@ -2879,16 +2960,11 @@ void KActionCollection::setWidget( QWidg
 
 void KActionCollection::findMainWindow( QWidget *w )
 {
-  // Note: topLevelWidget() stops too early, we can't use it.
-  QWidget * tl = w;
-  while ( tl->parentWidget() ) // lookup parent and store
-    tl = tl->parentWidget();
-
-  KMainWindow * mw = dynamic_cast<KMainWindow *>(tl); // try to see if it's a kmainwindow
+  KMainWindow * mw = findKMainWindow(w);
   if (mw)
     d->m_mainwindow = mw;
   else
-    kdDebug(129) << "KAction::plugMainWindowAccel: Toplevel widget isn't a KMainWindow, can't plug accel. " << tl << endl;
+    kdDebug(129) << "KAction::plugMainWindowAccel: Toplevel widget isn't a KMainWindow, can't plug accel. " << w << endl;
 }
 
 void KActionCollection::_insert( KAction* action )
@@ -3423,6 +3499,12 @@ void KActionMenu::virtual_hook( int id, 
 
 void KToolBarPopupAction::virtual_hook( int id, void* data )
 { KAction::virtual_hook( id, data ); }
+
+void KToggleMenuBarAction::virtual_hook( int id, void* data )
+{ KToggleAction::virtual_hook( id, data ); }
+
+void KToggleStatusBarAction::virtual_hook( int id, void* data )
+{ KToggleAction::virtual_hook( id, data ); }
 
 void KToggleToolBarAction::virtual_hook( int id, void* data )
 { KToggleAction::virtual_hook( id, data ); }
Index: kmenubar.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kmenubar.h,v
retrieving revision 1.51
diff -u -b -p -r1.51 kmenubar.h
--- kmenubar.h	2002/03/20 07:45:32	1.51
+++ kmenubar.h	2002/04/15 05:10:43
@@ -75,11 +75,16 @@ public:
     virtual void        setGeometry( int x, int y, int w, int h );
 
     virtual void show();
+
+signals:
+    void visibilityChanged( bool );
+
 protected slots:
     void slotReadConfig();
 
 protected:
     void showEvent( QShowEvent* );
+    void hideEvent( QHideEvent* );
     bool eventFilter(QObject *, QEvent *);
 
 protected:
Index: kmenubar.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kmenubar.cpp,v
retrieving revision 1.157
diff -u -b -p -r1.157 kmenubar.cpp
--- kmenubar.cpp	2002/03/20 07:45:32	1.157
+++ kmenubar.cpp	2002/04/15 05:10:43
@@ -174,6 +174,15 @@ void KMenuBar::showEvent( QShowEvent *e 
 #endif
     }
     QMenuBar::showEvent(e);
+
+    emit visibilityChanged( true );
+}
+
+void KMenuBar::hideEvent( QHideEvent *e )
+{
+    QMenuBar::hideEvent(e);
+
+    emit visibilityChanged( false );
 }
 
 void KMenuBar::setGeometry( int x, int y, int w, int h )
Index: kstatusbar.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kstatusbar.h,v
retrieving revision 1.34
diff -u -b -p -r1.34 kstatusbar.h
--- kstatusbar.h	2002/03/04 00:51:51	1.34
+++ kstatusbar.h	2002/04/15 05:10:44
@@ -178,6 +178,15 @@ signals:
    */
   void released( int );
 
+  /**
+   *  Emitted when member function show() or hide() are called.
+   */
+  void visibilityChanged( bool );
+
+protected:
+  void showEvent( QShowEvent* );
+  void hideEvent( QHideEvent* );
+
 private:
   QIntDict<KStatusBarLabel> items;
   class KStatusBarPrivate* d;
Index: kstatusbar.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kstatusbar.cpp,v
retrieving revision 1.42
diff -u -b -p -r1.42 kstatusbar.cpp
--- kstatusbar.cpp	2001/10/12 10:13:53	1.42
+++ kstatusbar.cpp	2002/04/15 05:10:44
@@ -136,6 +136,20 @@ void KStatusBar::setItemFixed(int id, in
     kdDebug() << "KStatusBar::setItemFixed: bad item id: " << id << endl;
 }
 
+void KStatusBar::showEvent(QShowEvent* e)
+{
+  emit visibilityChanged(true);
+
+  QStatusBar::showEvent(e);
+}
+
+void KStatusBar::hideEvent(QHideEvent* e)
+{
+  emit visibilityChanged(false);
+
+  QStatusBar::hideEvent(e);
+}
+
 #include "kstatusbar.moc"
 
 //Eh!!!


More information about the kde-core-devel mailing list