From c6cb726c5694f8a35711a48a7bf5e2a6723aeba8 Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Thu, 8 Aug 2013 16:12:57 +0100 Subject: Glib callbacks send events through qt. --- libqmenumodel/src/menunode.cpp | 24 +++++++------ libqmenumodel/src/menunode.h | 14 ++++++++ libqmenumodel/src/qdbusactiongroup.cpp | 64 +++++++++++++++++++++++++++++++--- libqmenumodel/src/qdbusactiongroup.h | 34 +++++++++++++++++- libqmenumodel/src/qdbusmenumodel.cpp | 25 ++++++++++--- libqmenumodel/src/qdbusmenumodel.h | 2 ++ libqmenumodel/src/qdbusobject.cpp | 55 +++++++++++++++++++++++++---- libqmenumodel/src/qdbusobject.h | 17 ++++++++- libqmenumodel/src/qmenumodel.cpp | 57 ++++++++++++++++++++++-------- libqmenumodel/src/qmenumodel.h | 18 +++++++--- 10 files changed, 263 insertions(+), 47 deletions(-) (limited to 'libqmenumodel') diff --git a/libqmenumodel/src/menunode.cpp b/libqmenumodel/src/menunode.cpp index ed9a984..10313d5 100644 --- a/libqmenumodel/src/menunode.cpp +++ b/libqmenumodel/src/menunode.cpp @@ -21,6 +21,9 @@ #include #include +#include + +const QEvent::Type MenuNodeItemChangeEvent::eventType = static_cast(QEvent::registerEventType()); MenuNode::MenuNode(const QString &linkType, GMenuModel *model, MenuNode *parent, int pos, QObject *listener) : m_model(model), @@ -242,15 +245,16 @@ void MenuNode::onItemsChanged(GMenuModel *model, gint position, gint removed, gi self->m_currentOpAdded = added; self->m_currentOpRemoved = removed; - const QMetaObject *mobj = self->m_listener->metaObject(); - if (!mobj->invokeMethod(self->m_listener, - "onItemsChanged", - Q_ARG(MenuNode*, self), - Q_ARG(int, position), - Q_ARG(int, removed), - Q_ARG(int, added))) - { - qWarning() << "Slot 'onItemsChanged(MenuNode*, int, int, int)' not found in" << self->m_listener; - } + MenuNodeItemChangeEvent mnice(self, position, added, removed); + QCoreApplication::sendEvent(self->m_listener, &mnice); + self->commitOperation(); } + +MenuNodeItemChangeEvent::MenuNodeItemChangeEvent(MenuNode* _node, int _position, int _removed, int _added) + : QEvent(MenuNodeItemChangeEvent::eventType), + node(_node), + position(_position), + removed(_removed), + added(_added) +{} diff --git a/libqmenumodel/src/menunode.h b/libqmenumodel/src/menunode.h index 21fc5bc..9e22e82 100644 --- a/libqmenumodel/src/menunode.h +++ b/libqmenumodel/src/menunode.h @@ -24,6 +24,7 @@ #include #include #include +#include extern "C" { #include @@ -73,4 +74,17 @@ private: static void onItemsChanged(GMenuModel *model, gint position, gint removed, gint added, gpointer data); }; +class MenuNodeItemChangeEvent : public QEvent +{ +public: + static const QEvent::Type eventType; + + MenuNodeItemChangeEvent(MenuNode* node, int position, int removed, int added); + + MenuNode* node; + int position; + int removed; + int added; +}; + #endif diff --git a/libqmenumodel/src/qdbusactiongroup.cpp b/libqmenumodel/src/qdbusactiongroup.cpp index 824b5f0..48aa882 100644 --- a/libqmenumodel/src/qdbusactiongroup.cpp +++ b/libqmenumodel/src/qdbusactiongroup.cpp @@ -21,11 +21,18 @@ #include "qstateaction.h" #include "converter.h" +// Qt +#include +#include + extern "C" { #include #include } +const QEvent::Type DBusActionStateEvent::eventType = static_cast(QEvent::registerEventType()); +const QEvent::Type DBusActionVisiblityEvent::eventType = static_cast(QEvent::registerEventType()); + /*! \qmltype QDBusActionGroup \inherits QDBusObject @@ -53,7 +60,8 @@ extern "C" { /*! \internal */ QDBusActionGroup::QDBusActionGroup(QObject *parent) :QObject(parent), - m_actionGroup(NULL) + QDBusObject(this), + m_actionGroup(NULL) { } @@ -184,7 +192,8 @@ void QDBusActionGroup::setActionGroup(GDBusActionGroup *ag) gchar **actions = g_action_group_list_actions(m_actionGroup); for(guint i=0; i < g_strv_length(actions); i++) { - Q_EMIT actionAppear(actions[i]); + DBusActionVisiblityEvent dave(actions[i], true); + QCoreApplication::sendEvent(this, &dave); } g_strfreev(actions); } @@ -218,23 +227,68 @@ void QDBusActionGroup::activateAction(const QString &name, const QVariant ¶m } } +bool QDBusActionGroup::event(QEvent* e) +{ + if (QDBusObject::event(e)) { + return true; + + } else if (e->type() == DBusActionVisiblityEvent::eventType) { + DBusActionVisiblityEvent *dave = static_cast(e); + + if (dave->visible) { + Q_EMIT actionAppear(dave->name); + } else { + Q_EMIT actionVanish(dave->name); + } + } else if (e->type() == DBusActionStateEvent::eventType) { + DBusActionStateEvent *dase = static_cast(e); + + Q_EMIT actionStateChanged(dase->name, dase->state); + } + return QObject::event(e); +} + /*! \internal */ void QDBusActionGroup::onActionAdded(GDBusActionGroup *, gchar *name, gpointer data) { QDBusActionGroup *self = reinterpret_cast(data); - Q_EMIT self->actionAppear(name); + + DBusActionVisiblityEvent dave(name, true); + QCoreApplication::sendEvent(self, &dave); } /*! \internal */ void QDBusActionGroup::onActionRemoved(GDBusActionGroup *, gchar *name, gpointer data) { QDBusActionGroup *self = reinterpret_cast(data); - Q_EMIT self->actionVanish(name); + + DBusActionVisiblityEvent dave(name, false); + QCoreApplication::sendEvent(self, &dave); } /*! \internal */ void QDBusActionGroup::onActionStateChanged(GDBusActionGroup *, gchar *name, GVariant *value, gpointer data) { QDBusActionGroup *self = reinterpret_cast(data); - Q_EMIT self->actionStateChanged(name, Converter::toQVariant(value)); + + DBusActionStateEvent dase(name, Converter::toQVariant(value)); + QCoreApplication::sendEvent(self, &dase); +} + +DBusActionEvent::DBusActionEvent(const QString& _name, QEvent::Type type) + : QEvent(type), + name(_name) +{ +} + +DBusActionVisiblityEvent::DBusActionVisiblityEvent(const QString& _name, bool _visible) + : DBusActionEvent(_name, DBusActionVisiblityEvent::eventType), + visible(_visible) +{ +} + +DBusActionStateEvent::DBusActionStateEvent(const QString& _name, const QVariant& _state) + : DBusActionEvent(_name, DBusActionStateEvent::eventType), + state(_state) +{ } diff --git a/libqmenumodel/src/qdbusactiongroup.h b/libqmenumodel/src/qdbusactiongroup.h index 0292a8f..dde33a4 100644 --- a/libqmenumodel/src/qdbusactiongroup.h +++ b/libqmenumodel/src/qdbusactiongroup.h @@ -24,6 +24,7 @@ #include #include +#include class QStateAction; @@ -69,6 +70,8 @@ protected: virtual void serviceAppear(GDBusConnection *connection); virtual void serviceVanish(GDBusConnection *connection); + virtual bool event(QEvent* e); + private: GActionGroup *m_actionGroup; int m_signalActionAddId; @@ -89,4 +92,33 @@ private: static void onActionStateChanged(GDBusActionGroup *ag, gchar *name, GVariant *value, gpointer data); }; -#endif +class DBusActionEvent : public QEvent +{ +public: + QString name; + +protected: + DBusActionEvent(const QString& name, QEvent::Type type); +}; + +class DBusActionVisiblityEvent : public DBusActionEvent +{ +public: + static const QEvent::Type eventType; + DBusActionVisiblityEvent(const QString& name, bool visible); + + bool visible; +}; + + +class DBusActionStateEvent : public DBusActionEvent +{ +public: + static const QEvent::Type eventType; + + DBusActionStateEvent(const QString& name, const QVariant& state); + + QVariant state; +}; + +#endif // QDBUSACTIONGROUP_H diff --git a/libqmenumodel/src/qdbusmenumodel.cpp b/libqmenumodel/src/qdbusmenumodel.cpp index 284b08b..572ad75 100644 --- a/libqmenumodel/src/qdbusmenumodel.cpp +++ b/libqmenumodel/src/qdbusmenumodel.cpp @@ -23,6 +23,9 @@ extern "C" { #include "qdbusmenumodel.h" +#include +#include + /*! \qmltype QDBusMenuModel \inherits QDBusObject @@ -49,7 +52,8 @@ extern "C" { \endcode */ QDBusMenuModel::QDBusMenuModel(QObject *parent) - : QMenuModel(0, parent) + : QMenuModel(0, parent), + QDBusObject(this) { } @@ -83,13 +87,24 @@ void QDBusMenuModel::start() void QDBusMenuModel::stop() { QDBusObject::disconnect(); - setMenuModel(NULL); + + MenuModelEvent mme(NULL); + QCoreApplication::sendEvent(this, &mme); +} + +bool QDBusMenuModel::event(QEvent* e) +{ + if (QDBusObject::event(e)) { + return true; + } + return QMenuModel::event(e); } /*! \internal */ void QDBusMenuModel::serviceVanish(GDBusConnection *) { - setMenuModel(NULL); + MenuModelEvent mme(NULL); + QCoreApplication::sendEvent(this, &mme); } /*! \internal */ @@ -98,7 +113,9 @@ void QDBusMenuModel::serviceAppear(GDBusConnection *connection) GMenuModel *model = G_MENU_MODEL(g_dbus_menu_model_get(connection, busName().toUtf8().data(), objectPath().toUtf8().data())); - setMenuModel(model); + + MenuModelEvent mme(model); + QCoreApplication::sendEvent(this, &mme); //setModel take care of the ref g_object_unref(model); } diff --git a/libqmenumodel/src/qdbusmenumodel.h b/libqmenumodel/src/qdbusmenumodel.h index 6943613..3474ce0 100644 --- a/libqmenumodel/src/qdbusmenumodel.h +++ b/libqmenumodel/src/qdbusmenumodel.h @@ -49,6 +49,8 @@ protected: virtual void serviceAppear(GDBusConnection *connection); virtual void serviceVanish(GDBusConnection *connection); + virtual bool event(QEvent* e); + private: // workaround to support int as busType void setIntBusType(int busType); diff --git a/libqmenumodel/src/qdbusobject.cpp b/libqmenumodel/src/qdbusobject.cpp index c586972..f289bf9 100644 --- a/libqmenumodel/src/qdbusobject.cpp +++ b/libqmenumodel/src/qdbusobject.cpp @@ -25,6 +25,9 @@ extern "C" { #include "qdbusobject.h" #include +#include + +const QEvent::Type DbusObjectServiceEvent::eventType = static_cast(QEvent::registerEventType()); /*! \qmltype QDBusObject @@ -73,8 +76,9 @@ extern "C" { \endlist */ -QDBusObject::QDBusObject() - :m_watchId(0), +QDBusObject::QDBusObject(QObject* listener) + :m_listener(listener), + m_watchId(0), m_busType(DBusEnums::None), m_status(DBusEnums::Disconnected) { @@ -181,14 +185,51 @@ void QDBusObject::onServiceAppeared(GDBusConnection *connection, const gchar *, { QDBusObject *self = reinterpret_cast(data); - self->serviceAppear(connection); - self->setStatus(DBusEnums::Connected); + if (self->m_listener) { + DbusObjectServiceEvent dose(connection, true); + QCoreApplication::sendEvent(self->m_listener, &dose); + } } void QDBusObject::onServiceVanished(GDBusConnection *connection, const gchar *, gpointer data) { - QDBusObject *self = reinterpret_cast(data); + QDBusObject *self = reinterpret_cast(data); + + if (self->m_listener) { + DbusObjectServiceEvent dose(connection, false); + QCoreApplication::sendEvent(self->m_listener, &dose); + } +} + +bool QDBusObject::event(QEvent* e) +{ + if (e->type() == DbusObjectServiceEvent::eventType) { + DbusObjectServiceEvent *dose = static_cast(e); + if (dose->visible) { + serviceAppear(dose->connection); + setStatus(DBusEnums::Connected); + } else { + setStatus(DBusEnums::Connecting); + serviceVanish(dose->connection); + } + return true; + } + return false; +} + +DbusObjectServiceEvent::DbusObjectServiceEvent(GDBusConnection* _connection, bool _visible) + : QEvent(DbusObjectServiceEvent::eventType), + connection(_connection), + visible(_visible) +{ + if (connection) { + g_object_ref(connection); + } +} - self->setStatus(DBusEnums::Connecting); - self->serviceVanish(connection); +DbusObjectServiceEvent::~DbusObjectServiceEvent() +{ + if (connection) { + g_object_unref(connection); + } } diff --git a/libqmenumodel/src/qdbusobject.h b/libqmenumodel/src/qdbusobject.h index 324d3fd..747a7fc 100644 --- a/libqmenumodel/src/qdbusobject.h +++ b/libqmenumodel/src/qdbusobject.h @@ -21,6 +21,7 @@ #define QDBUSOBJECT_H #include +#include #include "dbus-enums.h" @@ -32,7 +33,7 @@ typedef struct _GDBusConnection GDBusConnection; class QDBusObject { public: - QDBusObject(); + QDBusObject(QObject* listener); ~QDBusObject(); DBusEnums::BusType busType() const; @@ -59,7 +60,10 @@ protected: virtual void objectPathChanged(const QString &objectPath) = 0; virtual void statusChanged(DBusEnums::ConnectionStatus status) = 0; + virtual bool event(QEvent* e); + private: + QObject* m_listener; guint m_watchId; DBusEnums::BusType m_busType; QString m_busName; @@ -73,4 +77,15 @@ private: static void onServiceVanished(GDBusConnection *connection, const gchar *name, gpointer data); }; +class DbusObjectServiceEvent : public QEvent +{ +public: + static const QEvent::Type eventType; + DbusObjectServiceEvent(GDBusConnection* connection, bool visible); + ~DbusObjectServiceEvent(); + + GDBusConnection* connection; + bool visible; +}; + #endif diff --git a/libqmenumodel/src/qmenumodel.cpp b/libqmenumodel/src/qmenumodel.cpp index 9bc9e02..2b1397a 100644 --- a/libqmenumodel/src/qmenumodel.cpp +++ b/libqmenumodel/src/qmenumodel.cpp @@ -28,6 +28,8 @@ extern "C" { #include #include +const QEvent::Type MenuModelEvent::eventType = static_cast(QEvent::registerEventType()); + /*! \qmltype QMenuModel \brief The QMenuModel class implements the base list model for menus @@ -225,28 +227,37 @@ QVariant QMenuModel::getExtraProperties(MenuNode *node, int row) const return extra; } -/*! \internal */ -void QMenuModel::onItemsChanged(MenuNode *node, - int position, - int removed, - int added) +bool QMenuModel::event(QEvent* e) { - QModelIndex index = indexFromNode(node); - if (removed > 0) { - beginRemoveRows(index, position, position + removed - 1); + if (e->type() == MenuNodeItemChangeEvent::eventType) { + MenuNodeItemChangeEvent *mnice = static_cast(e); - node->commitOperation(); + QModelIndex index = indexFromNode(mnice->node); + if (mnice->removed > 0) { + beginRemoveRows(index, mnice->position, mnice->position + mnice->removed - 1); - endRemoveRows(); - } + mnice->node->commitOperation(); + + endRemoveRows(); + } + + if (mnice->added > 0) { + beginInsertRows(index, mnice->position, mnice->position + mnice->added - 1); + + mnice->node->commitOperation(); + + endInsertRows(); + } + return true; - if (added > 0) { - beginInsertRows(index, position, position + added - 1); + } else if (e->type() == MenuModelEvent::eventType) { - node->commitOperation(); + MenuModelEvent *mme = static_cast(e); - endInsertRows(); + setMenuModel(mme->model); + return true; } + return QAbstractItemModel::event(e); } /*! \internal */ @@ -292,3 +303,19 @@ bool QMenuModel::hasLink(MenuNode *node, int row, const QString &linkType) const MenuNode *child = node->child(row); return (child && (child->linkType() == linkType)); } + +MenuModelEvent::MenuModelEvent(GMenuModel* _model) + : QEvent(MenuModelEvent::eventType), + model(_model) +{ + if (model) { + g_object_ref(model); + } +} + +MenuModelEvent::~MenuModelEvent() +{ + if (model) { + g_object_unref(model); + } +} diff --git a/libqmenumodel/src/qmenumodel.h b/libqmenumodel/src/qmenumodel.h index 8887c81..54c09f5 100644 --- a/libqmenumodel/src/qmenumodel.h +++ b/libqmenumodel/src/qmenumodel.h @@ -21,6 +21,7 @@ #define QMENUTREEMODEL_H #include +#include class MenuNode; typedef struct _GMenuModel GMenuModel; @@ -28,7 +29,6 @@ typedef struct _GMenuModel GMenuModel; class QMenuModel : public QAbstractItemModel { Q_OBJECT - public: enum MenuRoles { Action = Qt::DisplayRole + 1, @@ -52,14 +52,13 @@ public: Q_SIGNALS: void countChanged(); -public Q_SLOTS: - void onItemsChanged(MenuNode *node, int position, int removed, int added); - protected: QMenuModel(GMenuModel *other=0, QObject *parent=0); void setMenuModel(GMenuModel *model); GMenuModel *menuModel() const; + virtual bool event(QEvent* e); + private: MenuNode *m_root; @@ -74,4 +73,15 @@ private: void clearModel(); }; +class MenuModelEvent : public QEvent +{ +public: + static const QEvent::Type eventType; + + MenuModelEvent(GMenuModel *model); + ~MenuModelEvent(); + + GMenuModel *model; +}; + #endif -- cgit v1.2.3 From 9be29519eb308eae8a2fafe7441324a53220726b Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Fri, 9 Aug 2013 08:10:21 +0100 Subject: bit of code cleanup --- libqmenumodel/src/qdbusactiongroup.cpp | 2 -- libqmenumodel/src/qdbusmenumodel.cpp | 3 +-- libqmenumodel/src/qdbusobject.h | 1 + 3 files changed, 2 insertions(+), 4 deletions(-) (limited to 'libqmenumodel') diff --git a/libqmenumodel/src/qdbusactiongroup.cpp b/libqmenumodel/src/qdbusactiongroup.cpp index 48aa882..72e3302 100644 --- a/libqmenumodel/src/qdbusactiongroup.cpp +++ b/libqmenumodel/src/qdbusactiongroup.cpp @@ -22,7 +22,6 @@ #include "converter.h" // Qt -#include #include extern "C" { @@ -231,7 +230,6 @@ bool QDBusActionGroup::event(QEvent* e) { if (QDBusObject::event(e)) { return true; - } else if (e->type() == DBusActionVisiblityEvent::eventType) { DBusActionVisiblityEvent *dave = static_cast(e); diff --git a/libqmenumodel/src/qdbusmenumodel.cpp b/libqmenumodel/src/qdbusmenumodel.cpp index 572ad75..29ee364 100644 --- a/libqmenumodel/src/qdbusmenumodel.cpp +++ b/libqmenumodel/src/qdbusmenumodel.cpp @@ -24,7 +24,6 @@ extern "C" { #include "qdbusmenumodel.h" #include -#include /*! \qmltype QDBusMenuModel @@ -116,7 +115,7 @@ void QDBusMenuModel::serviceAppear(GDBusConnection *connection) MenuModelEvent mme(model); QCoreApplication::sendEvent(this, &mme); - //setModel take care of the ref + //event handling takes care of the ref g_object_unref(model); } diff --git a/libqmenumodel/src/qdbusobject.h b/libqmenumodel/src/qdbusobject.h index 747a7fc..3a00301 100644 --- a/libqmenumodel/src/qdbusobject.h +++ b/libqmenumodel/src/qdbusobject.h @@ -60,6 +60,7 @@ protected: virtual void objectPathChanged(const QString &objectPath) = 0; virtual void statusChanged(DBusEnums::ConnectionStatus status) = 0; + // This is not a Qbject, but we are passed events from superclass qobjects. virtual bool event(QEvent* e); private: -- cgit v1.2.3 From db47d077dcd9ee6761de828c9194d3fa2331f9ea Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Fri, 9 Aug 2013 09:01:43 +0100 Subject: moved events to separate file. --- libqmenumodel/src/CMakeLists.txt | 1 + libqmenumodel/src/menunode.cpp | 11 +--- libqmenumodel/src/menunode.h | 14 ----- libqmenumodel/src/qdbusactiongroup.cpp | 22 +------- libqmenumodel/src/qdbusactiongroup.h | 30 ----------- libqmenumodel/src/qdbusmenumodel.cpp | 1 + libqmenumodel/src/qdbusobject.cpp | 20 +------ libqmenumodel/src/qdbusobject.h | 12 ----- libqmenumodel/src/qmenumodel.cpp | 20 +------ libqmenumodel/src/qmenumodel.h | 13 +---- libqmenumodel/src/qmenumodelevents.cpp | 96 +++++++++++++++++++++++++++++++++ libqmenumodel/src/qmenumodelevents.h | 99 ++++++++++++++++++++++++++++++++++ 12 files changed, 203 insertions(+), 136 deletions(-) create mode 100644 libqmenumodel/src/qmenumodelevents.cpp create mode 100644 libqmenumodel/src/qmenumodelevents.h (limited to 'libqmenumodel') diff --git a/libqmenumodel/src/CMakeLists.txt b/libqmenumodel/src/CMakeLists.txt index 1fb89df..2f7aac8 100644 --- a/libqmenumodel/src/CMakeLists.txt +++ b/libqmenumodel/src/CMakeLists.txt @@ -8,6 +8,7 @@ set(QMENUMODEL_SRC qdbusobject.cpp qdbusmenumodel.cpp qdbusactiongroup.cpp + qmenumodelevents.cpp qstateaction.cpp ) diff --git a/libqmenumodel/src/menunode.cpp b/libqmenumodel/src/menunode.cpp index 10313d5..f244f40 100644 --- a/libqmenumodel/src/menunode.cpp +++ b/libqmenumodel/src/menunode.cpp @@ -18,13 +18,12 @@ */ #include "menunode.h" +#include "qmenumodelevents.h" #include #include #include -const QEvent::Type MenuNodeItemChangeEvent::eventType = static_cast(QEvent::registerEventType()); - MenuNode::MenuNode(const QString &linkType, GMenuModel *model, MenuNode *parent, int pos, QObject *listener) : m_model(model), m_parent(parent), @@ -250,11 +249,3 @@ void MenuNode::onItemsChanged(GMenuModel *model, gint position, gint removed, gi self->commitOperation(); } - -MenuNodeItemChangeEvent::MenuNodeItemChangeEvent(MenuNode* _node, int _position, int _removed, int _added) - : QEvent(MenuNodeItemChangeEvent::eventType), - node(_node), - position(_position), - removed(_removed), - added(_added) -{} diff --git a/libqmenumodel/src/menunode.h b/libqmenumodel/src/menunode.h index 9e22e82..21fc5bc 100644 --- a/libqmenumodel/src/menunode.h +++ b/libqmenumodel/src/menunode.h @@ -24,7 +24,6 @@ #include #include #include -#include extern "C" { #include @@ -74,17 +73,4 @@ private: static void onItemsChanged(GMenuModel *model, gint position, gint removed, gint added, gpointer data); }; -class MenuNodeItemChangeEvent : public QEvent -{ -public: - static const QEvent::Type eventType; - - MenuNodeItemChangeEvent(MenuNode* node, int position, int removed, int added); - - MenuNode* node; - int position; - int removed; - int added; -}; - #endif diff --git a/libqmenumodel/src/qdbusactiongroup.cpp b/libqmenumodel/src/qdbusactiongroup.cpp index 72e3302..5000470 100644 --- a/libqmenumodel/src/qdbusactiongroup.cpp +++ b/libqmenumodel/src/qdbusactiongroup.cpp @@ -20,6 +20,7 @@ #include "qdbusactiongroup.h" #include "qstateaction.h" #include "converter.h" +#include "qmenumodelevents.h" // Qt #include @@ -29,9 +30,6 @@ extern "C" { #include } -const QEvent::Type DBusActionStateEvent::eventType = static_cast(QEvent::registerEventType()); -const QEvent::Type DBusActionVisiblityEvent::eventType = static_cast(QEvent::registerEventType()); - /*! \qmltype QDBusActionGroup \inherits QDBusObject @@ -272,21 +270,3 @@ void QDBusActionGroup::onActionStateChanged(GDBusActionGroup *, gchar *name, GVa DBusActionStateEvent dase(name, Converter::toQVariant(value)); QCoreApplication::sendEvent(self, &dase); } - -DBusActionEvent::DBusActionEvent(const QString& _name, QEvent::Type type) - : QEvent(type), - name(_name) -{ -} - -DBusActionVisiblityEvent::DBusActionVisiblityEvent(const QString& _name, bool _visible) - : DBusActionEvent(_name, DBusActionVisiblityEvent::eventType), - visible(_visible) -{ -} - -DBusActionStateEvent::DBusActionStateEvent(const QString& _name, const QVariant& _state) - : DBusActionEvent(_name, DBusActionStateEvent::eventType), - state(_state) -{ -} diff --git a/libqmenumodel/src/qdbusactiongroup.h b/libqmenumodel/src/qdbusactiongroup.h index dde33a4..f369a4e 100644 --- a/libqmenumodel/src/qdbusactiongroup.h +++ b/libqmenumodel/src/qdbusactiongroup.h @@ -24,7 +24,6 @@ #include #include -#include class QStateAction; @@ -92,33 +91,4 @@ private: static void onActionStateChanged(GDBusActionGroup *ag, gchar *name, GVariant *value, gpointer data); }; -class DBusActionEvent : public QEvent -{ -public: - QString name; - -protected: - DBusActionEvent(const QString& name, QEvent::Type type); -}; - -class DBusActionVisiblityEvent : public DBusActionEvent -{ -public: - static const QEvent::Type eventType; - DBusActionVisiblityEvent(const QString& name, bool visible); - - bool visible; -}; - - -class DBusActionStateEvent : public DBusActionEvent -{ -public: - static const QEvent::Type eventType; - - DBusActionStateEvent(const QString& name, const QVariant& state); - - QVariant state; -}; - #endif // QDBUSACTIONGROUP_H diff --git a/libqmenumodel/src/qdbusmenumodel.cpp b/libqmenumodel/src/qdbusmenumodel.cpp index 29ee364..f8b4a51 100644 --- a/libqmenumodel/src/qdbusmenumodel.cpp +++ b/libqmenumodel/src/qdbusmenumodel.cpp @@ -22,6 +22,7 @@ extern "C" { } #include "qdbusmenumodel.h" +#include "qmenumodelevents.h" #include diff --git a/libqmenumodel/src/qdbusobject.cpp b/libqmenumodel/src/qdbusobject.cpp index f289bf9..be9fb93 100644 --- a/libqmenumodel/src/qdbusobject.cpp +++ b/libqmenumodel/src/qdbusobject.cpp @@ -23,12 +23,11 @@ extern "C" { } #include "qdbusobject.h" +#include "qmenumodelevents.h" #include #include -const QEvent::Type DbusObjectServiceEvent::eventType = static_cast(QEvent::registerEventType()); - /*! \qmltype QDBusObject \brief The QDBusObject is a base class @@ -216,20 +215,3 @@ bool QDBusObject::event(QEvent* e) } return false; } - -DbusObjectServiceEvent::DbusObjectServiceEvent(GDBusConnection* _connection, bool _visible) - : QEvent(DbusObjectServiceEvent::eventType), - connection(_connection), - visible(_visible) -{ - if (connection) { - g_object_ref(connection); - } -} - -DbusObjectServiceEvent::~DbusObjectServiceEvent() -{ - if (connection) { - g_object_unref(connection); - } -} diff --git a/libqmenumodel/src/qdbusobject.h b/libqmenumodel/src/qdbusobject.h index 3a00301..1a87844 100644 --- a/libqmenumodel/src/qdbusobject.h +++ b/libqmenumodel/src/qdbusobject.h @@ -21,7 +21,6 @@ #define QDBUSOBJECT_H #include -#include #include "dbus-enums.h" @@ -78,15 +77,4 @@ private: static void onServiceVanished(GDBusConnection *connection, const gchar *name, gpointer data); }; -class DbusObjectServiceEvent : public QEvent -{ -public: - static const QEvent::Type eventType; - DbusObjectServiceEvent(GDBusConnection* connection, bool visible); - ~DbusObjectServiceEvent(); - - GDBusConnection* connection; - bool visible; -}; - #endif diff --git a/libqmenumodel/src/qmenumodel.cpp b/libqmenumodel/src/qmenumodel.cpp index 2b1397a..e77419f 100644 --- a/libqmenumodel/src/qmenumodel.cpp +++ b/libqmenumodel/src/qmenumodel.cpp @@ -25,11 +25,11 @@ extern "C" { #include "qmenumodel.h" #include "menunode.h" #include "converter.h" +#include "qmenumodelevents.h" + #include #include -const QEvent::Type MenuModelEvent::eventType = static_cast(QEvent::registerEventType()); - /*! \qmltype QMenuModel \brief The QMenuModel class implements the base list model for menus @@ -303,19 +303,3 @@ bool QMenuModel::hasLink(MenuNode *node, int row, const QString &linkType) const MenuNode *child = node->child(row); return (child && (child->linkType() == linkType)); } - -MenuModelEvent::MenuModelEvent(GMenuModel* _model) - : QEvent(MenuModelEvent::eventType), - model(_model) -{ - if (model) { - g_object_ref(model); - } -} - -MenuModelEvent::~MenuModelEvent() -{ - if (model) { - g_object_unref(model); - } -} diff --git a/libqmenumodel/src/qmenumodel.h b/libqmenumodel/src/qmenumodel.h index 54c09f5..bbb35a0 100644 --- a/libqmenumodel/src/qmenumodel.h +++ b/libqmenumodel/src/qmenumodel.h @@ -21,7 +21,6 @@ #define QMENUTREEMODEL_H #include -#include class MenuNode; typedef struct _GMenuModel GMenuModel; @@ -29,6 +28,7 @@ typedef struct _GMenuModel GMenuModel; class QMenuModel : public QAbstractItemModel { Q_OBJECT + public: enum MenuRoles { Action = Qt::DisplayRole + 1, @@ -73,15 +73,4 @@ private: void clearModel(); }; -class MenuModelEvent : public QEvent -{ -public: - static const QEvent::Type eventType; - - MenuModelEvent(GMenuModel *model); - ~MenuModelEvent(); - - GMenuModel *model; -}; - #endif diff --git a/libqmenumodel/src/qmenumodelevents.cpp b/libqmenumodel/src/qmenumodelevents.cpp new file mode 100644 index 0000000..c705d5c --- /dev/null +++ b/libqmenumodel/src/qmenumodelevents.cpp @@ -0,0 +1,96 @@ +/* + * 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: + * Nicholas Dedekind +#include +} + +#include "qmenumodelevents.h" + +const QEvent::Type MenuNodeItemChangeEvent::eventType = static_cast(QEvent::registerEventType()); +const QEvent::Type DBusActionStateEvent::eventType = static_cast(QEvent::registerEventType()); +const QEvent::Type DBusActionVisiblityEvent::eventType = static_cast(QEvent::registerEventType()); +const QEvent::Type MenuModelEvent::eventType = static_cast(QEvent::registerEventType()); +const QEvent::Type DbusObjectServiceEvent::eventType = static_cast(QEvent::registerEventType()); + +MenuNodeItemChangeEvent::MenuNodeItemChangeEvent(MenuNode* _node, int _position, int _removed, int _added) + : QEvent(MenuNodeItemChangeEvent::eventType), + node(_node), + position(_position), + removed(_removed), + added(_added) +{} + + +DBusActionEvent::DBusActionEvent(const QString& _name, QEvent::Type type) + : QEvent(type), + name(_name) +{ +} + + +DBusActionVisiblityEvent::DBusActionVisiblityEvent(const QString& _name, bool _visible) + : DBusActionEvent(_name, DBusActionVisiblityEvent::eventType), + visible(_visible) +{ +} + + +DBusActionStateEvent::DBusActionStateEvent(const QString& _name, const QVariant& _state) + : DBusActionEvent(_name, DBusActionStateEvent::eventType), + state(_state) +{ +} + + +DbusObjectServiceEvent::DbusObjectServiceEvent(GDBusConnection* _connection, bool _visible) + : QEvent(DbusObjectServiceEvent::eventType), + connection(_connection), + visible(_visible) +{ + if (connection) { + g_object_ref(connection); + } +} + + +DbusObjectServiceEvent::~DbusObjectServiceEvent() +{ + if (connection) { + g_object_unref(connection); + } +} + + +MenuModelEvent::MenuModelEvent(GMenuModel* _model) + : QEvent(MenuModelEvent::eventType), + model(_model) +{ + if (model) { + g_object_ref(model); + } +} + +MenuModelEvent::~MenuModelEvent() +{ + if (model) { + g_object_unref(model); + } +} diff --git a/libqmenumodel/src/qmenumodelevents.h b/libqmenumodel/src/qmenumodelevents.h new file mode 100644 index 0000000..be8f80d --- /dev/null +++ b/libqmenumodel/src/qmenumodelevents.h @@ -0,0 +1,99 @@ +/* + * 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: + * Nicholas Dedekind +#include + +class MenuNode; +typedef struct _GDBusConnection GDBusConnection; +typedef struct _GMenuModel GMenuModel; + +/* Event for a connection update for a dbus object */ +class DbusObjectServiceEvent : public QEvent +{ +public: + static const QEvent::Type eventType; + DbusObjectServiceEvent(GDBusConnection* connection, bool visible); + ~DbusObjectServiceEvent(); + + GDBusConnection* connection; + bool visible; +}; + +/* Event for an update to the gmenumodel */ +class MenuModelEvent : public QEvent +{ +public: + static const QEvent::Type eventType; + + MenuModelEvent(GMenuModel *model); + ~MenuModelEvent(); + + GMenuModel *model; +}; + +/* Event for a GAction (base) */ +class DBusActionEvent : public QEvent +{ +public: + QString name; + +protected: + DBusActionEvent(const QString& name, QEvent::Type type); +}; + +/* Event for a GAction add/remove */ +class DBusActionVisiblityEvent : public DBusActionEvent +{ +public: + static const QEvent::Type eventType; + DBusActionVisiblityEvent(const QString& name, bool visible); + + bool visible; +}; + +/* Event for a GAction state value update */ +class DBusActionStateEvent : public DBusActionEvent +{ +public: + static const QEvent::Type eventType; + + DBusActionStateEvent(const QString& name, const QVariant& state); + + QVariant state; +}; + +/* Event for changing gmenumodel entries */ +class MenuNodeItemChangeEvent : public QEvent +{ +public: + static const QEvent::Type eventType; + + MenuNodeItemChangeEvent(MenuNode* node, int position, int removed, int added); + + MenuNode* node; + int position; + int removed; + int added; +}; + +#endif //QMENUMODELEVENTS_H -- cgit v1.2.3 From 67394e49c25cb273b53cd2034bb2d31b14a7402a Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Thu, 15 Aug 2013 12:48:55 +0100 Subject: Added toGVariantWithSchema for UnityMenuModel::changeState --- libqmenumodel/src/converter.cpp | 132 +++++++++++++++++++++++++++++++++++ libqmenumodel/src/converter.h | 4 ++ libqmenumodel/src/unitymenumodel.cpp | 16 ++++- 3 files changed, 150 insertions(+), 2 deletions(-) (limited to 'libqmenumodel') diff --git a/libqmenumodel/src/converter.cpp b/libqmenumodel/src/converter.cpp index d9d90bd..69e6629 100644 --- a/libqmenumodel/src/converter.cpp +++ b/libqmenumodel/src/converter.cpp @@ -195,3 +195,135 @@ GVariant* Converter::toGVariant(const QVariant &value) return result; } +GVariant* Converter::toGVariantWithSchema(const QVariant &value, const char* schema) +{ + if (!g_variant_type_string_is_valid(schema)) { + return Converter::toGVariant(value); + } + + GVariant* result = NULL; + const GVariantType* schema_type; + schema_type = g_variant_type_new(schema); + + if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_BOOLEAN)) { + if (value.canConvert()) { + result = g_variant_new_boolean (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_BYTE)) { + if (value.canConvert()) { + result = g_variant_new_byte (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_INT16)) { + if (value.canConvert()) { + result = g_variant_new_int16 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_UINT16)) { + if (value.canConvert()) { + result = g_variant_new_uint16 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_INT32)) { + if (value.canConvert()) { + result = g_variant_new_int32 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_UINT32)) { + if (value.canConvert()) { + result = g_variant_new_uint32 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_INT64)) { + if (value.canConvert()) { + result = g_variant_new_int64 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_UINT64)) { + if (value.canConvert()) { + result = g_variant_new_uint64 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_DOUBLE)) { + if (value.canConvert()) { + result = g_variant_new_double (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_STRING)) { + if (value.canConvert()) { + result = g_variant_new_string(value.toString().toUtf8().data()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_VARIANT)) { + result = Converter::toGVariant(value); + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_VARDICT)) { + if (value.canConvert(QVariant::Map)) { + result = Converter::toGVariant(value.toMap()); + } + } else if (g_variant_type_is_array(schema_type)) { + if (value.canConvert(QVariant::List)) { + + const GVariantType* entry_type; + GVariant* data; + entry_type = g_variant_type_element(schema_type); + gchar* entryTypeString = g_variant_type_dup_string(entry_type); + + QVariantList lst = value.toList(); + GVariant **vars = g_new(GVariant*, lst.size()); + + bool ok = true; + for (int i=0; i < lst.size(); i++) { + data = Converter::toGVariantWithSchema(lst[i], entryTypeString); + + if (data) { + vars[i] = data; + } + else { + ok = false; + qWarning() << "Failed to convert list to array with schema:" << schema; + break; + } + } + if (ok) { + result = g_variant_new_array(entry_type, vars, lst.size()); + } + g_free(entryTypeString); + g_free(vars); + } + } else if (g_variant_type_is_tuple(schema_type)) { + if (value.canConvert(QVariant::List)) { + GVariant* data; + + QVariantList lst = value.toList(); + GVariant **vars = g_new(GVariant*, lst.size()); + + const GVariantType* entry_type = g_variant_type_first(schema_type); + + bool ok = true; + for (int i=0; i < lst.size(); i++) { + + gchar* entryTypeString = g_variant_type_dup_string(entry_type); + + data = Converter::toGVariantWithSchema(lst[i], entryTypeString); + + if (data) { + vars[i] = data; + } + else { + ok = false; + qWarning() << "Failed to convert list to tuple with schema:" << schema; + g_free(entryTypeString); + break; + } + g_free(entryTypeString); + + entry_type = g_variant_type_next(entry_type); + if (!entry_type) { + break; + } + } + if (ok) { + result = g_variant_new_tuple(vars, lst.size()); + } + g_free(vars); + } + } + + // fallback to straight convert. + if (!result) { + result = Converter::toGVariant(value); + } + return result; +} + diff --git a/libqmenumodel/src/converter.h b/libqmenumodel/src/converter.h index 5f05bc7..f47c09e 100644 --- a/libqmenumodel/src/converter.h +++ b/libqmenumodel/src/converter.h @@ -28,6 +28,10 @@ class Converter public: static QVariant toQVariant(GVariant *value); static GVariant* toGVariant(const QVariant &value); + + // This converts a QVariant to a GVariant using a provided gvariant schema as + // a conversion base (it will attempt to convert to this format). + static GVariant* toGVariantWithSchema(const QVariant &value, const char* schema); }; #endif // CONVERTER_H diff --git a/libqmenumodel/src/unitymenumodel.cpp b/libqmenumodel/src/unitymenumodel.cpp index e5407ab..50eff1c 100644 --- a/libqmenumodel/src/unitymenumodel.cpp +++ b/libqmenumodel/src/unitymenumodel.cpp @@ -607,13 +607,25 @@ void UnityMenuModel::activate(int index, const QVariant& parameter) void UnityMenuModel::changeState(int index, const QVariant& parameter) { GtkMenuTrackerItem* item; + GVariant* data; + GVariant* current_state; item = (GtkMenuTrackerItem *) g_sequence_get (g_sequence_get_iter_at_pos (priv->items, index)); if (!item) return; - GVariant* data = Converter::toGVariant(parameter); + current_state = gtk_menu_tracker_item_get_action_state (item); + if (current_state) { + // Attempt to convert the parameter to the expected type + data = Converter::toGVariantWithSchema(parameter, g_variant_get_type_string(current_state)); + g_variant_unref (current_state); + } else { + data = Converter::toGVariant(parameter); + } + gtk_menu_tracker_item_change_state (item, data); - g_variant_unref(data); + if (data) { + g_variant_unref(data); + } } -- cgit v1.2.3 From d83555e4e6b771e70237924bea8714df7868808a Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Tue, 20 Aug 2013 19:30:09 +0100 Subject: new unitymenumodel attribute types --- libqmenumodel/src/unitymenumodel.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'libqmenumodel') diff --git a/libqmenumodel/src/unitymenumodel.cpp b/libqmenumodel/src/unitymenumodel.cpp index e5407ab..031de9a 100644 --- a/libqmenumodel/src/unitymenumodel.cpp +++ b/libqmenumodel/src/unitymenumodel.cpp @@ -491,20 +491,34 @@ static QVariant attributeToQVariant(GVariant *value, const QString &type) QVariant result; if (type == "int") { - if (g_variant_is_of_type (value, G_VARIANT_TYPE_INT32)) + if (g_variant_is_of_type (value, G_VARIANT_TYPE_INT32)) { result = QVariant(g_variant_get_int32(value)); + } + } + else if (type == "int64") { + if (g_variant_is_of_type (value, G_VARIANT_TYPE_INT64)) { + result = QVariant((qlonglong)g_variant_get_int64(value)); + } } - if (type == "bool") { - if (g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN)) + else if (type == "bool") { + if (g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN)) { result = QVariant(g_variant_get_boolean(value)); + } } else if (type == "string") { - if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) + if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) { result = QVariant(g_variant_get_string(value, NULL)); + } } - if (type == "double") { - if (g_variant_is_of_type (value, G_VARIANT_TYPE_DOUBLE)) + else if (type == "double") { + if (g_variant_is_of_type (value, G_VARIANT_TYPE_DOUBLE)) { result = QVariant(g_variant_get_double(value)); + } + } + else if (type == "variant") { + if (g_variant_is_of_type (value, G_VARIANT_TYPE_VARIANT)) { + result = Converter::toQVariant(value); + } } else if (type == "icon") { GIcon *icon = g_icon_deserialize (value); -- cgit v1.2.3