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
152void Chart::componentComplete()
153{
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(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(index);
171}
172
173void Chart::clearSources(Chart::DataSourcesProperty *list)
174{
175 auto chart = reinterpret_cast<Chart *>(list->data);
176 std::for_each(chart->m_valueSources.cbegin(), chart->m_valueSources.cend(), [chart](ChartDataSource *source) {
177 source->disconnect(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(index)->disconnect(chart);
188 chart->m_valueSources.replace(index, source);
189 connect(source, &QObject::destroyed, chart, qOverload<QObject *>(&Chart::removeValueSource));
190 connect(source, &ChartDataSource::dataChanged, chart, &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(chart->m_valueSources.size() - 1);
198}
199
200#include "moc_Chart.cpp"
Abstract base class for data sources.
Abstract base class for all charts.
Definition Chart.h:22
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
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)
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 Tue Mar 26 2024 11:13:57 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.