KQuickCharts

Chart.cpp
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(ItemHasContents, true);
15 connect(this, &Chart::dataChanged, this, &Chart::onDataChanged);
16}
17
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
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
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(m_colorSource, &ChartDataSource::dataChanged, this, &Chart::dataChanged);
63 }
64
65 m_colorSource = colorSource;
66
67 if (m_colorSource) {
68 connect(m_colorSource, &ChartDataSource::dataChanged, this, &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
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(index, source);
101 connect(source, &QObject::destroyed, this, qOverload<QObject *>(&Chart::removeValueSource));
102 connect(source, &ChartDataSource::dataChanged, this, &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(index)->disconnect(this);
115 m_valueSources.remove(index);
116
117 Q_EMIT dataChanged();
118 Q_EMIT valueSourcesChanged();
119}
120
121void Chart::removeValueSource(QObject *source)
122{
123 auto itr = std::find_if(m_valueSources.begin(), m_valueSources.end(), [source](QObject *dataSource) {
124 return dataSource == source;
125 });
126
127 if (itr != m_valueSources.end()) {
128 (*itr)->disconnect(this);
129 m_valueSources.erase(itr);
130 }
131
132 Q_EMIT dataChanged();
133 Q_EMIT valueSourcesChanged();
134}
135
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
152int Chart::highlight() const
153{
154 return m_highlight;
155}
156
157void Chart::setHighlight(int newHighlight)
158{
159 if (newHighlight == m_highlight) {
160 return;
161 }
162
163 m_highlight = newHighlight;
164 Q_EMIT dataChanged();
165 Q_EMIT highlightChanged();
166}
167
168void Chart::resetHighlight()
169{
170 setHighlight(-1);
171}
172
173void Chart::componentComplete()
174{
176 Q_EMIT dataChanged();
177}
178
180{
181 auto color = input.convertTo(QColor::Hsl);
182 color.setHslF(color.hueF(), color.saturationF() * 0.5, color.lightnessF() * 0.5, color.alphaF() * 0.5);
183 return color.convertTo(QColor::Rgb);
184}
185
186void Chart::appendSource(Chart::DataSourcesProperty *list, ChartDataSource *source)
187{
188 auto chart = reinterpret_cast<Chart *>(list->data);
189 chart->insertValueSource(chart->valueSources().size(), source);
190}
191qsizetype Chart::sourceCount(Chart::DataSourcesProperty *list)
192{
193 return reinterpret_cast<Chart *>(list->data)->m_valueSources.count();
194}
195
196ChartDataSource *Chart::source(Chart::DataSourcesProperty *list, qsizetype index)
197{
198 return reinterpret_cast<Chart *>(list->data)->m_valueSources.at(index);
199}
200
201void Chart::clearSources(Chart::DataSourcesProperty *list)
202{
203 auto chart = reinterpret_cast<Chart *>(list->data);
204 std::for_each(chart->m_valueSources.cbegin(), chart->m_valueSources.cend(), [chart](ChartDataSource *source) {
205 source->disconnect(chart);
206 });
207 chart->m_valueSources.clear();
208 Q_EMIT chart->dataChanged();
209}
210
211void Chart::replaceSource(DataSourcesProperty *list, qsizetype index, ChartDataSource *source)
212{
213 auto chart = reinterpret_cast<Chart *>(list->data);
214 Q_ASSERT(index > 0 && index < chart->m_valueSources.size());
215 chart->m_valueSources.at(index)->disconnect(chart);
216 chart->m_valueSources.replace(index, source);
217 connect(source, &QObject::destroyed, chart, qOverload<QObject *>(&Chart::removeValueSource));
218 connect(source, &ChartDataSource::dataChanged, chart, &Chart::dataChanged);
219 Q_EMIT chart->dataChanged();
220}
221
222void Chart::removeLastSource(DataSourcesProperty *list)
223{
224 auto chart = reinterpret_cast<Chart *>(list->data);
225 chart->removeValueSource(chart->m_valueSources.size() - 1);
226}
227
228#include "moc_Chart.cpp"
Abstract base class for data sources.
Abstract base class for all charts.
Definition Chart.h:22
QColor desaturate(const QColor &input)
Desaturate and de-emphasise a color.
Definition Chart.cpp:179
ChartDataSource * shortNameSource
The data source to use for short names of chart items.
Definition Chart.h:54
QQmlListProperty< ChartDataSource > valueSources
The data sources providing the data this chart needs to render.
Definition Chart.h:70
ChartDataSource * nameSource
The data source to use for names of chart items.
Definition Chart.h:46
ChartDataSource * colorSource
The data source to use for colors of chart items.
Definition Chart.h:62
int highlight
The index of a value source to highlight.
Definition Chart.h:94
virtual void onDataChanged()=0
Called when the data of a value source changes.
IndexingMode
How to index color and name sources relative to the different value sources.
Definition Chart.h:33
IndexingMode indexingMode
The indexing mode used for indexing colors and names.
Definition Chart.h:81
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QColor convertTo(Spec colorSpec) const const
void setHslF(float h, float s, float l, float a)
const_reference at(qsizetype i) const const
iterator begin()
qsizetype count() const const
pointer data()
iterator end()
iterator erase(const_iterator begin, const_iterator end)
iterator insert(const_iterator before, parameter_type value)
void remove(qsizetype i, qsizetype n)
qsizetype size() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)
bool disconnect(const QMetaObject::Connection &connection)
virtual void componentComplete() override
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:23 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.