21 #include "modelprinter.h"
23 #include <unitymenumodel.h>
26 #include <QTextStream>
28 ModelPrinter::ModelPrinter(QObject *parent)
34 void ModelPrinter::setSourceModel(UnityMenuModel * sourceModel)
36 if (m_model !=
nullptr) {
39 if (m_model != sourceModel) {
40 m_model = sourceModel;
41 Q_EMIT modelChanged();
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);
51 UnityMenuModel* ModelPrinter::sourceModel()
const
56 QString ModelPrinter::text()
58 return getModelDataString(m_model, 0);
61 QString tabify(
int level) { QString str;
62 for (
int i = 0; i < level; i++) {
63 str += QLatin1String(
" ");
68 QString ModelPrinter::getModelDataString(UnityMenuModel* sourceModel,
int level)
71 return QLatin1String(
"");
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));
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);
90 stream << getModelDataString(childMenuModel, level+1);
96 QString ModelPrinter::getRowSring(UnityMenuModel* sourceModel,
int row,
int depth)
const
99 QTextStream stream(&str);
102 QHash<int, QByteArray> roleNames = sourceModel->roleNames();
103 QList<int> roles = roleNames.keys();
106 Q_FOREACH(
int role, roles) {
107 const QByteArray& roleName = roleNames[role];
108 stream << tabify(depth) << getVariantString(roleName, sourceModel->get(row, roleName));
113 QString ModelPrinter::getVariantString(
const QString& roleName,
const QVariant &vData)
const
116 QTextStream stream(&str);
118 if (vData.canConvert(QMetaType::QVariantMap)) {
119 QMapIterator<QString, QVariant> iter(vData.toMap());
120 while (iter.hasNext()) {
126 << iter.value().toString()