← Back to team overview

ayatana-commits team mailing list archive

[Branch ~agateau/plasma-widget-message-indicator/trunk] Rev 125: Refactoring: moving from custom data to custom items

 

------------------------------------------------------------
revno: 125
committer: Aurelien Gateau <aurelien.gateau@xxxxxxxxxxxxx>
branch nick: plasma-widget-message-indicator
timestamp: Mon 2010-03-08 14:41:50 +0100
message:
  Refactoring: moving from custom data to custom items
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:51:12 +0000
+++ src/listenermodel.cpp	2010-03-08 13:41:50 +0000
@@ -40,11 +40,20 @@
 
 enum
 {
-    ServerRole = Qt::UserRole+1,
-    IndicatorRole,
-    OutdatedKeyListRole
-};
-
+    OutdatedKeyListRole = Qt::UserRole+1
+};
+
+enum
+{
+    ActionItemType = QStandardItem::UserType,
+    ServerItemType,
+    IndicatorItemType
+};
+
+/**
+ * An item representing an action from a DBusMenu, for indicate servers which
+ * provide a "menu".
+ */
 class ActionItem : public QStandardItem
 {
 public:
@@ -65,25 +74,45 @@
         return mAction;
     }
 
+    int type() const
+    {
+        return ActionItemType;
+    }
+
 private:
     QAction* mAction;
 };
 
 
+/**
+ * An item representing a indicate server.
+ */
 class ServerItem : public QStandardItem, protected QObject
 {
 public:
-    ServerItem()
-    : mMenuImporter(0)
+    ServerItem(QIndicate::Listener::Server* server)
+    : mServer(server)
+    , mMenuImporter(0)
     , mNextActionItemRow(0)
     {}
 
     void setMenuImporter(DBusMenuImporter* importer)
     {
         mMenuImporter = importer;
+        mMenuImporter->setParent(this);
         mMenuImporter->menu()->installEventFilter(this);
     }
 
+    int type() const
+    {
+        return ServerItemType;
+    }
+
+    QIndicate::Listener::Server* server() const
+    {
+        return mServer;
+    }
+
 protected:
     virtual bool eventFilter(QObject*, QEvent* event)
     {
@@ -152,11 +181,37 @@
     }
 
 public:
+    QIndicate::Listener::Server* mServer;
     DBusMenuImporter* mMenuImporter;
     int mNextActionItemRow;
 };
 
 
+/**
+ * An item representing an indicator of an indicate server
+ */
+class IndicatorItem : public QStandardItem
+{
+public:
+    IndicatorItem(QIndicate::Listener::Indicator* indicator)
+    : mIndicator(indicator)
+    {}
+
+    QIndicate::Listener::Indicator* indicator() const
+    {
+        return mIndicator;
+    }
+
+    int type() const
+    {
+        return IndicatorItemType;
+    }
+
+private:
+    QIndicate::Listener::Indicator* mIndicator;
+};
+
+
 typedef QPair<QIndicate::Listener::Server*, QIndicate::Listener::Indicator*> ServerIndicatorPair;
 typedef QHash<ServerIndicatorPair, QStandardItem*> ItemForIndicatorHash;
 typedef QSet<QIndicate::Listener::Indicator*> IndicatorSet;
@@ -177,8 +232,10 @@
         QStringList outdatedKeyList = item->data(OutdatedKeyListRole).toStringList();
         item->setData(QVariant(QStringList()), OutdatedKeyListRole);
         Q_FOREACH(const QString& key, outdatedKeyList) {
-            QIndicate::Listener::Server* server = item->parent()->data(ServerRole).value<QIndicate::Listener::Server*>();
-            QIndicate::Listener::Indicator* indicator = item->data(IndicatorRole).value<QIndicate::Listener::Indicator*>();
+            IndicatorItem* indicatorItem = static_cast<IndicatorItem*>(item);
+            ServerItem* serverItem = static_cast<ServerItem*>(item->parent());
+            QIndicate::Listener::Server* server = serverItem->server();
+            QIndicate::Listener::Indicator* indicator = indicatorItem->indicator();
 
             mListener->getIndicatorPropertyAsVariant(server, indicator, key,
                                                      q, SLOT(slotPropertyReceived(
@@ -190,12 +247,15 @@
         }
     }
 
-    void removeIndicatorItem(QStandardItem* item) {
-        QIndicate::Listener::Server* server = item->parent()->data(ServerRole).value<QIndicate::Listener::Server*>();
-        QIndicate::Listener::Indicator* indicator = item->data(IndicatorRole).value<QIndicate::Listener::Indicator*>();
+    void removeIndicatorItem(QStandardItem* item)
+    {
+        IndicatorItem* indicatorItem = static_cast<IndicatorItem*>(item);
+        ServerItem* serverItem = static_cast<ServerItem*>(item->parent());
+        QIndicate::Listener::Server* server = serverItem->server();
+        QIndicate::Listener::Indicator* indicator = indicatorItem->indicator();
         mItemForIndicator.remove(ServerIndicatorPair(server, indicator));
-        QStandardItem* parent = item->parent();
-        parent->removeRow(item->row());
+
+        serverItem->removeRow(item->row());
     }
 };
 
@@ -259,8 +319,7 @@
         return;
     }
 
-    ServerItem* item = new ServerItem;
-    item->setData(QVariant::fromValue(server), ServerRole);
+    ServerItem* item = new ServerItem(server);
     item->setData(type, ServerTypeRole);
     d->mItemForServer.insert(server, item);
     appendRow(item);
@@ -355,7 +414,7 @@
     }
 
     QDBusInterface* iface = new QDBusInterface(dbusName, objectPath);
-    DBusMenuImporter* importer = new MyDBusMenuImporter(iface, this);
+    DBusMenuImporter* importer = new MyDBusMenuImporter(iface);
     serverItem->setMenuImporter(importer);
 }
 
@@ -410,8 +469,7 @@
         return;
     }
 
-    QStandardItem* indicatorItem = new QStandardItem();
-    indicatorItem->setData(QVariant::fromValue(indicator), IndicatorRole);
+    IndicatorItem* indicatorItem = new IndicatorItem(indicator);
     indicatorItem->setData(outdatedKeyList, OutdatedKeyListRole);
     d->mItemForIndicator.insert(ServerIndicatorPair(server, indicator), indicatorItem);
     serverItem->appendRow(indicatorItem);
@@ -473,11 +531,18 @@
         return;
     }
 
-    if (index.parent().isValid()) {
-        *indicator = index.data(IndicatorRole).value<QIndicate::Listener::Indicator*>();
-        *server = index.parent().data(ServerRole).value<QIndicate::Listener::Server*>();
-    } else {
-        *server = index.data(ServerRole).value<QIndicate::Listener::Server*>();
+    QStandardItem* item = itemFromIndex(index);
+    switch (item->type()) {
+    case IndicatorItemType:
+        *indicator = static_cast<IndicatorItem*>(item)->indicator();
+        *server = static_cast<ServerItem*>(item->parent())->server();
+        break;
+    case ServerItemType:
+        *server = static_cast<ServerItem*>(item)->server();
+        break;
+    case ActionItemType:
+        *server = static_cast<ServerItem*>(item->parent())->server();
+        break;
     }
 }
 
@@ -519,6 +584,12 @@
 
 void ListenerModel::activate(const QModelIndex& index)
 {
+    QStandardItem* item = itemFromIndex(index);
+    if (item->type() == ActionItemType) {
+        static_cast<ActionItem*>(item)->action()->trigger();
+        return;
+    }
+
     QIndicate::Listener::Server* server = 0;
     QIndicate::Listener::Indicator* indicator = 0;