-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslavewidget.cpp
More file actions
164 lines (135 loc) · 4.04 KB
/
slavewidget.cpp
File metadata and controls
164 lines (135 loc) · 4.04 KB
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
#include "slavewidget.h"
#include "ui_slavewidget.h"
#include <QIcon>
#include <QLayout>
#include <QSize>
#include <QDebug>
const QString SlaveWidget::s_iconPaths[2] = {
QStringLiteral(":/images/ledLow.png"),
QStringLiteral(":/images/ledHigh.png")
};
static const QSize s_iconSize(20, 20);
SlaveWidget::SlaveWidget(int idx, int oBits, int iBits, QWidget* parent)
: QWidget(parent)
, ui(new Ui::SlaveWidget)
, m_SlaveIdx(idx)
, m_oBits(oBits)
, m_iBits(iBits)
, m_outputGroup(new QButtonGroup(this))
, m_inputGroup(new QButtonGroup(this))
{
ui->setupUi(this);
ui->labelSlave->setText(QString("Slave %1").arg(m_SlaveIdx));
// non-exclusive
m_outputGroup->setExclusive(false);
m_inputGroup->setExclusive(false);
// connect signals
connect(m_outputGroup, &QButtonGroup::idToggled,
this, &SlaveWidget::onOutputToggled);
refresh();
}
SlaveWidget::~SlaveWidget()
{
delete ui;
}
// refresh all buttons
void SlaveWidget::refresh()
{
refreshOutput();
refreshInput();
}
// refresh output buttons
void SlaveWidget::refreshOutput()
{
QHBoxLayout* layoutOutput = qobject_cast<QHBoxLayout*>(ui->widgetOutput->layout());
if (layoutOutput == nullptr) {
layoutOutput = new QHBoxLayout(ui->widgetOutput);
ui->widgetOutput->setLayout(layoutOutput);
layoutOutput->setSpacing(0);
}
// clear existing btns
const auto buttons = m_outputGroup->buttons();
for (QAbstractButton* b : buttons) {
if (layoutOutput) {
layoutOutput->removeWidget(b);
}
b->deleteLater();
m_outputGroup->removeButton(b);
}
// create btns
for (int i = 0; i < m_oBits; ++i) {
auto* btn = new QToolButton(this);
btn->setAutoRaise(false);
btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
btn->setIcon(QIcon(s_iconPaths[0]));
btn->setIconSize(s_iconSize);
btn->setText(QString::number(i));
btn->setCheckable(true);
m_outputGroup->addButton(btn, i);
if (layoutOutput) {
layoutOutput->addWidget(btn);
}
}
}
// refresh input buttons
void SlaveWidget::refreshInput()
{
QHBoxLayout* layoutInput = qobject_cast<QHBoxLayout*>(ui->widgetInput->layout());
if (layoutInput == nullptr) {
layoutInput = new QHBoxLayout(ui->widgetInput);
ui->widgetInput->setLayout(layoutInput);
layoutInput->setSpacing(0);
}
// clear existing btns
const auto buttons = m_inputGroup->buttons();
for (QAbstractButton* b : buttons) {
if (layoutInput) {
layoutInput->removeWidget(b);
}
b->deleteLater();
m_inputGroup->removeButton(b);
}
// create btns
for (int i = 0; i < m_iBits; ++i) {
auto* btn = new QToolButton(this);
btn->setAutoRaise(false);
btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
btn->setIcon(QIcon(s_iconPaths[0]));
btn->setIconSize(s_iconSize);
btn->setText(QString::number(i));
m_inputGroup->addButton(btn, i);
if (layoutInput) {
layoutInput->addWidget(btn);
}
}
}
// refresh input bits for given byte
void SlaveWidget::refreshInput(int byteOffset, uint8_t value)
{
const int byteBits = 8;
for (int i = 0; i < byteBits; ++i) {
int idx = byteBits * byteOffset + i;
if (idx < 0 || idx >= m_iBits) {
continue;
}
// get bit value
const int bit = (value >> i) & 0x1;
// set icon
if (QAbstractButton* ab = m_inputGroup->button(idx)) {
if (auto* btn = qobject_cast<QToolButton*>(ab)) {
btn->setIcon(QIcon(s_iconPaths[bit]));
}
}
}
}
// toggle output bit
void SlaveWidget::onOutputToggled(int id, bool checked)
{
// set icon
if (auto* ab = m_outputGroup->button(id)) {
if (auto* btn = qobject_cast<QToolButton*>(ab)) {
btn->setIcon(QIcon(s_iconPaths[checked ? 1 : 0]));
}
}
emit outputChanged(m_SlaveIdx, id, checked);
}