2 * Copyright 2012 Canonical Ltd.
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.
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.
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/>.
17 * Renato Araujo Oliveira Filho <renato@canonical.com>
20#include "indicatorsmodel.h"
21#include "indicatorsmanager.h"
22#include "widgetsmap.h"
23#include "indicatorclientinterface.h"
30static bool comparePlugins(const IndicatorClientInterface::Ptr obj1, const IndicatorClientInterface::Ptr obj2)
32 return obj1->priority() < obj2->priority();
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.
53 Component.onCompleted: menuModel.load()
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()));
71IndicatorsModel::~IndicatorsModel()
73 disconnect(m_manager, 0, 0, 0);
74 m_manager->deleteLater();
75 m_widgetsMap->deleteLater();
79 \qmlmethod IndicatorsModel::get(int)
81 Returns the item at index in the model. This allows the item data to be accessed from JavaScript:
82 \b Note: methods should only be called after the Component has completed.
84QVariantMap IndicatorsModel::get(int row) const
88 QModelIndex index = this->index(row);
89 if (index.isValid()) {
90 QMap<int, QVariant> data = itemData(index);
91 const QHash<int, QByteArray> roleNames = this->roleNames();
92 Q_FOREACH(int i, roleNames.keys()) {
93 result.insert(roleNames[i], data[i]);
100 \qmlproperty IndicatorsModel::count
101 The number of data entries in the model.
103 \b Note: methods should only be called after the Component has completed.
105int IndicatorsModel::count() const
111 \qmlproperty WidgetsMap IndicatorsModel::widgetsMap
112 This property holds the map for all new widgets registered by dynamically loaded plugins.
114WidgetsMap* IndicatorsModel::widgetsMap() const
120 \qmlmethod IndicatorsModel::unload()
124void IndicatorsModel::load()
126 m_widgetsMap->clear();
132 \qmlmethod IndicatorsModel::unload()
136void IndicatorsModel::unload()
138 m_widgetsMap->clear();
143void IndicatorsModel::onIndicatorLoaded(const QString& indicator)
145 IndicatorClientInterface::Ptr plugin = m_manager->indicator(indicator);
149 QList<IndicatorClientInterface::Ptr>::iterator i = qLowerBound(m_plugins.begin(), m_plugins.end(), plugin, comparePlugins);
150 int insert_pos = qMin(i - m_plugins.begin(), m_plugins.count());
152 beginInsertRows(QModelIndex(), insert_pos, insert_pos);
154 m_widgetsMap->append(plugin->widgets());
156 QObject* obj = dynamic_cast<QObject*>(plugin.get());
159 QObject::connect(obj, SIGNAL(idChanged(const QString&)), this, SLOT(onIdChanged()));
160 QObject::connect(obj, SIGNAL(iconChanged(const QUrl&)), this, SLOT(onIconChanged()));
161 QObject::connect(obj, SIGNAL(titleChanged(const QString&)), this, SLOT(onTitleChanged()));
162 QObject::connect(obj, SIGNAL(labelChanged(const QString&)), this, SLOT(onLabelChanged()));
165 m_plugins.insert(i, plugin);
169 Q_EMIT widgetsMapChanged(m_widgetsMap);
173void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator)
175 IndicatorClientInterface::Ptr plugin = m_manager->indicator(indicator);
180 QMutableListIterator<IndicatorClientInterface::Ptr> iter(m_plugins);
181 while(iter.hasNext())
183 if (plugin == iter.next())
185 beginRemoveRows(QModelIndex(), i, i);
195void IndicatorsModel::onIdChanged()
197 notifyDataChanged(QObject::sender(), Identifier);
201void IndicatorsModel::onIconChanged()
203 notifyDataChanged(QObject::sender(), IconSource);
207void IndicatorsModel::onTitleChanged()
209 notifyDataChanged(QObject::sender(), Title);
213void IndicatorsModel::onLabelChanged()
215 notifyDataChanged(QObject::sender(), Label);
219void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
221 IndicatorClientInterface* plugin = dynamic_cast<IndicatorClientInterface*>(sender);
226 QMutableListIterator<IndicatorClientInterface::Ptr> iter(m_plugins);
227 while(iter.hasNext())
229 if (iter.next().get() == plugin)
231 QModelIndex changedIndex = this->index(index);
232 dataChanged(changedIndex, changedIndex, QVector<int>() << role);
240QHash<int, QByteArray> IndicatorsModel::roleNames() const
242 static QHash<int, QByteArray> roles;
243 if (roles.isEmpty()) {
244 roles[Identifier] = "identifier";
245 roles[Title] = "title";
246 roles[IconSource] = "iconSource";
247 roles[Label] = "label";
248 roles[Description] = "description";
249 roles[QMLComponent] = "component";
250 roles[InitialProperties] = "initialProperties";
251 roles[IsValid] = "isValid";
257int IndicatorsModel::columnCount(const QModelIndex &) const
263QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
265 if (!index.isValid() || index.row() >= m_plugins.size())
270 IndicatorClientInterface::Ptr plugin = m_plugins[index.row()];
275 attribute = QVariant(plugin->identifier());
280 attribute = QVariant(plugin->title());
285 return plugin->icon().toString();
287 attribute = QVariant("No plugin");
292 attribute = QVariant(plugin->label());
298 attribute = QVariant(plugin->description());
304 QQmlContext *context = QQmlEngine::contextForObject(this);
306 attribute = QVariant::fromValue<QQmlComponent*>(plugin->component(context->parentContext()->engine()));
310 case InitialProperties:
312 attribute = plugin->initialProperties();
316 attribute = QVariant(plugin != 0);
325QModelIndex IndicatorsModel::parent(const QModelIndex&) const
327 return QModelIndex();
331int IndicatorsModel::rowCount(const QModelIndex&) const
333 return m_plugins.count();
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list