@@ -0,0 +1,114 @@
2+ * Copyright 2013 Canonical Ltd.
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.
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.
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/>.
17+ * Nick Dedekind <nick.dedekind@canonical.com>
21+#include "modelprinter.h"
24+#include <QTextStream>
26+ModelPrinter::ModelPrinter(QObject *parent)
32+void ModelPrinter::setSourceModel(QAbstractItemModel * sourceModel)
34+ if (m_model != NULL) {
37+ if (m_model != sourceModel) {
38+ m_model = sourceModel;
39+ Q_EMIT modelChanged();
40+ Q_EMIT textChanged();
42+ if (m_model != NULL) {
43+ connect(m_model, SIGNAL(modelReset()), this, SIGNAL(textChanged()));
44+ connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SIGNAL(textChanged()));
45+ connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SIGNAL(textChanged()));
49+QAbstractItemModel* ModelPrinter::sourceModel() const
54+QString ModelPrinter::text(const QModelIndex& index) const
56+ return recurseString(index, 0);
59+QString tabify(int level) { QString str;
60+ for (int i = 0; i < level; i++) {
66+QString ModelPrinter::recurseString(const QModelIndex& index, int level) const
72+ QTextStream stream(&str);
74+ QHash<int, QByteArray> roleNames = m_model->roleNames();
75+ QList<int> roles = roleNames.keys();
77+ Q_FOREACH(int role, roles) {
78+ QVariant vData = m_model->data(index, role);
79+ if (vData.canConvert(QMetaType::QVariantMap)) {
80+ QMapIterator<QString, QVariant> iter(vData.toMap());
81+ while (iter.hasNext()) {
83+ stream << tabify(level)
88+ << iter.value().toString()
93+ stream << tabify(level)
101+ int rowCount = m_model->rowCount(index);
102+ stream << tabify(level)
109+ for (int i = 0; i < rowCount; i++) {
110+ QModelIndex child = m_model->index(i, 0, index);
111+ str += recurseString(child, level+1);