diff options
-rw-r--r-- | src/QMenuModel/plugin.cpp | 2 | ||||
-rw-r--r-- | src/common/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/common/qdbusactiongroup.cpp | 33 | ||||
-rw-r--r-- | src/common/qdbusactiongroup.h | 7 | ||||
-rw-r--r-- | src/common/qmenumodel.cpp | 10 | ||||
-rw-r--r-- | src/common/qstateaction.cpp | 68 | ||||
-rw-r--r-- | src/common/qstateaction.h | 43 | ||||
-rw-r--r-- | tests/client/actiongrouptest.cpp | 3 | ||||
-rw-r--r-- | tests/client/modeltest.cpp | 22 |
9 files changed, 148 insertions, 42 deletions
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 <QtDeclarative> @@ -31,6 +32,7 @@ void QMenuModelQmlPlugin::registerTypes(const char *uri) "QMenuModel is a interface"); qmlRegisterType<QDBusMenuModel>(uri, 0, 1, "QDBusMenuModel"); qmlRegisterType<QDBusActionGroup>(uri, 0, 1, "QDBusActionGroup"); + qmlRegisterType<QStateAction>(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 <QDebug> @@ -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 <QAction> #include <QSet> +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<QAction*> m_actions; + QSet<QStateAction*> 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<QMenuModel*>(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<QObject*>(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 <http://www.gnu.org/licenses/>. + * + * Authors: + * Renato Araujo Oliveira Filho <renato@canonical.com> + */ + +#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 <http://www.gnu.org/licenses/>. + * + * Authors: + * Renato Araujo Oliveira Filho <renato@canonical.com> + */ + +#ifndef QDBUSACTION_H +#define QDBUSACTION_H + +#include <QAction> +#include <QVariant> + +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 diff --git a/tests/client/actiongrouptest.cpp b/tests/client/actiongrouptest.cpp index 3843ace..6fc2f82 100644 --- a/tests/client/actiongrouptest.cpp +++ b/tests/client/actiongrouptest.cpp @@ -20,6 +20,7 @@ #include "qdbusmenumodel.h" #include "qdbusactiongroup.h" #include "dbusmenuscript.h" +#include "qstateaction.h" #include <QObject> #include <QSignalSpy> @@ -128,7 +129,7 @@ private Q_SLOTS: // Get Action QVariant action = m_model.data(m_model.index(1, 0), QMenuModel::Action); QVERIFY(action.isValid()); - QAction *act = m_actionGroup.action(action.toString()); + QStateAction *act = m_actionGroup.action(action.toString()); QVERIFY(act); act->trigger(); diff --git a/tests/client/modeltest.cpp b/tests/client/modeltest.cpp index 4cf7f54..347676e 100644 --- a/tests/client/modeltest.cpp +++ b/tests/client/modeltest.cpp @@ -144,55 +144,55 @@ private Q_SLOTS: QVariant e = m_model.data(m_model.index(0, 0), QMenuModel::Extra); QVERIFY(e.isValid()); - QObject *extra = e.value<QObject*>(); + QVariantMap extra = e.toMap(); // Boolean - QVariant v = extra->property("boolean"); + QVariant v = extra["boolean"]; QCOMPARE(v.type(), QVariant::Bool); QCOMPARE(v.toBool(), true); // Byte - v = extra->property("byte"); + v = extra["byte"]; QCOMPARE(v.typeName(), "uchar"); QCOMPARE(v.value<uchar>(), (uchar)42); // Int16 - v = extra->property("int16"); + v = extra["int16"]; QCOMPARE(v.typeName(), "short"); QCOMPARE(v.value<short>(), (short)-42); // UInt16 - v = extra->property("uint16"); + v = extra["uint16"]; QCOMPARE(v.typeName(), "ushort"); QCOMPARE(v.value<ushort>(), (ushort)42); // Int32 - v = extra->property("int32"); + v = extra["int32"]; QCOMPARE(v.type(), QVariant::Int); QCOMPARE(v.toInt(), -42); // UInt32 - v = extra->property("uint32"); + v = extra["uint32"]; QCOMPARE(v.type(), QVariant::UInt); QCOMPARE(v.toUInt(), (uint) 42); // Int64 - v = extra->property("int64"); + v = extra["int64"]; QCOMPARE(v.typeName(), "long"); QCOMPARE(v.value<long>(), (long) -42); // UInt64 - v = extra->property("uint64"); + v = extra["uint64"]; QCOMPARE(v.typeName(), "ulong"); QCOMPARE(v.value<ulong>(), (ulong) 42); // Double - v = extra->property("double"); + v = extra["double"]; QCOMPARE(v.type(), QVariant::Double); QCOMPARE(v.toDouble(), 42.42); // String - v = extra->property("string"); + v = extra["string"]; QCOMPARE(v.type(), QVariant::String); QCOMPARE(v.toString(), QString("42")); } |