-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdualbutton.cpp
110 lines (92 loc) · 3.02 KB
/
dualbutton.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
#include <stubserver/VisualizationClient.h>
#include "dualbutton.h"
#include "ui_dualbutton.h"
DualButton::DualButton(QWidget *parent, const char *title)
: SensorInterface(parent)
, led_l(NULL)
, led_r(NULL)
, ui(new Ui::DualButton)
{
ui->setupUi(this);
setCheckBox(ui->check);
ui->groupBox->setTitle(title);
connectTooltipTo(ui->groupBox);
led_l = new StatusLed(ui->statusLED_l);
led_r = new StatusLed(ui->statusLED_r);
// Connect the button and the checkbox
connect(this, &DualButton::valueChanged, this, &DualButton::updateUi);
connect(ui->button_l, &QPushButton::pressed, this, [this]{ buttonDown(0); });
connect(ui->button_l, &QPushButton::released, this, [this]{ buttonUp(0); });
connect(ui->button_r, &QPushButton::pressed, this, [this]{ buttonDown(1); });
connect(ui->button_r, &QPushButton::released, this, [this]{ buttonUp(1); });
connect(checkBox, &QCheckBox::stateChanged, this, &SensorInterface::checkBoxClicked);
}
DualButton::~DualButton()
{
setCheckBox(NULL);
delete led_l;
delete led_r;
delete ui;
}
/**
* Receives the notification when the sensor value changes.
*/
void DualButton::notify(const stubserver::VisibleDeviceState &sensor)
{
if (sensor.isDisconnected())
return;
try {
const stubserver::DualButtonState& state = dynamic_cast<const stubserver::DualButtonState&>(sensor);
// change sensor value
int newValue = state.getButtonStates();
led_l->setLedOn(state.isLedOn_l(), false);
led_r->setLedOn(state.isLedOn_r(), false);
emit valueChanged(newValue);
}
catch (const std::exception &e) {
qCritical("Caught exception: %s", e.what());
}
}
/**
* Change the button state and indirectly also the LED state.
* 0 for LEFT, 1 for RIGHT button
*/
void DualButton::buttonDown(int no)
{
if (no == 0)
currentValue |= 1;
else
currentValue |= 2;
activateManualControl();
emit valueChanged(currentValue);
}
void DualButton::buttonUp(int no)
{
if (no == 0)
currentValue ^= 1;
else
currentValue ^= 2;
emit valueChanged(currentValue);
}
/**
* @brief DualButton::updateUi signal receiver method when a button state changes
* @param newValue
*/
void DualButton::updateUi(int newValue)
{
currentValue = newValue;
if ((currentValue & 1) != 0)
ui->button_l->setStyleSheet("QPushButton {background-color: #A3C1DA; color: red;}");
else
ui->button_l->setStyleSheet("QPushButton {background-color: #909090; color: black;}");
if ((currentValue & 2) != 0)
ui->button_r->setStyleSheet("QPushButton {background-color: #A3C1DA; color: red;}");
else
ui->button_r->setStyleSheet("QPushButton {background-color: #909090; color: black;}");
// update LED status
led_l->setLedOn(led_l->isLedOn());
led_r->setLedOn(led_r->isLedOn());
// TODO: this method was called due to manual change?
//if (!manualControl)
// sensor->setValue(newValue);
}