-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionPanel.cpp
More file actions
215 lines (174 loc) · 6.33 KB
/
Copy pathConnectionPanel.cpp
File metadata and controls
215 lines (174 loc) · 6.33 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <QHBoxLayout>
#include <QLabel>
#include <QtSerialPort/QSerialPortInfo>
#include "ConnectionPanel.hpp"
#include "NetworkConnection.hpp"
#include "SerialPortConnection.hpp"
#include "EmulatorConnection.hpp"
ConnectionPanel::ConnectionPanel() {
typeSelect = new QComboBox();
typeSelect->addItem("Sieć", Type::Network);
typeSelect->addItem("Port szeregowy", Type::SerialPort);
typeSelect->addItem("Emulator", Type::Emulator);
typeSelect->setCurrentIndex(0);
connectBtn = new QPushButton();
contentLayout = new QHBoxLayout();
mainLayout = new QHBoxLayout();
mainLayout->setAlignment(Qt::AlignLeft);
mainLayout->addWidget(typeSelect);
mainLayout->addLayout(contentLayout);
mainLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding));
setLayout(mainLayout);
setupParamsLayout(Type::Network);
this->setConnectionState(Connection::Disconnected);
connect(connectBtn, SIGNAL(clicked()),
this, SLOT(handleConnectBtn()));
connect(typeSelect, SIGNAL(currentIndexChanged(int)),
this, SLOT(handleTypeChanged(int)));
}
void ConnectionPanel::handleTypeChanged(int index) {
QVariant data = typeSelect->itemData(index);
Type type = (Type) data.toInt();
this->currentType = type;
setupParamsLayout(type);
}
void ConnectionPanel::setupParamsLayout(Type type) {
clearContentLayout();
delete contentLayout;
contentLayout = new QHBoxLayout();
mainLayout->insertLayout(1, contentLayout);
switch (type) {
case Network:
setupNetworkLayout(contentLayout);
break;
case SerialPort:
setupSerialLayout(contentLayout);
break;
case Emulator:
setupEmulatorLayout(contentLayout);
break;
default: break; // Nic, pusty layout
}
}
void ConnectionPanel::clearContentLayout() {
QLayoutItem *item;
while ((item = contentLayout->takeAt(0)) != 0) {
if (item->widget() == connectBtn) {
contentLayout->removeWidget(connectBtn);
connectBtn->setParent(nullptr);
continue; // Usuń tylko z layoutu, samą instancję należy pozostawić
}
delete item->layout();
delete item->widget();
delete item;
}
}
void ConnectionPanel::setupNetworkLayout(QHBoxLayout *layout) {
network.hostLabel = new QLabel("Host:");
network.portLabel = new QLabel("Port:");
network.hostEdit = new QLineEdit();
network.hostEdit->setText(DEFAULT_NETWORK_HOST);
network.portEdit = new QSpinBox();
network.portEdit->setRange(1, 65535);
network.portEdit->setValue(DEFAULT_NETWORK_PORT);
layout->addWidget(network.hostLabel);
layout->addWidget(network.hostEdit);
layout->addWidget(network.portLabel);
layout->addWidget(network.portEdit);
layout->addWidget(connectBtn);
}
void ConnectionPanel::setupSerialLayout(QHBoxLayout *layout) {
serial.portNameLabel = new QLabel("Port:");
serial.baudRateLabel = new QLabel("Prędkość:");
serial.portNameSelect = new QComboBox();
for (auto portInfo : QSerialPortInfo::availablePorts()) {
QString text = portInfo.systemLocation() + " - " + portInfo.description();
serial.portNameSelect->addItem(text, portInfo.systemLocation());
if (portInfo.portName() == DEFAULT_SERIAL_PORT_NAME) {
int index= serial.portNameSelect->count();
serial.portNameSelect->setCurrentIndex(index);
}
}
if (serial.portNameSelect->currentIndex() == -1)
serial.portNameSelect->setCurrentIndex(0);
serial.baudRateSelect = new QComboBox();
for (auto baudRate : QSerialPortInfo::standardBaudRates()) {
QString text = QString::number(baudRate);
qint32 data = baudRate;
serial.baudRateSelect->addItem(text, data);
if (baudRate == DEFAULT_SERIAL_BAUD_RATE) {
int index= serial.baudRateSelect->count() - 1;
serial.baudRateSelect->setCurrentIndex(index);
}
}
layout->addWidget(serial.portNameLabel);
layout->addWidget(serial.portNameSelect);
layout->addWidget(serial.baudRateLabel);
layout->addWidget(serial.baudRateSelect);
layout->addWidget(connectBtn);
}
void ConnectionPanel::setupEmulatorLayout(QHBoxLayout *layout) {
layout->addWidget(new QLabel("tryb emulatora"));
layout->addWidget(connectBtn);
}
void ConnectionPanel::handleConnectBtn() {
if (connectionState == Connection::Disconnected)
sendDoConnect();
else if (connectionState == Connection::Connected)
emit doDisconnect();
else
; // Nic
}
void ConnectionPanel::sendDoConnect() {
switch (currentType) {
case Network:
sendDoConnectNetwork();
break;
case SerialPort:
sendDoConnectSerialPort();
break;
case Emulator:
sendDoConnectEmulator();
break;
}
}
void ConnectionPanel::sendDoConnectNetwork() {
QString host = network.hostEdit->text();
quint16 port = (quint16) network.portEdit->value();
NetworkConnection *connection = new NetworkConnection();
connection->setServerHost(host);
connection->setServerPort(port);
emit connectionChanged(connection);
emit doConnect();
}
void ConnectionPanel::sendDoConnectSerialPort() {
QString portName = serial.portNameSelect->currentData().toString();
qint32 baudRate = serial.baudRateSelect->currentData().toInt();
SerialPortConnection *connection = new SerialPortConnection();
connection->setPortName(portName);
connection->setBaudRate(baudRate);
emit connectionChanged(connection);
emit doConnect();
}
void ConnectionPanel::sendDoConnectEmulator() {
EmulatorConnection *connection = new EmulatorConnection();
emit connectionChanged(connection);
emit doConnect();
}
void ConnectionPanel::setConnectionState(Connection::State state) {
connectionState = state;
switch (state) {
case Connection::Connected:
connectBtn->setText("Rozłącz");
connectBtn->setEnabled(true);
break;
case Connection::Disconnected:
connectBtn->setText("Połącz");
connectBtn->setEnabled(true);
break;
case Connection::Connecting:
connectBtn->setText("Połącz");
connectBtn->setEnabled(false);
break;
}
}