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 RANGEGROUP_H |
9 | #define RANGEGROUP_H |
10 | |
11 | #include <functional> |
12 | |
13 | #include <QObject> |
14 | #include <qqmlregistration.h> |
15 | |
16 | #include "quickcharts_export.h" |
17 | |
18 | class ChartDataSource; |
19 | |
20 | /** |
21 | * An object that can be used as a grouped property to provide a value range for charts. |
22 | * |
23 | */ |
24 | class QUICKCHARTS_EXPORT RangeGroup : public QObject |
25 | { |
26 | Q_OBJECT |
27 | QML_NAMED_ELEMENT(Range) |
28 | QML_UNCREATABLE("Grouped Property" ) |
29 | |
30 | public: |
31 | struct RangeResult { |
32 | qreal start = 0.0; |
33 | qreal end = 0.0; |
34 | qreal distance = 0.0; |
35 | }; |
36 | |
37 | explicit RangeGroup(QObject *parent = nullptr); |
38 | |
39 | /** |
40 | * The start of this range. |
41 | * |
42 | * The default is 0. |
43 | */ |
44 | Q_PROPERTY(qreal from READ from WRITE setFrom NOTIFY fromChanged) |
45 | qreal from() const; |
46 | void setFrom(qreal from); |
47 | Q_SIGNAL void fromChanged(); |
48 | /** |
49 | * The end of this range. |
50 | * |
51 | * The default is 100. |
52 | */ |
53 | Q_PROPERTY(qreal to READ to WRITE setTo NOTIFY toChanged) |
54 | qreal to() const; |
55 | void setTo(qreal to); |
56 | Q_SIGNAL void toChanged(); |
57 | /** |
58 | * Whether to determine the range based on values of a chart. |
59 | * |
60 | * If true (the default), `from` and `to` are ignored and instead calculated |
61 | * from the minimum and maximum values of a chart's valueSources. |
62 | */ |
63 | Q_PROPERTY(bool automatic READ automatic WRITE setAutomatic NOTIFY automaticChanged) |
64 | bool automatic() const; |
65 | void setAutomatic(bool newAutomatic); |
66 | Q_SIGNAL void automaticChanged(); |
67 | /** |
68 | * The distance between from and to. |
69 | */ |
70 | Q_PROPERTY(qreal distance READ distance NOTIFY rangeChanged) |
71 | qreal distance() const; |
72 | /** |
73 | * The minimum size of the range. |
74 | * |
75 | * This is mostly relevant when automatic is true. Setting this value will |
76 | * ensure that the range will never be smaller than this value. The default |
77 | * is `std::numeric_limits<qreal>::min`, which means minimum is disabled. |
78 | */ |
79 | Q_PROPERTY(qreal minimum READ minimum WRITE setMinimum NOTIFY minimumChanged) |
80 | qreal minimum() const; |
81 | void setMinimum(qreal newMinimum); |
82 | Q_SIGNAL void minimumChanged(); |
83 | /** |
84 | * The amount with which the range increases. |
85 | * |
86 | * The total range will be limited to a multiple of this value. This is |
87 | * mostly useful when automatic is true. The default is 0.0, which means do |
88 | * not limit the range increment. |
89 | */ |
90 | Q_PROPERTY(qreal increment READ increment WRITE setIncrement NOTIFY incrementChanged) |
91 | qreal increment() const; |
92 | void setIncrement(qreal newIncrement); |
93 | Q_SIGNAL void incrementChanged(); |
94 | |
95 | bool isValid() const; |
96 | |
97 | Q_SIGNAL void rangeChanged(); |
98 | |
99 | RangeResult calculateRange(const QList<ChartDataSource *> &sources, |
100 | std::function<qreal(ChartDataSource *)> minimumCallback, |
101 | std::function<qreal(ChartDataSource *)> maximumCallback); |
102 | |
103 | private: |
104 | qreal m_from = 0.0; |
105 | qreal m_to = 100.0; |
106 | bool m_automatic = true; |
107 | qreal m_minimum = std::numeric_limits<qreal>::min(); |
108 | qreal m_increment = 0.0; |
109 | }; |
110 | |
111 | #endif // RANGEGROUP_H |
112 | |