Debian Codegraph @

src/core/components/sensors/graphitem.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "graphitem.h"
5
6 #include <QTimer>
7 #include <QtCharts/QValueAxis>
8 #include <algorithm>
9 #include <limits>
10
11 unsigned short const GraphItem::PointsCount = 120;
12
13 GraphItem::GraphItem(std::string_view name, std::string_view unit)
14 : QQuickItem()
15 , name_(name.data())
16 , unit_(unit.data())
17 , yMin_(std::numeric_limits<qreal>::max())
18 , yMax_(std::numeric_limits<qreal>::min())
19 {
20 setObjectName(name_);
21 points_.reserve(GraphItem::PointsCount);
22
23 connect(this, &GraphItem::visibleChanged, this, &GraphItem::refreshSeriePoints);
24 }
25
26 QString const &GraphItem::name() const
27 {
28 return name_;
29 }
30
31 QString const &GraphItem::unit() const
32 {
33 return unit_;
34 }
35
36 QString GraphItem::color() const
37 {
38 return QString::fromStdString(color_);
39 }
40
41 qreal GraphItem::value() const
42 {
43 return value_;
44 }
45
46 void GraphItem::initialRange(qreal min, qreal max)
47 {
48 if (max > min) {
49 yMin_ = min;
50 yMax_ = max;
51 }
52 }
53
54 void GraphItem::updateGraph(qreal value)
55 {
56 if (series_ != nullptr && !ignored()) {
57
58 if (points_.size() == PointsCount)
59 points_.removeFirst();
60
61 qreal newX;
62 auto lastX = points_.empty() ? PointsCount : points_.last().x();
63 if (static_cast<unsigned short>(lastX) ==
64 std::numeric_limits<unsigned short>::max()) {
65 // 65535 * 0.5 / 60 / 60 ~> 9h 10min @ 0.5s refresh rate between restarts
66
67 restartXPoints();
68 newX = PointsCount;
69 }
70 else {
71 newX = lastX + 1;
72 }
73
74 points_.append(QPointF(newX, value));
75 QTimer::singleShot(0, this, &GraphItem::refreshSeriePoints);
76
77 // update axes
78 xAxis_->setRange(newX - PointsCount + 1, newX);
79 updateYAxis(value);
80
81 value_ = value;
82 emit valueChanged(value);
83 }
84 }
85
86 std::optional<std::reference_wrapper<Importable::Importer>>
87 GraphItem::provideImporter(Item const &)
88 {
89 return {};
90 }
91
92 std::optional<std::reference_wrapper<Exportable::Exporter>>
93 GraphItem::provideExporter(Item const &)
94 {
95 return {};
96 }
97
98 bool GraphItem::provideActive() const
99 {
100 return active_;
101 }
102
103 std::string const &GraphItem::provideColor() const
104 {
105 return color_;
106 }
107
108 void GraphItem::takeActive(bool active)
109 {
110 if (active_ != active) {
111 active_ = active;
112
113 if (series_ != nullptr)
114 series_->setVisible(active);
115
116 emit activeChanged(active);
117 }
118 }
119
120 void GraphItem::takeColor(std::string const &color)
121 {
122 if (!color.empty() && color_ != color) {
123 color_ = color;
124
125 if (series_ != nullptr)
126 series_->setColor(color_.c_str());
127
128 emit colorChanged(QString::fromStdString(color_));
129 }
130 }
131
132 void GraphItem::configure(QAbstractSeries *series, QAbstractAxis *xAxis,
133 QAbstractAxis *yAxis)
134 {
135 series_ = dynamic_cast<QLineSeries *>(series);
136 series_->setColor(color_.c_str());
137 series_->setVisible(active_);
138
139 xAxis_ = xAxis;
140 yAxis_ = yAxis;
141
142 xAxis_->setRange(1, PointsCount);
143 yAxis_->setRange(yMin_, yMax_);
144 }
145
146 void GraphItem::updateYAxisRange(qreal min, qreal max)
147 {
148 yMin_ = std::min(yMin_, min);
149 yMax_ = std::max(yMax_, (max > yMin_) ? max : yMin_ + 1);
150
151 yAxis_->setRange(yMin_, yMax_);
152 }
153
154 void GraphItem::refreshSeriePoints()
155 {
156 if (isVisible() && series_ != nullptr)
157 series_->replace(points_);
158 }
159
160 bool GraphItem::active() const
161 {
162 return provideActive();
163 }
164
165 void GraphItem::active(bool active)
166 {
167 if (active_ != active) {
168 active_ = active;
169
170 if (series_ != nullptr)
171 series_->setVisible(active);
172
173 emit settingsChanged();
174 }
175 }
176
177 bool GraphItem::ignored() const
178 {
179 return ignored_;
180 }
181
182 void GraphItem::ignored(bool ignored)
183 {
184 if (ignored_ != ignored) {
185 ignored_ = ignored;
186
187 if (series_ != nullptr)
188 series_->setVisible(active_ && !ignored_);
189
190 emit ignoredChanged(ignored);
191 }
192 }
193
194 void GraphItem::updateYAxis(qreal value)
195 {
196 if (value < yMin_ || value > yMax_) {
197 updateYAxisRange(value, value);
198 emit yAxisRangeChanged(yMin_, yMax_);
199 }
200 }
201
202 void GraphItem::restartXPoints()
203 {
204 int pointX = 1;
205 for (auto &point : points_)
206 point.rx() = pointX++;
207 }