diff options
author | Nick Dedekind <nicholas.dedekind@gmail.com> | 2013-08-08 16:12:57 +0100 |
---|---|---|
committer | Nick Dedekind <nicholas.dedekind@gmail.com> | 2013-08-08 16:12:57 +0100 |
commit | c6cb726c5694f8a35711a48a7bf5e2a6723aeba8 (patch) | |
tree | 509f5b5c2cf1223274034721a611c55c5039b0ab /libqmenumodel/src | |
parent | 52b17007596bcd29ec0fe01468d28fddfcc18785 (diff) | |
download | qmenumodel-c6cb726c5694f8a35711a48a7bf5e2a6723aeba8.tar.gz qmenumodel-c6cb726c5694f8a35711a48a7bf5e2a6723aeba8.tar.bz2 qmenumodel-c6cb726c5694f8a35711a48a7bf5e2a6723aeba8.zip |
Glib callbacks send events through qt.
Diffstat (limited to 'libqmenumodel/src')
-rw-r--r-- | libqmenumodel/src/menunode.cpp | 24 | ||||
-rw-r--r-- | libqmenumodel/src/menunode.h | 14 | ||||
-rw-r--r-- | libqmenumodel/src/qdbusactiongroup.cpp | 64 | ||||
-rw-r--r-- | libqmenumodel/src/qdbusactiongroup.h | 34 | ||||
-rw-r--r-- | libqmenumodel/src/qdbusmenumodel.cpp | 25 | ||||
-rw-r--r-- | libqmenumodel/src/qdbusmenumodel.h | 2 | ||||
-rw-r--r-- | libqmenumodel/src/qdbusobject.cpp | 55 | ||||
-rw-r--r-- | libqmenumodel/src/qdbusobject.h | 17 | ||||
-rw-r--r-- | libqmenumodel/src/qmenumodel.cpp | 57 | ||||
-rw-r--r-- | libqmenumodel/src/qmenumodel.h | 18 |
10 files changed, 263 insertions, 47 deletions
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 <QMetaMethod> #include <QDebug> +#include <QCoreApplication> + +const QEvent::Type MenuNodeItemChangeEvent::eventType = static_cast<QEvent::Type>(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 <QPointer> #include <QMap> #include <QVariant> +#include <QEvent> extern "C" { #include <gio/gio.h> @@ -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 <QDebug> +#include <QCoreApplication> + extern "C" { #include <glib.h> #include <gio/gio.h> } +const QEvent::Type DBusActionStateEvent::eventType = static_cast<QEvent::Type>(QEvent::registerEventType()); +const QEvent::Type DBusActionVisiblityEvent::eventType = static_cast<QEvent::Type>(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<DBusActionVisiblityEvent*>(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<DBusActionStateEvent*>(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<QDBusActionGroup*>(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<QDBusActionGroup*>(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<QDBusActionGroup*>(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 <QObject> #include <QVariant> +#include <QEvent> 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 <QCoreApplication> +#include <QDebug> + /*! \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 <QDebug> +#include <QCoreApplication> + +const QEvent::Type DbusObjectServiceEvent::eventType = static_cast<QEvent::Type>(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<QDBusObject*>(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<QDBusObject*>(data); + QDBusObject *self = reinterpret_cast<QDBusObject*>(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<DbusObjectServiceEvent*>(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 <QString> +#include <QEvent> #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 <QCoreApplication> #include <QThread> +const QEvent::Type MenuModelEvent::eventType = static_cast<QEvent::Type>(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<MenuNodeItemChangeEvent*>(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<MenuModelEvent*>(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 <QAbstractItemModel> +#include <QEvent> 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 |