-
Notifications
You must be signed in to change notification settings - Fork 1
/
settingsdialog.cpp
170 lines (141 loc) · 6.83 KB
/
settingsdialog.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
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include <QIntValidator>
#include <QLineEdit>
#include <QSerialPortInfo>
static const char blankString[] = QT_TRANSLATE_NOOP("SettingsDialog", "N/A");
SettingsDialog::SettingsDialog(QWidget *parent) :
QDialog(parent),
m_ui(new Ui::SettingsDialog),
m_intValidator(new QIntValidator(0, 4000000, this))
{
m_ui->setupUi(this);
m_ui->baudRateBox->setInsertPolicy(QComboBox::NoInsert);
connect(m_ui->applyButton, &QPushButton::clicked,
this, &SettingsDialog::apply);
connect(m_ui->serialPortInfoListBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &SettingsDialog::showPortInfo);
connect(m_ui->baudRateBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &SettingsDialog::checkCustomBaudRatePolicy);
connect(m_ui->serialPortInfoListBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &SettingsDialog::checkCustomDevicePathPolicy);
fillPortsParameters();
fillPortsInfo();
updateSettings();
}
SettingsDialog::~SettingsDialog()
{
delete m_ui;
}
SettingsDialog::Settings SettingsDialog::settings() const
{
return m_currentSettings;
}
void SettingsDialog::showPortInfo(int idx)
{
if (idx == -1)
return;
const QStringList list = m_ui->serialPortInfoListBox->itemData(idx).toStringList();
m_ui->descriptionLabel->setText(tr("Description: %1").arg(list.count() > 1 ? list.at(1) : tr(blankString)));
m_ui->manufacturerLabel->setText(tr("Manufacturer: %1").arg(list.count() > 2 ? list.at(2) : tr(blankString)));
m_ui->serialNumberLabel->setText(tr("Serial number: %1").arg(list.count() > 3 ? list.at(3) : tr(blankString)));
m_ui->locationLabel->setText(tr("Location: %1").arg(list.count() > 4 ? list.at(4) : tr(blankString)));
m_ui->vidLabel->setText(tr("Vendor Identifier: %1").arg(list.count() > 5 ? list.at(5) : tr(blankString)));
m_ui->pidLabel->setText(tr("Product Identifier: %1").arg(list.count() > 6 ? list.at(6) : tr(blankString)));
}
void SettingsDialog::apply()
{
updateSettings();
hide();
}
void SettingsDialog::checkCustomBaudRatePolicy(int idx)
{
const bool isCustomBaudRate = !m_ui->baudRateBox->itemData(idx).isValid();
m_ui->baudRateBox->setEditable(isCustomBaudRate);
if (isCustomBaudRate) {
m_ui->baudRateBox->clearEditText();
QLineEdit *edit = m_ui->baudRateBox->lineEdit();
edit->setValidator(m_intValidator);
}
}
void SettingsDialog::checkCustomDevicePathPolicy(int idx)
{
const bool isCustomPath = !m_ui->serialPortInfoListBox->itemData(idx).isValid();
m_ui->serialPortInfoListBox->setEditable(isCustomPath);
if (isCustomPath)
m_ui->serialPortInfoListBox->clearEditText();
}
void SettingsDialog::fillPortsParameters()
{
m_ui->baudRateBox->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
m_ui->baudRateBox->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
m_ui->baudRateBox->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
m_ui->baudRateBox->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
m_ui->baudRateBox->addItem(tr("Custom"));
m_ui->baudRateBox->setCurrentIndex(3);
m_ui->dataBitsBox->addItem(QStringLiteral("5"), QSerialPort::Data5);
m_ui->dataBitsBox->addItem(QStringLiteral("6"), QSerialPort::Data6);
m_ui->dataBitsBox->addItem(QStringLiteral("7"), QSerialPort::Data7);
m_ui->dataBitsBox->addItem(QStringLiteral("8"), QSerialPort::Data8);
m_ui->dataBitsBox->setCurrentIndex(3);
m_ui->parityBox->addItem(tr("None"), QSerialPort::NoParity);
m_ui->parityBox->addItem(tr("Even"), QSerialPort::EvenParity);
m_ui->parityBox->addItem(tr("Odd"), QSerialPort::OddParity);
m_ui->parityBox->addItem(tr("Mark"), QSerialPort::MarkParity);
m_ui->parityBox->addItem(tr("Space"), QSerialPort::SpaceParity);
m_ui->stopBitsBox->addItem(QStringLiteral("1"), QSerialPort::OneStop);
#ifdef Q_OS_WIN
m_ui->stopBitsBox->addItem(tr("1.5"), QSerialPort::OneAndHalfStop);
#endif
m_ui->stopBitsBox->addItem(QStringLiteral("2"), QSerialPort::TwoStop);
m_ui->flowControlBox->addItem(tr("None"), QSerialPort::NoFlowControl);
m_ui->flowControlBox->addItem(tr("RTS/CTS"), QSerialPort::HardwareControl);
m_ui->flowControlBox->addItem(tr("XON/XOFF"), QSerialPort::SoftwareControl);
}
void SettingsDialog::fillPortsInfo()
{
m_ui->serialPortInfoListBox->clear();
QString description;
QString manufacturer;
QString serialNumber;
const auto infos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &info : infos) {
QStringList list;
description = info.description();
manufacturer = info.manufacturer();
serialNumber = info.serialNumber();
list << info.portName()
<< (!description.isEmpty() ? description : blankString)
<< (!manufacturer.isEmpty() ? manufacturer : blankString)
<< (!serialNumber.isEmpty() ? serialNumber : blankString)
<< info.systemLocation()
<< (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString)
<< (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString);
m_ui->serialPortInfoListBox->addItem(list.first(), list);
}
m_ui->serialPortInfoListBox->addItem(tr("Custom"));
}
void SettingsDialog::updateSettings()
{
m_currentSettings.name = m_ui->serialPortInfoListBox->currentText();
if (m_ui->baudRateBox->currentIndex() == 4) {
m_currentSettings.baudRate = m_ui->baudRateBox->currentText().toInt();
} else {
m_currentSettings.baudRate = static_cast<QSerialPort::BaudRate>(
m_ui->baudRateBox->itemData(m_ui->baudRateBox->currentIndex()).toInt());
}
m_currentSettings.stringBaudRate = QString::number(m_currentSettings.baudRate);
m_currentSettings.dataBits = static_cast<QSerialPort::DataBits>(
m_ui->dataBitsBox->itemData(m_ui->dataBitsBox->currentIndex()).toInt());
m_currentSettings.stringDataBits = m_ui->dataBitsBox->currentText();
m_currentSettings.parity = static_cast<QSerialPort::Parity>(
m_ui->parityBox->itemData(m_ui->parityBox->currentIndex()).toInt());
m_currentSettings.stringParity = m_ui->parityBox->currentText();
m_currentSettings.stopBits = static_cast<QSerialPort::StopBits>(
m_ui->stopBitsBox->itemData(m_ui->stopBitsBox->currentIndex()).toInt());
m_currentSettings.stringStopBits = m_ui->stopBitsBox->currentText();
m_currentSettings.flowControl = static_cast<QSerialPort::FlowControl>(
m_ui->flowControlBox->itemData(m_ui->flowControlBox->currentIndex()).toInt());
m_currentSettings.stringFlowControl = m_ui->flowControlBox->currentText();
m_currentSettings.localEchoEnabled = m_ui->localEchoCheckBox->isChecked();
}