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 \qmlproperty IndicatorsModel::count
72 The number of data entries in the model.
74 \b Note: methods should only be called after the Component has completed.
76int IndicatorsModel::count() const
82 \qmlmethod IndicatorsModel::unload()
86void IndicatorsModel::load()
93 \qmlmethod IndicatorsModel::unload()
97void IndicatorsModel::unload()
103void IndicatorsModel::onIndicatorLoaded(const QString& indicator)
105 Indicator::Ptr plugin = m_manager->indicator(indicator);
109 if (m_plugins.indexOf(plugin) >= 0)
112 // find the insert position
114 while (pos < count()) {
115 // keep going while the existing priority is less.
116 if (indicatorData(plugin, Priority).toInt() < data(index(pos), Priority).toInt())
121 QObject::connect(plugin.data(), SIGNAL(identifierChanged(const QString&)), this, SLOT(onIdentifierChanged()));
122 QObject::connect(plugin.data(), SIGNAL(indicatorPropertiesChanged(const QVariant&)), this, SLOT(onIndicatorPropertiesChanged()));
124 beginInsertRows(QModelIndex(), pos, pos);
126 m_plugins.insert(pos, plugin);
131void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator)
133 Indicator::Ptr plugin = m_manager->indicator(indicator);
138 QMutableListIterator<Indicator::Ptr> iter(m_plugins);
139 while(iter.hasNext())
141 if (plugin == iter.next())
143 beginRemoveRows(QModelIndex(), i, i);
153void IndicatorsModel::onIdentifierChanged()
155 notifyDataChanged(QObject::sender(), Identifier);
159void IndicatorsModel::onIndicatorPropertiesChanged()
161 notifyDataChanged(QObject::sender(), IndicatorProperties);
165void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
167 Indicator* plugin = qobject_cast<Indicator*>(sender);
172 QMutableListIterator<Indicator::Ptr> iter(m_plugins);
173 while(iter.hasNext())
175 if (iter.next().data() == plugin)
177 QModelIndex changedIndex = this->index(index);
178 dataChanged(changedIndex, changedIndex, QVector<int>() << role);
186QHash<int, QByteArray> IndicatorsModel::roleNames() const
188 static QHash<int, QByteArray> roles;
191 roles[Identifier] = "identifier";
192 roles[Priority] = "priority";
193 roles[Title] = "title";
194 roles[Description] = "description";
195 roles[WidgetSource] = "widgetSource";
196 roles[PageSource] = "pageSource";
197 roles[IndicatorProperties] = "indicatorProperties";
198 roles[IsValid] = "isValid";
204int IndicatorsModel::columnCount(const QModelIndex &) const
210QVariant IndicatorsModel::defaultData(Indicator::Ptr plugin, int role)
217 return plugin ? plugin->identifier() : "Unknown";
221 return shellAppDirectory()+"/Panel/Indicators/DefaultIndicatorWidget.qml";
223 return shellAppDirectory()+"/Panel/Indicators/DefaultIndicatorPage.qml";
228Q_INVOKABLE QVariant IndicatorsModel::data(int row, int role) const
230 return data(index(row, 0), role);
234QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
236 if (!index.isValid() || index.row() >= m_plugins.size())
239 Indicator::Ptr plugin = m_plugins[index.row()];
246 return QVariant(plugin->identifier());
249 case IndicatorProperties:
252 return QVariant(plugin->indicatorProperties());
256 return (plugin ? true : false);
262 return indicatorData(plugin, role);
269QVariant IndicatorsModel::indicatorData(const Indicator::Ptr& plugin, int role) const
271 if (plugin && m_parsed_indicator_data.contains(plugin->identifier()))
273 QVariantMap data = m_parsed_indicator_data[plugin->identifier()];
274 return data.value(roleNames()[role], QVariant());
276 return defaultData(plugin, role);
280QModelIndex IndicatorsModel::parent(const QModelIndex&) const
282 return QModelIndex();
286int IndicatorsModel::rowCount(const QModelIndex&) const
288 return m_plugins.count();
291void IndicatorsModel::setIndicatorData(const QVariant& data)
293 m_indicator_data = data;
295 m_parsed_indicator_data.clear();
296 QMap<QString, QVariant> map = data.toMap();
297 QMapIterator<QString, QVariant> iter(map);
298 while(iter.hasNext())
301 m_parsed_indicator_data[iter.key()] = iter.value().toMap();
304 Q_EMIT indicatorDataChanged(m_indicator_data);
307QVariant IndicatorsModel::indicatorData() const
309 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