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