on xenial help sign in
Added Nick's UnityMenuModelPaths extension
commit0bd68a7ecd671766aa08f8718fa4e79fe777636a authorMirco Müller <mirco.mueller@ubuntu.com> dateThu 12 Sep 2013, 17:28 (13 years ago) parentba59aae
4 files changed+273−1
plugins/Utils/CMakeLists.txt+2−1 plugins/Utils/plugin.cpp+2 plugins/Utils/unitymenumodelpaths.cpp+186 plugins/Utils/unitymenumodelpaths.h+83
plugins/Utils/CMakeLists.txt+2−1
@@ -9,6 +9,7 @@ set(QMLPLUGIN_SRC
99 qlimitproxymodelqml.cpp
1010 qsortfilterproxymodelqml.cpp
1111 ubuntuwindow.cpp
12+ unitymenumodelpaths.cpp
1213 plugin.cpp
1314 )
1415
@@ -21,7 +22,7 @@ add_library(Utils-qml SHARED
2122# files directly in targets.
2223set_target_properties(Utils-qml PROPERTIES COMPILE_FLAGS -fvisibility=default)
2324
24−qt5_use_modules(Utils-qml Qml Quick)
25+qt5_use_modules(Utils-qml Qml Quick DBus)
2526
2627# export the qmldir qmltypes and plugin files
2728export_qmlfiles(Utils Utils)
plugins/Utils/plugin.cpp+2
@@ -28,6 +28,7 @@
2828#include "qlimitproxymodelqml.h"
2929#include "qsortfilterproxymodelqml.h"
3030#include "ubuntuwindow.h"
31+#include "unitymenumodelpaths.h"
3132
3233static QObject* applicationsPathsSingleton(QQmlEngine* engine, QJSEngine* scriptEngine) {
3334 Q_UNUSED(engine);
@@ -41,6 +42,7 @@ void UtilsPlugin::registerTypes(const char *uri)
4142 qmlRegisterType<QAbstractItemModel>();
4243 qmlRegisterType<QLimitProxyModelQML>(uri, 0, 1, "LimitProxyModel");
4344 qmlRegisterType<QSortFilterProxyModelQML>(uri, 0, 1, "SortFilterProxyModel");
45+ qmlRegisterType<UnityMenuModelPaths>(uri, 0, 1, "UnityMenuModelPaths");
4446 qmlRegisterExtendedType<QQuickWindow, UbuntuWindow>(uri, 0, 1, "Window");
4547 qmlRegisterSingletonType<ApplicationPaths>(uri, 0, 1, "ApplicationPaths", applicationsPathsSingleton);
4648}
plugins/Utils/unitymenumodelpaths.cppnew file+186
@@ -0,0 +1,186 @@
1+/*
2+ * Copyright (C) 2013 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Author: Nick Dedekind <nick.dedekind@canonical.com>
17+ */
18+
19+
20+#include "unitymenumodelpaths.h"
21+#include <QDBusArgument>
22+#include <QDebug>
23+
24+static QVariant parseVariantData(const QVariant& var);
25+
26+UnityMenuModelPaths::UnityMenuModelPaths(QObject *parent)
27+ : QObject(parent)
28+{
29+}
30+
31+QVariant UnityMenuModelPaths::source() const
32+{
33+ return m_sourceData;
34+}
35+
36+void UnityMenuModelPaths::setSource(const QVariant& data)
37+{
38+ if (m_sourceData != data) {
39+ m_sourceData = data;
40+ Q_EMIT sourceChanged();
41+
42+ updateData();
43+ }
44+}
45+
46+const QDBusArgument &operator>>(const QDBusArgument &arg, QVariantMap &map)
47+{
48+ arg.beginMap();
49+ map.clear();
50+ while (!arg.atEnd()) {
51+ QString key;
52+ QVariant value;
53+ arg.beginMapEntry();
54+
55+ arg >> key >> value;
56+ map.insertMulti(key, parseVariantData(value)); // re-parse for qdbusargument
57+
58+ arg.endMapEntry();
59+ }
60+ arg.endMap();
61+ return arg;
62+}
63+
64+static QVariant parseVariantData(const QVariant& var) {
65+ if ((int)var.type() == QMetaType::User) {
66+ if (var.userType() == qMetaTypeId<QDBusArgument>()) {
67+
68+ QDBusArgument arg(var.value<QDBusArgument>());
69+ if (arg.currentType() == QDBusArgument::MapType) {
70+ QVariantMap map;
71+ arg >> map;
72+ return map;
73+ }
74+ return arg.asVariant();
75+ }
76+ }
77+ return var;
78+}
79+
80+void UnityMenuModelPaths::updateData()
81+{
82+ QVariantMap dataMap = parseVariantData(m_sourceData).toMap();
83+
84+ if (!m_busNameHint.isEmpty() && dataMap.contains(m_busNameHint)) {
85+ setBusName(dataMap[m_busNameHint].toByteArray());
86+ } else {
87+ setBusName("");
88+ }
89+
90+ if (!m_menuObjectPathHint.isEmpty() && dataMap.contains(m_menuObjectPathHint)) {
91+ setMenuObjectPath(dataMap[m_menuObjectPathHint].toByteArray());
92+ } else {
93+ setMenuObjectPath("");
94+ }
95+
96+ if (!m_actionsHint.isEmpty() && dataMap.contains(m_actionsHint)) {
97+ setActions(dataMap[m_actionsHint].toMap());
98+ } else {
99+ setActions(QVariantMap());
100+ }
101+}
102+
103+QByteArray UnityMenuModelPaths::busName() const
104+{
105+ return m_busName;
106+}
107+
108+void UnityMenuModelPaths::setBusName(const QByteArray &name)
109+{
110+ if (m_busName != name) {
111+ m_busName = name;
112+ Q_EMIT busNameChanged();
113+ }
114+}
115+
116+QVariantMap UnityMenuModelPaths::actions() const
117+{
118+ return m_actions;
119+}
120+
121+void UnityMenuModelPaths::setActions(const QVariantMap &actions)
122+{
123+ if (m_actions != actions) {
124+ qDebug() << "UnityMenuModelPaths setActions " << actions;
125+ m_actions = actions;
126+ Q_EMIT actionsChanged();
127+ }
128+}
129+
130+QByteArray UnityMenuModelPaths::menuObjectPath() const
131+{
132+ return m_menuObjectPath;
133+}
134+
135+void UnityMenuModelPaths::setMenuObjectPath(const QByteArray &path)
136+{
137+ if (m_menuObjectPath != path) {
138+ m_menuObjectPath = path;
139+ Q_EMIT menuObjectPathChanged();
140+ }
141+}
142+
143+QByteArray UnityMenuModelPaths::busNameHint() const
144+{
145+ return m_busNameHint;
146+}
147+
148+void UnityMenuModelPaths::setBusNameHint(const QByteArray &nameHint)
149+{
150+ if (m_busNameHint != nameHint) {
151+ m_busNameHint = nameHint;
152+ Q_EMIT busNameHintChanged();
153+
154+ updateData();
155+ }
156+}
157+
158+QByteArray UnityMenuModelPaths::actionsHint() const
159+{
160+ return m_actionsHint;
161+}
162+
163+void UnityMenuModelPaths::setActionsHint(const QByteArray &actionsHint)
164+{
165+ if (m_actionsHint != actionsHint) {
166+ m_actionsHint = actionsHint;
167+ Q_EMIT actionsHintChanged();
168+
169+ updateData();
170+ }
171+}
172+
173+QByteArray UnityMenuModelPaths::menuObjectPathHint() const
174+{
175+ return m_menuObjectPathHint;
176+}
177+
178+void UnityMenuModelPaths::setMenuObjectPathHint(const QByteArray &pathHint)
179+{
180+ if (m_menuObjectPathHint != pathHint) {
181+ m_menuObjectPathHint = pathHint;
182+ Q_EMIT menuObjectPathHintChanged();
183+
184+ updateData();
185+ }
186+}
plugins/Utils/unitymenumodelpaths.hnew file+83
@@ -0,0 +1,83 @@
1+/*
2+ * Copyright (C) 2013 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Author: Nick Dedekind <nick.dedekind@canonical.com>
17+ */
18+
19+#ifndef UNITY_MENU_MODEL_PATHS_H
20+#define UNITY_MENU_MODEL_PATHS_H
21+
22+#include <QtQml/QQmlEngine>
23+#include <QtQml/QQmlExtensionPlugin>
24+
25+class UnityMenuModelPaths : public QObject
26+{
27+ Q_OBJECT
28+
29+ Q_PROPERTY(QVariant source READ source WRITE setSource NOTIFY sourceChanged)
30+ Q_PROPERTY(QByteArray busName READ busName NOTIFY busNameChanged)
31+ Q_PROPERTY(QVariantMap actions READ actions NOTIFY actionsChanged)
32+ Q_PROPERTY(QByteArray menuObjectPath READ menuObjectPath NOTIFY menuObjectPathChanged)
33+
34+ Q_PROPERTY(QByteArray busNameHint READ busNameHint WRITE setBusNameHint NOTIFY busNameHintChanged)
35+ Q_PROPERTY(QByteArray actionsHint READ actionsHint WRITE setActionsHint NOTIFY actionsHintChanged)
36+ Q_PROPERTY(QByteArray menuObjectPathHint READ menuObjectPathHint WRITE setMenuObjectPathHint NOTIFY menuObjectPathHintChanged)
37+
38+public:
39+ explicit UnityMenuModelPaths(QObject *parent = 0);
40+
41+ QVariant source() const;
42+ void setSource(const QVariant& data);
43+
44+ QByteArray busName() const;
45+ QVariantMap actions() const;
46+ QByteArray menuObjectPath() const;
47+
48+ QByteArray busNameHint() const;
49+ QByteArray actionsHint() const;
50+ QByteArray menuObjectPathHint() const;
51+
52+Q_SIGNALS:
53+ void sourceChanged();
54+ void busNameChanged();
55+ void actionsChanged();
56+ void menuObjectPathChanged();
57+
58+ void busNameHintChanged();
59+ void actionsHintChanged();
60+ void menuObjectPathHintChanged();
61+
62+private:
63+ void setBusName(const QByteArray &name);
64+ void setActions(const QVariantMap& actions);
65+ void setMenuObjectPath(const QByteArray &path);
66+
67+ void setBusNameHint(const QByteArray& nameHint);
68+ void setActionsHint(const QByteArray &actionsHint);
69+ void setMenuObjectPathHint(const QByteArray &pathHint);
70+
71+ void updateData();
72+
73+ QVariant m_sourceData;
74+ QByteArray m_busName;
75+ QVariantMap m_actions;
76+ QByteArray m_menuObjectPath;
77+
78+ QByteArray m_busNameHint;
79+ QByteArray m_actionsHint;
80+ QByteArray m_menuObjectPathHint;
81+};
82+
83+#endif // UNITY_MENU_MODEL_PATHS_H