From 494c616951d41f2f394a7157f8970e14096f41b4 Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Tue, 30 Apr 2013 14:46:08 +0200 Subject: Fix examples directory layout Move examples into a dedicated 'serialport' directory. This is in line with what the other modules do, and makes sure that the examples show up in a sensible place even for the 'combined' source packages. Task-number: QTBUG-30912 Change-Id: Iefa2b634df3d2eb34f655b34f6fb24a224b78869 Reviewed-by: Friedemann Kleint Reviewed-by: Laszlo Papp Reviewed-by: Jerome Pasion --- examples/serialport/terminal/console.cpp | 105 +++++++++++ examples/serialport/terminal/console.h | 73 ++++++++ .../terminal/images/application-exit.png | Bin 0 -> 11200 bytes examples/serialport/terminal/images/clear.png | Bin 0 -> 12543 bytes examples/serialport/terminal/images/connect.png | Bin 0 -> 15374 bytes examples/serialport/terminal/images/disconnect.png | Bin 0 -> 15092 bytes examples/serialport/terminal/images/settings.png | Bin 0 -> 16039 bytes examples/serialport/terminal/main.cpp | 53 ++++++ examples/serialport/terminal/mainwindow.cpp | 179 ++++++++++++++++++ examples/serialport/terminal/mainwindow.h | 90 +++++++++ examples/serialport/terminal/mainwindow.ui | 162 ++++++++++++++++ examples/serialport/terminal/settingsdialog.cpp | 204 +++++++++++++++++++++ examples/serialport/terminal/settingsdialog.h | 102 +++++++++++ examples/serialport/terminal/settingsdialog.ui | 170 +++++++++++++++++ examples/serialport/terminal/terminal.pro | 26 +++ examples/serialport/terminal/terminal.qrc | 9 + 16 files changed, 1173 insertions(+) create mode 100644 examples/serialport/terminal/console.cpp create mode 100644 examples/serialport/terminal/console.h create mode 100644 examples/serialport/terminal/images/application-exit.png create mode 100644 examples/serialport/terminal/images/clear.png create mode 100644 examples/serialport/terminal/images/connect.png create mode 100644 examples/serialport/terminal/images/disconnect.png create mode 100644 examples/serialport/terminal/images/settings.png create mode 100644 examples/serialport/terminal/main.cpp create mode 100644 examples/serialport/terminal/mainwindow.cpp create mode 100644 examples/serialport/terminal/mainwindow.h create mode 100644 examples/serialport/terminal/mainwindow.ui create mode 100644 examples/serialport/terminal/settingsdialog.cpp create mode 100644 examples/serialport/terminal/settingsdialog.h create mode 100644 examples/serialport/terminal/settingsdialog.ui create mode 100644 examples/serialport/terminal/terminal.pro create mode 100644 examples/serialport/terminal/terminal.qrc (limited to 'examples/serialport/terminal') diff --git a/examples/serialport/terminal/console.cpp b/examples/serialport/terminal/console.cpp new file mode 100644 index 00000000..7f3f891e --- /dev/null +++ b/examples/serialport/terminal/console.cpp @@ -0,0 +1,105 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov +** Copyright (C) 2012 Laszlo Papp +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "console.h" + +#include + +#include + +Console::Console(QWidget *parent) + : QPlainTextEdit(parent) + , localEchoEnabled(false) +{ + document()->setMaximumBlockCount(100); + QPalette p = palette(); + p.setColor(QPalette::Base, Qt::black); + p.setColor(QPalette::Text, Qt::green); + setPalette(p); + +} + +void Console::putData(const QByteArray &data) +{ + insertPlainText(QString(data)); + + QScrollBar *bar = verticalScrollBar(); + bar->setValue(bar->maximum()); +} + +void Console::setLocalEchoEnabled(bool set) +{ + localEchoEnabled = set; +} + +void Console::keyPressEvent(QKeyEvent *e) +{ + switch (e->key()) { + case Qt::Key_Backspace: + case Qt::Key_Left: + case Qt::Key_Right: + case Qt::Key_Up: + case Qt::Key_Down: + // skip processing + break; + default: + if (localEchoEnabled) + QPlainTextEdit::keyPressEvent(e); + emit getData(e->text().toLocal8Bit()); + } +} + +void Console::mousePressEvent(QMouseEvent *e) +{ + Q_UNUSED(e) + setFocus(); +} + +void Console::mouseDoubleClickEvent(QMouseEvent *e) +{ + Q_UNUSED(e) +} + +void Console::contextMenuEvent(QContextMenuEvent *e) +{ + Q_UNUSED(e) +} diff --git a/examples/serialport/terminal/console.h b/examples/serialport/terminal/console.h new file mode 100644 index 00000000..9b5ba1f8 --- /dev/null +++ b/examples/serialport/terminal/console.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov +** Copyright (C) 2012 Laszlo Papp +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CONSOLE_H +#define CONSOLE_H + +#include + +class Console : public QPlainTextEdit +{ + Q_OBJECT + +signals: + void getData(const QByteArray &data); + +public: + explicit Console(QWidget *parent = 0); + + void putData(const QByteArray &data); + + void setLocalEchoEnabled(bool set); + +protected: + virtual void keyPressEvent(QKeyEvent *e); + virtual void mousePressEvent(QMouseEvent *e); + virtual void mouseDoubleClickEvent(QMouseEvent *e); + virtual void contextMenuEvent(QContextMenuEvent *e); + +private: + bool localEchoEnabled; + +}; + +#endif // CONSOLE_H diff --git a/examples/serialport/terminal/images/application-exit.png b/examples/serialport/terminal/images/application-exit.png new file mode 100644 index 00000000..32be6b3f Binary files /dev/null and b/examples/serialport/terminal/images/application-exit.png differ diff --git a/examples/serialport/terminal/images/clear.png b/examples/serialport/terminal/images/clear.png new file mode 100644 index 00000000..aa612f1f Binary files /dev/null and b/examples/serialport/terminal/images/clear.png differ diff --git a/examples/serialport/terminal/images/connect.png b/examples/serialport/terminal/images/connect.png new file mode 100644 index 00000000..dd5a51e9 Binary files /dev/null and b/examples/serialport/terminal/images/connect.png differ diff --git a/examples/serialport/terminal/images/disconnect.png b/examples/serialport/terminal/images/disconnect.png new file mode 100644 index 00000000..fd58f7a4 Binary files /dev/null and b/examples/serialport/terminal/images/disconnect.png differ diff --git a/examples/serialport/terminal/images/settings.png b/examples/serialport/terminal/images/settings.png new file mode 100644 index 00000000..3d1042e2 Binary files /dev/null and b/examples/serialport/terminal/images/settings.png differ diff --git a/examples/serialport/terminal/main.cpp b/examples/serialport/terminal/main.cpp new file mode 100644 index 00000000..e3565190 --- /dev/null +++ b/examples/serialport/terminal/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov +** Copyright (C) 2012 Laszlo Papp +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/examples/serialport/terminal/mainwindow.cpp b/examples/serialport/terminal/mainwindow.cpp new file mode 100644 index 00000000..47864a25 --- /dev/null +++ b/examples/serialport/terminal/mainwindow.cpp @@ -0,0 +1,179 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov +** Copyright (C) 2012 Laszlo Papp +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include "console.h" +#include "settingsdialog.h" + +#include +#include + +//! [0] +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ +//! [0] + ui->setupUi(this); + console = new Console; + console->setEnabled(false); + setCentralWidget(console); +//! [1] + serial = new QSerialPort(this); +//! [1] + settings = new SettingsDialog; + + ui->actionConnect->setEnabled(true); + ui->actionDisconnect->setEnabled(false); + ui->actionQuit->setEnabled(true); + ui->actionConfigure->setEnabled(true); + + initActionsConnections(); + + connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, + SLOT(handleError(QSerialPort::SerialPortError))); + +//! [2] + connect(serial, SIGNAL(readyRead()), this, SLOT(readData())); +//! [2] + connect(console, SIGNAL(getData(QByteArray)), this, SLOT(writeData(QByteArray))); +//! [3] +} +//! [3] + +MainWindow::~MainWindow() +{ + delete settings; + delete ui; +} + +//! [4] +void MainWindow::openSerialPort() +{ + SettingsDialog::Settings p = settings->settings(); + serial->setPortName(p.name); + if (serial->open(QIODevice::ReadWrite)) { + if (serial->setBaudRate(p.baudRate) + && serial->setDataBits(p.dataBits) + && serial->setParity(p.parity) + && serial->setStopBits(p.stopBits) + && serial->setFlowControl(p.flowControl)) { + + console->setEnabled(true); + console->setLocalEchoEnabled(p.localEchoEnabled); + ui->actionConnect->setEnabled(false); + ui->actionDisconnect->setEnabled(true); + ui->actionConfigure->setEnabled(false); + ui->statusBar->showMessage(tr("Connected to %1 : %2, %3, %4, %5, %6") + .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits) + .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl)); + + } else { + serial->close(); + QMessageBox::critical(this, tr("Error"), serial->errorString()); + + ui->statusBar->showMessage(tr("Open error")); + } + } else { + QMessageBox::critical(this, tr("Error"), serial->errorString()); + + ui->statusBar->showMessage(tr("Configure error")); + } +} +//! [4] + +//! [5] +void MainWindow::closeSerialPort() +{ + serial->close(); + console->setEnabled(false); + ui->actionConnect->setEnabled(true); + ui->actionDisconnect->setEnabled(false); + ui->actionConfigure->setEnabled(true); + ui->statusBar->showMessage(tr("Disconnected")); +} +//! [5] + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Simple Terminal"), + tr("The Simple Terminal example demonstrates how to " + "use the Qt Serial Port module in modern GUI applications " + "using Qt, with a menu bar, toolbars, and a status bar.")); +} + +//! [6] +void MainWindow::writeData(const QByteArray &data) +{ + serial->write(data); +} +//! [6] + +//! [7] +void MainWindow::readData() +{ + QByteArray data = serial->readAll(); + console->putData(data); +} +//! [7] + +//! [8] +void MainWindow::handleError(QSerialPort::SerialPortError error) +{ + if (error == QSerialPort::ResourceError) { + QMessageBox::critical(this, tr("Critical Error"), serial->errorString()); + closeSerialPort(); + } +} +//! [8] + +void MainWindow::initActionsConnections() +{ + connect(ui->actionConnect, SIGNAL(triggered()), this, SLOT(openSerialPort())); + connect(ui->actionDisconnect, SIGNAL(triggered()), this, SLOT(closeSerialPort())); + connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(close())); + connect(ui->actionConfigure, SIGNAL(triggered()), settings, SLOT(show())); + connect(ui->actionClear, SIGNAL(triggered()), console, SLOT(clear())); + connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about())); + connect(ui->actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt())); +} diff --git a/examples/serialport/terminal/mainwindow.h b/examples/serialport/terminal/mainwindow.h new file mode 100644 index 00000000..1be7f893 --- /dev/null +++ b/examples/serialport/terminal/mainwindow.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov +** Copyright (C) 2012 Laszlo Papp +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +#include + +#include + +QT_BEGIN_NAMESPACE + +namespace Ui { +class MainWindow; +} + +QT_END_NAMESPACE + +class Console; +class SettingsDialog; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private slots: + void openSerialPort(); + void closeSerialPort(); + void about(); + void writeData(const QByteArray &data); + void readData(); + + void handleError(QSerialPort::SerialPortError error); + +private: + void initActionsConnections(); + +private: + Ui::MainWindow *ui; + Console *console; + SettingsDialog *settings; + QSerialPort *serial; +}; + +#endif // MAINWINDOW_H diff --git a/examples/serialport/terminal/mainwindow.ui b/examples/serialport/terminal/mainwindow.ui new file mode 100644 index 00000000..452fdd53 --- /dev/null +++ b/examples/serialport/terminal/mainwindow.ui @@ -0,0 +1,162 @@ + + + MainWindow + + + + 0 + 0 + 400 + 300 + + + + Simple Terminal + + + + + + + + 0 + 0 + 400 + 19 + + + + + Calls + + + + + + + + + Tools + + + + + + + Help + + + + + + + + + + + TopToolBarArea + + + false + + + + + + + + + + &About + + + About program + + + Alt+A + + + + + About Qt + + + + + + :/images/connect.png:/images/connect.png + + + C&onnect + + + Connect to serial port + + + Ctrl+O + + + + + + :/images/disconnect.png:/images/disconnect.png + + + &Disconnect + + + Disconnect from serial port + + + Ctrl+D + + + + + + :/images/settings.png:/images/settings.png + + + &Configure + + + Configure serial port + + + Alt+C + + + + + + :/images/clear.png:/images/clear.png + + + C&lear + + + Clear data + + + Alt+L + + + + + + :/images/application-exit.png:/images/application-exit.png + + + &Quit + + + Ctrl+Q + + + + + + + + + diff --git a/examples/serialport/terminal/settingsdialog.cpp b/examples/serialport/terminal/settingsdialog.cpp new file mode 100644 index 00000000..65f6b87c --- /dev/null +++ b/examples/serialport/terminal/settingsdialog.cpp @@ -0,0 +1,204 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov +** Copyright (C) 2012 Laszlo Papp +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "settingsdialog.h" +#include "ui_settingsdialog.h" + +#include +#include +#include + +QT_USE_NAMESPACE + +SettingsDialog::SettingsDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::SettingsDialog) +{ + ui->setupUi(this); + + intValidator = new QIntValidator(0, 4000000, this); + + ui->baudRateBox->setInsertPolicy(QComboBox::NoInsert); + + connect(ui->applyButton, SIGNAL(clicked()), + this, SLOT(apply())); + connect(ui->serialPortInfoListBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(showPortInfo(int))); + connect(ui->baudRateBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(checkCustomBaudRatePolicy(int))); + + fillPortsParameters(); + fillPortsInfo(); + + updateSettings(); +} + +SettingsDialog::~SettingsDialog() +{ + delete ui; +} + +SettingsDialog::Settings SettingsDialog::settings() const +{ + return currentSettings; +} + +void SettingsDialog::showPortInfo(int idx) +{ + if (idx != -1) { + QStringList list = ui->serialPortInfoListBox->itemData(idx).toStringList(); + ui->descriptionLabel->setText(tr("Description: %1").arg(list.at(1))); + ui->manufacturerLabel->setText(tr("Manufacturer: %1").arg(list.at(2))); + ui->locationLabel->setText(tr("Location: %1").arg(list.at(3))); + ui->vidLabel->setText(tr("Vendor Identifier: %1").arg(list.at(4))); + ui->pidLabel->setText(tr("Product Identifier: %1").arg(list.at(5))); + } +} + +void SettingsDialog::apply() +{ + updateSettings(); + hide(); +} + +void SettingsDialog::checkCustomBaudRatePolicy(int idx) +{ + bool isCustomBaudRate = !ui->baudRateBox->itemData(idx).isValid(); + ui->baudRateBox->setEditable(isCustomBaudRate); + if (isCustomBaudRate) { + ui->baudRateBox->clearEditText(); + QLineEdit *edit = ui->baudRateBox->lineEdit(); + edit->setValidator(intValidator); + } +} + +void SettingsDialog::fillPortsParameters() +{ + // fill baud rate (is not the entire list of available values, + // desired values??, add your independently) + ui->baudRateBox->addItem(QLatin1String("9600"), QSerialPort::Baud9600); + ui->baudRateBox->addItem(QLatin1String("19200"), QSerialPort::Baud19200); + ui->baudRateBox->addItem(QLatin1String("38400"), QSerialPort::Baud38400); + ui->baudRateBox->addItem(QLatin1String("115200"), QSerialPort::Baud115200); + ui->baudRateBox->addItem(QLatin1String("Custom")); + + // fill data bits + ui->dataBitsBox->addItem(QLatin1String("5"), QSerialPort::Data5); + ui->dataBitsBox->addItem(QLatin1String("6"), QSerialPort::Data6); + ui->dataBitsBox->addItem(QLatin1String("7"), QSerialPort::Data7); + ui->dataBitsBox->addItem(QLatin1String("8"), QSerialPort::Data8); + ui->dataBitsBox->setCurrentIndex(3); + + // fill parity + ui->parityBox->addItem(QLatin1String("None"), QSerialPort::NoParity); + ui->parityBox->addItem(QLatin1String("Even"), QSerialPort::EvenParity); + ui->parityBox->addItem(QLatin1String("Odd"), QSerialPort::OddParity); + ui->parityBox->addItem(QLatin1String("Mark"), QSerialPort::MarkParity); + ui->parityBox->addItem(QLatin1String("Space"), QSerialPort::SpaceParity); + + // fill stop bits + ui->stopBitsBox->addItem(QLatin1String("1"), QSerialPort::OneStop); +#ifdef Q_OS_WIN + ui->stopBitsBox->addItem(QLatin1String("1.5"), QSerialPort::OneAndHalfStop); +#endif + ui->stopBitsBox->addItem(QLatin1String("2"), QSerialPort::TwoStop); + + // fill flow control + ui->flowControlBox->addItem(QLatin1String("None"), QSerialPort::NoFlowControl); + ui->flowControlBox->addItem(QLatin1String("RTS/CTS"), QSerialPort::HardwareControl); + ui->flowControlBox->addItem(QLatin1String("XON/XOFF"), QSerialPort::SoftwareControl); +} + +void SettingsDialog::fillPortsInfo() +{ + ui->serialPortInfoListBox->clear(); + foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { + QStringList list; + list << info.portName() + << info.description() + << info.manufacturer() + << info.systemLocation() + << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString()) + << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString()); + + ui->serialPortInfoListBox->addItem(list.first(), list); + } +} + +void SettingsDialog::updateSettings() +{ + currentSettings.name = ui->serialPortInfoListBox->currentText(); + + // Baud Rate + if (ui->baudRateBox->currentIndex() == 4) { + // custom baud rate + currentSettings.baudRate = ui->baudRateBox->currentText().toInt(); + } else { + // standard baud rate + currentSettings.baudRate = static_cast( + ui->baudRateBox->itemData(ui->baudRateBox->currentIndex()).toInt()); + } + currentSettings.stringBaudRate = QString::number(currentSettings.baudRate); + + // Data bits + currentSettings.dataBits = static_cast( + ui->dataBitsBox->itemData(ui->dataBitsBox->currentIndex()).toInt()); + currentSettings.stringDataBits = ui->dataBitsBox->currentText(); + + // Parity + currentSettings.parity = static_cast( + ui->parityBox->itemData(ui->parityBox->currentIndex()).toInt()); + currentSettings.stringParity = ui->parityBox->currentText(); + + // Stop bits + currentSettings.stopBits = static_cast( + ui->stopBitsBox->itemData(ui->stopBitsBox->currentIndex()).toInt()); + currentSettings.stringStopBits = ui->stopBitsBox->currentText(); + + // Flow control + currentSettings.flowControl = static_cast( + ui->flowControlBox->itemData(ui->flowControlBox->currentIndex()).toInt()); + currentSettings.stringFlowControl = ui->flowControlBox->currentText(); + + // Additional options + currentSettings.localEchoEnabled = ui->localEchoCheckBox->isChecked(); +} diff --git a/examples/serialport/terminal/settingsdialog.h b/examples/serialport/terminal/settingsdialog.h new file mode 100644 index 00000000..ff8f8b67 --- /dev/null +++ b/examples/serialport/terminal/settingsdialog.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov +** Copyright (C) 2012 Laszlo Papp +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SETTINGSDIALOG_H +#define SETTINGSDIALOG_H + +#include +#include + +QT_USE_NAMESPACE + +QT_BEGIN_NAMESPACE + +namespace Ui { +class SettingsDialog; +} + +class QIntValidator; + +QT_END_NAMESPACE + +class SettingsDialog : public QDialog +{ + Q_OBJECT + +public: + struct Settings { + QString name; + qint32 baudRate; + QString stringBaudRate; + QSerialPort::DataBits dataBits; + QString stringDataBits; + QSerialPort::Parity parity; + QString stringParity; + QSerialPort::StopBits stopBits; + QString stringStopBits; + QSerialPort::FlowControl flowControl; + QString stringFlowControl; + bool localEchoEnabled; + }; + + explicit SettingsDialog(QWidget *parent = 0); + ~SettingsDialog(); + + Settings settings() const; + +private slots: + void showPortInfo(int idx); + void apply(); + void checkCustomBaudRatePolicy(int idx); + +private: + void fillPortsParameters(); + void fillPortsInfo(); + void updateSettings(); + +private: + Ui::SettingsDialog *ui; + Settings currentSettings; + QIntValidator *intValidator; +}; + +#endif // SETTINGSDIALOG_H diff --git a/examples/serialport/terminal/settingsdialog.ui b/examples/serialport/terminal/settingsdialog.ui new file mode 100644 index 00000000..28c1211a --- /dev/null +++ b/examples/serialport/terminal/settingsdialog.ui @@ -0,0 +1,170 @@ + + + SettingsDialog + + + + 0 + 0 + 281 + 262 + + + + Settings + + + + + + Select Parameters + + + + + + BaudRate: + + + + + + + + + + Data bits: + + + + + + + + + + Parity: + + + + + + + + + + Stop bits: + + + + + + + + + + Flow control: + + + + + + + + + + + + + Select Serial Port + + + + + + + + + Description: + + + + + + + Manufacturer: + + + + + + + Location: + + + + + + + Vendor ID: + + + + + + + Product ID: + + + + + + + + + + + + Qt::Horizontal + + + + 96 + 20 + + + + + + + + Apply + + + + + + + + + Additional options + + + + + + Local echo + + + true + + + + + + + + + + + diff --git a/examples/serialport/terminal/terminal.pro b/examples/serialport/terminal/terminal.pro new file mode 100644 index 00000000..0a5b545a --- /dev/null +++ b/examples/serialport/terminal/terminal.pro @@ -0,0 +1,26 @@ +greaterThan(QT_MAJOR_VERSION, 4) { + QT += widgets serialport +} else { + include($$QTSERIALPORT_PROJECT_ROOT/src/serialport/qt4support/serialport.prf) +} + +TARGET = terminal +TEMPLATE = app + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + settingsdialog.cpp \ + console.cpp + +HEADERS += \ + mainwindow.h \ + settingsdialog.h \ + console.h + +FORMS += \ + mainwindow.ui \ + settingsdialog.ui + +RESOURCES += \ + terminal.qrc diff --git a/examples/serialport/terminal/terminal.qrc b/examples/serialport/terminal/terminal.qrc new file mode 100644 index 00000000..0b498794 --- /dev/null +++ b/examples/serialport/terminal/terminal.qrc @@ -0,0 +1,9 @@ + + + images/connect.png + images/disconnect.png + images/application-exit.png + images/settings.png + images/clear.png + + -- cgit v1.2.3