From d1c96de04570d8365c2ab2fde597ccd21c7a3dc2 Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Tue, 30 Jul 2013 12:23:42 +0200 Subject: Add action role (replacing actionState role) --- libqmenumodel/src/CMakeLists.txt | 2 ++ libqmenumodel/src/unitymenuaction.cpp | 60 +++++++++++++++++++++++++++++++++++ libqmenumodel/src/unitymenuaction.h | 59 ++++++++++++++++++++++++++++++++++ libqmenumodel/src/unitymenumodel.cpp | 36 +++++++++++++++++---- 4 files changed, 151 insertions(+), 6 deletions(-) create mode 100644 libqmenumodel/src/unitymenuaction.cpp create mode 100644 libqmenumodel/src/unitymenuaction.h (limited to 'libqmenumodel/src') 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 . + * + * Authors: + * Nick Dedekind + */ + +#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&)), 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 . + * + * Authors: + * Nick Dedekind + */ + +#ifndef UNITYMENUACTION_H +#define UNITYMENUACTION_H + +#include +#include + +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 #include -#include - 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 UnityMenuModel::roleNames() const names[IconRole] = "icon"; names[TypeRole] = "type"; names[ExtendedAttributesRole] = "ext"; - names[ActionStateRole] = "actionState"; + names[ActionRole] = "action"; return names; } -- cgit v1.2.3