From c2746565786204a732ff5531434bf636f8df605c Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Thu, 22 Aug 2013 12:56:43 +0100 Subject: Using qt event loop to pass UnityMenuAction events --- libqmenumodel/src/CMakeLists.txt | 1 + libqmenumodel/src/unitymenuaction.cpp | 71 +++++++++++++++++------------ libqmenumodel/src/unitymenuaction.h | 13 +++--- libqmenumodel/src/unitymenuactionevents.cpp | 46 +++++++++++++++++++ libqmenumodel/src/unitymenuactionevents.h | 65 ++++++++++++++++++++++++++ libqmenumodel/src/unitymenumodel.cpp | 23 ++++++---- 6 files changed, 176 insertions(+), 43 deletions(-) create mode 100644 libqmenumodel/src/unitymenuactionevents.cpp create mode 100644 libqmenumodel/src/unitymenuactionevents.h (limited to 'libqmenumodel') diff --git a/libqmenumodel/src/CMakeLists.txt b/libqmenumodel/src/CMakeLists.txt index be278dd..e360a70 100644 --- a/libqmenumodel/src/CMakeLists.txt +++ b/libqmenumodel/src/CMakeLists.txt @@ -11,6 +11,7 @@ set(QMENUMODEL_SRC qdbusactiongroup.cpp qstateaction.cpp unitymenuaction.cpp + unitymenuactionevents.cpp unitymenumodel.cpp unitymenumodelevents.cpp unitythemediconprovider.cpp diff --git a/libqmenumodel/src/unitymenuaction.cpp b/libqmenumodel/src/unitymenuaction.cpp index c2c7034..b41fd3e 100644 --- a/libqmenumodel/src/unitymenuaction.cpp +++ b/libqmenumodel/src/unitymenuaction.cpp @@ -18,6 +18,7 @@ #include "unitymenuaction.h" #include "unitymenumodel.h" +#include "unitymenuactionevents.h" #include @@ -71,41 +72,20 @@ QVariant UnityMenuAction::state() const return m_state; } -bool UnityMenuAction::isEnabled() const +void UnityMenuAction::setState(const QVariant& state) { - return m_enabled; -} - -bool UnityMenuAction::isValid() const -{ - return m_valid; -} - -void UnityMenuAction::onAdded(bool enabled, const QVariant &state) -{ - if (m_enabled != enabled) { - m_enabled = enabled; - Q_EMIT enabledChanged(m_enabled); - } if (m_state != state) { m_state = state; Q_EMIT stateChanged(m_state); } - if (m_valid != true) { - m_valid = true; - Q_EMIT validChanged(m_valid); - } } -void UnityMenuAction::onRemoved() +bool UnityMenuAction::isEnabled() const { - if (m_valid != false) { - m_valid = false; - Q_EMIT validChanged(m_valid); - } + return m_enabled; } -void UnityMenuAction::onEnabledChanged(bool enabled) +void UnityMenuAction::setEnabled(bool enabled) { if (m_enabled != enabled) { m_enabled = enabled; @@ -113,11 +93,16 @@ void UnityMenuAction::onEnabledChanged(bool enabled) } } -void UnityMenuAction::onStateChanged(const QVariant &state) +bool UnityMenuAction::isValid() const { - if (m_state != state) { - m_state = state; - Q_EMIT stateChanged(m_state); + return m_valid; +} + +void UnityMenuAction::setValid(bool valid) +{ + if (m_valid != valid) { + m_valid = valid; + Q_EMIT validChanged(m_valid); } } @@ -134,3 +119,31 @@ void UnityMenuAction::unregisterAction() m_model->unregisterAction(this); } } + +bool UnityMenuAction::event(QEvent* e) +{ + if (e->type() == UnityMenuActionAddEvent::eventType) { + UnityMenuActionAddEvent *umaae = static_cast(e); + + setEnabled(umaae->enabled); + setState(umaae->state); + setValid(true); + return true; + } else if (e->type() == UnityMenuActionEnabledChangedEvent::eventType) { + UnityMenuActionEnabledChangedEvent *umaece = static_cast(e); + + setEnabled(umaece->enabled); + return true; + } else if (e->type() == UnityMenuActionStateChangeEvent::eventType) { + UnityMenuActionStateChangeEvent *umasce = static_cast(e); + + setState(umasce->state); + return true; + } else if (e->type() == UnityMenuActionRemoveEvent::eventType) { + UnityMenuActionRemoveEvent *umare = static_cast(e); + + setValid(false); + return true; + } + return QObject::event(e); +} diff --git a/libqmenumodel/src/unitymenuaction.h b/libqmenumodel/src/unitymenuaction.h index 7c19fc5..b8391fe 100644 --- a/libqmenumodel/src/unitymenuaction.h +++ b/libqmenumodel/src/unitymenuaction.h @@ -46,12 +46,6 @@ public: bool isEnabled() const; bool isValid() const; -public Q_SLOTS: - void onAdded(bool enabled, const QVariant &state); - void onRemoved(); - void onEnabledChanged(bool enabled); - void onStateChanged(const QVariant &state); - Q_SIGNALS: Q_INVOKABLE void activate(const QVariant& parameter = QVariant()); Q_INVOKABLE void changeState(const QVariant& parameter); @@ -62,6 +56,13 @@ Q_SIGNALS: void enabledChanged(bool enabled); void validChanged(bool valid); +protected: + virtual bool event(QEvent* e); + + void setState(const QVariant& state); + void setEnabled(bool enabled); + void setValid(bool valid); + private: void unregisterAction(); void registerAction(); diff --git a/libqmenumodel/src/unitymenuactionevents.cpp b/libqmenumodel/src/unitymenuactionevents.cpp new file mode 100644 index 0000000..f05e536 --- /dev/null +++ b/libqmenumodel/src/unitymenuactionevents.cpp @@ -0,0 +1,46 @@ +/* + * 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 (QEvent::registerEventType()); +const QEvent::Type UnityMenuActionRemoveEvent::eventType = static_cast(QEvent::registerEventType()); +const QEvent::Type UnityMenuActionEnabledChangedEvent::eventType = static_cast(QEvent::registerEventType()); +const QEvent::Type UnityMenuActionStateChangeEvent::eventType = static_cast(QEvent::registerEventType()); + +UnityMenuActionAddEvent::UnityMenuActionAddEvent(bool _enabled, const QVariant& _state) + : QEvent(UnityMenuActionAddEvent::eventType), + enabled(_enabled), + state(_state) +{} + +UnityMenuActionRemoveEvent::UnityMenuActionRemoveEvent() + : QEvent(UnityMenuActionRemoveEvent::eventType) +{ +} + +UnityMenuActionEnabledChangedEvent::UnityMenuActionEnabledChangedEvent(bool _enabled) + : QEvent(UnityMenuActionEnabledChangedEvent::eventType), + enabled(_enabled) +{} + +UnityMenuActionStateChangeEvent::UnityMenuActionStateChangeEvent(const QVariant& _state) + : QEvent(UnityMenuActionStateChangeEvent::eventType), + state(_state) +{} diff --git a/libqmenumodel/src/unitymenuactionevents.h b/libqmenumodel/src/unitymenuactionevents.h new file mode 100644 index 0000000..44cb5de --- /dev/null +++ b/libqmenumodel/src/unitymenuactionevents.h @@ -0,0 +1,65 @@ +/* + * 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 + +/* Event for a unitymenuaction add */ +class UnityMenuActionAddEvent : public QEvent +{ +public: + static const QEvent::Type eventType; + UnityMenuActionAddEvent(bool enabled, const QVariant& state); + + bool enabled; + QVariant state; +}; + +/* Event for a unitymenuaction remove */ +class UnityMenuActionRemoveEvent : public QEvent +{ +public: + static const QEvent::Type eventType; + UnityMenuActionRemoveEvent(); +}; + +/* Event for change in enabled value of a unitymenuaction */ +class UnityMenuActionEnabledChangedEvent : public QEvent +{ +public: + static const QEvent::Type eventType; + UnityMenuActionEnabledChangedEvent(bool enabled); + + int enabled; +}; + +/* Event for change in state value of a unitymenuaction */ +class UnityMenuActionStateChangeEvent : public QEvent +{ +public: + static const QEvent::Type eventType; + UnityMenuActionStateChangeEvent(const QVariant& state); + + QVariant state; +}; + +#endif //UNITYMENUACTIONEVENTS_H diff --git a/libqmenumodel/src/unitymenumodel.cpp b/libqmenumodel/src/unitymenumodel.cpp index 953af01..b99f406 100644 --- a/libqmenumodel/src/unitymenumodel.cpp +++ b/libqmenumodel/src/unitymenumodel.cpp @@ -21,6 +21,7 @@ #include "actionstateparser.h" #include "unitymenumodelevents.h" #include "unitymenuaction.h" +#include "unitymenuactionevents.h" #include #include @@ -765,7 +766,9 @@ void UnityMenuModel::onRegisteredActionNameChanged(const QString& name) if (g_action_group_query_action (G_ACTION_GROUP (priv->muxer), action_name, &enabled, ¶meter_type, NULL, NULL, &state)) { - action->onAdded(enabled, Converter::toQVariant(state)); + UnityMenuActionAddEvent umaae(enabled, Converter::toQVariant(state)); + QCoreApplication::sendEvent(action, &umaae); + if (state) { g_variant_unref (state); } @@ -805,7 +808,8 @@ void UnityMenuModelPrivate::registeredActionAdded(GtkActionObserverItem *obse action = (UnityMenuAction *) g_object_get_qdata (G_OBJECT (observer_item), unity_menu_action_quark ()); // FIXME - needs to go through event loop if (action) { - action->onAdded(enabled, Converter::toQVariant(state)); + UnityMenuActionAddEvent umaae(enabled, Converter::toQVariant(state)); + QCoreApplication::sendEvent(action, &umaae); } } @@ -813,9 +817,10 @@ void UnityMenuModelPrivate::registeredActionEnabledChanged(GtkActionObserverItem { UnityMenuAction *action; action = (UnityMenuAction *) g_object_get_qdata (G_OBJECT (observer_item), unity_menu_action_quark ()); - // FIXME - needs to go through event loop + if (action) { - action->onEnabledChanged(enabled); + UnityMenuActionEnabledChangedEvent umaece(enabled); + QCoreApplication::sendEvent(action, &umaece); } } @@ -823,9 +828,10 @@ void UnityMenuModelPrivate::registeredActionStateChanged(GtkActionObserverItem * { UnityMenuAction *action; action = (UnityMenuAction *) g_object_get_qdata (G_OBJECT (observer_item), unity_menu_action_quark ()); - // FIXME - needs to go through event loop + if (action) { - action->onStateChanged(Converter::toQVariant(state)); + UnityMenuActionStateChangeEvent umasce(Converter::toQVariant(state)); + QCoreApplication::sendEvent(action, &umasce); } } @@ -833,8 +839,9 @@ void UnityMenuModelPrivate::registeredActionRemoved(GtkActionObserverItem *obser { UnityMenuAction *action; action = (UnityMenuAction *) g_object_get_qdata (G_OBJECT (observer_item), unity_menu_action_quark ()); - // FIXME - needs to go through event loop + if (action) { - action->onRemoved(); + UnityMenuActionRemoveEvent umare; + QCoreApplication::sendEvent(action, &umare); } } -- cgit v1.2.3