@@ -0,0 +1,122 @@
1+/*
2+ * Copyright 2014 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+#include "fakeindicatorsmodel.h"
21+#include "indicators.h"
22+
23+#include <paths.h>
24+
25+#include <QQmlContext>
26+#include <QQmlEngine>
27+#include <QDebug>
28+
29+FakeIndicatorsModel::FakeIndicatorsModel(QObject *parent)
30+ : QAbstractListModel(parent)
31+{
32+ QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
33+ QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
34+ QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
35+}
36+
37+/*! \internal */
38+FakeIndicatorsModel::~FakeIndicatorsModel()
39+{
40+ unload();
41+}
42+
43+int FakeIndicatorsModel::count() const
44+{
45+ return rowCount();
46+}
47+
48+void FakeIndicatorsModel::load(const QString&)
49+{
50+}
51+
52+void FakeIndicatorsModel::unload()
53+{
54+ beginResetModel();
55+
56+ qDeleteAll(m_indicators);
57+ m_indicators.clear();
58+
59+ endResetModel();
60+}
61+
62+
63+void FakeIndicatorsModel::append(const QVariantMap& row)
64+{
65+ Indicator* new_row = new QHash<int, QVariant>();
66+ for (auto iter = row.begin(); iter != row.end(); ++iter )
67+ {
68+ int key = roleNames().key(iter.key().toUtf8(), -1);
69+ if (key != -1) {
70+ new_row->insert(key, iter.value());
71+ }
72+ }
73+
74+ beginInsertRows(QModelIndex(), m_indicators.count(), m_indicators.count());
75+
76+ m_indicators.append(new_row);
77+
78+ endInsertRows();
79+}
80+
81+QHash<int, QByteArray> FakeIndicatorsModel::roleNames() const
82+{
83+ static QHash<int, QByteArray> roles;
84+ if (roles.isEmpty())
85+ {
86+ roles[IndicatorsModelRole::Identifier] = "identifier";
87+ roles[IndicatorsModelRole::Position] = "position";
88+ roles[IndicatorsModelRole::WidgetSource] = "widgetSource";
89+ roles[IndicatorsModelRole::PageSource] = "pageSource";
90+ roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
91+ }
92+ return roles;
93+}
94+
95+int FakeIndicatorsModel::columnCount(const QModelIndex &) const
96+{
97+ return 1;
98+}
99+
100+Q_INVOKABLE QVariant FakeIndicatorsModel::data(int row, int role) const
101+{
102+ return data(index(row, 0), role);
103+}
104+
105+QVariant FakeIndicatorsModel::data(const QModelIndex &index, int role) const
106+{
107+ if (!index.isValid() || index.row() >= m_indicators.size())
108+ return QVariant();
109+
110+ Indicator* indicator = m_indicators[index.row()];
111+ return indicator->value(role, QVariant());
112+}
113+
114+QModelIndex FakeIndicatorsModel::parent(const QModelIndex&) const
115+{
116+ return QModelIndex();
117+}
118+
119+int FakeIndicatorsModel::rowCount(const QModelIndex&) const
120+{
121+ return m_indicators.count();
122+}