From a03360ce3fa4b2de50917188a8a816992e864ba1 Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Thu, 13 Sep 2012 14:14:56 -0300 Subject: Created class QStateAction to use as GAction proxy. --- src/QMenuModel/plugin.cpp | 2 ++ src/common/CMakeLists.txt | 2 ++ src/common/qdbusactiongroup.cpp | 33 +++++++------------- src/common/qdbusactiongroup.h | 7 +++-- src/common/qmenumodel.cpp | 10 +++--- src/common/qstateaction.cpp | 68 +++++++++++++++++++++++++++++++++++++++++ src/common/qstateaction.h | 43 ++++++++++++++++++++++++++ 7 files changed, 135 insertions(+), 30 deletions(-) create mode 100644 src/common/qstateaction.cpp create mode 100644 src/common/qstateaction.h (limited to 'src') diff --git a/src/QMenuModel/plugin.cpp b/src/QMenuModel/plugin.cpp index 9fd7922..fbd943c 100644 --- a/src/QMenuModel/plugin.cpp +++ b/src/QMenuModel/plugin.cpp @@ -21,6 +21,7 @@ #include "qmenumodel.h" #include "qdbusmenumodel.h" #include "qdbusactiongroup.h" +#include "qstateaction.h" #include @@ -31,6 +32,7 @@ void QMenuModelQmlPlugin::registerTypes(const char *uri) "QMenuModel is a interface"); qmlRegisterType(uri, 0, 1, "QDBusMenuModel"); qmlRegisterType(uri, 0, 1, "QDBusActionGroup"); + qmlRegisterType(uri, 0, 1, "QStateAction"); } Q_EXPORT_PLUGIN2(qmenumodel, QMenuModelQmlPlugin) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index a4f5e2e..ad3343a 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -6,6 +6,7 @@ set(QMENUMODELCOMMON_SRC qdbusobject.cpp qdbusmenumodel.cpp qdbusactiongroup.cpp + qstateaction.cpp ) set(QMENUMODELCOMMON_HEADERS @@ -14,6 +15,7 @@ set(QMENUMODELCOMMON_HEADERS qdbusobject.h qdbusmenumodel.h qdbusactiongroup.h + qstateaction.h ) qt4_wrap_cpp(QMENUMODELCOMMON_MOC diff --git a/src/common/qdbusactiongroup.cpp b/src/common/qdbusactiongroup.cpp index e453215..2b3528e 100644 --- a/src/common/qdbusactiongroup.cpp +++ b/src/common/qdbusactiongroup.cpp @@ -18,6 +18,7 @@ */ #include "qdbusactiongroup.h" +#include "qstateaction.h" #include "converter.h" #include @@ -64,13 +65,13 @@ QDBusActionGroup::~QDBusActionGroup() /*! \qmlmethod QDBusActionGroup::action(QString name) - Look for a action with the same name and return a \l QAction object. + Look for a action with the same name and return a \l QStateAction object. \bold Note: methods should only be called after the Component has completed. */ -QAction *QDBusActionGroup::action(const QString &name) +QStateAction *QDBusActionGroup::action(const QString &name) { - Q_FOREACH(QAction *act, m_actions) { + Q_FOREACH(QStateAction *act, m_actions) { if (act->text() == name) { return act; } @@ -170,19 +171,13 @@ void QDBusActionGroup::setActionGroup(GDBusActionGroup *ag) /*! \internal */ void QDBusActionGroup::addAction(const char *actionName) { - QAction *act = new QAction(actionName, this); + QStateAction *act = new QStateAction(actionName, this); act->setEnabled(g_action_group_get_action_enabled(m_actionGroup, actionName)); - const GVariantType *stateType = g_action_group_get_action_state_type(m_actionGroup, actionName); - if (stateType == G_VARIANT_TYPE_BOOLEAN) { - act->setCheckable(true); - - GVariant *actState = g_action_group_get_action_state(m_actionGroup, actionName); - if (actState != NULL) { - act->setChecked(g_variant_get_boolean(actState)); - g_variant_unref(actState); - } + GVariant *actState = g_action_group_get_action_state(m_actionGroup, actionName); + if (actState) { + act->setState(Converter::parseGVariant(actState)); } QObject::connect(act, SIGNAL(triggered()), this, SLOT(onActionTriggered())); @@ -204,7 +199,7 @@ void QDBusActionGroup::onActionTriggered() /*! \internal */ void QDBusActionGroup::removeAction(const char *actionName) { - Q_FOREACH(QAction *act, m_actions) { + Q_FOREACH(QStateAction *act, m_actions) { if (act->text() == actionName) { m_actions.remove(act); delete act; @@ -217,15 +212,9 @@ void QDBusActionGroup::removeAction(const char *actionName) /*! \internal */ void QDBusActionGroup::updateAction(const char *actionName, GVariant *state) { - QAction *action = this->action(actionName); + QStateAction *action = this->action(actionName); if ((action != NULL) && (state != NULL)) { - - const GVariantType *stateType = g_variant_get_type(state); - if (stateType == G_VARIANT_TYPE_BOOLEAN) { - action->setChecked(g_variant_get_boolean(state)); - } - - Q_EMIT actionStateChanged(actionName, Converter::parseGVariant(state)); + action->setState(Converter::parseGVariant(state)); } } diff --git a/src/common/qdbusactiongroup.h b/src/common/qdbusactiongroup.h index 3ff7927..28e4f25 100644 --- a/src/common/qdbusactiongroup.h +++ b/src/common/qdbusactiongroup.h @@ -26,6 +26,8 @@ #include #include +class QStateAction; + class QDBusActionGroup : public QObject, public QDBusObject { Q_OBJECT @@ -46,13 +48,12 @@ Q_SIGNALS: void busNameChanged(const QString &busNameChanged); void objectPathChanged(const QString &objectPath); void statusChanged(ConnectionStatus status); - void actionStateChanged(const QString &name, QVariant value); void countChanged(int count); public Q_SLOTS: void start(); void stop(); - QAction *action(const QString &actionName); + QStateAction *action(const QString &actionName); protected: virtual void serviceAppear(GDBusConnection *connection); @@ -64,7 +65,7 @@ private Q_SLOTS: private: GActionGroup *m_actionGroup; - QSet m_actions; + QSet m_actions; int m_signalActionAddId; int m_signalActionRemovedId; int m_signalStateChangedId; diff --git a/src/common/qmenumodel.cpp b/src/common/qmenumodel.cpp index af29ded..accc47d 100644 --- a/src/common/qmenumodel.cpp +++ b/src/common/qmenumodel.cpp @@ -183,7 +183,7 @@ QString QMenuModel::parseExtraPropertyName(const QString &name) const { QString newName(name); if (name.startsWith("x-")) { - newName = name.right(name.length() - 2); + newName = name.mid(2); } return newName.replace("-", "_"); } @@ -196,17 +196,17 @@ QVariant QMenuModel::getExtraProperties(const QModelIndex &index) const return QVariant(); } - QObject *extra = new QObject(const_cast(this)); + QVariantMap extra; const gchar *attrName = NULL; GVariant *value = NULL; while (g_menu_attribute_iter_get_next (iter, &attrName, &value)) { if (strncmp("x-", attrName, 2) == 0) { - - extra->setProperty(parseExtraPropertyName(attrName).toLatin1(), Converter::parseGVariant(value)); + extra.insert(parseExtraPropertyName(attrName), + Converter::parseGVariant(value)); } } - return QVariant::fromValue(extra); + return extra; } /*! \internal */ diff --git a/src/common/qstateaction.cpp b/src/common/qstateaction.cpp new file mode 100644 index 0000000..a9ecabc --- /dev/null +++ b/src/common/qstateaction.cpp @@ -0,0 +1,68 @@ +/* + * Copyright 2012 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: + * Renato Araujo Oliveira Filho + */ + +#include "qstateaction.h" + +/*! + \qmlclass QStateAction + \inherits QAction + + \brief A QStateAction implementation to be used with \l QDBusActionGroup + + \bold {This component is under heavy development.} + + This class can be used as a proxy for an action that is exported over D-Bus + + \code + QDBusActionGroup { + id: actionGroup + busType: 1 + busName: "com.ubuntu.menu" + objectPath: "com/ubuntu/menu/actions" + } + + Button { + visible: actionGroup.getAction("button.bvisible").status + } + \endcode +*/ + +/*! \internal */ +QStateAction::QStateAction(const QString &text, QObject *parent) + :QAction(text, parent) +{ +} + +/*! + \qmlproperty int QStateAction::state + This property holds the current action state +*/ +QVariant QStateAction::state() const +{ + return m_state; +} + +/*! \internal */ +void QStateAction::setState(const QVariant &state) +{ + if (m_state != state) { + m_state = state; + Q_EMIT stateChanged(m_state); + } +} diff --git a/src/common/qstateaction.h b/src/common/qstateaction.h new file mode 100644 index 0000000..3ed6bf3 --- /dev/null +++ b/src/common/qstateaction.h @@ -0,0 +1,43 @@ +/* + * Copyright 2012 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: + * Renato Araujo Oliveira Filho + */ + +#ifndef QDBUSACTION_H +#define QDBUSACTION_H + +#include +#include + +class QStateAction : public QAction +{ + Q_OBJECT + Q_PROPERTY(QVariant state READ state WRITE setState NOTIFY stateChanged) +public: + QStateAction(const QString &text="", QObject *parent=0); + + QVariant state() const; + void setState(const QVariant &state); + +Q_SIGNALS: + void stateChanged(QVariant state); + +private: + QVariant m_state; +}; + +#endif -- cgit v1.2.3