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"
23#include "indicators.h"
31 \qmltype IndicatorsModel
32 \inherits QAbstractListModel
34 \brief The IndicatorsModel class defines the list model for indicators
36 \b {This component is under heavy development.}
38 This class expose the available indicators.
48 Component.onCompleted: menuModel.load()
52IndicatorsModel::IndicatorsModel(QObject *parent)
53 : QAbstractListModel(parent)
55 m_manager = new IndicatorsManager(this);
56 QObject::connect(m_manager, SIGNAL(indicatorLoaded(const QString&)), this, SLOT(onIndicatorLoaded(const QString&)));
57 QObject::connect(m_manager, SIGNAL(indicatorAboutToBeUnloaded(const QString&)), this, SLOT(onIndicatorAboutToBeUnloaded(const QString&)));
59 QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
60 QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
61 QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
65IndicatorsModel::~IndicatorsModel()
67 disconnect(m_manager, 0, 0, 0);
68 m_manager->deleteLater();
72 \qmlproperty IndicatorsModel::count
73 The number of data entries in the model.
75 \b Note: methods should only be called after the Component has completed.
77int IndicatorsModel::count() const
83 \qmlmethod IndicatorsModel::unload()
87void IndicatorsModel::load()
94 \qmlmethod IndicatorsModel::unload()
96 Unload all indicators.
98void IndicatorsModel::unload()
104void IndicatorsModel::onIndicatorLoaded(const QString& indicator_name)
106 Indicator::Ptr indicator = m_manager->indicator(indicator_name);
112 if (m_indicators.indexOf(indicator) >= 0)
117 // find the insert position
119 while (pos < count())
121 // keep going while the existing priority is less.
122 if (indicatorData(indicator, IndicatorsModelRole::Priority).toInt() < data(index(pos), IndicatorsModelRole::Priority).toInt())
127 QObject::connect(indicator.data(), SIGNAL(identifierChanged(const QString&)), this, SLOT(onIdentifierChanged()));
128 QObject::connect(indicator.data(), SIGNAL(indicatorPropertiesChanged(const QVariant&)), this, SLOT(onIndicatorPropertiesChanged()));
130 beginInsertRows(QModelIndex(), pos, pos);
132 m_indicators.insert(pos, indicator);
137void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator_name)
139 Indicator::Ptr indicator = m_manager->indicator(indicator_name);
146 QMutableListIterator<Indicator::Ptr> iter(m_indicators);
147 while(iter.hasNext())
149 if (indicator == iter.next())
151 beginRemoveRows(QModelIndex(), i, i);
162void IndicatorsModel::onIdentifierChanged()
164 notifyDataChanged(QObject::sender(), IndicatorsModelRole::Identifier);
168void IndicatorsModel::onIndicatorPropertiesChanged()
170 notifyDataChanged(QObject::sender(), IndicatorsModelRole::IndicatorProperties);
174void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
176 Indicator* indicator = qobject_cast<Indicator*>(sender);
183 QMutableListIterator<Indicator::Ptr> iter(m_indicators);
184 while(iter.hasNext())
186 if (indicator == iter.next())
188 QModelIndex changedIndex = this->index(index);
189 dataChanged(changedIndex, changedIndex, QVector<int>() << role);
197QHash<int, QByteArray> IndicatorsModel::roleNames() const
199 static QHash<int, QByteArray> roles;
202 roles[IndicatorsModelRole::Identifier] = "identifier";
203 roles[IndicatorsModelRole::Priority] = "priority";
204 roles[IndicatorsModelRole::Title] = "title";
205 roles[IndicatorsModelRole::Description] = "description";
206 roles[IndicatorsModelRole::WidgetSource] = "widgetSource";
207 roles[IndicatorsModelRole::PageSource] = "pageSource";
208 roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
209 roles[IndicatorsModelRole::IsValid] = "isValid";
215int IndicatorsModel::columnCount(const QModelIndex &) const
221QVariant IndicatorsModel::defaultData(Indicator::Ptr indicator, int role)
225 case IndicatorsModelRole::Priority:
227 case IndicatorsModelRole::Title:
228 return indicator ? indicator->identifier() : "Unknown";
229 case IndicatorsModelRole::Description:
231 case IndicatorsModelRole::WidgetSource:
232 return shellAppDirectory()+"/Panel/Indicators/DefaultIndicatorWidget.qml";
233 case IndicatorsModelRole::PageSource:
234 return shellAppDirectory()+"/Panel/Indicators/DefaultIndicatorPage.qml";
239Q_INVOKABLE QVariant IndicatorsModel::data(int row, int role) const
241 return data(index(row, 0), role);
245QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
247 if (!index.isValid() || index.row() >= m_indicators.size())
250 Indicator::Ptr indicator = m_indicators[index.row()];
254 case IndicatorsModelRole::Identifier:
257 return QVariant(indicator->identifier());
260 case IndicatorsModelRole::IndicatorProperties:
263 return QVariant(indicator->indicatorProperties());
266 case IndicatorsModelRole::IsValid:
267 return (indicator ? true : false);
268 case IndicatorsModelRole::Priority:
269 case IndicatorsModelRole::Title:
270 case IndicatorsModelRole::Description:
271 case IndicatorsModelRole::WidgetSource:
272 case IndicatorsModelRole::PageSource:
273 return indicatorData(indicator, role);
280QVariant IndicatorsModel::indicatorData(const Indicator::Ptr& indicator, int role) const
282 if (indicator && m_parsed_indicator_data.contains(indicator->identifier()))
284 QVariantMap data = m_parsed_indicator_data[indicator->identifier()];
285 return data.value(roleNames()[role], QVariant());
287 return defaultData(indicator, role);
291QModelIndex IndicatorsModel::parent(const QModelIndex&) const
293 return QModelIndex();
297int IndicatorsModel::rowCount(const QModelIndex&) const
299 return m_indicators.count();
302void IndicatorsModel::setIndicatorData(const QVariant& data)
304 m_indicator_data = data;
306 m_parsed_indicator_data.clear();
307 QMap<QString, QVariant> map = data.toMap();
308 QMapIterator<QString, QVariant> iter(map);
309 while(iter.hasNext())
312 m_parsed_indicator_data[iter.key()] = iter.value().toMap();
315 Q_EMIT indicatorDataChanged(m_indicator_data);
318QVariant IndicatorsModel::indicatorData() const
320 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