on bdbbb20 help sign in
unity8/plugins/Unity/Indicators/indicatorsmodel.cpp
301 lines 7.5 kB history raw
1/*
2 * Copyright 2012 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 * Renato Araujo Oliveira Filho <renato@canonical.com>
18 */
20#include "indicatorsmodel.h"
21#include "indicatorsmanager.h"
22#include "indicator.h"
23#include "indicators.h"
25#include <paths.h>
27#include <QQmlContext>
28#include <QQmlEngine>
29#include <QDebug>
31/*!
32 \qmltype IndicatorsModel
33 \inherits QAbstractListModel
35 \brief The IndicatorsModel class defines the list model for indicators
37 \b {This component is under heavy development.}
39 This class expose the available indicators.
41 \code
42 IndicatorsModel {
43 id: menuModel
44 }
46 ListView {
47 id: view
48 model: menuModel
49 Component.onCompleted: menuModel.load()
50 }
51 \endcode
52*/
53IndicatorsModel::IndicatorsModel(QObject *parent)
54 : QAbstractListModel(parent)
56 m_manager = new IndicatorsManager(this);
57 QObject::connect(m_manager, &IndicatorsManager::indicatorLoaded, this, &IndicatorsModel::onIndicatorLoaded);
58 QObject::connect(m_manager, &IndicatorsManager::indicatorAboutToBeUnloaded, this, &IndicatorsModel::onIndicatorAboutToBeUnloaded);
59 QObject::connect(m_manager, &IndicatorsManager::profileChanged, this, &IndicatorsModel::profileChanged);
61 QObject::connect(this, &IndicatorsModel::rowsInserted, this, &IndicatorsModel::countChanged);
62 QObject::connect(this, &IndicatorsModel::rowsRemoved, this, &IndicatorsModel::countChanged);
63 QObject::connect(this, &IndicatorsModel::modelReset, this, &IndicatorsModel::countChanged);
66/*! \internal */
67IndicatorsModel::~IndicatorsModel()
69 disconnect(m_manager, 0, 0, 0);
70 m_manager->deleteLater();
73/*!
74 \qmlproperty IndicatorsModel::count
75 The number of data entries in the model.
77 \b Note: methods should only be called after the Component has completed.
78*/
79int IndicatorsModel::count() const
81 return rowCount();
84/*!
85 \qmlproperty IndicatorsModel::profile
86 The indicator profile to use for the model.
88 \b Note: methods should only be called after the Component has completed.
89*/
90QString IndicatorsModel::profile() const
92 return m_manager->profile();
95/*!
96 \qmlproperty IndicatorsModel::setProfile
97 Set the indicator profile to use for the model.
99 \b Note: methods should only be called after the Component has completed.
100*/
101void IndicatorsModel::setProfile(const QString &profile)
103 m_manager->setProfile(profile);
106bool IndicatorsModel::light() const
108 return m_light;
111void IndicatorsModel::setLight(const bool &light)
113 if (m_light != light) {
114 m_light = light;
115 Q_EMIT lightChanged();
116 }
119/*!
120 \qmlmethod IndicatorsModel::unload()
122 Load all indicators.
123*/
124void IndicatorsModel::load()
126 m_indicators.clear();
127 m_manager->load();
130/*!
131 \qmlmethod IndicatorsModel::unload()
133 Unload all indicators.
134*/
135void IndicatorsModel::unload()
137 m_manager->unload();
140/*! \internal */
141void IndicatorsModel::onIndicatorLoaded(const QString& indicator_name)
143 Indicator::Ptr indicator = m_manager->indicator(indicator_name);
144 if (!indicator)
145 {
146 return;
147 }
149 if (m_indicators.indexOf(indicator) >= 0)
150 {
151 return;
152 }
154 // find the insert position
155 int pos = 0;
156 while (pos < count())
157 {
158 // keep going while the existing position is greater. (put lower position on end)
159 if (indicator->position() >= data(index(pos), IndicatorsModelRole::Position).toInt())
160 break;
161 pos++;
162 }
164 QObject::connect(indicator.data(), &Indicator::identifierChanged, this, &IndicatorsModel::onIdentifierChanged);
165 QObject::connect(indicator.data(), &Indicator::indicatorPropertiesChanged, this, &IndicatorsModel::onIndicatorPropertiesChanged);
167 beginInsertRows(QModelIndex(), pos, pos);
169 m_indicators.insert(pos, indicator);
170 endInsertRows();
173/*! \internal */
174void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator_name)
176 Indicator::Ptr indicator = m_manager->indicator(indicator_name);
177 if (!indicator)
178 {
179 return;
180 }
182 int i = 0;
183 QMutableListIterator<Indicator::Ptr> iter(m_indicators);
184 while(iter.hasNext())
185 {
186 if (indicator == iter.next())
187 {
188 beginRemoveRows(QModelIndex(), i, i);
189 iter.remove();
190 endRemoveRows();
191 break;
192 }
193 i++;
194 }
198/*! \internal */
199void IndicatorsModel::onIdentifierChanged()
201 notifyDataChanged(QObject::sender(), IndicatorsModelRole::Identifier);
204/*! \internal */
205void IndicatorsModel::onIndicatorPropertiesChanged()
207 notifyDataChanged(QObject::sender(), IndicatorsModelRole::IndicatorProperties);
210/*! \internal */
211void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
213 Indicator* indicator = qobject_cast<Indicator*>(sender);
214 if (!indicator)
215 {
216 return;
217 }
219 int index = 0;
220 QMutableListIterator<Indicator::Ptr> iter(m_indicators);
221 while(iter.hasNext())
222 {
223 if (indicator == iter.next())
224 {
225 QModelIndex changedIndex = this->index(index);
226 dataChanged(changedIndex, changedIndex, QVector<int>() << role);
227 break;
228 }
229 index++;
230 }
233/*! \internal */
234QHash<int, QByteArray> IndicatorsModel::roleNames() const
236 static QHash<int, QByteArray> roles;
237 if (roles.isEmpty())
238 {
239 roles[IndicatorsModelRole::Identifier] = "identifier";
240 roles[IndicatorsModelRole::Position] = "position";
241 roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
242 }
243 return roles;
246/*! \internal */
247int IndicatorsModel::columnCount(const QModelIndex &) const
249 return 1;
252QVariant IndicatorsModel::data(int row, int role) const
254 return data(index(row, 0), role);
257/*! \internal */
258QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
260 if (!index.isValid() || index.row() >= m_indicators.size())
261 return QVariant();
263 Indicator::Ptr indicator = m_indicators.at(index.row());
265 switch (role)
266 {
267 case IndicatorsModelRole::Identifier:
268 if (indicator)
269 {
270 return indicator->identifier();
271 }
272 break;
273 case IndicatorsModelRole::Position:
274 if (indicator)
275 {
276 return indicator->position();
277 }
278 break;
279 case IndicatorsModelRole::IndicatorProperties:
280 if (indicator)
281 {
282 return indicator->indicatorProperties();
283 }
284 break;
285 default:
286 break;
287 }
288 return QVariant();
291/*! \internal */
292QModelIndex IndicatorsModel::parent(const QModelIndex&) const
294 return QModelIndex();
297/*! \internal */
298int IndicatorsModel::rowCount(const QModelIndex&) const
300 return m_indicators.count();