20 #include "indicatorsmodel.h"
21 #include "indicatorsmanager.h"
22 #include "indicator.h"
23 #include "indicators.h"
27 #include <QQmlContext>
53 IndicatorsModel::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);
67 IndicatorsModel::~IndicatorsModel()
69 disconnect(m_manager, 0, 0, 0);
70 m_manager->deleteLater();
79 int IndicatorsModel::count()
const
90 QString IndicatorsModel::profile()
const
92 return m_manager->profile();
101 void IndicatorsModel::setProfile(
const QString &profile)
103 m_manager->setProfile(profile);
111 void IndicatorsModel::load()
113 m_indicators.clear();
122 void IndicatorsModel::unload()
128 void IndicatorsModel::onIndicatorLoaded(
const QString& indicator_name)
130 Indicator::Ptr indicator = m_manager->indicator(indicator_name);
136 if (m_indicators.indexOf(indicator) >= 0)
143 while (pos < count())
146 if (indicator->position() >= data(index(pos), IndicatorsModelRole::Position).toInt())
151 QObject::connect(indicator.data(), &Indicator::identifierChanged,
this, &IndicatorsModel::onIdentifierChanged);
152 QObject::connect(indicator.data(), &Indicator::indicatorPropertiesChanged,
this, &IndicatorsModel::onIndicatorPropertiesChanged);
154 beginInsertRows(QModelIndex(), pos, pos);
156 m_indicators.insert(pos, indicator);
161 void IndicatorsModel::onIndicatorAboutToBeUnloaded(
const QString& indicator_name)
163 Indicator::Ptr indicator = m_manager->indicator(indicator_name);
170 QMutableListIterator<Indicator::Ptr> iter(m_indicators);
171 while(iter.hasNext())
173 if (indicator == iter.next())
175 beginRemoveRows(QModelIndex(), i, i);
186 void IndicatorsModel::onIdentifierChanged()
188 notifyDataChanged(QObject::sender(), IndicatorsModelRole::Identifier);
192 void IndicatorsModel::onIndicatorPropertiesChanged()
194 notifyDataChanged(QObject::sender(), IndicatorsModelRole::IndicatorProperties);
198 void IndicatorsModel::notifyDataChanged(QObject *sender,
int role)
200 Indicator* indicator = qobject_cast<Indicator*>(sender);
207 QMutableListIterator<Indicator::Ptr> iter(m_indicators);
208 while(iter.hasNext())
210 if (indicator == iter.next())
212 QModelIndex changedIndex = this->index(index);
213 dataChanged(changedIndex, changedIndex, QVector<int>() << role);
221 QHash<int, QByteArray> IndicatorsModel::roleNames()
const
223 static QHash<int, QByteArray> roles;
226 roles[IndicatorsModelRole::Identifier] =
"identifier";
227 roles[IndicatorsModelRole::Position] =
"position";
228 roles[IndicatorsModelRole::IndicatorProperties] =
"indicatorProperties";
234 int IndicatorsModel::columnCount(
const QModelIndex &)
const
239 Q_INVOKABLE QVariant IndicatorsModel::data(
int row,
int role)
const
241 return data(index(row, 0), role);
245 QVariant 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::Position:
263 return QVariant(indicator->position());
266 case IndicatorsModelRole::IndicatorProperties:
269 return QVariant(indicator->indicatorProperties());
279 QModelIndex IndicatorsModel::parent(
const QModelIndex&)
const
281 return QModelIndex();
285 int IndicatorsModel::rowCount(
const QModelIndex&)
const
287 return m_indicators.count();