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 "indicatorclientinterface.h"
29static bool comparePlugins(const IndicatorClientInterface::Ptr obj1, const IndicatorClientInterface::Ptr obj2)
31 return obj1->priority() < obj2->priority();
35 \qmltype IndicatorsModel
36 \inherits QAbstractListModel
38 \brief The IndicatorsModel class defines the list model for indicators plugins
40 \b {This component is under heavy development.}
42 This class expose the available indicators.
52 Component.onCompleted: menuModel.load()
56IndicatorsModel::IndicatorsModel(QObject *parent)
57 : QAbstractListModel(parent)
59 m_manager = new IndicatorsManager(this);
60 QObject::connect(m_manager, SIGNAL(indicatorLoaded(const QString&)), this, SLOT(onIndicatorLoaded(const QString&)));
61 QObject::connect(m_manager, SIGNAL(indicatorAboutToBeUnloaded(const QString&)), this, SLOT(onIndicatorAboutToBeUnloaded(const QString&)));
63 QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
64 QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
65 QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
69IndicatorsModel::~IndicatorsModel()
71 disconnect(m_manager, 0, 0, 0);
72 m_manager->deleteLater();
76 \qmlmethod IndicatorsModel::get(int)
78 Returns the item at index in the model. This allows the item data to be accessed from JavaScript:
79 \b Note: methods should only be called after the Component has completed.
81QVariantMap IndicatorsModel::get(int row) const
85 QModelIndex index = this->index(row);
86 if (index.isValid()) {
87 QMap<int, QVariant> data = itemData(index);
88 const QHash<int, QByteArray> roleNames = this->roleNames();
89 Q_FOREACH(int i, roleNames.keys()) {
90 result.insert(roleNames[i], data[i]);
97 \qmlproperty IndicatorsModel::count
98 The number of data entries in the model.
100 \b Note: methods should only be called after the Component has completed.
102int IndicatorsModel::count() const
108 \qmlmethod IndicatorsModel::unload()
112void IndicatorsModel::load()
119 \qmlmethod IndicatorsModel::unload()
123void IndicatorsModel::unload()
129void IndicatorsModel::onIndicatorLoaded(const QString& indicator)
131 IndicatorClientInterface::Ptr plugin = m_manager->indicator(indicator);
135 QList<IndicatorClientInterface::Ptr>::iterator i = qLowerBound(m_plugins.begin(), m_plugins.end(), plugin, comparePlugins);
136 int insert_pos = qMin(i - m_plugins.begin(), m_plugins.count());
138 beginInsertRows(QModelIndex(), insert_pos, insert_pos);
140 QObject* obj = dynamic_cast<QObject*>(plugin.get());
143 QObject::connect(obj, SIGNAL(identifierChanged(const QString&)), this, SLOT(onIdentifierChanged()));
144 QObject::connect(obj, SIGNAL(iconChanged(const QUrl&)), this, SLOT(onIconChanged()));
145 QObject::connect(obj, SIGNAL(titleChanged(const QString&)), this, SLOT(onTitleChanged()));
146 QObject::connect(obj, SIGNAL(labelChanged(const QString&)), this, SLOT(onLabelChanged()));
149 m_plugins.insert(i, plugin);
155void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator)
157 IndicatorClientInterface::Ptr plugin = m_manager->indicator(indicator);
162 QMutableListIterator<IndicatorClientInterface::Ptr> iter(m_plugins);
163 while(iter.hasNext())
165 if (plugin == iter.next())
167 beginRemoveRows(QModelIndex(), i, i);
177void IndicatorsModel::onIdentifierChanged()
179 notifyDataChanged(QObject::sender(), Identifier);
183void IndicatorsModel::onIconChanged()
185 notifyDataChanged(QObject::sender(), IconSource);
189void IndicatorsModel::onTitleChanged()
191 notifyDataChanged(QObject::sender(), Title);
195void IndicatorsModel::onLabelChanged()
197 notifyDataChanged(QObject::sender(), Label);
201void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
203 IndicatorClientInterface* plugin = dynamic_cast<IndicatorClientInterface*>(sender);
208 QMutableListIterator<IndicatorClientInterface::Ptr> iter(m_plugins);
209 while(iter.hasNext())
211 if (iter.next().get() == plugin)
213 QModelIndex changedIndex = this->index(index);
214 dataChanged(changedIndex, changedIndex, QVector<int>() << role);
222QHash<int, QByteArray> IndicatorsModel::roleNames() const
224 static QHash<int, QByteArray> roles;
225 if (roles.isEmpty()) {
226 roles[Identifier] = "identifier";
227 roles[Title] = "title";
228 roles[IconSource] = "iconSource";
229 roles[Label] = "label";
230 roles[Description] = "description";
231 roles[QMLComponent] = "component";
232 roles[InitialProperties] = "initialProperties";
233 roles[IsValid] = "isValid";
239int IndicatorsModel::columnCount(const QModelIndex &) const
245QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
247 if (!index.isValid() || index.row() >= m_plugins.size())
252 IndicatorClientInterface::Ptr plugin = m_plugins[index.row()];
257 attribute = QVariant(plugin->identifier());
262 attribute = QVariant(plugin->title());
267 return plugin->icon().toString();
269 attribute = QVariant("No plugin");
274 attribute = QVariant(plugin->label());
280 attribute = QVariant(plugin->description());
286 return plugin->componentSource();
289 case InitialProperties:
291 attribute = plugin->initialProperties();
295 attribute = QVariant(plugin != 0);
304QModelIndex IndicatorsModel::parent(const QModelIndex&) const
306 return QModelIndex();
310int IndicatorsModel::rowCount(const QModelIndex&) const
312 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