diff options
author | Nick Dedekind <nicholas.dedekind@gmail.com> | 2013-07-30 12:23:42 +0200 |
---|---|---|
committer | Lars Uebernickel <lars.uebernickel@canonical.com> | 2013-07-30 12:23:42 +0200 |
commit | d1c96de04570d8365c2ab2fde597ccd21c7a3dc2 (patch) | |
tree | 6abe5ac6cea1593c4effdea00c3087e4c9f37ede /libqmenumodel | |
parent | d47194b0794968fa7713a8c7c9cfec7f1e4f8a17 (diff) | |
download | qmenumodel-d1c96de04570d8365c2ab2fde597ccd21c7a3dc2.tar.gz qmenumodel-d1c96de04570d8365c2ab2fde597ccd21c7a3dc2.tar.bz2 qmenumodel-d1c96de04570d8365c2ab2fde597ccd21c7a3dc2.zip |
Add action role (replacing actionState role)
Diffstat (limited to 'libqmenumodel')
-rw-r--r-- | libqmenumodel/QMenuModel/plugin.cpp | 3 | ||||
-rw-r--r-- | libqmenumodel/src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | libqmenumodel/src/unitymenuaction.cpp | 60 | ||||
-rw-r--r-- | libqmenumodel/src/unitymenuaction.h | 59 | ||||
-rw-r--r-- | libqmenumodel/src/unitymenumodel.cpp | 36 |
5 files changed, 154 insertions, 6 deletions
diff --git a/libqmenumodel/QMenuModel/plugin.cpp b/libqmenumodel/QMenuModel/plugin.cpp index 0629099..c1a5e40 100644 --- a/libqmenumodel/QMenuModel/plugin.cpp +++ b/libqmenumodel/QMenuModel/plugin.cpp @@ -22,6 +22,7 @@ #include "qdbusmenumodel.h" #include "qdbusactiongroup.h" #include "qstateaction.h" +#include "unitymenuaction.h" #include "unitymenumodel.h" #include "unitythemediconprovider.h" @@ -40,6 +41,8 @@ void QMenuModelQmlPlugin::registerTypes(const char *uri) "QStateAction must be created by QDBusActionGroup::action"); qmlRegisterUncreatableType<DBusEnums>(uri, 0, 1, "DBus", "DBus is only a namespace"); + qmlRegisterUncreatableType<UnityMenuAction>(uri, 0, 1, "UnityMenuAction", + "UnityMenuAction must be created by UnityMenuModel"); qmlRegisterType<QDBusMenuModel>(uri, 0, 1, "QDBusMenuModel"); qmlRegisterType<QDBusActionGroup>(uri, 0, 1, "QDBusActionGroup"); diff --git a/libqmenumodel/src/CMakeLists.txt b/libqmenumodel/src/CMakeLists.txt index cf9c426..e0ee902 100644 --- a/libqmenumodel/src/CMakeLists.txt +++ b/libqmenumodel/src/CMakeLists.txt @@ -10,6 +10,7 @@ set(QMENUMODEL_SRC qdbusmenumodel.cpp qdbusactiongroup.cpp qstateaction.cpp + unitymenuaction.cpp unitymenumodel.cpp unitythemediconprovider.cpp gtk/gtkactionmuxer.c @@ -57,6 +58,7 @@ set(QMENUMODEL_HEADERS qdbusobject.h qmenumodel.h qstateaction.h + unitymenuaction.h unitymenumodel.h unitythemediconprovider.h ) diff --git a/libqmenumodel/src/unitymenuaction.cpp b/libqmenumodel/src/unitymenuaction.cpp new file mode 100644 index 0000000..f391bfe --- /dev/null +++ b/libqmenumodel/src/unitymenuaction.cpp @@ -0,0 +1,60 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Nick Dedekind <nick.dedekind@canonical.com> + */ + +#include "unitymenuaction.h" +#include "unitymenumodel.h" + +UnityMenuAction::UnityMenuAction(QObject* parent) + : QObject(parent), + m_model(NULL), + m_index(-1) +{ +} + +int UnityMenuAction::index() const +{ + return m_index; +} + +void UnityMenuAction::setIndex(int index) +{ + if (m_index != index) { + m_index = index; + Q_EMIT indexChanged(index); + } +} + +UnityMenuModel* UnityMenuAction::model() const +{ + return m_model; +} + +void UnityMenuAction::setModel(UnityMenuModel* model) +{ + if (m_model != model) { + if (m_model) { + disconnect(m_model); + } + m_model = model; + + connect(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)), SIGNAL(stateChanged())); + + Q_EMIT modelChanged(model); + } +} diff --git a/libqmenumodel/src/unitymenuaction.h b/libqmenumodel/src/unitymenuaction.h new file mode 100644 index 0000000..26be02b --- /dev/null +++ b/libqmenumodel/src/unitymenuaction.h @@ -0,0 +1,59 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Nick Dedekind <nick.dedekind@canonical.com> + */ + +#ifndef UNITYMENUACTION_H +#define UNITYMENUACTION_H + +#include <QObject> +#include <QVariant> + +typedef struct _GVariant GVariant; +class UnityMenuModel; + +class UnityMenuAction : public QObject +{ + Q_OBJECT + Q_PROPERTY(QVariant state READ state WRITE updateState NOTIFY stateChanged) + Q_PROPERTY(int index READ index WRITE setIndex NOTIFY indexChanged) + Q_PROPERTY(UnityMenuModel* model READ model WRITE setModel NOTIFY modelChanged) +public: + UnityMenuAction(QObject* parent=0); + + int index() const; + void setIndex(int index); + + UnityMenuModel* model() const; + void setModel(UnityMenuModel* model); + + virtual QVariant state() const = 0; + Q_INVOKABLE virtual void updateState(const QVariant& = QVariant()) = 0; + +Q_SIGNALS: + void stateChanged(); + void indexChanged(int index); + void modelChanged(UnityMenuModel* model); + +private: + UnityMenuModel* m_model; + int m_index; +}; + +Q_DECLARE_METATYPE(UnityMenuAction*) + +#endif // UNITYMENUACTION_H diff --git a/libqmenumodel/src/unitymenumodel.cpp b/libqmenumodel/src/unitymenumodel.cpp index 8bbb601..72a5985 100644 --- a/libqmenumodel/src/unitymenumodel.cpp +++ b/libqmenumodel/src/unitymenumodel.cpp @@ -19,12 +19,11 @@ #include "unitymenumodel.h" #include "converter.h" #include "actionstateparser.h" +#include "unitymenuaction.h" #include <QIcon> #include <QQmlComponent> -#include <QIcon> - extern "C" { #include "gtk/gtkactionmuxer.h" #include "gtk/gtkmenutracker.h" @@ -41,7 +40,7 @@ enum MenuRoles { IconRole, TypeRole, ExtendedAttributesRole, - ActionStateRole + ActionRole }; class UnityMenuModelPrivate @@ -76,6 +75,31 @@ public: static void menuItemChanged(GObject *object, GParamSpec *pspec, gpointer user_data); }; +class UnityGtkMenuTrackerItemAction : public UnityMenuAction +{ +public: + UnityGtkMenuTrackerItemAction(int index, UnityMenuModelPrivate* priv) + : UnityMenuAction(priv->model), + d(priv) + { + setModel(priv->model); + setIndex(index); + } + + virtual QVariant state() const { + GtkMenuTrackerItem* item = (GtkMenuTrackerItem *) g_sequence_get (g_sequence_get_iter_at_pos (d->items, index())); + if (!item) { + return QVariant(); + } + return d->itemState(item); + } + + virtual void updateState(const QVariant& param = QVariant()) { } + +private: + UnityMenuModelPrivate* d; +}; + void menu_item_free (gpointer data) { GtkMenuTrackerItem *item = (GtkMenuTrackerItem *) data; @@ -411,8 +435,8 @@ QVariant UnityMenuModel::data(const QModelIndex &index, int role) const return map ? *map : QVariant(); } - case ActionStateRole: - return priv->itemState(item); + case ActionRole: + return QVariant::fromValue(new UnityGtkMenuTrackerItemAction(index.row(), priv)); default: return QVariant(); @@ -440,7 +464,7 @@ QHash<int, QByteArray> UnityMenuModel::roleNames() const names[IconRole] = "icon"; names[TypeRole] = "type"; names[ExtendedAttributesRole] = "ext"; - names[ActionStateRole] = "actionState"; + names[ActionRole] = "action"; return names; } |