KQuickCharts

RangeGroup.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 "RangeGroup.h"
9
10#include <cmath>
11
12#include <QList>
13
14RangeGroup::RangeGroup(QObject *parent)
15 : QObject(parent)
16{
17 connect(this, &RangeGroup::fromChanged, this, &RangeGroup::rangeChanged);
18 connect(this, &RangeGroup::toChanged, this, &RangeGroup::rangeChanged);
19 connect(this, &RangeGroup::automaticChanged, this, &RangeGroup::rangeChanged);
20 connect(this, &RangeGroup::minimumChanged, this, &RangeGroup::rangeChanged);
21 connect(this, &RangeGroup::incrementChanged, this, &RangeGroup::rangeChanged);
22}
23
24qreal RangeGroup::from() const
25{
26 return m_from;
27}
28
29void RangeGroup::setFrom(qreal from)
30{
31 if (qFuzzyCompare(m_from, from)) {
32 return;
33 }
34
35 m_from = from;
36 Q_EMIT fromChanged();
37}
38
39qreal RangeGroup::to() const
40{
41 return m_to;
42}
43
44void RangeGroup::setTo(qreal to)
45{
46 if (qFuzzyCompare(m_to, to)) {
47 return;
48 }
49
50 m_to = to;
51 Q_EMIT toChanged();
52}
53
54bool RangeGroup::automatic() const
55{
56 return m_automatic;
57}
58
59void RangeGroup::setAutomatic(bool automatic)
60{
61 if (m_automatic == automatic) {
62 return;
63 }
64
65 m_automatic = automatic;
66 Q_EMIT automaticChanged();
67}
68
69qreal RangeGroup::distance() const
70{
71 return m_to - m_from;
72}
73
74qreal RangeGroup::minimum() const
75{
76 return m_minimum;
77}
78
79void RangeGroup::setMinimum(qreal newMinimum)
80{
81 if (newMinimum == m_minimum) {
82 return;
83 }
84
85 m_minimum = newMinimum;
86 Q_EMIT minimumChanged();
87}
88
89qreal RangeGroup::increment() const
90{
91 return m_increment;
92}
93
94void RangeGroup::setIncrement(qreal newIncrement)
95{
96 if (newIncrement == m_increment) {
97 return;
98 }
99
100 m_increment = newIncrement;
101 Q_EMIT incrementChanged();
102}
103
104bool RangeGroup::isValid() const
105{
106 return m_automatic || (m_to > m_from);
107}
108
109RangeGroup::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(sources.begin(), sources.end(), [&](ChartDataSource *source) {
123 min = std::min(min, minimumCallback(source));
124 max = std::max(max, maximumCallback(source));
125 });
126 }
127
128 max = std::max(max, m_minimum);
129 if (m_increment > 0.0) {
130 max = m_increment * std::ceil(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"
Abstract base class for data sources.
qreal from
The start of this range.
Definition RangeGroup.h:44
qreal increment
The amount with which the range increases.
Definition RangeGroup.h:90
bool automatic
Whether to determine the range based on values of a chart.
Definition RangeGroup.h:63
qreal to
The end of this range.
Definition RangeGroup.h:53
qreal distance
The distance between from and to.
Definition RangeGroup.h:70
qreal minimum
The minimum size of the range.
Definition RangeGroup.h:79
iterator begin()
iterator end()
Q_EMITQ_EMIT
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 Mon Nov 18 2024 12:08:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.