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
10Chart::Chart(QQuickItem *parent)
11 : QQuickItem(parent)
12{
13 setFlag(flag: ItemHasContents, enabled: true);
14 connect(sender: this, signal: &Chart::dataChanged, context: this, slot: &Chart::onDataChanged);
15}
16
17ChartDataSource *Chart::nameSource() const
18{
19 return m_nameSource;
20}
21
22void Chart::setNameSource(ChartDataSource *nameSource)
23{
24 if (m_nameSource == nameSource) {
25 return;
26 }
27
28 m_nameSource = nameSource;
29 Q_EMIT dataChanged();
30 Q_EMIT nameSourceChanged();
31}
32
33ChartDataSource *Chart::shortNameSource() const
34{
35 return m_shortNameSource;
36}
37
38void Chart::setShortNameSource(ChartDataSource *shortNameSource)
39{
40 if (m_shortNameSource == shortNameSource) {
41 return;
42 }
43
44 m_shortNameSource = shortNameSource;
45 Q_EMIT dataChanged();
46 Q_EMIT shortNameSourceChanged();
47}
48
49ChartDataSource *Chart::colorSource() const
50{
51 return m_colorSource;
52}
53
54void Chart::setColorSource(ChartDataSource *colorSource)
55{
56 if (m_colorSource == colorSource) {
57 return;
58 }
59
60 if (m_colorSource) {
61 disconnect(sender: m_colorSource, signal: &ChartDataSource::dataChanged, receiver: this, slot: &Chart::dataChanged);
62 }
63
64 m_colorSource = colorSource;
65
66 if (m_colorSource) {
67 connect(sender: m_colorSource, signal: &ChartDataSource::dataChanged, context: this, slot: &Chart::dataChanged);
68 }
69
70 Q_EMIT dataChanged();
71 Q_EMIT colorSourceChanged();
72}
73
74Chart::DataSourcesProperty Chart::valueSourcesProperty()
75{
76 return DataSourcesProperty{
77 this,
78 this,
79 &Chart::appendSource,
80 &Chart::sourceCount,
81 &Chart::source,
82 &Chart::clearSources,
83 &Chart::replaceSource,
84 &Chart::removeLastSource,
85 };
86}
87
88QList<ChartDataSource *> Chart::valueSources() const
89{
90 return m_valueSources;
91}
92
93void Chart::insertValueSource(int index, ChartDataSource *source)
94{
95 if (index < 0) {
96 return;
97 }
98
99 m_valueSources.insert(i: index, t: source);
100 connect(sender: source, signal: &QObject::destroyed, context: this, slot: qOverload<QObject *>(&Chart::removeValueSource));
101 connect(sender: source, signal: &ChartDataSource::dataChanged, context: this, slot: &Chart::dataChanged);
102
103 Q_EMIT dataChanged();
104 Q_EMIT valueSourcesChanged();
105}
106
107void Chart::removeValueSource(int index)
108{
109 if (index < 0 || index >= m_valueSources.count()) {
110 return;
111 }
112
113 m_valueSources.at(i: index)->disconnect(receiver: this);
114 m_valueSources.remove(i: index);
115
116 Q_EMIT dataChanged();
117 Q_EMIT valueSourcesChanged();
118}
119
120void Chart::removeValueSource(QObject *source)
121{
122 auto itr = std::find_if(first: m_valueSources.begin(), last: m_valueSources.end(), pred: [source](QObject *dataSource) {
123 return dataSource == source;
124 });
125
126 if (itr != m_valueSources.end()) {
127 (*itr)->disconnect(receiver: this);
128 m_valueSources.erase(pos: itr);
129 }
130
131 Q_EMIT dataChanged();
132 Q_EMIT valueSourcesChanged();
133}
134
135Chart::IndexingMode Chart::indexingMode() const
136{
137 return m_indexingMode;
138}
139
140void Chart::setIndexingMode(IndexingMode newIndexingMode)
141{
142 if (newIndexingMode == m_indexingMode) {
143 return;
144 }
145
146 m_indexingMode = newIndexingMode;
147 Q_EMIT dataChanged();
148 Q_EMIT indexingModeChanged();
149}
150
151int Chart::highlight() const
152{
153 return m_highlight;
154}
155
156void Chart::setHighlight(int newHighlight)
157{
158 if (newHighlight == m_highlight) {
159 return;
160 }
161
162 m_highlight = newHighlight;
163 Q_EMIT dataChanged();
164 Q_EMIT highlightChanged();
165}
166
167void Chart::resetHighlight()
168{
169 setHighlight(-1);
170}
171
172void Chart::componentComplete()
173{
174 QQuickItem::componentComplete();
175 Q_EMIT dataChanged();
176}
177
178QColor Chart::desaturate(const QColor &input)
179{
180 auto color = input.convertTo(colorSpec: QColor::Hsl);
181 color.setHslF(h: color.hueF(), s: color.saturationF() * 0.5, l: color.lightnessF() * 0.5, a: color.alphaF() * 0.5);
182 return color.convertTo(colorSpec: QColor::Rgb);
183}
184
185void Chart::appendSource(Chart::DataSourcesProperty *list, ChartDataSource *source)
186{
187 auto chart = reinterpret_cast<Chart *>(list->data);
188 chart->insertValueSource(index: chart->valueSources().size(), source);
189}
190qsizetype Chart::sourceCount(Chart::DataSourcesProperty *list)
191{
192 return reinterpret_cast<Chart *>(list->data)->m_valueSources.count();
193}
194
195ChartDataSource *Chart::source(Chart::DataSourcesProperty *list, qsizetype index)
196{
197 return reinterpret_cast<Chart *>(list->data)->m_valueSources.at(i: index);
198}
199
200void Chart::clearSources(Chart::DataSourcesProperty *list)
201{
202 auto chart = reinterpret_cast<Chart *>(list->data);
203 std::for_each(first: chart->m_valueSources.cbegin(), last: chart->m_valueSources.cend(), f: [chart](ChartDataSource *source) {
204 source->disconnect(receiver: chart);
205 });
206 chart->m_valueSources.clear();
207 Q_EMIT chart->dataChanged();
208}
209
210void Chart::replaceSource(DataSourcesProperty *list, qsizetype index, ChartDataSource *source)
211{
212 auto chart = reinterpret_cast<Chart *>(list->data);
213 Q_ASSERT(index > 0 && index < chart->m_valueSources.size());
214 chart->m_valueSources.at(i: index)->disconnect(receiver: chart);
215 chart->m_valueSources.replace(i: index, t: source);
216 connect(sender: source, signal: &QObject::destroyed, context: chart, slot: qOverload<QObject *>(&Chart::removeValueSource));
217 connect(sender: source, signal: &ChartDataSource::dataChanged, context: chart, slot: &Chart::dataChanged);
218 Q_EMIT chart->dataChanged();
219}
220
221void Chart::removeLastSource(DataSourcesProperty *list)
222{
223 auto chart = reinterpret_cast<Chart *>(list->data);
224 chart->removeValueSource(index: chart->m_valueSources.size() - 1);
225}
226
227#include "moc_Chart.cpp"
228

source code of kquickcharts/src/Chart.cpp