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()
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.
83QVariantMap IndicatorsModel::get(int row) const
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]);
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.
104int IndicatorsModel::count() const
110 \qmlproperty WidgetsMap IndicatorsModel::widgetsMap
111 This property holds the map for all new widgets registered by dynamically loaded plugins.
113WidgetsMap* IndicatorsModel::widgetsMap() const
119 \qmlmethod IndicatorsModel::unload()
123void IndicatorsModel::load()
125 m_widgetsMap->clear();
131 \qmlmethod IndicatorsModel::unload()
135void IndicatorsModel::unload()
137 m_widgetsMap->clear();
142void IndicatorsModel::onIndicatorLoaded(const QString& indicator)
144 IndicatorClientInterface::Ptr plugin = m_manager->indicator(indicator);
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());
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()));
163 m_plugins.insert(i, plugin);
167 Q_EMIT widgetsMapChanged(m_widgetsMap);
171void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator)
173 IndicatorClientInterface::Ptr plugin = m_manager->indicator(indicator);
178 QMutableListIterator<IndicatorClientInterface::Ptr> iter(m_plugins);
179 while(iter.hasNext())
181 if (plugin == iter.next())
183 beginRemoveRows(QModelIndex(), i, i);
193void IndicatorsModel::onIconChanged()
195 notifyDataChanged(QObject::sender(), IconSource);
199void IndicatorsModel::onTitleChanged()
201 notifyDataChanged(QObject::sender(), Title);
205void IndicatorsModel::onLabelChanged()
207 notifyDataChanged(QObject::sender(), Label);
211void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
213 IndicatorClientInterface* plugin = dynamic_cast<IndicatorClientInterface*>(sender);
218 QMutableListIterator<IndicatorClientInterface::Ptr> iter(m_plugins);
219 while(iter.hasNext())
221 if (iter.next().get() == plugin)
223 QModelIndex changedIndex = this->index(index);
224 dataChanged(changedIndex, changedIndex, QVector<int>() << role);
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";
248int IndicatorsModel::columnCount(const QModelIndex &) const
254QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
256 if (!index.isValid() || index.row() >= m_plugins.size())
261 IndicatorClientInterface::Ptr plugin = m_plugins[index.row()];
266 attribute = QVariant(plugin->title());
271 return plugin->icon().toString();
273 attribute = QVariant("No plugin");
278 attribute = QVariant(plugin->label());
284 attribute = QVariant(plugin->description());
290 QQmlContext *context = QQmlEngine::contextForObject(this);
292 attribute = QVariant::fromValue<QQmlComponent*>(plugin->component(context->parentContext()->engine()));
296 case InitialProperties:
298 attribute = plugin->initialProperties();
302 attribute = QVariant(plugin != 0);
311QModelIndex IndicatorsModel::parent(const QModelIndex&) const
313 return QModelIndex();
317int IndicatorsModel::rowCount(const QModelIndex&) const
319 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