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"
32 \qmltype IndicatorsModel
33 \inherits QAbstractListModel
35 \brief The IndicatorsModel class defines the list model for indicators
37 \b {This component is under heavy development.}
39 This class expose the available indicators.
49 Component.onCompleted: menuModel.load()
53IndicatorsModel::IndicatorsModel(QObject *parent)
54 : QAbstractListModel(parent)
56 m_manager = new IndicatorsManager(this);
57 QObject::connect(m_manager, &IndicatorsManager::indicatorLoaded, this, &IndicatorsModel::onIndicatorLoaded);
58 QObject::connect(m_manager, &IndicatorsManager::indicatorAboutToBeUnloaded, this, &IndicatorsModel::onIndicatorAboutToBeUnloaded);
59 QObject::connect(m_manager, &IndicatorsManager::profileChanged, this, &IndicatorsModel::profileChanged);
61 QObject::connect(this, &IndicatorsModel::rowsInserted, this, &IndicatorsModel::countChanged);
62 QObject::connect(this, &IndicatorsModel::rowsRemoved, this, &IndicatorsModel::countChanged);
63 QObject::connect(this, &IndicatorsModel::modelReset, this, &IndicatorsModel::countChanged);
67IndicatorsModel::~IndicatorsModel()
69 disconnect(m_manager, 0, 0, 0);
70 m_manager->deleteLater();
74 \qmlproperty IndicatorsModel::count
75 The number of data entries in the model.
77 \b Note: methods should only be called after the Component has completed.
79int IndicatorsModel::count() const
85 \qmlproperty IndicatorsModel::profile
86 The indicator profile to use for the model.
88 \b Note: methods should only be called after the Component has completed.
90QString IndicatorsModel::profile() const
92 return m_manager->profile();
96 \qmlproperty IndicatorsModel::setProfile
97 Set the indicator profile to use for the model.
99 \b Note: methods should only be called after the Component has completed.
101void IndicatorsModel::setProfile(const QString &profile)
103 m_manager->setProfile(profile);
106bool IndicatorsModel::light() const
111void IndicatorsModel::setLight(const bool &light)
113 if (m_light != light) {
115 Q_EMIT lightChanged();
120 \qmlmethod IndicatorsModel::unload()
124void IndicatorsModel::load()
126 m_indicators.clear();
131 \qmlmethod IndicatorsModel::unload()
133 Unload all indicators.
135void IndicatorsModel::unload()
141void IndicatorsModel::onIndicatorLoaded(const QString& indicator_name)
143 Indicator::Ptr indicator = m_manager->indicator(indicator_name);
149 if (m_indicators.indexOf(indicator) >= 0)
154 // find the insert position
156 while (pos < count())
158 // keep going while the existing position is greater. (put lower position on end)
159 if (indicator->position() >= data(index(pos), IndicatorsModelRole::Position).toInt())
164 QObject::connect(indicator.data(), &Indicator::identifierChanged, this, &IndicatorsModel::onIdentifierChanged);
165 QObject::connect(indicator.data(), &Indicator::indicatorPropertiesChanged, this, &IndicatorsModel::onIndicatorPropertiesChanged);
167 beginInsertRows(QModelIndex(), pos, pos);
169 m_indicators.insert(pos, indicator);
174void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator_name)
176 Indicator::Ptr indicator = m_manager->indicator(indicator_name);
183 QMutableListIterator<Indicator::Ptr> iter(m_indicators);
184 while(iter.hasNext())
186 if (indicator == iter.next())
188 beginRemoveRows(QModelIndex(), i, i);
199void IndicatorsModel::onIdentifierChanged()
201 notifyDataChanged(QObject::sender(), IndicatorsModelRole::Identifier);
205void IndicatorsModel::onIndicatorPropertiesChanged()
207 notifyDataChanged(QObject::sender(), IndicatorsModelRole::IndicatorProperties);
211void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
213 Indicator* indicator = qobject_cast<Indicator*>(sender);
220 QMutableListIterator<Indicator::Ptr> iter(m_indicators);
221 while(iter.hasNext())
223 if (indicator == iter.next())
225 QModelIndex changedIndex = this->index(index);
226 dataChanged(changedIndex, changedIndex, QVector<int>() << role);
234QHash<int, QByteArray> IndicatorsModel::roleNames() const
236 static QHash<int, QByteArray> roles;
239 roles[IndicatorsModelRole::Identifier] = "identifier";
240 roles[IndicatorsModelRole::Position] = "position";
241 roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
247int IndicatorsModel::columnCount(const QModelIndex &) const
252QVariant IndicatorsModel::data(int row, int role) const
254 return data(index(row, 0), role);
258QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
260 if (!index.isValid() || index.row() >= m_indicators.size())
263 Indicator::Ptr indicator = m_indicators.at(index.row());
267 case IndicatorsModelRole::Identifier:
270 return indicator->identifier();
273 case IndicatorsModelRole::Position:
276 return indicator->position();
279 case IndicatorsModelRole::IndicatorProperties:
282 return indicator->indicatorProperties();
292QModelIndex IndicatorsModel::parent(const QModelIndex&) const
294 return QModelIndex();
298int IndicatorsModel::rowCount(const QModelIndex&) const
300 return m_indicators.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