-
Notifications
You must be signed in to change notification settings - Fork 3
/
trend_line.cpp
173 lines (138 loc) · 4.21 KB
/
trend_line.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* \file trend_line.cpp
* \brief Individual trend line
* \copyright
* 2021 Andrew Buettner (ABi)
*
* \section LICENSE
*
* QModbusTool - A QT Based Modbus Client
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// c++ includes
#include <limits> // std::numeric_limits
#include <QStringBuilder> // operator%
#include <algorithm> // std::fill
// C includes
/* -none- */
// project includes
#include "trend_line.h" // local include
#include "exceptions.h" // AppException
#include "configure_trend_line.h" // ConfigureTrendLine
TrendLine::TrendLine(TrendWindow *parent,
const quint16 reg,
const quint8 node) :
QPushButton(parent->m_scroll_container),
m_reg_number{reg},
m_device_id{node},
m_num_points{parent->m_timestamps.size()},
m_signed_value{true},
m_mult{1.0},
m_offset{0.0},
m_pen_color(Qt::blue),
m_history(m_num_points),
m_last_value(),
m_next_index{0},
m_parent{parent}
{
connect(this, &TrendLine::clicked, this, &TrendLine::on_clicked);
setupUi();
}
void TrendLine::set_data(const quint16 value) noexcept
{
if (m_signed_value) {
const auto v = static_cast<qint16>(value);
m_last_value = qint32(v);
} else {
m_last_value = qint32(value);
}
}
void TrendLine::update()
{
if (!bool(m_last_value)) {
throw AppException(tr("Update called on invalid data"));
}
auto v = double(m_last_value.value()) * m_mult;
v += m_offset;
m_parent->update_min_max(v);
m_history[m_next_index] = v;
if (++m_next_index >= m_num_points) {
m_next_index = 0;
}
m_last_value.reset();
}
TrendLine::operator bool() const noexcept
{
return m_last_value.has_value();
}
const double& TrendLine::operator[] (int index) const
{
auto array_index = m_next_index + index;
if (array_index >= m_num_points) {
array_index -= m_num_points;
}
return m_history.at(array_index);
}
void TrendLine::configure(const double m, const double b, const bool set_signed)
{
m_mult = m;
m_offset = b;
m_signed_value = set_signed;
}
void TrendLine::set_color(const QColor &pen_color) noexcept
{
m_pen_color = pen_color;
setupUi();
}
TrendLine::operator quint32() const noexcept
{
return m_parent->get_key(m_reg_number, m_device_id);
}
TrendLine::operator QPen() const noexcept
{
return QPen(m_pen_color);
}
void TrendLine::on_clicked()
{
auto dlg = ConfigureTrendLine(this);
dlg.exec();
}
void TrendLine::setupUi()
{
setFixedWidth(m_parent->m_add_button->size().width());
setAutoDefault(false);
const auto reg_number = QString::number(m_reg_number);
const auto reg_padding = QString(5 - reg_number.size(), QChar('0'));
setText("------\n" % reg_padding %
reg_number % QChar('@') % QString::number(m_device_id));
setStyleSheet("QPushButton {color: " % m_pen_color.name() % ";}");
}
void TrendLine::save_configuration(QDomElement &node) const
{
node.setAttribute("register", QString::number(m_reg_number));
node.setAttribute("node", QString::number(m_device_id));
node.setAttribute("signed", QString::number(int(m_signed_value)));
node.setAttribute("m", QString::number(m_mult));
node.setAttribute("b", QString::number(m_offset));
node.setAttribute("color", m_pen_color.name());
}
void TrendLine::resize(int new_size)
{
m_next_index = 0;
m_num_points = new_size;
m_history.resize(new_size);
std::fill(m_history.begin(), m_history.end(), m_offset);
}