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"
30 \qmltype IndicatorsModel
31 \inherits QAbstractListModel
33 \brief The IndicatorsModel class defines the list model for indicators plugins
35 \b {This component is under heavy development.}
37 This class expose the available indicators.
47 Component.onCompleted: menuModel.load()
51IndicatorsModel::IndicatorsModel(QObject *parent)
52 : QAbstractListModel(parent)
54 m_manager = new IndicatorsManager(this);
55 QObject::connect(m_manager, SIGNAL(indicatorLoaded(const QString&)), this, SLOT(onIndicatorLoaded(const QString&)));
56 QObject::connect(m_manager, SIGNAL(indicatorAboutToBeUnloaded(const QString&)), this, SLOT(onIndicatorAboutToBeUnloaded(const QString&)));
58 QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
59 QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
60 QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
64IndicatorsModel::~IndicatorsModel()
66 disconnect(m_manager, 0, 0, 0);
67 m_manager->deleteLater();
71 \qmlmethod IndicatorsModel::get(int)
73 Returns the item at index in the model. This allows the item data to be accessed from JavaScript:
74 \b Note: methods should only be called after the Component has completed.
76QVariantMap IndicatorsModel::get(int row) const
80 QModelIndex index = this->index(row);
81 if (index.isValid()) {
82 QMap<int, QVariant> data = itemData(index);
83 const QHash<int, QByteArray> roleNames = this->roleNames();
84 Q_FOREACH(int i, roleNames.keys()) {
85 result.insert(roleNames[i], data[i]);
92 \qmlproperty IndicatorsModel::count
93 The number of data entries in the model.
95 \b Note: methods should only be called after the Component has completed.
97int IndicatorsModel::count() const
103 \qmlmethod IndicatorsModel::unload()
107void IndicatorsModel::load()
114 \qmlmethod IndicatorsModel::unload()
118void IndicatorsModel::unload()
124void IndicatorsModel::onIndicatorLoaded(const QString& indicator)
126 Indicator::Ptr plugin = m_manager->indicator(indicator);
130 if (m_plugins.indexOf(plugin) >= 0)
133 // find the insert position
135 while (pos < count()) {
136 // keep going while the existing priority is less.
137 if (indicatorData(plugin, Priority).toInt() < data(index(pos), Priority).toInt())
142 QObject::connect(plugin.data(), SIGNAL(identifierChanged(const QString&)), this, SLOT(onIdentifierChanged()));
143 QObject::connect(plugin.data(), SIGNAL(indicatorPropertiesChanged(const QVariant&)), this, SLOT(onIndicatorPropertiesChanged()));
145 beginInsertRows(QModelIndex(), pos, pos);
147 m_plugins.insert(pos, plugin);
152void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator)
154 Indicator::Ptr plugin = m_manager->indicator(indicator);
159 QMutableListIterator<Indicator::Ptr> iter(m_plugins);
160 while(iter.hasNext())
162 if (plugin == iter.next())
164 beginRemoveRows(QModelIndex(), i, i);
174void IndicatorsModel::onIdentifierChanged()
176 notifyDataChanged(QObject::sender(), Identifier);
180void IndicatorsModel::onIndicatorPropertiesChanged()
182 notifyDataChanged(QObject::sender(), IndicatorProperties);
186void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
188 Indicator* plugin = qobject_cast<Indicator*>(sender);
193 QMutableListIterator<Indicator::Ptr> iter(m_plugins);
194 while(iter.hasNext())
196 if (iter.next().data() == plugin)
198 QModelIndex changedIndex = this->index(index);
199 dataChanged(changedIndex, changedIndex, QVector<int>() << role);
207QHash<int, QByteArray> IndicatorsModel::roleNames() const
209 static QHash<int, QByteArray> roles;
212 roles[Identifier] = "identifier";
213 roles[Priority] = "priority";
214 roles[Title] = "title";
215 roles[Description] = "description";
216 roles[WidgetSource] = "widgetSource";
217 roles[PageSource] = "pageSource";
218 roles[IndicatorProperties] = "indicatorProperties";
219 roles[IsValid] = "isValid";
225int IndicatorsModel::columnCount(const QModelIndex &) const
231QVariant IndicatorsModel::defaultData(Indicator::Ptr plugin, int role)
238 return plugin ? plugin->identifier() : "Unknown";
242 return shellAppDirectory()+"/Panel/Indicators/DefaultIndicatorWidget.qml";
244 return shellAppDirectory()+"/Panel/Indicators/DefaultIndicatorPage.qml";
250QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
252 if (!index.isValid() || index.row() >= m_plugins.size())
255 Indicator::Ptr plugin = m_plugins[index.row()];
262 return QVariant(plugin->identifier());
265 case IndicatorProperties:
268 return QVariant(plugin->indicatorProperties());
272 return (plugin ? true : false);
278 return indicatorData(plugin, role);
285QVariant IndicatorsModel::indicatorData(const Indicator::Ptr& plugin, int role) const
287 if (plugin && m_parsed_indicator_data.contains(plugin->identifier()))
289 QVariantMap data = m_parsed_indicator_data[plugin->identifier()];
290 return data.value(roleNames()[role], QVariant());
292 return defaultData(plugin, role);
296QModelIndex IndicatorsModel::parent(const QModelIndex&) const
298 return QModelIndex();
302int IndicatorsModel::rowCount(const QModelIndex&) const
304 return m_plugins.count();
307void IndicatorsModel::setIndicatorData(const QVariant& data)
309 m_indicator_data = data;
311 m_parsed_indicator_data.clear();
312 QMap<QString, QVariant> map = data.toMap();
313 QMapIterator<QString, QVariant> iter(map);
314 while(iter.hasNext())
317 m_parsed_indicator_data[iter.key()] = iter.value().toMap();
320 Q_EMIT indicatorDataChanged(m_indicator_data);
323QVariant IndicatorsModel::indicatorData() const
325 return m_indicator_data;
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