@@ -1,191 +0,0 @@
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 "rootactionstate.h"
20−#include "indicators.h"
21−#include "cachedunitymenumodel.h"
22−
23−#include <unitymenumodel.h>
24−
25−// Qt
26−#include <QDebug>
27−
28−VisibleIndicatorsModel::VisibleIndicatorsModel(QObject *parent)
29− : QIdentityProxyModel(parent)
30−{
31− connect(this, SIGNAL(modelReset()), SIGNAL(countChanged()));
32− connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), SIGNAL(countChanged()));
33− connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), SIGNAL(countChanged()));
34−}
35−
36−VisibleIndicatorsModel::~VisibleIndicatorsModel()
37−{
38− qDeleteAll(m_rootActions);
39− m_rootActions.clear();
40−
41− qDeleteAll(m_cachedModels);
42− m_cachedModels.clear();
43−
44− m_visible.clear();
45−}
46−
47−QHash<int, QByteArray> VisibleIndicatorsModel::roleNames() const
48−{
49− return sourceModel() ? sourceModel()->roleNames() : QHash<int, QByteArray>();
50−}
51−
52−QString VisibleIndicatorsModel::profile() const
53−{
54− return m_profile;
55−}
56−
57−void VisibleIndicatorsModel::setProfile(const QString& profile)
58−{
59− if (m_profile != profile) {
60− m_profile = profile;
61− Q_EMIT profileChanged();
62− }
63−}
64−
65−void VisibleIndicatorsModel::setModel(QAbstractItemModel *itemModel)
66−{
67− if (itemModel != sourceModel()) {
68− if (sourceModel() != NULL) {
69− sourceModel()->disconnect(this);
70− }
71−
72− setSourceModel(itemModel);
73−
74− if (sourceModel() != NULL) {
75− // ... and use our own
76− connect(sourceModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
77− SLOT(sourceRowsInserted(QModelIndex,int,int)));
78− connect(sourceModel(), SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
79− SLOT(sourceRowsAboutToBeRemoved(QModelIndex,int,int)));
80− connect(sourceModel(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
81− SLOT(sourceDataChanged(QModelIndex,QModelIndex)));
82− }
83− Q_EMIT modelChanged();
84− }
85−}
86−
87−void VisibleIndicatorsModel::sourceRowsInserted(const QModelIndex&, int start, int end)
88−{
89− for (int row = start; row <= end; row++) {
90− RootActionState* actionState = new RootActionState();
91− connect(actionState, SIGNAL(updated()), SLOT(onActionStateUpdated()));
92− m_rootActions.insert(row, actionState);
93− m_visible.insert(row, false);
94−
95− m_cachedModels.insert(row, new CachedUnityMenuModel);
96− updateCachedModel(sourceModel()->index(row, 0));
97− }
98−}
99−
100−void VisibleIndicatorsModel::sourceRowsAboutToBeRemoved(const QModelIndex&, int start, int end)
101−{
102− for (int row = start; row <= end; row++) {
103− if (m_rootActions.count() > start) {
104− delete m_rootActions[start];
105− m_rootActions.removeAt(start);
106− }
107− if (m_visible.count() > start) {
108− m_visible.removeAt(start);
109− }
110− if (m_cachedModels.count() > start) {
111− delete m_cachedModels[start];
112− m_cachedModels.removeAt(start);
113− }
114− }
115−}
116−
117−void VisibleIndicatorsModel::sourceDataChanged(const QModelIndex& topRight, const QModelIndex& bottomRight)
118−{
119− for (int row = topRight.row(); row <= bottomRight.row(); row++) {
120− updateCachedModel(sourceModel()->index(row, 0));
121− }
122−}
123−
124−void VisibleIndicatorsModel::onActionStateUpdated()
125−{
126− RootActionState* actionState = qobject_cast<RootActionState*>(sender());
127− int changedRow = m_rootActions.indexOf(actionState);
128−
129− QModelIndex modelIndex = this->index(changedRow, 0);
130− if (!modelIndex.isValid()) {
131− return;
132− }
133−
134− while (m_visible.count() <= changedRow) {
135− m_visible.append(false);
136− }
137−
138− if (actionState->isVisible() != m_visible[changedRow]) {
139− m_visible[changedRow] = actionState->isVisible();
140−
141− Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << IndicatorsModelRole::IsVisible);
142− }
143−}
144−
145−QVariant VisibleIndicatorsModel::data(const QModelIndex &index, int role) const
146−{
147− if (role != IndicatorsModelRole::IsVisible)
148− return QIdentityProxyModel::data(index, role);
149−
150− if (!index.isValid() || m_visible.count() <= index.row())
151− return false;
152−
153− return m_visible[index.row()];
154−}
155−
156−void VisibleIndicatorsModel::updateCachedModel(const QModelIndex& index)
157−{
158− if (!index.isValid()) {
159− return;
160− }
161− QVariant data = sourceModel()->data(index, IndicatorsModelRole::IndicatorProperties);
162− if (!data.isValid()) {
163− return;
164− }
165− int row = index.row();
166−
167− if (m_cachedModels.count() <= row) {
168− return;
169− }
170− CachedUnityMenuModel* cachedModel = m_cachedModels[row];
171−
172− QVariantMap properties = data.toMap();
173−
174− QVariantMap menuObjectPaths = properties["menuObjectPaths"].toMap();
175− if (!menuObjectPaths.contains(m_profile)) {
176− return;
177− }
178−
179− QString menuObjectPath = menuObjectPaths[m_profile].toString();
180− QString busName = properties["busName"].toString();
181− QString actionsObjectPath = properties["actionsObjectPath"].toString();
182−
183− cachedModel->setBusName(busName);
184− cachedModel->setMenuObjectPath(menuObjectPath);
185− QVariantMap actions; actions["indicator"] = actionsObjectPath;
186− cachedModel->setActions(actions);
187−
188− if (m_rootActions.count() > row) {
189− m_rootActions[row]->setMenu(cachedModel->model());
190− }
191−}