1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtSerialPort module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include "dialog.h"
52
53 #include <QComboBox>
54 #include <QGridLayout>
55 #include <QLabel>
56 #include <QLineEdit>
57 #include <QPushButton>
58 #include <QSerialPortInfo>
59 #include <QSpinBox>
60
Dialog(QWidget * parent)61 Dialog::Dialog(QWidget *parent) :
62 QDialog(parent),
63 m_serialPortLabel(new QLabel(tr("Serial port:"))),
64 m_serialPortComboBox(new QComboBox),
65 m_waitRequestLabel(new QLabel(tr("Wait request, msec:"))),
66 m_waitRequestSpinBox(new QSpinBox),
67 m_responseLabel(new QLabel(tr("Response:"))),
68 m_responseLineEdit(new QLineEdit(tr("Hello, I'm Slave."))),
69 m_trafficLabel(new QLabel(tr("No traffic."))),
70 m_statusLabel(new QLabel(tr("Status: Not running."))),
71 m_runButton(new QPushButton(tr("Start")))
72 {
73 m_waitRequestSpinBox->setRange(0, 10000);
74 m_waitRequestSpinBox->setValue(10000);
75
76 const auto infos = QSerialPortInfo::availablePorts();
77 for (const QSerialPortInfo &info : infos)
78 m_serialPortComboBox->addItem(info.portName());
79
80 auto mainLayout = new QGridLayout;
81 mainLayout->addWidget(m_serialPortLabel, 0, 0);
82 mainLayout->addWidget(m_serialPortComboBox, 0, 1);
83 mainLayout->addWidget(m_waitRequestLabel, 1, 0);
84 mainLayout->addWidget(m_waitRequestSpinBox, 1, 1);
85 mainLayout->addWidget(m_runButton, 0, 2, 2, 1);
86 mainLayout->addWidget(m_responseLabel, 2, 0);
87 mainLayout->addWidget(m_responseLineEdit, 2, 1, 1, 3);
88 mainLayout->addWidget(m_trafficLabel, 3, 0, 1, 4);
89 mainLayout->addWidget(m_statusLabel, 4, 0, 1, 5);
90 setLayout(mainLayout);
91
92 setWindowTitle(tr("Blocking Slave"));
93 m_serialPortComboBox->setFocus();
94
95 connect(m_runButton, &QPushButton::clicked, this, &Dialog::startSlave);
96 connect(&m_thread, &SlaveThread::request, this,&Dialog::showRequest);
97 connect(&m_thread, &SlaveThread::error, this, &Dialog::processError);
98 connect(&m_thread, &SlaveThread::timeout, this, &Dialog::processTimeout);
99
100 connect(m_serialPortComboBox, QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
101 this, &Dialog::activateRunButton);
102 connect(m_waitRequestSpinBox, &QSpinBox::textChanged, this, &Dialog::activateRunButton);
103 connect(m_responseLineEdit, &QLineEdit::textChanged, this, &Dialog::activateRunButton);
104 }
105
startSlave()106 void Dialog::startSlave()
107 {
108 m_runButton->setEnabled(false);
109 m_statusLabel->setText(tr("Status: Running, connected to port %1.")
110 .arg(m_serialPortComboBox->currentText()));
111 m_thread.startSlave(m_serialPortComboBox->currentText(),
112 m_waitRequestSpinBox->value(),
113 m_responseLineEdit->text());
114 }
115
showRequest(const QString & s)116 void Dialog::showRequest(const QString &s)
117 {
118 m_trafficLabel->setText(tr("Traffic, transaction #%1:"
119 "\n\r-request: %2"
120 "\n\r-response: %3")
121 .arg(++m_transactionCount)
122 .arg(s)
123 .arg(m_responseLineEdit->text()));
124 }
125
processError(const QString & s)126 void Dialog::processError(const QString &s)
127 {
128 activateRunButton();
129 m_statusLabel->setText(tr("Status: Not running, %1.").arg(s));
130 m_trafficLabel->setText(tr("No traffic."));
131 }
132
processTimeout(const QString & s)133 void Dialog::processTimeout(const QString &s)
134 {
135 m_statusLabel->setText(tr("Status: Running, %1.").arg(s));
136 m_trafficLabel->setText(tr("No traffic."));
137 }
138
activateRunButton()139 void Dialog::activateRunButton()
140 {
141 m_runButton->setEnabled(true);
142 }
143