on bionic help sign in
unity8/plugins/Unity/Indicators/indicatorsmodel.cpp
288 lines 7.3 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);
106/*!
107 \qmlmethod IndicatorsModel::unload()
109 Load all indicators.
110*/
111void IndicatorsModel::load()
113 m_indicators.clear();
114 m_manager->load();
117/*!
118 \qmlmethod IndicatorsModel::unload()
120 Unload all indicators.
121*/
122void IndicatorsModel::unload()
124 m_manager->unload();
127/*! \internal */
128void IndicatorsModel::onIndicatorLoaded(const QString& indicator_name)
130 Indicator::Ptr indicator = m_manager->indicator(indicator_name);
131 if (!indicator)
132 {
133 return;
134 }
136 if (m_indicators.indexOf(indicator) >= 0)
137 {
138 return;
139 }
141 // find the insert position
142 int pos = 0;
143 while (pos < count())
144 {
145 // keep going while the existing position is greater. (put lower position on end)
146 if (indicator->position() >= data(index(pos), IndicatorsModelRole::Position).toInt())
147 break;
148 pos++;
149 }
151 QObject::connect(indicator.data(), &Indicator::identifierChanged, this, &IndicatorsModel::onIdentifierChanged);
152 QObject::connect(indicator.data(), &Indicator::indicatorPropertiesChanged, this, &IndicatorsModel::onIndicatorPropertiesChanged);
154 beginInsertRows(QModelIndex(), pos, pos);
156 m_indicators.insert(pos, indicator);
157 endInsertRows();
160/*! \internal */
161void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator_name)
163 Indicator::Ptr indicator = m_manager->indicator(indicator_name);
164 if (!indicator)
165 {
166 return;
167 }
169 int i = 0;
170 QMutableListIterator<Indicator::Ptr> iter(m_indicators);
171 while(iter.hasNext())
172 {
173 if (indicator == iter.next())
174 {
175 beginRemoveRows(QModelIndex(), i, i);
176 iter.remove();
177 endRemoveRows();
178 break;
179 }
180 i++;
181 }
185/*! \internal */
186void IndicatorsModel::onIdentifierChanged()
188 notifyDataChanged(QObject::sender(), IndicatorsModelRole::Identifier);
191/*! \internal */
192void IndicatorsModel::onIndicatorPropertiesChanged()
194 notifyDataChanged(QObject::sender(), IndicatorsModelRole::IndicatorProperties);
197/*! \internal */
198void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
200 Indicator* indicator = qobject_cast<Indicator*>(sender);
201 if (!indicator)
202 {
203 return;
204 }
206 int index = 0;
207 QMutableListIterator<Indicator::Ptr> iter(m_indicators);
208 while(iter.hasNext())
209 {
210 if (indicator == iter.next())
211 {
212 QModelIndex changedIndex = this->index(index);
213 dataChanged(changedIndex, changedIndex, QVector<int>() << role);
214 break;
215 }
216 index++;
217 }
220/*! \internal */
221QHash<int, QByteArray> IndicatorsModel::roleNames() const
223 static QHash<int, QByteArray> roles;
224 if (roles.isEmpty())
225 {
226 roles[IndicatorsModelRole::Identifier] = "identifier";
227 roles[IndicatorsModelRole::Position] = "position";
228 roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
229 }
230 return roles;
233/*! \internal */
234int IndicatorsModel::columnCount(const QModelIndex &) const
236 return 1;
239QVariant IndicatorsModel::data(int row, int role) const
241 return data(index(row, 0), role);
244/*! \internal */
245QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
247 if (!index.isValid() || index.row() >= m_indicators.size())
248 return QVariant();
250 Indicator::Ptr indicator = m_indicators.at(index.row());
252 switch (role)
253 {
254 case IndicatorsModelRole::Identifier:
255 if (indicator)
256 {
257 return indicator->identifier();
258 }
259 break;
260 case IndicatorsModelRole::Position:
261 if (indicator)
262 {
263 return indicator->position();
264 }
265 break;
266 case IndicatorsModelRole::IndicatorProperties:
267 if (indicator)
268 {
269 return indicator->indicatorProperties();
270 }
271 break;
272 default:
273 break;
274 }
275 return QVariant();
278/*! \internal */
279QModelIndex IndicatorsModel::parent(const QModelIndex&) const
281 return QModelIndex();
284/*! \internal */
285int IndicatorsModel::rowCount(const QModelIndex&) const
287 return m_indicators.count();