1/*
2 * This file is part of KQuickCharts
3 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8#include "Chart.h"
9#include "datasource/ChartDataSource.h"
10
11Chart::Chart(QQuickItem *parent)
12 : QQuickItem(parent)
13{
14 setFlag(flag: ItemHasContents, enabled: true);
15 connect(sender: this, signal: &Chart::dataChanged, context: this, slot: &Chart::onDataChanged);
16}
17
18ChartDataSource *Chart::nameSource() const
19{
20 return m_nameSource;
21}
22
23void Chart::setNameSource(ChartDataSource *nameSource)
24{
25 if (m_nameSource == nameSource) {
26 return;
27 }
28
29 m_nameSource = nameSource;
30 Q_EMIT dataChanged();
31 Q_EMIT nameSourceChanged();
32}
33
34ChartDataSource *Chart::shortNameSource() const
35{
36 return m_shortNameSource;
37}
38
39void Chart::setShortNameSource(ChartDataSource *shortNameSource)
40{
41 if (m_shortNameSource == shortNameSource) {
42 return;
43 }
44
45 m_shortNameSource = shortNameSource;
46 Q_EMIT dataChanged();
47 Q_EMIT shortNameSourceChanged();
48}
49
50ChartDataSource *Chart::colorSource() const
51{
52 return m_colorSource;
53}
54
55void Chart::setColorSource(ChartDataSource *colorSource)
56{
57 if (m_colorSource == colorSource) {
58 return;
59 }
60
61 if (m_colorSource) {
62 disconnect(sender: m_colorSource, signal: &ChartDataSource::dataChanged, receiver: this, slot: &Chart::dataChanged);
63 }
64
65 m_colorSource = colorSource;
66
67 if (m_colorSource) {
68 connect(sender: m_colorSource, signal: &ChartDataSource::dataChanged, context: this, slot: &Chart::dataChanged);
69 }
70
71 Q_EMIT dataChanged();
72 Q_EMIT colorSourceChanged();
73}
74
75Chart::DataSourcesProperty Chart::valueSourcesProperty()
76{
77 return DataSourcesProperty{
78 this,
79 this,
80 &Chart::appendSource,
81 &Chart::sourceCount,
82 &Chart::source,
83 &Chart::clearSources,
84 &Chart::replaceSource,
85 &Chart::removeLastSource,
86 };
87}
88
89QList<ChartDataSource *> Chart::valueSources() const
90{
91 return m_valueSources;
92}
93
94void Chart::insertValueSource(int index, ChartDataSource *source)
95{
96 if (index < 0) {
97 return;
98 }
99
100 m_valueSources.insert(i: index, t: source);
101 connect(sender: source, signal: &QObject::destroyed, context: this, slot: qOverload<QObject *>(&Chart::removeValueSource));
102 connect(sender: source, signal: &ChartDataSource::dataChanged, context: this, slot: &Chart::dataChanged);
103
104 Q_EMIT dataChanged();
105 Q_EMIT valueSourcesChanged();
106}
107
108void Chart::removeValueSource(int index)
109{
110 if (index < 0 || index >= m_valueSources.count()) {
111 return;
112 }
113
114 m_valueSources.at(i: index)->disconnect(receiver: this);
115 m_valueSources.remove(i: index);
116
117 Q_EMIT dataChanged();
118 Q_EMIT valueSourcesChanged();
119}
120
121void Chart::removeValueSource(QObject *source)
122{
123 auto itr = std::find_if(first: m_valueSources.begin(), last: m_valueSources.end(), pred: [source](QObject *dataSource) {
124 return dataSource == source;
125 });
126
127 if (itr != m_valueSources.end()) {
128 (*itr)->disconnect(receiver: this);
129 m_valueSources.erase(pos: itr);
130 }
131
132 Q_EMIT dataChanged();
133 Q_EMIT valueSourcesChanged();
134}
135
136Chart::IndexingMode Chart::indexingMode() const
137{
138 return m_indexingMode;
139}
140
141void Chart::setIndexingMode(IndexingMode newIndexingMode)
142{
143 if (newIndexingMode == m_indexingMode) {
144 return;
145 }
146
147 m_indexingMode = newIndexingMode;
148 Q_EMIT dataChanged();
149 Q_EMIT indexingModeChanged();
150}
151
152void Chart::componentComplete()
153{
154 QQuickItem::componentComplete();
155 Q_EMIT dataChanged();
156}
157
158void Chart::appendSource(Chart::DataSourcesProperty *list, ChartDataSource *source)
159{
160 auto chart = reinterpret_cast<Chart *>(list->data);
161 chart->insertValueSource(index: chart->valueSources().size(), source);
162}
163qsizetype Chart::sourceCount(Chart::DataSourcesProperty *list)
164{
165 return reinterpret_cast<Chart *>(list->data)->m_valueSources.count();
166}
167
168ChartDataSource *Chart::source(Chart::DataSourcesProperty *list, qsizetype index)
169{
170 return reinterpret_cast<Chart *>(list->data)->m_valueSources.at(i: index);
171}
172
173void Chart::clearSources(Chart::DataSourcesProperty *list)
174{
175 auto chart = reinterpret_cast<Chart *>(list->data);
176 std::for_each(first: chart->m_valueSources.cbegin(), last: chart->m_valueSources.cend(), f: [chart](ChartDataSource *source) {
177 source->disconnect(receiver: chart);
178 });
179 chart->m_valueSources.clear();
180 Q_EMIT chart->dataChanged();
181}
182
183void Chart::replaceSource(DataSourcesProperty *list, qsizetype index, ChartDataSource *source)
184{
185 auto chart = reinterpret_cast<Chart *>(list->data);
186 Q_ASSERT(index > 0 && index < chart->m_valueSources.size());
187 chart->m_valueSources.at(i: index)->disconnect(receiver: chart);
188 chart->m_valueSources.replace(i: index, t: source);
189 connect(sender: source, signal: &QObject::destroyed, context: chart, slot: qOverload<QObject *>(&Chart::removeValueSource));
190 connect(sender: source, signal: &ChartDataSource::dataChanged, context: chart, slot: &Chart::dataChanged);
191 Q_EMIT chart->dataChanged();
192}
193
194void Chart::removeLastSource(DataSourcesProperty *list)
195{
196 auto chart = reinterpret_cast<Chart *>(list->data);
197 chart->removeValueSource(index: chart->m_valueSources.size() - 1);
198}
199
200#include "moc_Chart.cpp"
201

source code of kquickcharts/src/Chart.cpp