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 | #ifndef CHART_H |
9 | #define CHART_H |
10 | |
11 | #include <QQuickItem> |
12 | #include <qqmlregistration.h> |
13 | |
14 | #include "datasource/ChartDataSource.h" |
15 | |
16 | #include "quickcharts_export.h" |
17 | |
18 | /** |
19 | * Abstract base class for all charts. |
20 | */ |
21 | class QUICKCHARTS_EXPORT Chart : public QQuickItem |
22 | { |
23 | Q_OBJECT |
24 | QML_ELEMENT |
25 | QML_UNCREATABLE("Base class" ) |
26 | |
27 | public: |
28 | using DataSourcesProperty = QQmlListProperty<ChartDataSource>; |
29 | |
30 | /** |
31 | * How to index color and name sources relative to the different value sources. |
32 | */ |
33 | enum IndexingMode { |
34 | IndexSourceValues = 1, ///< Index each value, restart indexing for each value source. |
35 | IndexEachSource, ///< Index each value source, never index individual values. |
36 | IndexAllValues ///< Index each value, continuing with the index for each value source. |
37 | }; |
38 | Q_ENUM(IndexingMode) |
39 | |
40 | explicit Chart(QQuickItem *parent = nullptr); |
41 | ~Chart() override = default; |
42 | |
43 | /** |
44 | * The data source to use for names of chart items. |
45 | */ |
46 | Q_PROPERTY(ChartDataSource *nameSource READ nameSource WRITE setNameSource NOTIFY nameSourceChanged) |
47 | ChartDataSource *nameSource() const; |
48 | void setNameSource(ChartDataSource *nameSource); |
49 | Q_SIGNAL void nameSourceChanged(); |
50 | |
51 | /** |
52 | * The data source to use for short names of chart items. |
53 | */ |
54 | Q_PROPERTY(ChartDataSource *shortNameSource READ shortNameSource WRITE setShortNameSource NOTIFY shortNameSourceChanged) |
55 | ChartDataSource *shortNameSource() const; |
56 | void setShortNameSource(ChartDataSource *shortNameSource); |
57 | Q_SIGNAL void shortNameSourceChanged(); |
58 | |
59 | /** |
60 | * The data source to use for colors of chart items. |
61 | */ |
62 | Q_PROPERTY(ChartDataSource *colorSource READ colorSource WRITE setColorSource NOTIFY colorSourceChanged) |
63 | ChartDataSource *colorSource() const; |
64 | void setColorSource(ChartDataSource *colorSource); |
65 | Q_SIGNAL void colorSourceChanged(); |
66 | |
67 | /** |
68 | * The data sources providing the data this chart needs to render. |
69 | */ |
70 | Q_PROPERTY(QQmlListProperty<ChartDataSource> valueSources READ valueSourcesProperty NOTIFY valueSourcesChanged) |
71 | DataSourcesProperty valueSourcesProperty(); |
72 | QList<ChartDataSource *> valueSources() const; |
73 | Q_SIGNAL void valueSourcesChanged(); |
74 | Q_INVOKABLE void insertValueSource(int index, ChartDataSource *source); |
75 | Q_INVOKABLE void removeValueSource(int index); |
76 | Q_INVOKABLE void removeValueSource(QObject *source); |
77 | |
78 | /** |
79 | * The indexing mode used for indexing colors and names. |
80 | */ |
81 | Q_PROPERTY(IndexingMode indexingMode READ indexingMode WRITE setIndexingMode NOTIFY indexingModeChanged) |
82 | IndexingMode indexingMode() const; |
83 | void setIndexingMode(IndexingMode newIndexingMode); |
84 | Q_SIGNAL void indexingModeChanged(); |
85 | |
86 | Q_SIGNAL void dataChanged(); |
87 | |
88 | protected: |
89 | /** |
90 | * Called when the data of a value source changes. |
91 | * |
92 | * This method should be reimplemented by subclasses. It is called whenever |
93 | * the data of one of the value sources changes. Subclasses should use this |
94 | * to make sure that they update whatever internal state they use for |
95 | * rendering, then call update() to schedule rendering the item. |
96 | */ |
97 | virtual void onDataChanged() = 0; |
98 | void componentComplete() override; |
99 | |
100 | private: |
101 | static void appendSource(DataSourcesProperty *list, ChartDataSource *source); |
102 | static qsizetype sourceCount(DataSourcesProperty *list); |
103 | static ChartDataSource *source(DataSourcesProperty *list, qsizetype index); |
104 | static void clearSources(DataSourcesProperty *list); |
105 | static void replaceSource(DataSourcesProperty *list, qsizetype index, ChartDataSource *source); |
106 | static void removeLastSource(DataSourcesProperty *list); |
107 | |
108 | ChartDataSource *m_nameSource = nullptr; |
109 | ChartDataSource *m_shortNameSource = nullptr; |
110 | ChartDataSource *m_colorSource = nullptr; |
111 | QList<ChartDataSource *> m_valueSources; |
112 | IndexingMode m_indexingMode = IndexEachSource; |
113 | }; |
114 | |
115 | #endif // CHART_H |
116 | |