KQuickCharts

Chart.h
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 */
21class QUICKCHARTS_EXPORT Chart : public QQuickItem
22{
23 Q_OBJECT
24 QML_ELEMENT
25 QML_UNCREATABLE("Base class")
26
27public:
29
30 /**
31 * How to index color and name sources relative to the different value sources.
32 */
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 /**
87 * The index of a value source to highlight.
88 *
89 * Highlighting is dependant on Chart type, but will generally mean that
90 * other value sources are rendered with lower opacity.
91 *
92 * By default, this is -1 which means nothing is highlighted.
93 */
94 Q_PROPERTY(int highlight READ highlight WRITE setHighlight NOTIFY highlightChanged RESET resetHighlight)
95 int highlight() const;
96 void setHighlight(int highlight);
97 void resetHighlight();
98 Q_SIGNAL void highlightChanged();
99
100 Q_SIGNAL void dataChanged();
101
102protected:
103 /**
104 * Called when the data of a value source changes.
105 *
106 * This method should be reimplemented by subclasses. It is called whenever
107 * the data of one of the value sources changes. Subclasses should use this
108 * to make sure that they update whatever internal state they use for
109 * rendering, then call update() to schedule rendering the item.
110 */
111 virtual void onDataChanged() = 0;
112 void componentComplete() override;
113
114 /**
115 * Desaturate and de-emphasise a color.
116 *
117 * Mainly intended as a standard for ensuring everything but the highlighted
118 * item is desaturated.
119 */
120 QColor desaturate(const QColor &input);
121
122private:
123 static void appendSource(DataSourcesProperty *list, ChartDataSource *source);
124 static qsizetype sourceCount(DataSourcesProperty *list);
125 static ChartDataSource *source(DataSourcesProperty *list, qsizetype index);
126 static void clearSources(DataSourcesProperty *list);
127 static void replaceSource(DataSourcesProperty *list, qsizetype index, ChartDataSource *source);
128 static void removeLastSource(DataSourcesProperty *list);
129
130 ChartDataSource *m_nameSource = nullptr;
131 ChartDataSource *m_shortNameSource = nullptr;
132 ChartDataSource *m_colorSource = nullptr;
133 QList<ChartDataSource *> m_valueSources;
134 IndexingMode m_indexingMode = IndexEachSource;
135 int m_highlight = -1;
136};
137
138#endif // CHART_H
Abstract base class for data sources.
Abstract base class for all charts.
Definition Chart.h:22
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
@ IndexEachSource
Index each value source, never index individual values.
Definition Chart.h:35
Q_ENUM(...)
Q_PROPERTY(...)
virtual void componentComplete() override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:01:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.