diff options
author | Lars Uebernickel <lars.uebernickel@canonical.com> | 2013-09-03 12:02:47 +0200 |
---|---|---|
committer | Lars Uebernickel <lars.uebernickel@canonical.com> | 2013-09-03 12:02:47 +0200 |
commit | 936588550f0f91ee95386931a5297b46c3ba9c14 (patch) | |
tree | 10a2bda08b46cd472be4e0201ab3b6dd720009ea | |
parent | 2641328dca5a7db154e5dbca7d9904d3c321974e (diff) | |
download | qmenumodel-936588550f0f91ee95386931a5297b46c3ba9c14.tar.gz qmenumodel-936588550f0f91ee95386931a5297b46c3ba9c14.tar.bz2 qmenumodel-936588550f0f91ee95386931a5297b46c3ba9c14.zip |
Add UnityMenuAction::index property
-rw-r--r-- | libqmenumodel/src/unitymenuaction.cpp | 16 | ||||
-rw-r--r-- | libqmenumodel/src/unitymenuaction.h | 7 | ||||
-rw-r--r-- | libqmenumodel/src/unitymenumodel.cpp | 103 | ||||
-rw-r--r-- | libqmenumodel/src/unitymenumodel.h | 2 |
4 files changed, 82 insertions, 46 deletions
diff --git a/libqmenumodel/src/unitymenuaction.cpp b/libqmenumodel/src/unitymenuaction.cpp index b41fd3e..46edf16 100644 --- a/libqmenumodel/src/unitymenuaction.cpp +++ b/libqmenumodel/src/unitymenuaction.cpp @@ -26,7 +26,8 @@ UnityMenuAction::UnityMenuAction(QObject* parent) : QObject(parent), m_valid(false), m_enabled(false), - m_model(NULL) + m_model(NULL), + m_index(-1) { } @@ -106,6 +107,19 @@ void UnityMenuAction::setValid(bool valid) } } +int UnityMenuAction::index() const +{ + return m_index; +} + +void UnityMenuAction::setIndex(int i) +{ + if (i != m_index) { + m_index = i; + Q_EMIT indexChanged(m_index); + } +} + void UnityMenuAction::registerAction() { if (m_model) { diff --git a/libqmenumodel/src/unitymenuaction.h b/libqmenumodel/src/unitymenuaction.h index b8391fe..7479d2c 100644 --- a/libqmenumodel/src/unitymenuaction.h +++ b/libqmenumodel/src/unitymenuaction.h @@ -32,6 +32,8 @@ class UnityMenuAction: public QObject Q_PROPERTY(bool enabled READ isEnabled NOTIFY enabledChanged) Q_PROPERTY(bool valid READ isValid NOTIFY validChanged) Q_PROPERTY(UnityMenuModel* model READ model WRITE setModel NOTIFY modelChanged) + Q_PROPERTY(int index READ index WRITE setIndex NOTIFY indexChanged) + public: UnityMenuAction(QObject* parent = 0); ~UnityMenuAction(); @@ -42,6 +44,9 @@ public: UnityMenuModel* model() const; void setModel(UnityMenuModel* model); + int index() const; + void setIndex(int i); + QVariant state() const; bool isEnabled() const; bool isValid() const; @@ -55,6 +60,7 @@ Q_SIGNALS: void stateChanged(const QVariant& name); void enabledChanged(bool enabled); void validChanged(bool valid); + void indexChanged(int index); protected: virtual bool event(QEvent* e); @@ -72,6 +78,7 @@ private: bool m_valid; bool m_enabled; UnityMenuModel* m_model; + int m_index; }; #endif // UNITYMENUACTIONGROUP_H diff --git a/libqmenumodel/src/unitymenumodel.cpp b/libqmenumodel/src/unitymenumodel.cpp index 026cb93..2000d5b 100644 --- a/libqmenumodel/src/unitymenumodel.cpp +++ b/libqmenumodel/src/unitymenumodel.cpp @@ -93,6 +93,9 @@ public: static void registeredActionEnabledChanged(GtkSimpleActionObserver *observer_item, const gchar *action_name, gboolean enabled); static void registeredActionStateChanged(GtkSimpleActionObserver *observer_item, const gchar *action_name, GVariant *state); static void registeredActionRemoved(GtkSimpleActionObserver *observer_item, const gchar *action_name); + + gchar * fullActionName(UnityMenuAction *action); + void updateRegisteredAction(UnityMenuAction *action); }; void menu_item_free (gpointer data) @@ -641,34 +644,6 @@ QVariant UnityMenuModel::get(int row, const QByteArray &role) return this->data(this->index(row, 0), priv->roles[role]); } -UnityMenuAction * UnityMenuModel::getAction(int row, const QByteArray &name) -{ - GSequenceIter *iter; - GtkMenuTrackerItem *item; - const gchar *action_namespace; - gchar *action_name; - UnityMenuAction *action; - - iter = g_sequence_get_iter_at_pos (priv->items, row); - if (g_sequence_iter_is_end (iter)) - return NULL; - - item = (GtkMenuTrackerItem *) g_sequence_get (iter); - action_namespace = gtk_menu_tracker_item_get_action_namespace (item); - if (action_namespace != NULL) - action_name = g_strjoin (".", action_namespace, name.constData(), NULL); - else - action_name = g_strdup (name.constData()); - - action = new UnityMenuAction(this); - action->setModel(this); - action->setName(action_name); - - g_free (action_name); - - return action; -} - void UnityMenuModel::activate(int index, const QVariant& parameter) { GtkMenuTrackerItem *item; @@ -783,6 +758,7 @@ void UnityMenuModel::registerAction(UnityMenuAction* action) priv->registeredActions[action] = observer_item; connect(action, SIGNAL(nameChanged(const QString&)), SLOT(onRegisteredActionNameChanged(const QString&))); + connect(action, SIGNAL(indexChanged(int)), SLOT(onRegisteredActionIndexChanged(int))); connect(action, SIGNAL(activate(const QVariant&)), SLOT(onRegisteredActionActivated(const QVariant&))); connect(action, SIGNAL(changeState(const QVariant&)), SLOT(onRegisteredActionStateChanged(const QVariant&))); } @@ -800,26 +776,51 @@ void UnityMenuModel::unregisterAction(UnityMenuAction* action) } } -void UnityMenuModel::onRegisteredActionNameChanged(const QString& name) +/* Returns the full name for @action + * + * If @action is associated with a menu item that is inside of a + * section or submenu with "action-namespace" set, this namespace + * is prepended to @action->name() + */ +char * UnityMenuModelPrivate::fullActionName(UnityMenuAction *action) { - UnityMenuAction* action = qobject_cast<UnityMenuAction*>(sender()); - if (!action || !priv->registeredActions.contains(action)) - return; + GSequenceIter *iter; + const gchar *name; + gchar *full_name = NULL; - GtkSimpleActionObserver* observer_item; - observer_item = priv->registeredActions[action]; + name = action->name().toUtf8().constData(); - QByteArray nameArray = name.toUtf8(); - const gchar* action_name = nameArray.constData(); + iter = g_sequence_get_iter_at_pos (this->items, action->index()); + if (!g_sequence_iter_is_end (iter)) { + GtkMenuTrackerItem *item; + const gchar *action_namespace; - gtk_simple_action_observer_register_action (observer_item, action_name); + item = (GtkMenuTrackerItem *) g_sequence_get (iter); + action_namespace = gtk_menu_tracker_item_get_action_namespace (item); + if (action_namespace != NULL) + return g_strjoin (".", action_namespace, name, NULL); + } + + return g_strdup (name); +} - const GVariantType *parameter_type; +void UnityMenuModelPrivate::updateRegisteredAction(UnityMenuAction *action) +{ + GtkSimpleActionObserver *observer_item; + gchar *action_name; gboolean enabled; GVariant *state; - if (g_action_group_query_action (G_ACTION_GROUP (priv->muxer), action_name, - &enabled, ¶meter_type, NULL, NULL, &state)) + if (!action || !this->registeredActions.contains(action)) + return; + + action_name = fullActionName(action); + + observer_item = this->registeredActions[action]; + gtk_simple_action_observer_register_action (observer_item, action_name); + + if (g_action_group_query_action (G_ACTION_GROUP (this->muxer), action_name, + &enabled, NULL, NULL, NULL, &state)) { UnityMenuActionAddEvent umaae(enabled, Converter::toQVariant(state)); QCoreApplication::sendEvent(action, &umaae); @@ -828,6 +829,18 @@ void UnityMenuModel::onRegisteredActionNameChanged(const QString& name) g_variant_unref (state); } } + + g_free(action_name); +} + +void UnityMenuModel::onRegisteredActionNameChanged(const QString& name) +{ + priv->updateRegisteredAction(qobject_cast<UnityMenuAction*>(sender())); +} + +void UnityMenuModel::onRegisteredActionIndexChanged(int index) +{ + priv->updateRegisteredAction(qobject_cast<UnityMenuAction*>(sender())); } void UnityMenuModel::onRegisteredActionActivated(const QVariant& parameter) @@ -836,10 +849,11 @@ void UnityMenuModel::onRegisteredActionActivated(const QVariant& parameter) if (!action || action->name().isEmpty()) return; - QByteArray nameArray = action->name().toUtf8(); - const gchar* action_name = nameArray.constData(); + gchar* action_name = priv->fullActionName(action); g_action_group_activate_action (G_ACTION_GROUP (priv->muxer), action_name, Converter::toGVariant(parameter)); + + g_free(action_name); } void UnityMenuModel::onRegisteredActionStateChanged(const QVariant& parameter) @@ -848,10 +862,11 @@ void UnityMenuModel::onRegisteredActionStateChanged(const QVariant& parameter) if (!action || action->name().isEmpty()) return; - QByteArray nameArray = action->name().toUtf8(); - const gchar* action_name = nameArray.constData(); + gchar* action_name = priv->fullActionName(action); g_action_group_change_action_state (G_ACTION_GROUP (priv->muxer), action_name, Converter::toGVariant(parameter)); + + g_free(action_name); } void UnityMenuModelPrivate::registeredActionAdded(GtkSimpleActionObserver *observer_item, diff --git a/libqmenumodel/src/unitymenumodel.h b/libqmenumodel/src/unitymenumodel.h index 1391b27..30bc62d 100644 --- a/libqmenumodel/src/unitymenumodel.h +++ b/libqmenumodel/src/unitymenumodel.h @@ -58,7 +58,6 @@ public: Q_INVOKABLE QObject * submenu(int position, QQmlComponent* actionStateParser = NULL); Q_INVOKABLE bool loadExtendedAttributes(int position, const QVariantMap &schema); Q_INVOKABLE QVariant get(int row, const QByteArray &role); - Q_INVOKABLE UnityMenuAction * getAction(int row, const QByteArray &name); Q_INVOKABLE void activate(int index, const QVariant& parameter = QVariant()); Q_INVOKABLE void changeState(int index, const QVariant& parameter); @@ -74,6 +73,7 @@ Q_SIGNALS: protected Q_SLOTS: void onRegisteredActionNameChanged(const QString& name); + void onRegisteredActionIndexChanged(int); void onRegisteredActionActivated(const QVariant& parameter); void onRegisteredActionStateChanged(const QVariant& parameter); |