Terminal Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtSerialPort module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#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-&
gt;setupUi(this
);
m_ui-&
gt;baudRateBox-&
gt;setInsertPolicy(QComboBox::
NoInsert);
connect(m_ui-&
gt;applyButton, &
amp;QPushButton::
clicked,
this
, &
amp;SettingsDialog::
apply);
connect(m_ui-&
gt;serialPortInfoListBox, QOverload&
lt;int
&
gt;::
of(&
amp;QComboBox::
currentIndexChanged),
this
, &
amp;SettingsDialog::
showPortInfo);
connect(m_ui-&
gt;baudRateBox, QOverload&
lt;int
&
gt;::
of(&
amp;QComboBox::
currentIndexChanged),
this
, &
amp;SettingsDialog::
checkCustomBaudRatePolicy);
connect(m_ui-&
gt;serialPortInfoListBox, QOverload&
lt;int
&
gt;::
of(&
amp;QComboBox::
currentIndexChanged),
this
, &
amp;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-&
gt;serialPortInfoListBox-&
gt;itemData(idx).toStringList();
m_ui-&
gt;descriptionLabel-&
gt;setText(tr("Description: %1"
).arg(list.count() &
gt; 1
? list.at(1
) : tr(blankString)));
m_ui-&
gt;manufacturerLabel-&
gt;setText(tr("Manufacturer: %1"
).arg(list.count() &
gt; 2
? list.at(2
) : tr(blankString)));
m_ui-&
gt;serialNumberLabel-&
gt;setText(tr("Serial number: %1"
).arg(list.count() &
gt; 3
? list.at(3
) : tr(blankString)));
m_ui-&
gt;locationLabel-&
gt;setText(tr("Location: %1"
).arg(list.count() &
gt; 4
? list.at(4
) : tr(blankString)));
m_ui-&
gt;vidLabel-&
gt;setText(tr("Vendor Identifier: %1"
).arg(list.count() &
gt; 5
? list.at(5
) : tr(blankString)));
m_ui-&
gt;pidLabel-&
gt;setText(tr("Product Identifier: %1"
).arg(list.count() &
gt; 6
? list.at(6
) : tr(blankString)));
}
void
SettingsDialog::
apply()
{
updateSettings();
hide();
}
void
SettingsDialog::
checkCustomBaudRatePolicy(int
idx)
{
const
bool
isCustomBaudRate =
!
m_ui-&
gt;baudRateBox-&
gt;itemData(idx).isValid();
m_ui-&
gt;baudRateBox-&
gt;setEditable(isCustomBaudRate);
if
(isCustomBaudRate) {
m_ui-&
gt;baudRateBox-&
gt;clearEditText();
QLineEdit *
edit =
m_ui-&
gt;baudRateBox-&
gt;lineEdit();
edit-&
gt;setValidator(m_intValidator);
}
}
void
SettingsDialog::
checkCustomDevicePathPolicy(int
idx)
{
const
bool
isCustomPath =
!
m_ui-&
gt;serialPortInfoListBox-&
gt;itemData(idx).isValid();
m_ui-&
gt;serialPortInfoListBox-&
gt;setEditable(isCustomPath);
if
(isCustomPath)
m_ui-&
gt;serialPortInfoListBox-&
gt;clearEditText();
}
void
SettingsDialog::
fillPortsParameters()
{
m_ui-&
gt;baudRateBox-&
gt;addItem(QStringLiteral("9600"
), QSerialPort::
Baud9600);
m_ui-&
gt;baudRateBox-&
gt;addItem(QStringLiteral("19200"
), QSerialPort::
Baud19200);
m_ui-&
gt;baudRateBox-&
gt;addItem(QStringLiteral("38400"
), QSerialPort::
Baud38400);
m_ui-&
gt;baudRateBox-&
gt;addItem(QStringLiteral("115200"
), QSerialPort::
Baud115200);
m_ui-&
gt;baudRateBox-&
gt;addItem(tr("Custom"
));
m_ui-&
gt;dataBitsBox-&
gt;addItem(QStringLiteral("5"
), QSerialPort::
Data5);
m_ui-&
gt;dataBitsBox-&
gt;addItem(QStringLiteral("6"
), QSerialPort::
Data6);
m_ui-&
gt;dataBitsBox-&
gt;addItem(QStringLiteral("7"
), QSerialPort::
Data7);
m_ui-&
gt;dataBitsBox-&
gt;addItem(QStringLiteral("8"
), QSerialPort::
Data8);
m_ui-&
gt;dataBitsBox-&
gt;setCurrentIndex(3
);
m_ui-&
gt;parityBox-&
gt;addItem(tr("None"
), QSerialPort::
NoParity);
m_ui-&
gt;parityBox-&
gt;addItem(tr("Even"
), QSerialPort::
EvenParity);
m_ui-&
gt;parityBox-&
gt;addItem(tr("Odd"
), QSerialPort::
OddParity);
m_ui-&
gt;parityBox-&
gt;addItem(tr("Mark"
), QSerialPort::
MarkParity);
m_ui-&
gt;parityBox-&
gt;addItem(tr("Space"
), QSerialPort::
SpaceParity);
m_ui-&
gt;stopBitsBox-&
gt;addItem(QStringLiteral("1"
), QSerialPort::
OneStop);
#ifdef Q_OS_WIN
m_ui-&
gt;stopBitsBox-&
gt;addItem(tr("1.5"
), QSerialPort::
OneAndHalfStop);
#endif
m_ui-&
gt;stopBitsBox-&
gt;addItem(QStringLiteral("2"
), QSerialPort::
TwoStop);
m_ui-&
gt;flowControlBox-&
gt;addItem(tr("None"
), QSerialPort::
NoFlowControl);
m_ui-&
gt;flowControlBox-&
gt;addItem(tr("RTS/CTS"
), QSerialPort::
HardwareControl);
m_ui-&
gt;flowControlBox-&
gt;addItem(tr("XON/XOFF"
), QSerialPort::
SoftwareControl);
}
void
SettingsDialog::
fillPortsInfo()
{
m_ui-&
gt;serialPortInfoListBox-&
gt;clear();
QString description;
QString manufacturer;
QString serialNumber;
const
auto
infos =
QSerialPortInfo::
availablePorts();
for
(const
QSerialPortInfo &
amp;info : infos) {
QStringList list;
description =
info.description();
manufacturer =
info.manufacturer();
serialNumber =
info.serialNumber();
list &
lt;&
lt; info.portName()
&
lt;&
lt; (!
description.isEmpty() ? description : blankString)
&
lt;&
lt; (!
manufacturer.isEmpty() ? manufacturer : blankString)
&
lt;&
lt; (!
serialNumber.isEmpty() ? serialNumber : blankString)
&
lt;&
lt; info.systemLocation()
&
lt;&
lt; (info.vendorIdentifier() ? QString::
number(info.vendorIdentifier(), 16
) : blankString)
&
lt;&
lt; (info.productIdentifier() ? QString::
number(info.productIdentifier(), 16
) : blankString);
m_ui-&
gt;serialPortInfoListBox-&
gt;addItem(list.first(), list);
}
m_ui-&
gt;serialPortInfoListBox-&
gt;addItem(tr("Custom"
));
}
void
SettingsDialog::
updateSettings()
{
m_currentSettings.name =
m_ui-&
gt;serialPortInfoListBox-&
gt;currentText();
if
(m_ui-&
gt;baudRateBox-&
gt;currentIndex() ==
4
) {
m_currentSettings.baudRate =
m_ui-&
gt;baudRateBox-&
gt;currentText().toInt();
}
else
{
m_currentSettings.baudRate =
static_cast
&
lt;QSerialPort::
BaudRate&
gt;(
m_ui-&
gt;baudRateBox-&
gt;itemData(m_ui-&
gt;baudRateBox-&
gt;currentIndex()).toInt());
}
m_currentSettings.stringBaudRate =
QString::
number(m_currentSettings.baudRate);
m_currentSettings.dataBits =
static_cast
&
lt;QSerialPort::
DataBits&
gt;(
m_ui-&
gt;dataBitsBox-&
gt;itemData(m_ui-&
gt;dataBitsBox-&
gt;currentIndex()).toInt());
m_currentSettings.stringDataBits =
m_ui-&
gt;dataBitsBox-&
gt;currentText();
m_currentSettings.parity =
static_cast
&
lt;QSerialPort::
Parity&
gt;(
m_ui-&
gt;parityBox-&
gt;itemData(m_ui-&
gt;parityBox-&
gt;currentIndex()).toInt());
m_currentSettings.stringParity =
m_ui-&
gt;parityBox-&
gt;currentText();
m_currentSettings.stopBits =
static_cast
&
lt;QSerialPort::
StopBits&
gt;(
m_ui-&
gt;stopBitsBox-&
gt;itemData(m_ui-&
gt;stopBitsBox-&
gt;currentIndex()).toInt());
m_currentSettings.stringStopBits =
m_ui-&
gt;stopBitsBox-&
gt;currentText();
m_currentSettings.flowControl =
static_cast
&
lt;QSerialPort::
FlowControl&
gt;(
m_ui-&
gt;flowControlBox-&
gt;itemData(m_ui-&
gt;flowControlBox-&
gt;currentIndex()).toInt());
m_currentSettings.stringFlowControl =
m_ui-&
gt;flowControlBox-&
gt;currentText();
m_currentSettings.localEchoEnabled =
m_ui-&
gt;localEchoCheckBox-&
gt;isChecked();
}