[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    ksystemtray context menu actions
From:       "Aaron J. Seigo" <aseigo () olympusproject ! org>
Date:       2002-09-26 19:47:14
[Download RAW message or body]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi...

ksystemtray puts two items in its menu: quit  and restore/minimize ... 
however, since they are manually inserted the only way for subclasses to 
access these items is to perform ugly hacks like iterating through the menu 
and comparing the text used... kmix was doing this, for instance, to find the 
quit item.

attached is a patch that changes ksystemtray to use kactions and keep them in 
an action collection. this allows easy and standard access to the items it 
adds and allows for future flexibility.

i'd like to commit this for 3.1 to address a usability issue identified with 
the kmix system tray icon: users don't connect "restore" with "open up the 
mixer window". attached is a patch that shows how, with kactions, this would 
be easily addressed.

is this ok to commit?

- -- 
Aaron J. Seigo
GPG Fingerprint: 8B8B 2209 0C6F 7C47 B1EA  EE75 D6B7 2EB1 A7F1 DB43

"Everything should be made as simple as possible, but not simpler"
    - Albert Einstein
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9k2RD1rcusafx20MRAowNAKCCyDwSHb4N3Uyt5xmeqjYL0s9rMACfWCvm
ZDVi9bfdjfQO3kz9zCV3y4Y=
=DiEO
-----END PGP SIGNATURE-----

["ksystray_actions.diff" (text/x-diff)]

? kmessagebox.patch
? notifyStdAction.diff
? ksystray_actions.diff
? kaction_isPlugged_widget.diff
Index: ksystemtray.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/ksystemtray.cpp,v
retrieving revision 1.21
diff -u -3 -d -p -r1.21 ksystemtray.cpp
--- ksystemtray.cpp	2002/06/24 08:11:18	1.21
+++ ksystemtray.cpp	2002/09/26 19:07:38
@@ -17,6 +17,9 @@
     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
 */
+
+#include "kaction.h"
+#include "kshortcut.h"
 #include "ksystemtray.h"
 #include "kpopupmenu.h"
 #include "kapplication.h"
@@ -39,9 +42,28 @@ const int XFocusIn = FocusIn;
 extern Time qt_x_time;
 #endif
 
+class KSystemTrayPrivate
+{
+public:
+    KSystemTrayPrivate()
+    {
+        actionCollection = 0;
+    }
+
+    ~KSystemTrayPrivate()
+    {
+        delete actionCollection;
+    }
+
+    KActionCollection* actionCollection;
+};
+
 KSystemTray::KSystemTray( QWidget* parent, const char* name )
     : QLabel( parent, name, WType_TopLevel )
 {
+    d = new KSystemTrayPrivate;
+    d->actionCollection = new KActionCollection(this);
+
 #ifndef Q_WS_QWS
     // FIXME(E): Talk with QWS
     KWin::setSystemTrayWindowFor( winId(), parent?parent->topLevelWidget()->winId(): qt_xrootwin() );
@@ -51,11 +73,24 @@ KSystemTray::KSystemTray( QWidget* paren
     menu = new KPopupMenu( this );
     menu->insertTitle( kapp->miniIcon(), kapp->caption() );
     move( -1000, -1000 );
+    KAction* quitAction = KStdAction::quit(this, SIGNAL(quitSelected()), d->actionCollection);
+
+    if (parentWidget())
+    {
+        connect(quitAction, SIGNAL(activated()), parentWidget(), SLOT(close()));
+        new KAction(i18n("Minimize"), KShortcut(), 
+                    this, SLOT( toggleMinimizeRestore() ), 
+                    d->actionCollection, "minimizeRestore");
+    }
+    else
+    {
+        connect(quitAction, SIGNAL(activated()), qApp, SLOT(closeAllWindows()));
+    }
 }
 
 KSystemTray::~KSystemTray()
 {
-
+    delete d;
 }
 
 
@@ -63,16 +98,15 @@ void KSystemTray::showEvent( QShowEvent 
 {
     if ( !hasQuit ) {
 	menu->insertSeparator();
-	int quitID;
-	if ( parentWidget() ) {
-	    minimizeRestoreId = menu->insertItem(i18n("Minimize"), this, SLOT( toggleMinimizeRestore() ) );
-	    quitID=menu->insertItem(SmallIcon("exit"), i18n("&Quit"), parentWidget(), SLOT(close() ) );
-	}
-	else {
-	    minimizeRestoreId = -1;
-	    quitID=menu->insertItem(SmallIcon("exit"), i18n("&Quit"), qApp, SLOT(closeAllWindows() ) );
-	}
-	menu->connectItem(quitID,this,SIGNAL(quitSelected()));
+        KAction* action = d->actionCollection->action("minimizeRestore");
+
+        if (action)
+        {
+            action->plug(menu);
+        }
+
+        action = d->actionCollection->action(KStdAction::name(KStdAction::Quit));
+        action->plug(menu);
 	hasQuit = 1;
     }
 }
@@ -116,10 +150,11 @@ void KSystemTray::mousePressEvent( QMous
 	// fall through
     case RightButton:
 	if ( parentWidget() ) {
+            KAction* action = d->actionCollection->action("minimizeRestore");
 	    if ( parentWidget()->isVisible() )
-		menu->changeItem( minimizeRestoreId, i18n("Minimize") );
+		action->setText( i18n("Minimize") );
 	    else
-		menu->changeItem( minimizeRestoreId, i18n("Restore") );
+		action->setText( i18n("Restore") );
 	}
 	contextMenuAboutToShow( menu );
 	menu->popup( e->globalPos() );
@@ -160,6 +195,11 @@ void KSystemTray::toggleMinimizeRestore(
     } else {
 	parentWidget()->hide();
     }
+}
+
+KAction* KSystemTray::action(const char* name)
+{
+    return d->actionCollection->action(name);
 }
 
 void KSystemTray::virtual_hook( int, void* )
Index: ksystemtray.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/ksystemtray.h,v
retrieving revision 1.6
diff -u -3 -d -p -r1.6 ksystemtray.h
--- ksystemtray.h	2002/06/24 08:11:18	1.6
+++ ksystemtray.h	2002/09/26 19:07:38
@@ -20,6 +20,7 @@
 
 #include <qlabel.h>
 
+class KAction;
 class KPopupMenu;
 class KSystemTrayPrivate;
 
@@ -115,6 +116,13 @@ protected:
        context menu becomes visible.
      */
     virtual void contextMenuAboutToShow( KPopupMenu* menu );
+
+
+    /**
+       Easy access to the actions in the context menu
+       Currently includes KStdAction::quit and minimizeRestore
+    */
+    KAction* action(const char* name);
 
     /**
        Reimplemented for internal reasons.

["kmix_systrayactions.diff" (text/x-diff)]

? ann
? kmix_systrayactions.diff
Index: kmixdockwidget.cpp
===================================================================
RCS file: /home/kde/kdemultimedia/kmix/kmixdockwidget.cpp,v
retrieving revision 1.21
diff -u -3 -d -p -r1.21 kmixdockwidget.cpp
--- kmixdockwidget.cpp	2002/09/26 17:49:30	1.21
+++ kmixdockwidget.cpp	2002/09/26 19:07:59
@@ -20,6 +20,7 @@
  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
+#include <kaction.h>
 #include <klocale.h>
 #include <kapplication.h>
 #include <kpanelapplet.h>
@@ -178,6 +179,18 @@ void KMixDockWidget::wheelEvent(QWheelEv
 
 void KMixDockWidget::contextMenuAboutToShow( KPopupMenu* menu )
 {
+    KAction* showAction = action("minimizeRestore");
+    if ( parentWidget() && showAction ) 
+    {
+        if ( parentWidget()->isVisible() )
+        {
+            showAction->setText( i18n("Hide Mixer Window") );
+        }
+        else
+        {
+            showAction->setText( i18n("Show Mixer Window") );
+        }
+    }
 }
 
 #include "kmixdockwidget.moc"


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic