on bionic help sign in
unity8/plugins/Unity/Indicators/modelprinter.cpp
152 lines 4.3 kB history raw
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.dedekind@canonical.com>
18 */
20// self
21#include "modelprinter.h"
23#include <unitymenumodel.h>
25// Qt
26#include <QTextStream>
28ModelPrinter::ModelPrinter(QObject *parent)
29 : QObject(parent)
30 , m_model(nullptr)
34void ModelPrinter::setSourceModel(UnityMenuModel * sourceModel)
36 if (m_model != nullptr) {
37 disconnect(m_model);
38 }
39 if (m_model != sourceModel) {
40 m_model = sourceModel;
41 Q_EMIT modelChanged();
42 Q_EMIT textChanged();
43 }
44 if (m_model != nullptr) {
45 connect(m_model, &UnityMenuModel::rowsInserted, this, &ModelPrinter::textChanged);
46 connect(m_model, &UnityMenuModel::rowsRemoved, this, &ModelPrinter::textChanged);
47 connect(m_model, &UnityMenuModel::dataChanged, this, &ModelPrinter::textChanged);
48 }
51UnityMenuModel* ModelPrinter::sourceModel() const
53 return m_model;
56QString ModelPrinter::text()
58 return getModelDataString(m_model, 0);
61QString tabify(int level) { QString str;
62 for (int i = 0; i < level; i++) {
63 str += QLatin1String(" ");
64 }
65 return str;
68QString ModelPrinter::getModelDataString(UnityMenuModel* sourceModel, int level)
70 if (!sourceModel)
71 return QLatin1String("");
73 QString str;
74 QTextStream stream(&str);
76 int rowCount = sourceModel->rowCount();
77 for (int row = 0; row < rowCount; row++) {
79 stream << getRowSring(sourceModel, row, level) << endl;
81 UnityMenuModel* childMenuModel = qobject_cast<UnityMenuModel*>(sourceModel->submenu(row));
82 if (childMenuModel) {
84 if (!m_children.contains(childMenuModel)) {
85 m_children << childMenuModel;
86 connect(childMenuModel, &UnityMenuModel::rowsInserted, this, &ModelPrinter::textChanged);
87 connect(childMenuModel, &UnityMenuModel::rowsRemoved, this, &ModelPrinter::textChanged);
88 connect(childMenuModel, &UnityMenuModel::dataChanged, this, &ModelPrinter::textChanged);
89 }
90 stream << getModelDataString(childMenuModel, level+1);
91 }
92 }
93 return str;
96QString ModelPrinter::getRowSring(UnityMenuModel* sourceModel, int row, int depth) const
98 QString str;
99 QTextStream stream(&str);
101 // Print out this row
102 QHash<int, QByteArray> roleNames = sourceModel->roleNames();
103 QList<int> roles = roleNames.keys();
104 qSort(roles);
106 Q_FOREACH(int role, roles) {
107 const QByteArray& roleName = roleNames[role];
108 stream << getVariantString(roleName, sourceModel->get(row, roleName), depth);
109 }
110 return str;
113QString ModelPrinter::getVariantString(const QVariant& vData) const
115 if (vData.type() == QVariant::List) {
116 QStringList strList;
117 for (const auto& v : vData.toList())
118 strList.append(getVariantString(v));
120 return '[' + strList.join(", ") + ']';
121 }
123 return vData.toString();
126QString ModelPrinter::getVariantString(const QString& roleName, const QVariant &vData, int depth) const
128 QString str;
129 QTextStream stream(&str);
131 if (vData.canConvert(QMetaType::QVariantMap)) {
132 QMapIterator<QString, QVariant> iter(vData.toMap());
133 while (iter.hasNext()) {
134 iter.next();
135 stream << tabify(depth);
136 stream << roleName
137 << "."
138 << iter.key()
139 << ": "
140 << getVariantString(iter.value())
141 << endl;
142 }
143 }
144 else {
145 stream << tabify(depth);
146 stream << roleName
147 << ": "
148 << getVariantString(vData)
149 << endl;
150 }
151 return str;