← Back to team overview

ayatana-commits team mailing list archive

[Branch ~agateau/plasma-widget-message-indicator/trunk] Rev 123: Show action items before indicators.

 

------------------------------------------------------------
revno: 123
committer: Aurelien Gateau <aurelien.gateau@xxxxxxxxxxxxx>
branch nick: plasma-widget-message-indicator
timestamp: Mon 2010-03-08 11:51:12 +0100
message:
  Show action items before indicators.
modified:
  src/listenermodel.cpp


--
lp:plasma-widget-message-indicator
https://code.launchpad.net/~agateau/plasma-widget-message-indicator/trunk

Your team ayatana-commits is subscribed to branch lp:plasma-widget-message-indicator.
To unsubscribe from this branch go to https://code.launchpad.net/~agateau/plasma-widget-message-indicator/trunk/+edit-subscription.
=== modified file 'src/listenermodel.cpp'
--- src/listenermodel.cpp	2010-03-08 10:29:54 +0000
+++ src/listenermodel.cpp	2010-03-08 10:51:12 +0000
@@ -12,7 +12,9 @@
 #include "listenermodel.h"
 
 // Qt
+#include <QActionEvent>
 #include <QDBusInterface>
+#include <QEvent>
 #include <QMenu>
 #include <QRegExp>
 #include <QTime>
@@ -43,22 +45,118 @@
     OutdatedKeyListRole
 };
 
-class ServerItem : public QStandardItem
+class ActionItem : public QStandardItem
+{
+public:
+    ActionItem(QAction* action)
+    : mAction(action)
+    {
+        update();
+    }
+
+    void update()
+    {
+        setIcon(mAction->icon());
+        setText(mAction->iconText());
+    }
+
+    QAction* action() const
+    {
+        return mAction;
+    }
+
+private:
+    QAction* mAction;
+};
+
+
+class ServerItem : public QStandardItem, protected QObject
 {
 public:
     ServerItem()
     : mMenuImporter(0)
+    , mNextActionItemRow(0)
     {}
 
     void setMenuImporter(DBusMenuImporter* importer)
     {
         mMenuImporter = importer;
+        mMenuImporter->menu()->installEventFilter(this);
+    }
+
+protected:
+    virtual bool eventFilter(QObject*, QEvent* event)
+    {
+        QActionEvent *actionEvent = 0;
+        switch (event->type()) {
+        case QEvent::ActionAdded:
+        case QEvent::ActionChanged:
+        case QEvent::ActionRemoved:
+            actionEvent = static_cast<QActionEvent *>(event);
+            break;
+        default:
+            return false;
+        }
+        switch (event->type()) {
+        case QEvent::ActionAdded:
+            addAction(actionEvent->action());
+            break;
+        case QEvent::ActionChanged:
+            updateAction(actionEvent->action());
+            break;
+        case QEvent::ActionRemoved:
+            removeAction(actionEvent->action());
+            break;
+        default:
+            break;
+        }
+        return false;
+    }
+
+private:
+    void addAction(QAction* action)
+    {
+        insertRow(mNextActionItemRow, new ActionItem(action));
+        mNextActionItemRow++;
+    }
+
+    void updateAction(QAction* action)
+    {
+        ActionItem* item = findActionItemForAction(action);
+        if (item) {
+            item->update();
+        } else {
+            kWarning() << "No item for action" << action->text();
+        }
+    }
+
+    void removeAction(QAction* action)
+    {
+        ActionItem* item = findActionItemForAction(action);
+        if (item) {
+            delete item;
+        } else {
+            kWarning() << "No item for action" << action->text();
+        }
+    }
+
+    ActionItem* findActionItemForAction(QAction* action) const
+    {
+        for (int row=0; row < mNextActionItemRow; ++row) {
+            ActionItem* item = static_cast<ActionItem*>(child(row));
+            if (item->action() == action) {
+                return item;
+            }
+        }
+        return 0;
     }
 
 public:
     DBusMenuImporter* mMenuImporter;
+    int mNextActionItemRow;
 };
 
+
 typedef QPair<QIndicate::Listener::Server*, QIndicate::Listener::Indicator*> ServerIndicatorPair;
 typedef QHash<ServerIndicatorPair, QStandardItem*> ItemForIndicatorHash;
 typedef QSet<QIndicate::Listener::Indicator*> IndicatorSet;