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