-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelay.cpp
150 lines (124 loc) · 3.68 KB
/
relay.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
#include <QGridLayout>
#include <QLabel>
#include <QDebug>
#include <string>
#include "relay.h"
#include "ui_relay.h"
/**
* Load a pixmap from ressources.
*/
static QPixmap loadPixmap(const char *p)
{
std::string path(":/rc_all/images/");
std::string full(path + p);
QPixmap pixmap(full.c_str());
return pixmap.scaledToWidth(180);
}
Relay::Relay(QWidget *parent, const char *title, bool dual)
: QWidget(parent)
, ui(new Ui::Relay)
, numRelays(0)
{
memset(statusLED, 0, sizeof(statusLED));
ui->setupUi(this);
ui->scrollArea->setWidgetResizable(true);
ui->groupBox->setTitle(title);
connectTooltipTo(ui->groupBox);
if (dual) {
imgOn = new QPixmap(loadPixmap("dual_on.png"));
imgOff = new QPixmap(loadPixmap("dual_off.png"));
}
else {
imgOn = new QPixmap(loadPixmap("single_on.png"));
imgOff = new QPixmap(loadPixmap("single_off.png"));
}
connect(this, &Relay::valueChanged, this, &Relay::updateUi);
}
Relay::~Relay()
{
delete ui;
delete imgOn;
delete imgOff;
for (int i = 0; i < numRelays; ++i)
delete statusLED[i];
}
/**
* @brief Sensor::updateUi signal receiver method when a value changes
* @param newValue
*/
void Relay::updateUi(const stubserver::RelayState *rs)
{
// e.g. the RemoteSwitch has dynamic switches
int count = rs->getNumSwitches();
if (count == 0)
return;
if (numRelays < count)
{
// first init or dynamically add a switch (RemoteSwitch code)
QGridLayout *layout;
if (numRelays == 0)
{
layout = new QGridLayout();
layout->setHorizontalSpacing(20);
}
else
layout = (QGridLayout*) ui->scrollAreaWidgetContents->layout();
int height = 0;
for (int i = numRelays; i < count; ++i)
{
QPixmap *img = (rs->isOn(i) ? imgOn : imgOff);
int minHeight = img->height() + 10;
QLabel *lbl = new QLabel();
//lbl->setPixmap(*img);
lbl->setMinimumHeight(minHeight);
layout->setRowMinimumHeight(i, minHeight);
height += minHeight;
layout->addWidget(lbl, i, 0);
imgWidgets[i] = lbl;
lbl = new QLabel();
lbl->setText(rs->getLabel(i).c_str());
lbl->setMinimumHeight(img->height() + 10);
layout->addWidget(lbl, i, 1);
if (rs->getLedConfig(i) != stubserver::StatusLedConfig::LED_HIDDEN) {
QWidget *led = new QWidget();
led->setMinimumWidth(12);
led->setMinimumHeight(12);
led->setMaximumWidth(12);
led->setMaximumHeight(12);
statusLED[i] = new StatusLed(led);
layout->addWidget(led, i, 2);
}
}
if (numRelays == 0)
{
ui->scrollAreaWidgetContents->setMinimumHeight(height);
ui->scrollAreaWidgetContents->setLayout(layout);
}
numRelays = count;
}
// now show the relay state
for (int i = 0; i < count; ++i)
{
QPixmap *img = rs->isOn(i) ? imgOn : imgOff;
QLabel *lbl = imgWidgets[i];
lbl->setPixmap(*img);
if (statusLED[i])
statusLED[i]->setLedOn(rs->isLedOn(i));
}
}
/**
* @brief Sensor::notify
* @param sensor
*/
void Relay::notify(const stubserver::VisibleDeviceState &state)
{
if (state.isDisconnected())
return;
try {
const stubserver::RelayState& rs = dynamic_cast<const stubserver::RelayState&>(state);
emit valueChanged(&rs);
}
catch (const std::exception &e) {
qCritical("Caught exception: %s", e.what());
}
}