1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
4 ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
5 ** Contact: http://www.qt.io/licensing/
6 **
7 ** This file is part of the QtSerialPort module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL21$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see http://www.qt.io/terms-conditions. For further
16 ** information use the contact form at http://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 2.1 or version 3 as published by the Free
21 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
22 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
23 ** following information to ensure the GNU Lesser General Public License
24 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
25 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 **
27 ** As a special exception, The Qt Company gives you certain additional
28 ** rights. These rights are described in The Qt Company LGPL Exception
29 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 **
31 ** $QT_END_LICENSE$
32 **
33 ****************************************************************************/
34
35 #include "settingsdialog.h"
36 #include "ui_settingsdialog.h"
37
38 #include <QtSerialPort/QSerialPortInfo>
39 #include <QIntValidator>
40 #include <QLineEdit>
41
42 QT_USE_NAMESPACE
43
44 static const char blankString[] = QT_TRANSLATE_NOOP("SettingsDialog", "N/A");
45
SettingsDialog(QWidget * parent)46 SettingsDialog::SettingsDialog(QWidget *parent) :
47 QDialog(parent),
48 ui(new Ui::SettingsDialog)
49 {
50 ui->setupUi(this);
51
52 intValidator = new QIntValidator(0, 4000000, this);
53
54 ui->baudRateBox->setInsertPolicy(QComboBox::NoInsert);
55
56 connect(ui->applyButton, &QPushButton::clicked,
57 this, &SettingsDialog::apply);
58 connect(ui->serialPortInfoListBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
59 this, &SettingsDialog::showPortInfo);
60 connect(ui->baudRateBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
61 this, &SettingsDialog::checkCustomBaudRatePolicy);
62 connect(ui->serialPortInfoListBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
63 this, &SettingsDialog::checkCustomDevicePathPolicy);
64
65 fillPortsParameters();
66 fillPortsInfo();
67
68 updateSettings();
69 }
70
~SettingsDialog()71 SettingsDialog::~SettingsDialog()
72 {
73 delete ui;
74 }
75
settings() const76 SettingsDialog::Settings SettingsDialog::settings() const
77 {
78 return currentSettings;
79 }
80
showPortInfo(int idx)81 void SettingsDialog::showPortInfo(int idx)
82 {
83 if (idx == -1)
84 return;
85
86 QStringList list = ui->serialPortInfoListBox->itemData(idx).toStringList();
87 ui->descriptionLabel->setText(tr("Description: %1").arg(list.count() > 1 ? list.at(1) : tr(blankString)));
88 ui->manufacturerLabel->setText(tr("Manufacturer: %1").arg(list.count() > 2 ? list.at(2) : tr(blankString)));
89 ui->serialNumberLabel->setText(tr("Serial number: %1").arg(list.count() > 3 ? list.at(3) : tr(blankString)));
90 ui->locationLabel->setText(tr("Location: %1").arg(list.count() > 4 ? list.at(4) : tr(blankString)));
91 ui->vidLabel->setText(tr("Vendor Identifier: %1").arg(list.count() > 5 ? list.at(5) : tr(blankString)));
92 ui->pidLabel->setText(tr("Product Identifier: %1").arg(list.count() > 6 ? list.at(6) : tr(blankString)));
93 }
94
apply()95 void SettingsDialog::apply()
96 {
97 updateSettings();
98 hide();
99 }
100
checkCustomBaudRatePolicy(int idx)101 void SettingsDialog::checkCustomBaudRatePolicy(int idx)
102 {
103 bool isCustomBaudRate = !ui->baudRateBox->itemData(idx).isValid();
104 ui->baudRateBox->setEditable(isCustomBaudRate);
105 if (isCustomBaudRate) {
106 ui->baudRateBox->clearEditText();
107 QLineEdit *edit = ui->baudRateBox->lineEdit();
108 edit->setValidator(intValidator);
109 }
110 }
111
checkCustomDevicePathPolicy(int idx)112 void SettingsDialog::checkCustomDevicePathPolicy(int idx)
113 {
114 bool isCustomPath = !ui->serialPortInfoListBox->itemData(idx).isValid();
115 ui->serialPortInfoListBox->setEditable(isCustomPath);
116 if (isCustomPath)
117 ui->serialPortInfoListBox->clearEditText();
118 }
119
fillPortsParameters()120 void SettingsDialog::fillPortsParameters()
121 {
122 ui->baudRateBox->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
123 ui->baudRateBox->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
124 ui->baudRateBox->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
125 ui->baudRateBox->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
126 ui->baudRateBox->addItem(tr("Custom"));
127
128 ui->dataBitsBox->addItem(QStringLiteral("5"), QSerialPort::Data5);
129 ui->dataBitsBox->addItem(QStringLiteral("6"), QSerialPort::Data6);
130 ui->dataBitsBox->addItem(QStringLiteral("7"), QSerialPort::Data7);
131 ui->dataBitsBox->addItem(QStringLiteral("8"), QSerialPort::Data8);
132 ui->dataBitsBox->setCurrentIndex(3);
133
134 ui->parityBox->addItem(tr("None"), QSerialPort::NoParity);
135 ui->parityBox->addItem(tr("Even"), QSerialPort::EvenParity);
136 ui->parityBox->addItem(tr("Odd"), QSerialPort::OddParity);
137 ui->parityBox->addItem(tr("Mark"), QSerialPort::MarkParity);
138 ui->parityBox->addItem(tr("Space"), QSerialPort::SpaceParity);
139
140 ui->stopBitsBox->addItem(QStringLiteral("1"), QSerialPort::OneStop);
141 #ifdef Q_OS_WIN
142 ui->stopBitsBox->addItem(tr("1.5"), QSerialPort::OneAndHalfStop);
143 #endif
144 ui->stopBitsBox->addItem(QStringLiteral("2"), QSerialPort::TwoStop);
145
146 ui->flowControlBox->addItem(tr("None"), QSerialPort::NoFlowControl);
147 ui->flowControlBox->addItem(tr("RTS/CTS"), QSerialPort::HardwareControl);
148 ui->flowControlBox->addItem(tr("XON/XOFF"), QSerialPort::SoftwareControl);
149 }
150
fillPortsInfo()151 void SettingsDialog::fillPortsInfo()
152 {
153 ui->serialPortInfoListBox->clear();
154 QString description;
155 QString manufacturer;
156 QString serialNumber;
157 foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
158 QStringList list;
159 description = info.description();
160 manufacturer = info.manufacturer();
161 serialNumber = info.serialNumber();
162 list << info.portName()
163 << (!description.isEmpty() ? description : blankString)
164 << (!manufacturer.isEmpty() ? manufacturer : blankString)
165 << (!serialNumber.isEmpty() ? serialNumber : blankString)
166 << info.systemLocation()
167 << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString)
168 << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString);
169
170 ui->serialPortInfoListBox->addItem(list.first(), list);
171 }
172
173 ui->serialPortInfoListBox->addItem(tr("Custom"));
174 }
175
updateSettings()176 void SettingsDialog::updateSettings()
177 {
178 currentSettings.name = ui->serialPortInfoListBox->currentText();
179
180 if (ui->baudRateBox->currentIndex() == 4) {
181 currentSettings.baudRate = ui->baudRateBox->currentText().toInt();
182 } else {
183 currentSettings.baudRate = static_cast<QSerialPort::BaudRate>(
184 ui->baudRateBox->itemData(ui->baudRateBox->currentIndex()).toInt());
185 }
186 currentSettings.stringBaudRate = QString::number(currentSettings.baudRate);
187
188 currentSettings.dataBits = static_cast<QSerialPort::DataBits>(
189 ui->dataBitsBox->itemData(ui->dataBitsBox->currentIndex()).toInt());
190 currentSettings.stringDataBits = ui->dataBitsBox->currentText();
191
192 currentSettings.parity = static_cast<QSerialPort::Parity>(
193 ui->parityBox->itemData(ui->parityBox->currentIndex()).toInt());
194 currentSettings.stringParity = ui->parityBox->currentText();
195
196 currentSettings.stopBits = static_cast<QSerialPort::StopBits>(
197 ui->stopBitsBox->itemData(ui->stopBitsBox->currentIndex()).toInt());
198 currentSettings.stringStopBits = ui->stopBitsBox->currentText();
199
200 currentSettings.flowControl = static_cast<QSerialPort::FlowControl>(
201 ui->flowControlBox->itemData(ui->flowControlBox->currentIndex()).toInt());
202 currentSettings.stringFlowControl = ui->flowControlBox->currentText();
203
204 currentSettings.localEchoEnabled = ui->localEchoCheckBox->isChecked();
205 }
206