20#include "indicatorsmodel.h"
21#include "indicatorsmanager.h"
23#include "indicators.h"
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();
79int IndicatorsModel::count()
const
90QString IndicatorsModel::profile()
const
92 return m_manager->profile();
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();
124void IndicatorsModel::load()
126 m_indicators.clear();
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)
156 while (pos < count())
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();