on xenial help sign in
Added textual representation of menu model to indicators-client app
commit423e7153e9bd45ae1a77454dfaaca1f64e79c9d1 authorNick Dedekind <nicholas.dedekind@gmail.com> dateFri 5 Jul 2013, 15:15 (13 years ago) parentd9d659e
7 files changed+247−18
Panel/Indicators/client/IndicatorsClient.qml+8−9 Panel/Indicators/client/IndicatorsPage.qml+25−9 Panel/Indicators/client/IndicatorsTree.qml+63 plugins/Unity/Indicators/CMakeLists.txt+1 plugins/Unity/Indicators/modelprinter.cpp+98 plugins/Unity/Indicators/modelprinter.h+50 plugins/Unity/Indicators/plugin.cpp+2
Panel/Indicators/client/IndicatorsClient.qml+8−9
@@ -27,16 +27,15 @@ Rectangle {
2727
2828 PageStack {
2929 id: pages
30−
3130 anchors.fill: parent
32− Component.onCompleted: root.reset()
33− }
31+ Component.onCompleted: reset()
3432
35− function reset() {
36− pages.clear();
37− var component = Qt.createComponent("IndicatorsList.qml");
38− var page = component.createObject(pages);
39− console.log(component.errorString());
40− pages.push(page);
33+ function reset() {
34+ clear();
35+ var component = Qt.createComponent("IndicatorsList.qml");
36+ var page = component.createObject(pages);
37+ console.log(component.errorString());
38+ push(page);
39+ }
4140 }
4241}
Panel/Indicators/client/IndicatorsPage.qml+25−9
@@ -19,31 +19,47 @@
1919
2020import QtQuick 2.0
2121import Ubuntu.Components 0.1
22+import Ubuntu.Components.ListItems 0.1 as ListItem
2223
2324Page {
2425 id: _page
2526
2627 title: indicatorProperties && indicatorProperties.title ? indicatorProperties.title : ""
2728 property variant indicatorProperties
28− property alias pageSource : page_loader.source
29+ property string pageSource : page_loader.source
2930
3031 anchors.fill: parent
3132
33+ ListItem.Standard {
34+ id: button
35+
36+ text: "Enable Visual Representation"
37+ control: Switch {
38+ id: checked
39+ checked: true
40+ }
41+ }
42+
3243 Loader {
3344 id: page_loader
3445 objectName: "page_loader"
3546
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"
3755
38− onStatusChanged: {
39− if (status == Loader.Ready) {
40− for(var pName in indicatorProperties) {
41− if (item.hasOwnProperty(pName)) {
42− item[pName] = indicatorProperties[pName];
43− }
56+ onLoaded: {
57+ for(var pName in indicatorProperties) {
58+ if (item.hasOwnProperty(pName)) {
59+ item[pName] = indicatorProperties[pName];
4460 }
45− item.start();
4661 }
62+ item.start();
4763 }
4864 }
4965}
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
2020 indicators.h
2121 indicatorsmanager.cpp
2222 indicatorsmodel.cpp
23+ modelprinter.cpp
2324 plugin.cpp
2425)
2526
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 @@
2727#include "indicatorsmanager.h"
2828#include "indicatorsmodel.h"
2929#include "indicators.h"
30+#include "modelprinter.h"
3031
3132void IndicatorsPlugin::registerTypes(const char *uri)
3233{
@@ -35,6 +36,7 @@ void IndicatorsPlugin::registerTypes(const char *uri)
3536 qmlRegisterType<IndicatorsManager>(uri, 0, 1, "IndicatorsManager");
3637 qmlRegisterType<FlatMenuProxyModel>(uri, 0, 1, "FlatMenuProxyModel");
3738 qmlRegisterType<IndicatorsModel>(uri, 0, 1, "IndicatorsModel");
39+ qmlRegisterType<ModelPrinter>(uri, 0, 1, "ModelPrinter");
3840
3941 qmlRegisterUncreatableType<ActionState>(uri, 0, 1, "ActionState", "Can't create ActionState class");
4042 qmlRegisterUncreatableType<NetworkActionState>(uri, 0, 1, "NetworkActionState", "Can't create NetworkActionState class");