xref: /OK3568_Linux_fs/app/forlinx/forlinx_qt/terminal/mainwindow.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 "mainwindow.h"
36*4882a593Smuzhiyun #include "ui_mainwindow.h"
37*4882a593Smuzhiyun #include "console.h"
38*4882a593Smuzhiyun #include "settingsdialog.h"
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #include <QMessageBox>
41*4882a593Smuzhiyun #include <QLabel>
42*4882a593Smuzhiyun #include <QtSerialPort/QSerialPort>
43*4882a593Smuzhiyun #include <DWKeyboard/KeyboardGlobal.h>
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun //! [0]
MainWindow(QWidget * parent)46*4882a593Smuzhiyun MainWindow::MainWindow(QWidget *parent) :
47*4882a593Smuzhiyun     QMainWindow(parent),
48*4882a593Smuzhiyun     ui(new Ui::MainWindow)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun //! [0]
51*4882a593Smuzhiyun     ui->setupUi(this);
52*4882a593Smuzhiyun     console = new Console;
53*4882a593Smuzhiyun     console->setEnabled(false);
54*4882a593Smuzhiyun     setCentralWidget(console);
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun     GlobalInit();
57*4882a593Smuzhiyun     console->installEventFilter(this);
58*4882a593Smuzhiyun     console->viewport()->installEventFilter(this);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun //! [1]
61*4882a593Smuzhiyun     serial = new QSerialPort(this);
62*4882a593Smuzhiyun //! [1]
63*4882a593Smuzhiyun     settings = new SettingsDialog;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun     ui->actionConnect->setEnabled(true);
66*4882a593Smuzhiyun     ui->actionDisconnect->setEnabled(false);
67*4882a593Smuzhiyun     ui->actionQuit->setEnabled(true);
68*4882a593Smuzhiyun     ui->actionConfigure->setEnabled(true);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun     status = new QLabel;
71*4882a593Smuzhiyun     ui->statusBar->addWidget(status);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun     initActionsConnections();
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun     connect(serial, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error),
76*4882a593Smuzhiyun             this, &MainWindow::handleError);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun //! [2]
79*4882a593Smuzhiyun     connect(serial, &QSerialPort::readyRead, this, &MainWindow::readData);
80*4882a593Smuzhiyun //! [2]
81*4882a593Smuzhiyun     connect(console, &Console::getData, this, &MainWindow::writeData);
82*4882a593Smuzhiyun //! [3]
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun //! [3]
85*4882a593Smuzhiyun 
~MainWindow()86*4882a593Smuzhiyun MainWindow::~MainWindow()
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun     delete settings;
89*4882a593Smuzhiyun     delete ui;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
eventFilter(QObject * watched,QEvent * event)92*4882a593Smuzhiyun bool MainWindow::eventFilter(QObject *watched, QEvent *event)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun     static bool mFocusIn = false;
95*4882a593Smuzhiyun     if (event->type()==QEvent::FocusIn)
96*4882a593Smuzhiyun     {
97*4882a593Smuzhiyun 	mFocusIn = true;
98*4882a593Smuzhiyun     }
99*4882a593Smuzhiyun     else if (event->type()==QEvent::FocusOut)
100*4882a593Smuzhiyun     {
101*4882a593Smuzhiyun 	PlatformInputContextBase->FocusOut(watched);
102*4882a593Smuzhiyun 	mFocusIn = false;
103*4882a593Smuzhiyun     }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun     if (mFocusIn && event->type() == QEvent::MouseButtonPress) {
106*4882a593Smuzhiyun    	QMouseEvent *e = (QMouseEvent *)event;
107*4882a593Smuzhiyun 	PlatformInputContextBase->FocusIn(watched, e->globalPos());
108*4882a593Smuzhiyun     }
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun     return QMainWindow::eventFilter(watched,event);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun //! [4]
openSerialPort()115*4882a593Smuzhiyun void MainWindow::openSerialPort()
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun     SettingsDialog::Settings p = settings->settings();
118*4882a593Smuzhiyun     serial->setPortName(p.name);
119*4882a593Smuzhiyun     serial->setBaudRate(p.baudRate);
120*4882a593Smuzhiyun     serial->setDataBits(p.dataBits);
121*4882a593Smuzhiyun     serial->setParity(p.parity);
122*4882a593Smuzhiyun     serial->setStopBits(p.stopBits);
123*4882a593Smuzhiyun     serial->setFlowControl(p.flowControl);
124*4882a593Smuzhiyun     if (serial->open(QIODevice::ReadWrite)) {
125*4882a593Smuzhiyun         console->setEnabled(true);
126*4882a593Smuzhiyun         console->setLocalEchoEnabled(p.localEchoEnabled);
127*4882a593Smuzhiyun         ui->actionConnect->setEnabled(false);
128*4882a593Smuzhiyun         ui->actionDisconnect->setEnabled(true);
129*4882a593Smuzhiyun         ui->actionConfigure->setEnabled(false);
130*4882a593Smuzhiyun         showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
131*4882a593Smuzhiyun                           .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
132*4882a593Smuzhiyun                           .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
133*4882a593Smuzhiyun     } else {
134*4882a593Smuzhiyun         QMessageBox::critical(this, tr("Error"), serial->errorString());
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun         showStatusMessage(tr("Open error"));
137*4882a593Smuzhiyun     }
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun //! [4]
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun //! [5]
closeSerialPort()142*4882a593Smuzhiyun void MainWindow::closeSerialPort()
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun     if (serial->isOpen())
145*4882a593Smuzhiyun         serial->close();
146*4882a593Smuzhiyun     console->setEnabled(false);
147*4882a593Smuzhiyun     ui->actionConnect->setEnabled(true);
148*4882a593Smuzhiyun     ui->actionDisconnect->setEnabled(false);
149*4882a593Smuzhiyun     ui->actionConfigure->setEnabled(true);
150*4882a593Smuzhiyun     showStatusMessage(tr("Disconnected"));
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun //! [5]
153*4882a593Smuzhiyun 
about()154*4882a593Smuzhiyun void MainWindow::about()
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun     QMessageBox::about(this, tr("About Simple Terminal"),
157*4882a593Smuzhiyun                        tr("The <b>Simple Terminal</b> example demonstrates how to "
158*4882a593Smuzhiyun                           "use the Qt Serial Port module in modern GUI applications "
159*4882a593Smuzhiyun                           "using Qt, with a menu bar, toolbars, and a status bar."));
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun //! [6]
writeData(const QByteArray & data)163*4882a593Smuzhiyun void MainWindow::writeData(const QByteArray &data)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun     serial->write(data);
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun //! [6]
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun //! [7]
readData()170*4882a593Smuzhiyun void MainWindow::readData()
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun     QByteArray data = serial->readAll();
173*4882a593Smuzhiyun     console->putData(data);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun //! [7]
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun //! [8]
handleError(QSerialPort::SerialPortError error)178*4882a593Smuzhiyun void MainWindow::handleError(QSerialPort::SerialPortError error)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun     if (error == QSerialPort::ResourceError) {
181*4882a593Smuzhiyun         QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
182*4882a593Smuzhiyun         closeSerialPort();
183*4882a593Smuzhiyun     }
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun //! [8]
186*4882a593Smuzhiyun 
initActionsConnections()187*4882a593Smuzhiyun void MainWindow::initActionsConnections()
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun     connect(ui->actionConnect, &QAction::triggered, this, &MainWindow::openSerialPort);
190*4882a593Smuzhiyun     connect(ui->actionDisconnect, &QAction::triggered, this, &MainWindow::closeSerialPort);
191*4882a593Smuzhiyun     connect(ui->actionQuit, &QAction::triggered, this, &MainWindow::close);
192*4882a593Smuzhiyun     connect(ui->actionConfigure, &QAction::triggered, settings, &MainWindow::show);
193*4882a593Smuzhiyun     connect(ui->actionClear, &QAction::triggered, console, &Console::clear);
194*4882a593Smuzhiyun     connect(ui->actionAbout, &QAction::triggered, this, &MainWindow::about);
195*4882a593Smuzhiyun     connect(ui->actionAboutQt, &QAction::triggered, qApp, &QApplication::aboutQt);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun     connect(ui->actionExit, &QAction::triggered, this, [=](){
198*4882a593Smuzhiyun     close();
199*4882a593Smuzhiyun     });
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
showStatusMessage(const QString & message)203*4882a593Smuzhiyun void MainWindow::showStatusMessage(const QString &message)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun     status->setText(message);
206*4882a593Smuzhiyun }
207