on 4c4b83e help sign in
unity8/plugins/IndicatorsClient/indicatorsmodel.cpp
320 lines 8.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 "widgetsmap.h"
23#include "indicatorclientinterface.h"
25#include <QQmlContext>
26#include <QQmlEngine>
27#include <QDebug>
30static bool comparePlugins(const IndicatorClientInterface::Ptr obj1, const IndicatorClientInterface::Ptr obj2)
32 return obj1->priority() < obj2->priority();
35/*!
36 \qmltype IndicatorsModel
37 \inherits QAbstractListModel
39 \brief The IndicatorsModel class defines the list model for indicators plugins
41 \b {This component is under heavy development.}
43 This class expose the available indicators.
45 \code
46 IndicatorsModel {
47 id: menuModel
48 }
50 ListView {
51 id: view
52 model: menuModel
53 Component.onCompleted: menuModel.load()
54 }
55 \endcode
56*/
57IndicatorsModel::IndicatorsModel(QObject *parent)
58 : QAbstractListModel(parent)
60 m_manager = new IndicatorsManager(this);
61 QObject::connect(m_manager, SIGNAL(loaded(const QString&)), this, SLOT(onIndicatorLoaded(const QString&)));
62 QObject::connect(m_manager, SIGNAL(aboutToBeUnloaded(const QString&)), this, SLOT(onIndicatorAboutToBeUnloaded(const QString&)));
63 m_widgetsMap = new WidgetsMap;
65 QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
66 QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
67 QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
70/*! \internal */
71IndicatorsModel::~IndicatorsModel()
73 delete m_widgetsMap;
74 delete m_manager;
77/*!
78 \qmlmethod IndicatorsModel::get(int)
80 Returns the item at index in the model. This allows the item data to be accessed from JavaScript:
81 \b Note: methods should only be called after the Component has completed.
82*/
83QVariantMap IndicatorsModel::get(int row) const
85 QVariantMap result;
87 QModelIndex index = this->index(row);
88 if (index.isValid()) {
89 QMap<int, QVariant> data = itemData(index);
90 const QHash<int, QByteArray> roleNames = this->roleNames();
91 Q_FOREACH(int i, roleNames.keys()) {
92 result.insert(roleNames[i], data[i]);
93 }
94 }
95 return result;
98/*!
99 \qmlproperty IndicatorsModel::count
100 The number of data entries in the model.
102 \b Note: methods should only be called after the Component has completed.
103*/
104int IndicatorsModel::count() const
106 return rowCount();
109/*!
110 \qmlproperty WidgetsMap IndicatorsModel::widgetsMap
111 This property holds the map for all new widgets registered by dynamically loaded plugins.
112*/
113WidgetsMap* IndicatorsModel::widgetsMap() const
115 return m_widgetsMap;
118/*!
119 \qmlmethod IndicatorsModel::unload()
121 Load all plugins.
122*/
123void IndicatorsModel::load()
125 m_widgetsMap->clear();
126 m_plugins.clear();
127 m_manager->load();
130/*!
131 \qmlmethod IndicatorsModel::unload()
133 Unload all plugins.
134*/
135void IndicatorsModel::unload()
137 m_widgetsMap->clear();
138 m_manager->unload();
141/*! \internal */
142void IndicatorsModel::onIndicatorLoaded(const QString& indicator)
144 IndicatorClientInterface::Ptr plugin = m_manager->indicator(indicator);
145 if (!plugin)
146 return;
148 QList<IndicatorClientInterface::Ptr>::iterator i = qLowerBound(m_plugins.begin(), m_plugins.end(), plugin, comparePlugins);
149 int insert_pos = qMin(i - m_plugins.begin(), m_plugins.count());
151 beginInsertRows(QModelIndex(), insert_pos, insert_pos);
153 m_widgetsMap->append(plugin->widgets());
155 QObject* obj = dynamic_cast<QObject*>(plugin.get());
156 if (obj)
157 {
158 QObject::connect(obj, SIGNAL(iconChanged(const QUrl&)), this, SLOT(onIconChanged()));
159 QObject::connect(obj, SIGNAL(titleChanged(const QString&)), this, SLOT(onTitleChanged()));
160 QObject::connect(obj, SIGNAL(labelChanged(const QString&)), this, SLOT(onLabelChanged()));
161 }
163 m_plugins.insert(i, plugin);
165 endInsertRows();
167 Q_EMIT widgetsMapChanged(m_widgetsMap);
170/*! \internal */
171void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator)
173 IndicatorClientInterface::Ptr plugin = m_manager->indicator(indicator);
174 if (!plugin)
175 return;
177 int i = 0;
178 QMutableListIterator<IndicatorClientInterface::Ptr> iter(m_plugins);
179 while(iter.hasNext())
180 {
181 if (plugin == iter.next())
182 {
183 beginRemoveRows(QModelIndex(), i, i);
184 iter.remove();
185 endRemoveRows();
186 }
187 i++;
188 }
192/*! \internal */
193void IndicatorsModel::onIconChanged()
195 notifyDataChanged(QObject::sender(), IconSource);
198/*! \internal */
199void IndicatorsModel::onTitleChanged()
201 notifyDataChanged(QObject::sender(), Title);
204/*! \internal */
205void IndicatorsModel::onLabelChanged()
207 notifyDataChanged(QObject::sender(), Label);
210/*! \internal */
211void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
213 IndicatorClientInterface* plugin = dynamic_cast<IndicatorClientInterface*>(sender);
214 if (!plugin)
215 return;
217 int index = 0;
218 QMutableListIterator<IndicatorClientInterface::Ptr> iter(m_plugins);
219 while(iter.hasNext())
220 {
221 if (iter.next().get() == plugin)
222 {
223 QModelIndex changedIndex = this->index(index);
224 dataChanged(changedIndex, changedIndex, QVector<int>() << role);
225 break;
226 }
227 index++;
228 }
231/*! \internal */
232QHash<int, QByteArray> IndicatorsModel::roleNames() const
234 static QHash<int, QByteArray> roles;
235 if (roles.isEmpty()) {
236 roles[Title] = "title";
237 roles[IconSource] = "iconSource";
238 roles[Label] = "label";
239 roles[Description] = "description";
240 roles[QMLComponent] = "component";
241 roles[InitialProperties] = "initialProperties";
242 roles[IsValid] = "isValid";
243 }
244 return roles;
247/*! \internal */
248int IndicatorsModel::columnCount(const QModelIndex &) const
250 return 1;
253/*! \internal */
254QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
256 if (!index.isValid() || index.row() >= m_plugins.size())
257 return QVariant();
259 QVariant attribute;
261 IndicatorClientInterface::Ptr plugin = m_plugins[index.row()];
263 switch (role) {
264 case Title:
265 if (plugin) {
266 attribute = QVariant(plugin->title());
267 }
268 break;
269 case IconSource:
270 if (plugin) {
271 return plugin->icon().toString();
272 } else {
273 attribute = QVariant("No plugin");
274 }
275 break;
276 case Label:
277 if (plugin) {
278 attribute = QVariant(plugin->label());
279 }
280 break;
281 case Description:
282 {
283 if (plugin) {
284 attribute = QVariant(plugin->description());
285 }
286 break;
287 }
288 case QMLComponent:
289 if (plugin) {
290 QQmlContext *context = QQmlEngine::contextForObject(this);
291 if (context) {
292 attribute = QVariant::fromValue<QQmlComponent*>(plugin->component(context->parentContext()->engine()));
293 }
294 }
295 break;
296 case InitialProperties:
297 if (plugin) {
298 attribute = plugin->initialProperties();
299 }
300 break;
301 case IsValid:
302 attribute = QVariant(plugin != 0);
303 break;
304 default:
305 break;
306 }
307 return attribute;
310/*! \internal */
311QModelIndex IndicatorsModel::parent(const QModelIndex&) const
313 return QModelIndex();
316/*! \internal */
317int IndicatorsModel::rowCount(const QModelIndex&) const
319 return m_plugins.count();