@@ -0,0 +1,98 @@
2+ * Copyright (C) 2012 Canonical, Ltd.
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.
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.
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/>.
18+#include "modelprinter.h"
23+ModelPrinter::ModelPrinter(QObject *parent)
29+void ModelPrinter::setSourceModel(QAbstractItemModel * sourceModel)
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()));
37+ if (m_model != sourceModel)
39+ m_model = sourceModel;
40+ Q_EMIT modelChanged();
41+ Q_EMIT sourceChanged();
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()));
51+QAbstractItemModel* ModelPrinter::sourceModel() const
56+QString ModelPrinter::getString(const QModelIndex& index) const
58+ return recurse_string(index, 0);
61+QString tabify(int level) { QString str;
62+ for (int i = 0; i < level; i++) {
68+QString ModelPrinter::recurse_string(const QModelIndex& index, int level) const
71+ QTextStream stream(&str);
73+ QHash<int, QByteArray> roleNames = m_model->roleNames();
74+ QList<int> roles = roleNames.keys();
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()) {
82+ stream << tabify(level) << roleNames[role] << "." << iter.key() << ": " << iter.value().toString() << endl;
86+ stream << tabify(level) << roleNames[role] << ": " << vData.toString() << endl;
90+ int rowCount = m_model->rowCount(index);
91+ stream << tabify(level) << "child count" << ": " << rowCount << endl;
92+ for (int i = 0; i < rowCount; i++)
94+ QModelIndex child = m_model->index(i, 0, index);
95+ str += recurse_string(child, level+1);