@@ -0,0 +1,88 @@
1+/*
2+ * Copyright (C) 2012, 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 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 General Public License for more details.
12+ *
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/>.
15+ */
16+
17+// self
18+#include "visibleindicatorsmodel.h"
19+#include "indicators.h"
20+
21+VisibleIndicatorsModel::VisibleIndicatorsModel(QObject *parent)
22+ : QIdentityProxyModel(parent),
23+ m_inserting(false)
24+{
25+ QObject::connect(this, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)), this, SLOT(onBeginRowInserted(QModelIndex, int, int)));
26+ QObject::connect(this, SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(onRowInserted(QModelIndex, int, int)));
27+}
28+
29+QHash<int, QByteArray> VisibleIndicatorsModel::roleNames() const
30+{
31+ static QHash<int, QByteArray> roles;
32+ if (roles.isEmpty() && sourceModel())
33+ {
34+ roles = sourceModel()->roleNames();
35+ roles[IndicatorsModelRole::IsVisible] = "isVisible";
36+ }
37+ return roles;
38+}
39+
40+void VisibleIndicatorsModel::setSourceModel(QAbstractItemModel *model)
41+{
42+ if (sourceModel() != model) {
43+ QIdentityProxyModel::setSourceModel(model);
44+ Q_EMIT modelChanged();
45+ }
46+}
47+
48+QVariantMap VisibleIndicatorsModel::visible() const
49+{
50+ return m_visible;
51+}
52+
53+void VisibleIndicatorsModel::onBeginRowInserted(const QModelIndex&, int, int)
54+{
55+ m_inserting = true;
56+}
57+
58+void VisibleIndicatorsModel::onRowInserted(const QModelIndex&, int, int)
59+{
60+ m_inserting = false;
61+}
62+
63+void VisibleIndicatorsModel::setVisible(const QVariantMap& visible)
64+{
65+ if (m_visible != visible) {
66+ m_visible = visible;
67+ Q_EMIT visibleChanged();
68+
69+ // need to tell the view that the visible data has changed.
70+ if (!m_inserting && rowCount() > 0) {
71+ Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0), QVector<int>() << IndicatorsModelRole::IsVisible);
72+ }
73+ }
74+}
75+
76+QVariant VisibleIndicatorsModel::data(const QModelIndex &index, int role) const
77+{
78+ if (role != IndicatorsModelRole::IsVisible) {
79+ return QIdentityProxyModel::data(index, role);
80+ }
81+
82+ if (!index.isValid()) {
83+ return false;
84+ }
85+
86+ QString ident = QIdentityProxyModel::data(index, IndicatorsModelRole::Identifier).toString();
87+ return m_visible.value(ident, false).toBool();
88+}