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 "RangeGroup.h" |
9 | |
10 | #include <cmath> |
11 | |
12 | #include <QList> |
13 | |
14 | RangeGroup::RangeGroup(QObject *parent) |
15 | : QObject(parent) |
16 | { |
17 | connect(sender: this, signal: &RangeGroup::fromChanged, context: this, slot: &RangeGroup::rangeChanged); |
18 | connect(sender: this, signal: &RangeGroup::toChanged, context: this, slot: &RangeGroup::rangeChanged); |
19 | connect(sender: this, signal: &RangeGroup::automaticChanged, context: this, slot: &RangeGroup::rangeChanged); |
20 | connect(sender: this, signal: &RangeGroup::minimumChanged, context: this, slot: &RangeGroup::rangeChanged); |
21 | connect(sender: this, signal: &RangeGroup::incrementChanged, context: this, slot: &RangeGroup::rangeChanged); |
22 | } |
23 | |
24 | qreal RangeGroup::from() const |
25 | { |
26 | return m_from; |
27 | } |
28 | |
29 | void RangeGroup::setFrom(qreal from) |
30 | { |
31 | if (qFuzzyCompare(p1: m_from, p2: from)) { |
32 | return; |
33 | } |
34 | |
35 | m_from = from; |
36 | Q_EMIT fromChanged(); |
37 | } |
38 | |
39 | qreal RangeGroup::to() const |
40 | { |
41 | return m_to; |
42 | } |
43 | |
44 | void RangeGroup::setTo(qreal to) |
45 | { |
46 | if (qFuzzyCompare(p1: m_to, p2: to)) { |
47 | return; |
48 | } |
49 | |
50 | m_to = to; |
51 | Q_EMIT toChanged(); |
52 | } |
53 | |
54 | bool RangeGroup::automatic() const |
55 | { |
56 | return m_automatic; |
57 | } |
58 | |
59 | void RangeGroup::setAutomatic(bool automatic) |
60 | { |
61 | if (m_automatic == automatic) { |
62 | return; |
63 | } |
64 | |
65 | m_automatic = automatic; |
66 | Q_EMIT automaticChanged(); |
67 | } |
68 | |
69 | qreal RangeGroup::distance() const |
70 | { |
71 | return m_to - m_from; |
72 | } |
73 | |
74 | qreal RangeGroup::minimum() const |
75 | { |
76 | return m_minimum; |
77 | } |
78 | |
79 | void RangeGroup::setMinimum(qreal newMinimum) |
80 | { |
81 | if (newMinimum == m_minimum) { |
82 | return; |
83 | } |
84 | |
85 | m_minimum = newMinimum; |
86 | Q_EMIT minimumChanged(); |
87 | } |
88 | |
89 | qreal RangeGroup::increment() const |
90 | { |
91 | return m_increment; |
92 | } |
93 | |
94 | void RangeGroup::setIncrement(qreal newIncrement) |
95 | { |
96 | if (newIncrement == m_increment) { |
97 | return; |
98 | } |
99 | |
100 | m_increment = newIncrement; |
101 | Q_EMIT incrementChanged(); |
102 | } |
103 | |
104 | bool RangeGroup::isValid() const |
105 | { |
106 | return m_automatic || (m_to > m_from); |
107 | } |
108 | |
109 | RangeGroup::RangeResult RangeGroup::calculateRange(const QList<ChartDataSource *> &sources, |
110 | std::function<qreal(ChartDataSource *)> minimumCallback, |
111 | std::function<qreal(ChartDataSource *)> maximumCallback) |
112 | { |
113 | RangeResult result; |
114 | |
115 | auto min = std::numeric_limits<qreal>::max(); |
116 | auto max = std::numeric_limits<qreal>::min(); |
117 | |
118 | if (!m_automatic) { |
119 | min = m_from; |
120 | max = m_to; |
121 | } else { |
122 | std::for_each(first: sources.begin(), last: sources.end(), f: [&](ChartDataSource *source) { |
123 | min = std::min(a: min, b: minimumCallback(source)); |
124 | max = std::max(a: max, b: maximumCallback(source)); |
125 | }); |
126 | } |
127 | |
128 | max = std::max(a: max, b: m_minimum); |
129 | if (m_increment > 0.0) { |
130 | max = m_increment * std::ceil(x: max / m_increment); |
131 | } |
132 | |
133 | result.start = min; |
134 | result.end = max; |
135 | result.distance = max - min; |
136 | |
137 | return result; |
138 | } |
139 | |
140 | #include "moc_RangeGroup.cpp" |
141 | |