8#include "LegendModel.h"
11#include "datasource/ChartDataSource.h"
13LegendModel::LegendModel(
QObject *parent)
20 static QHash<int, QByteArray> names = {
22 {ShortNameRole,
"shortName"},
30int LegendModel::rowCount(
const QModelIndex &parent)
const
36 return m_items.
size();
41 if (!
checkIndex(
index, CheckIndexOption::ParentIsInvalid | CheckIndexOption::IndexIsValid)) {
47 return m_items.at(
index.row()).name;
49 return m_items.at(
index.row()).shortName;
51 return m_items.at(
index.row()).color;
53 return m_items.at(
index.row()).value;
59Chart *LegendModel::chart()
const
64void LegendModel::setChart(
Chart *newChart)
66 if (newChart == m_chart) {
71 for (
const auto &connection : std::as_const(m_connections)) {
74 m_connections.clear();
82int LegendModel::sourceIndex()
const
87void LegendModel::setSourceIndex(
int index)
89 if (
index == m_sourceIndex) {
93 m_sourceIndex =
index;
95 Q_EMIT sourceIndexChanged();
98void LegendModel::queueUpdate()
100 if (!m_updateQueued) {
101 m_updateQueued =
true;
106void LegendModel::queueDataChange()
108 if (!m_dataChangeQueued) {
109 m_dataChangeQueued =
true;
114void LegendModel::update()
116 m_updateQueued =
false;
125 ChartDataSource *colorSource = m_chart->colorSource();
126 ChartDataSource *nameSource = m_chart->nameSource();
127 ChartDataSource *shortNameSource = m_chart->shortNameSource();
132 auto sources = m_chart->valueSources();
133 int itemCount = countItems();
135 std::transform(sources.cbegin(), sources.cend(), std::back_inserter(m_connections), [
this](ChartDataSource *source) {
136 return connect(source, &ChartDataSource::dataChanged, this, &LegendModel::queueDataChange, Qt::UniqueConnection);
141 if ((!colorSource && !(nameSource || shortNameSource)) || itemCount <= 0) {
147 m_connections.push_back(
connect(colorSource, &ChartDataSource::dataChanged,
this, &LegendModel::queueDataChange,
Qt::UniqueConnection));
151 m_connections.push_back(
connect(nameSource, &ChartDataSource::dataChanged,
this, &LegendModel::queueDataChange,
Qt::UniqueConnection));
154 if (shortNameSource) {
155 m_connections.push_back(
connect(shortNameSource, &ChartDataSource::dataChanged,
this, &LegendModel::queueDataChange,
Qt::UniqueConnection));
158 for (
int i = 0; i < itemCount; ++i) {
160 item.name = nameSource ? nameSource->item(i).
toString() : QString();
161 item.shortName = shortNameSource ? shortNameSource->item(i).
toString() : QString();
162 item.color = colorSource ? colorSource->item(i).
value<QColor>() : QColor();
163 item.value = getValueForItem(i);
164 m_items.push_back(item);
170void LegendModel::updateData()
172 ChartDataSource *colorSource = m_chart->colorSource();
173 ChartDataSource *nameSource = m_chart->nameSource();
174 ChartDataSource *shortNameSource = m_chart->shortNameSource();
176 auto itemCount = countItems();
178 m_dataChangeQueued =
false;
180 if (itemCount !=
int(m_items.size())) {
186 QList<QList<int>> changedRows(itemCount);
188 std::for_each(m_items.begin(), m_items.end(), [&, i = 0](LegendItem &item)
mutable {
189 auto name = nameSource ? nameSource->item(i).toString() : QString{};
190 if (item.name != name) {
192 changedRows[i] << NameRole;
195 auto shortName = shortNameSource ? shortNameSource->item(i).
toString() : QString{};
196 if (item.shortName != shortName) {
197 item.shortName = shortName;
198 changedRows[i] << ShortNameRole;
201 auto color = colorSource ? colorSource->item(i).
toString() : QColor{};
202 if (item.color != color) {
204 changedRows[i] << ColorRole;
207 auto value = getValueForItem(i);
208 if (item.value != value) {
210 changedRows[i] << ValueRole;
216 for (
auto i = 0; i < changedRows.size(); ++i) {
217 auto changedRoles = changedRows.at(i);
218 if (!changedRoles.isEmpty()) {
219 Q_EMIT dataChanged(index(i, 0), index(i, 0), changedRoles);
224int LegendModel::countItems()
226 auto sources = m_chart->valueSources();
229 switch (m_chart->indexingMode()) {
231 if (sources.count() > 0) {
232 itemCount = sources.at(0)->itemCount();
236 itemCount = sources.count();
239 itemCount = std::accumulate(sources.cbegin(), sources.cend(), 0, [](
int current, ChartDataSource *source) ->
int {
240 return current + source->itemCount();
248QVariant LegendModel::getValueForItem(
int item)
250 const auto sources = m_chart->valueSources();
251 auto value = QVariant{};
253 switch (m_chart->indexingMode()) {
255 value = sources.at(0)->item(item);
258 value = sources.at(item)->first();
261 for (
auto source : sources) {
262 if (item >= source->itemCount()) {
263 item -= source->itemCount();
265 value = source->item(item);
275#include "moc_LegendModel.cpp"
Abstract base class for all charts.
@ IndexAllValues
Index each value, continuing with the index for each value source.
@ IndexSourceValues
Index each value, restart indexing for each value source.
@ IndexEachSource
Index each value source, never index individual values.
bool checkIndex(const QModelIndex &index, CheckIndexOptions options) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
qsizetype size() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QString toString() const const