on xenial help sign in
Added textual representation of menu model to indicators-client app
423e715Nick Dedekind
Panel/Indicators/client/IndicatorsClient.qml+8−9
@@ -27,16 +27,15 @@ Rectangle {
27
27
28 PageStack {
28 PageStack {
29 id: pages
29 id: pages
30−
31 anchors.fill: parent
30 anchors.fill: parent
32− Component.onCompleted: root.reset()
31+ Component.onCompleted: reset()
33− }
34
32
35− function reset() {
33+ function reset() {
36− pages.clear();
34+ clear();
37− var component = Qt.createComponent("IndicatorsList.qml");
35+ var component = Qt.createComponent("IndicatorsList.qml");
38− var page = component.createObject(pages);
36+ var page = component.createObject(pages);
39− console.log(component.errorString());
37+ console.log(component.errorString());
40− pages.push(page);
38+ push(page);
39+ }
41 }
40 }
42}
41}
Panel/Indicators/client/IndicatorsPage.qml+25−9
@@ -19,31 +19,47 @@
19
19
20import QtQuick 2.0
20import QtQuick 2.0
21import Ubuntu.Components 0.1
21import Ubuntu.Components 0.1
22+import Ubuntu.Components.ListItems 0.1 as ListItem
22
23
23Page {
24Page {
24 id: _page
25 id: _page
25
26
26 title: indicatorProperties && indicatorProperties.title ? indicatorProperties.title : ""
27 title: indicatorProperties && indicatorProperties.title ? indicatorProperties.title : ""
27 property variant indicatorProperties
28 property variant indicatorProperties
28− property alias pageSource : page_loader.source
29+ property string pageSource : page_loader.source
29
30
30 anchors.fill: parent
31 anchors.fill: parent
31
32
33+ ListItem.Standard {
34+ id: button
35+
36+ text: "Enable Visual Representation"
37+ control: Switch {
38+ id: checked
39+ checked: true
40+ }
41+ }
42+
32 Loader {
43 Loader {
33 id: page_loader
44 id: page_loader
34 objectName: "page_loader"
45 objectName: "page_loader"
35
46
36− anchors.fill: parent
47+ anchors {
48+ top: button.bottom
49+ left: parent.left
50+ right: parent.right
51+ bottom: parent.bottom
52+ topMargin: units.gu(2)
53+ }
54+ source : checked.checked ? _page.pageSource : "IndicatorsTree.qml"
37
55
38− onStatusChanged: {
56+ onLoaded: {
39− if (status == Loader.Ready) {
40− for(var pName in indicatorProperties) {
57+ for(var pName in indicatorProperties) {
41− if (item.hasOwnProperty(pName)) {
58+ if (item.hasOwnProperty(pName)) {
42− item[pName] = indicatorProperties[pName];
59+ item[pName] = indicatorProperties[pName];
43− }
44 }
60 }
45− item.start();
46 }
61 }
62+ item.start();
47 }
63 }
48 }
64 }
49}
65}
Panel/Indicators/client/IndicatorsTree.qmlnew file+63
@@ -0,0 +1,63 @@
1+/*
2+ * Copyright 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 Lesser 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 Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Authors:
17+ * Nick Dedekind <nick.dededkind@canonical.com
18+ */
19+
20+import QtQuick 2.0
21+import Ubuntu.Components 0.1
22+import Unity.Indicators 0.1 as Indicators
23+import QMenuModel 0.1
24+
25+Page {
26+ id: page
27+ anchors.fill: parent
28+
29+
30+ property alias busType: menuModel.busType
31+ property alias busName: menuModel.busName
32+ property string actionsObjectPath
33+ property var menuObjectPaths: undefined
34+ readonly property string device: "phone"
35+
36+ property string deviceMenuObjectPath: menuObjectPaths.hasOwnProperty(device) ? menuObjectPaths[device] : ""
37+
38+ function start() {
39+ menuModel.start();
40+ }
41+
42+ QDBusMenuModel {
43+ id: menuModel
44+ objectPath: page.deviceMenuObjectPath
45+ }
46+
47+ Indicators.ModelPrinter {
48+ id: printer
49+ model: menuModel
50+
51+ onSourceChanged: all_data.text = printer.getString()
52+ }
53+
54+ Flickable {
55+ anchors.fill: parent
56+ contentHeight: all_data.height
57+ clip:true
58+ Text {
59+ id: all_data
60+ color: "white"
61+ }
62+ }
63+}
plugins/Unity/Indicators/CMakeLists.txt+1
@@ -20,6 +20,7 @@ set(IndicatorsQML_SOURCES
20 indicators.h
20 indicators.h
21 indicatorsmanager.cpp
21 indicatorsmanager.cpp
22 indicatorsmodel.cpp
22 indicatorsmodel.cpp
23+ modelprinter.cpp
23 plugin.cpp
24 plugin.cpp
24)
25)
25
26
plugins/Unity/Indicators/modelprinter.cppnew file+98
@@ -0,0 +1,98 @@
1+/*
2+ * Copyright (C) 2012 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+
17+// self
18+#include "modelprinter.h"
19+
20+// Qt
21+#include <QDebug>
22+
23+ModelPrinter::ModelPrinter(QObject *parent)
24+ : QObject(parent)
25+ , m_model(NULL)
26+{
27+}
28+
29+void ModelPrinter::setSourceModel(QAbstractItemModel * sourceModel)
30+{
31+ if (m_model != NULL)
32+ {
33+ disconnect(m_model, SIGNAL(modelReset()), this, SIGNAL(sourceChanged()));
34+ disconnect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SIGNAL(sourceChanged()));
35+ disconnect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SIGNAL(sourceChanged()));
36+ }
37+ if (m_model != sourceModel)
38+ {
39+ m_model = sourceModel;
40+ Q_EMIT modelChanged();
41+ Q_EMIT sourceChanged();
42+ }
43+ if (m_model != NULL)
44+ {
45+ connect(m_model, SIGNAL(modelReset()), this, SIGNAL(sourceChanged()));
46+ connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SIGNAL(sourceChanged()));
47+ connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SIGNAL(sourceChanged()));
48+ }
49+}
50+
51+QAbstractItemModel* ModelPrinter::sourceModel() const
52+{
53+ return m_model;
54+}
55+
56+QString ModelPrinter::getString(const QModelIndex& index) const
57+{
58+ return recurse_string(index, 0);
59+}
60+
61+QString tabify(int level) { QString str;
62+ for (int i = 0; i < level; i++) {
63+ str += " ";
64+ }
65+ return str;
66+}
67+
68+QString ModelPrinter::recurse_string(const QModelIndex& index, int level) const
69+{
70+ QString str;
71+ QTextStream stream(&str);
72+
73+ QHash<int, QByteArray> roleNames = m_model->roleNames();
74+ QList<int> roles = roleNames.keys();
75+ qSort(roles);
76+ Q_FOREACH(int role, roles) {
77+ QVariant vData = m_model->data(index, role);
78+ if (vData.canConvert(QMetaType::QVariantMap)) {
79+ QMapIterator<QString, QVariant> iter(vData.toMap());
80+ while (iter.hasNext()) {
81+ iter.next();
82+ stream << tabify(level) << roleNames[role] << "." << iter.key() << ": " << iter.value().toString() << endl;
83+ }
84+ }
85+ else {
86+ stream << tabify(level) << roleNames[role] << ": " << vData.toString() << endl;
87+ }
88+ }
89+
90+ int rowCount = m_model->rowCount(index);
91+ stream << tabify(level) << "child count" << ": " << rowCount << endl;
92+ for (int i = 0; i < rowCount; i++)
93+ {
94+ QModelIndex child = m_model->index(i, 0, index);
95+ str += recurse_string(child, level+1);
96+ }
97+ return str;
98+}
plugins/Unity/Indicators/modelprinter.hnew file+50
@@ -0,0 +1,50 @@
1+/*
2+ * Copyright 2012 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Authors:
17+ * Nick Dedekind <nick.dedekind@canonical.com>
18+ */
19+
20+#ifndef MODELPRINTER_H
21+#define MODELPRINTER_H
22+
23+#include <QSortFilterProxyModel>
24+
25+// This class acts as a namespace only, with the addition that its enums
26+// are registered to be exposed on the QML side.
27+class ModelPrinter : public QObject
28+{
29+ Q_OBJECT
30+
31+ Q_PROPERTY(QAbstractItemModel* model READ sourceModel WRITE setSourceModel NOTIFY modelChanged)
32+public:
33+ ModelPrinter(QObject* parent=0);
34+
35+ void setSourceModel(QAbstractItemModel* sourceModel);
36+ QAbstractItemModel* sourceModel() const;
37+
38+ Q_INVOKABLE QString getString(const QModelIndex& index = QModelIndex()) const;
39+
40+Q_SIGNALS:
41+ void sourceChanged();
42+ void modelChanged();
43+
44+private:
45+ QString recurse_string(const QModelIndex& index, int level) const;
46+ QAbstractItemModel* m_model;
47+};
48+
49+#endif // MODELPRINTER_H
50+
plugins/Unity/Indicators/plugin.cpp+2
@@ -27,6 +27,7 @@
27#include "indicatorsmanager.h"
27#include "indicatorsmanager.h"
28#include "indicatorsmodel.h"
28#include "indicatorsmodel.h"
29#include "indicators.h"
29#include "indicators.h"
30+#include "modelprinter.h"
30
31
31void IndicatorsPlugin::registerTypes(const char *uri)
32void IndicatorsPlugin::registerTypes(const char *uri)
32{
33{
@@ -35,6 +36,7 @@ void IndicatorsPlugin::registerTypes(const char *uri)
35 qmlRegisterType<IndicatorsManager>(uri, 0, 1, "IndicatorsManager");
36 qmlRegisterType<IndicatorsManager>(uri, 0, 1, "IndicatorsManager");
36 qmlRegisterType<FlatMenuProxyModel>(uri, 0, 1, "FlatMenuProxyModel");
37 qmlRegisterType<FlatMenuProxyModel>(uri, 0, 1, "FlatMenuProxyModel");
37 qmlRegisterType<IndicatorsModel>(uri, 0, 1, "IndicatorsModel");
38 qmlRegisterType<IndicatorsModel>(uri, 0, 1, "IndicatorsModel");
39+ qmlRegisterType<ModelPrinter>(uri, 0, 1, "ModelPrinter");
38
40
39 qmlRegisterUncreatableType<ActionState>(uri, 0, 1, "ActionState", "Can't create ActionState class");
41 qmlRegisterUncreatableType<ActionState>(uri, 0, 1, "ActionState", "Can't create ActionState class");
40 qmlRegisterUncreatableType<NetworkActionState>(uri, 0, 1, "NetworkActionState", "Can't create NetworkActionState class");
42 qmlRegisterUncreatableType<NetworkActionState>(uri, 0, 1, "NetworkActionState", "Can't create NetworkActionState class");