# HG changeset patch # User Hasan Yavuz ÖZDERYA # Date 2015-05-23 20:55:10 # Node ID 6a311d7228183a7d01dd856c8a2526f6c8a27e2f # Parent acee6b77993ce1f751a415868cdccbbdd4cc8046 moved port settings tab to a separate class, PortControl diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,10 +49,11 @@ endif (QWT_USE_STATIC) include_directories(${QWT_INCLUDE_DIR}) # wrap UI files -qt5_wrap_ui(UI_FILES mainwindow.ui about_dialog.ui) +qt5_wrap_ui(UI_FILES mainwindow.ui portcontrol.ui about_dialog.ui) # Tell CMake to create the helloworld executable -add_executable(serialplot main.cpp mainwindow.cpp customcheckablebutton.cpp ${UI_FILES}) +add_executable(serialplot main.cpp mainwindow.cpp portcontrol.cpp + customcheckablebutton.cpp ${UI_FILES}) # Use the Widgets module from Qt 5. target_link_libraries(serialplot ${QWT_LIBRARY}) diff --git a/mainwindow.cpp b/mainwindow.cpp --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -19,7 +19,6 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -#include #include #include #include @@ -31,9 +30,13 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - ui(new Ui::MainWindow) + ui(new Ui::MainWindow), + portControl(&serialPort) { ui->setupUi(this); + ui->tabWidget->insertTab(0, &portControl, "Port"); + ui->tabWidget->setCurrentIndex(0); + setupAboutDialog(); // init UI signals @@ -42,23 +45,11 @@ MainWindow::MainWindow(QWidget *parent) QObject::connect(ui->actionHelpAbout, &QAction::triggered, &aboutDialog, &QWidget::show); - // port tab signals - QObject::connect(ui->pbReloadPorts, &QPushButton::clicked, - this, &MainWindow::loadPortList); - - QObject::connect(ui->pbOpenPort, &QPushButton::clicked, - this, &MainWindow::togglePort); - - QObject::connect(this, &MainWindow::portToggled, + QObject::connect(&portControl, &PortControl::portToggled, this, &MainWindow::onPortToggled); - QObject::connect(ui->cbPortList, - SELECT::OVERLOAD_OF(&QComboBox::activated), - this, &MainWindow::selectPort); - - QObject::connect(ui->cbBaudRate, - SELECT::OVERLOAD_OF(&QComboBox::activated), - this, &MainWindow::selectBaudRate); + QObject::connect(&portControl, &PortControl::skipByteRequested, + this, &MainWindow::skipByte); QObject::connect(ui->spNumOfSamples, SELECT::OVERLOAD_OF(&QSpinBox::valueChanged), this, &MainWindow::onNumOfSamplesChanged); @@ -93,58 +84,10 @@ MainWindow::MainWindow(QWidget *parent) QObject::connect(&numberFormatButtons, SIGNAL(buttonToggled(int, bool)), this, SLOT(onNumberFormatButtonToggled(int, bool))); - // setup parity selection buttons - parityButtons.addButton(ui->rbNoParity, (int) QSerialPort::NoParity); - parityButtons.addButton(ui->rbEvenParity, (int) QSerialPort::EvenParity); - parityButtons.addButton(ui->rbOddParity, (int) QSerialPort::OddParity); - - QObject::connect(&parityButtons, - SELECT::OVERLOAD_OF(&QButtonGroup::buttonClicked), - this, &MainWindow::selectParity); - - // setup data bits selection buttons - dataBitsButtons.addButton(ui->rb8Bits, (int) QSerialPort::Data8); - dataBitsButtons.addButton(ui->rb7Bits, (int) QSerialPort::Data7); - dataBitsButtons.addButton(ui->rb6Bits, (int) QSerialPort::Data6); - dataBitsButtons.addButton(ui->rb5Bits, (int) QSerialPort::Data5); - - QObject::connect(&dataBitsButtons, - SELECT::OVERLOAD_OF(&QButtonGroup::buttonClicked), - this, &MainWindow::selectDataBits); - - // setup stop bits selection buttons - stopBitsButtons.addButton(ui->rb1StopBit, (int) QSerialPort::OneStop); - stopBitsButtons.addButton(ui->rb2StopBit, (int) QSerialPort::TwoStop); - - QObject::connect(&stopBitsButtons, - SELECT::OVERLOAD_OF(&QButtonGroup::buttonClicked), - this, &MainWindow::selectStopBits); - - // setup flow control selection buttons - flowControlButtons.addButton(ui->rbNoFlowControl, - (int) QSerialPort::NoFlowControl); - flowControlButtons.addButton(ui->rbHardwareControl, - (int) QSerialPort::HardwareControl); - flowControlButtons.addButton(ui->rbSoftwareControl, - (int) QSerialPort::SoftwareControl); - - QObject::connect(&flowControlButtons, - SELECT::OVERLOAD_OF(&QButtonGroup::buttonClicked), - this, &MainWindow::selectFlowControl); - // init port signals QObject::connect(&(this->serialPort), SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onPortError(QSerialPort::SerialPortError))); - // init skip byte button - skipByteRequested = false; - QObject::connect(ui->pbSkipByte, &QPushButton::clicked, - this, &MainWindow::skipByte); - - loadPortList(); - loadBaudRateList(); - ui->cbBaudRate->setCurrentIndex(ui->cbBaudRate->findText("9600")); - // set limits for axis limit boxes ui->spYmin->setRange((-1) * std::numeric_limits::max(), std::numeric_limits::max()); @@ -220,150 +163,8 @@ void MainWindow::setupAboutDialog() uiAboutDialog.lbAbout->setText(aboutText); } -void MainWindow::loadPortList() -{ - QString currentSelection = ui->cbPortList->currentText(); - - ui->cbPortList->clear(); - - for (auto port : QSerialPortInfo::availablePorts()) - { - ui->cbPortList->addItem(port.portName()); - } - - // find current selection in the new list, maybe it doesn't exist anymore? - int currentSelectionIndex = ui->cbPortList->findText(currentSelection); - if (currentSelectionIndex >= 0) - { - ui->cbPortList->setCurrentIndex(currentSelectionIndex); - } - else // our port doesn't exist anymore, close port if it's open - { - if (serialPort.isOpen()) togglePort(); - } -} - -void MainWindow::loadBaudRateList() -{ - ui->cbBaudRate->clear(); - - for (auto baudRate : QSerialPortInfo::standardBaudRates()) - { - ui->cbBaudRate->addItem(QString::number(baudRate)); - } -} - -void MainWindow::togglePort() -{ - if (serialPort.isOpen()) - { - serialPort.close(); - qDebug() << "Port closed, " << serialPort.portName(); - emit portToggled(false); - } - else - { - serialPort.setPortName(ui->cbPortList->currentText()); - - // open port - if (serialPort.open(QIODevice::ReadWrite)) - { - qDebug() << "Port opened, " << serialPort.portName(); - emit portToggled(true); - - // set baud rate - if (!serialPort.setBaudRate(ui->cbBaudRate->currentText().toInt())) - { - qDebug() << "Set baud rate failed during port opening: " - << serialPort.error(); - } - } - else - { - qDebug() << "Port open error: " << serialPort.error(); - } - } -} - -void MainWindow::selectPort(QString portName) -{ - // has selection actually changed - if (portName != serialPort.portName()) - { - // if another port is already open, close it by toggling - if (serialPort.isOpen()) - { - togglePort(); - - // open new selection by toggling - togglePort(); - } - } -} - -void MainWindow::selectBaudRate(QString baudRate) -{ - if (serialPort.isOpen()) - { - if (!serialPort.setBaudRate(baudRate.toInt())) - { - qDebug() << "Set baud rate failed during select: " - << serialPort.error(); - } - else - { - qDebug() << "Baud rate changed: " << serialPort.baudRate(); - } - } -} - -void MainWindow::selectParity(int parity) -{ - if (serialPort.isOpen()) - { - if(!serialPort.setParity((QSerialPort::Parity) parity)) - { - qDebug() << "Set parity failed: " << serialPort.error(); - } - } -} - -void MainWindow::selectDataBits(int dataBits) -{ - if (serialPort.isOpen()) - { - if(!serialPort.setDataBits((QSerialPort::DataBits) dataBits)) - { - qDebug() << "Set data bits failed: " << serialPort.error(); - } - } -} - -void MainWindow::selectStopBits(int stopBits) -{ - if (serialPort.isOpen()) - { - if(!serialPort.setStopBits((QSerialPort::StopBits) stopBits)) - { - qDebug() << "Set stop bits failed: " << serialPort.error(); - } - } -} - -void MainWindow::selectFlowControl(int flowControl) -{ - if (serialPort.isOpen()) - { - if(!serialPort.setFlowControl((QSerialPort::FlowControl) flowControl)) - { - qDebug() << "Set flow control failed: " << serialPort.error(); - } - } -} - void MainWindow::onPortToggled(bool open) { - ui->pbOpenPort->setChecked(open); // make sure demo mode is disabled if (open && isDemoRunning()) enableDemo(false); ui->actionDemoMode->setEnabled(!open); @@ -459,9 +260,9 @@ void MainWindow::onPortError(QSerialPort if (serialPort.isOpen()) { qDebug() << "Closing port on resource error: " << serialPort.portName(); - togglePort(); + portControl.togglePort(); } - loadPortList(); + portControl.loadPortList(); break; default: qDebug() << "Unhandled port error: " << error; @@ -659,14 +460,14 @@ void MainWindow::selectNumberFormat(Numb QObject::disconnect(&(this->serialPort), &QSerialPort::readyRead, 0, 0); QObject::connect(&(this->serialPort), &QSerialPort::readyRead, this, &MainWindow::onDataReadyASCII); - ui->pbSkipByte->setDisabled(true); + portControl.enableSkipByte(); } else { QObject::disconnect(&(this->serialPort), &QSerialPort::readyRead, 0, 0); QObject::connect(&(this->serialPort), &QSerialPort::readyRead, this, &MainWindow::onDataReady); - ui->pbSkipByte->setEnabled(true); + portControl.enableSkipByte(false); } } diff --git a/mainwindow.h b/mainwindow.h --- a/mainwindow.h +++ b/mainwindow.h @@ -31,6 +31,7 @@ #include #include +#include "portcontrol.h" #include "ui_about_dialog.h" namespace Ui { @@ -60,13 +61,10 @@ private: Ui::MainWindow *ui; QButtonGroup numberFormatButtons; - QButtonGroup parityButtons; - QButtonGroup dataBitsButtons; - QButtonGroup stopBitsButtons; - QButtonGroup flowControlButtons; QDialog aboutDialog; void setupAboutDialog(); + PortControl portControl; QSerialPort serialPort; @@ -98,17 +96,7 @@ private: QColor makeColor(unsigned int channelIndex); private slots: - void loadPortList(); - void loadBaudRateList(); - void togglePort(); - void selectPort(QString portName); void onPortToggled(bool open); - void selectBaudRate(QString baudRate); - void selectParity(int parity); // parity must be one of QSerialPort::Parity - void selectDataBits(int dataBits); // bits must be one of QSerialPort::DataBits - void selectStopBits(int stopBits); // stopBits must be one of QSerialPort::StopBits - void selectFlowControl(int flowControl); // flowControl must be one of QSerialPort::FlowControl - void onDataReady(); // used with binary number formats void onDataReadyASCII(); // used with ASCII number format void onPortError(QSerialPort::SerialPortError error); @@ -127,9 +115,6 @@ private slots: void demoTimerTimeout(); void enableDemo(bool enabled); - -signals: - void portToggled(bool open); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui --- a/mainwindow.ui +++ b/mainwindow.ui @@ -51,296 +51,6 @@ false - - - Port - - - - - - - - - - Port: - - - - - - - - 0 - 0 - - - - true - - - - - - - Qt::ImhPreferNumbers - - - true - - - - - - - Baud Rate: - - - - - - - - 0 - 0 - - - - - 30 - 16777215 - - - - - - - - - - - - - - - QFrame::NoFrame - - - - - - No Parity - - - true - - - - - - - Odd Parity - - - - - - - Even Parity - - - - - - - Qt::Vertical - - - - 20 - 2 - - - - - - - - - - - - - - 8 bits - - - true - - - - - - - 7 bits - - - - - - - 6 bits - - - - - - - 5 bits - - - - - - - - - - - - - 1 Stop Bit - - - true - - - - - - - 2 Stop Bit - - - - - - - Qt::Vertical - - - - 20 - 2 - - - - - - - - - - - - - - No Flow Control - - - true - - - - - - - Hardware Control - - - - - - - Software Control - - - - - - - Qt::Vertical - - - - 20 - 2 - - - - - - - - - - - - - - - Qt::Horizontal - - - - 10000 - 20 - - - - - - - - - - - 0 - 50 - - - - Open - - - true - - - - - - - Skip Byte - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - Data Format @@ -684,11 +394,6 @@
qwt_plot.h
1 - - CustomCheckableButton - QPushButton -
customcheckablebutton.h
-
diff --git a/portcontrol.cpp b/portcontrol.cpp new file mode 100644 --- /dev/null +++ b/portcontrol.cpp @@ -0,0 +1,247 @@ +/* + Copyright © 2015 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + serialplot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with serialplot. If not, see . +*/ + +#include "portcontrol.h" +#include "ui_portcontrol.h" + +#include +#include +#include "utils.h" + +PortControl::PortControl(QSerialPort* port, QWidget* parent) : + QWidget(parent), + ui(new Ui::PortControl) +{ + ui->setupUi(this); + + serialPort = port; + + QObject::connect(ui->pbReloadPorts, &QPushButton::clicked, + this, &PortControl::loadPortList); + + QObject::connect(ui->pbOpenPort, &QPushButton::clicked, + this, &PortControl::togglePort); + + QObject::connect(ui->cbPortList, + SELECT::OVERLOAD_OF(&QComboBox::activated), + this, &PortControl::selectPort); + + QObject::connect(ui->cbBaudRate, + SELECT::OVERLOAD_OF(&QComboBox::activated), + this, &PortControl::selectBaudRate); + + // setup parity selection buttons + parityButtons.addButton(ui->rbNoParity, (int) QSerialPort::NoParity); + parityButtons.addButton(ui->rbEvenParity, (int) QSerialPort::EvenParity); + parityButtons.addButton(ui->rbOddParity, (int) QSerialPort::OddParity); + + QObject::connect(&parityButtons, + SELECT::OVERLOAD_OF(&QButtonGroup::buttonClicked), + this, &PortControl::selectParity); + + // setup data bits selection buttons + dataBitsButtons.addButton(ui->rb8Bits, (int) QSerialPort::Data8); + dataBitsButtons.addButton(ui->rb7Bits, (int) QSerialPort::Data7); + dataBitsButtons.addButton(ui->rb6Bits, (int) QSerialPort::Data6); + dataBitsButtons.addButton(ui->rb5Bits, (int) QSerialPort::Data5); + + QObject::connect(&dataBitsButtons, + SELECT::OVERLOAD_OF(&QButtonGroup::buttonClicked), + this, &PortControl::selectDataBits); + + // setup stop bits selection buttons + stopBitsButtons.addButton(ui->rb1StopBit, (int) QSerialPort::OneStop); + stopBitsButtons.addButton(ui->rb2StopBit, (int) QSerialPort::TwoStop); + + QObject::connect(&stopBitsButtons, + SELECT::OVERLOAD_OF(&QButtonGroup::buttonClicked), + this, &PortControl::selectStopBits); + + // setup flow control selection buttons + flowControlButtons.addButton(ui->rbNoFlowControl, + (int) QSerialPort::NoFlowControl); + flowControlButtons.addButton(ui->rbHardwareControl, + (int) QSerialPort::HardwareControl); + flowControlButtons.addButton(ui->rbSoftwareControl, + (int) QSerialPort::SoftwareControl); + + QObject::connect(&flowControlButtons, + SELECT::OVERLOAD_OF(&QButtonGroup::buttonClicked), + this, &PortControl::selectFlowControl); + + // init skip byte button + QObject::connect(ui->pbSkipByte, &QPushButton::clicked, + [this](){emit skipByteRequested();}); + + loadPortList(); + loadBaudRateList(); + ui->cbBaudRate->setCurrentIndex(ui->cbBaudRate->findText("9600")); +} + +PortControl::~PortControl() +{ + delete ui; +} + +void PortControl::loadPortList() +{ + QString currentSelection = ui->cbPortList->currentText(); + + ui->cbPortList->clear(); + + for (auto port : QSerialPortInfo::availablePorts()) + { + ui->cbPortList->addItem(port.portName()); + } + + // find current selection in the new list, maybe it doesn't exist anymore? + int currentSelectionIndex = ui->cbPortList->findText(currentSelection); + if (currentSelectionIndex >= 0) + { + ui->cbPortList->setCurrentIndex(currentSelectionIndex); + } + else // our port doesn't exist anymore, close port if it's open + { + if (serialPort->isOpen()) togglePort(); + } +} + +void PortControl::loadBaudRateList() +{ + ui->cbBaudRate->clear(); + + for (auto baudRate : QSerialPortInfo::standardBaudRates()) + { + ui->cbBaudRate->addItem(QString::number(baudRate)); + } +} + +void PortControl::selectBaudRate(QString baudRate) +{ + if (serialPort->isOpen()) + { + if (!serialPort->setBaudRate(baudRate.toInt())) + { + qDebug() << "Set baud rate failed during select: " + << serialPort->error(); + } + else + { + qDebug() << "Baud rate changed: " << serialPort->baudRate(); + } + } +} + +void PortControl::selectParity(int parity) +{ + if (serialPort->isOpen()) + { + if(!serialPort->setParity((QSerialPort::Parity) parity)) + { + qDebug() << "Set parity failed: " << serialPort->error(); + } + } +} + +void PortControl::selectDataBits(int dataBits) +{ + if (serialPort->isOpen()) + { + if(!serialPort->setDataBits((QSerialPort::DataBits) dataBits)) + { + qDebug() << "Set data bits failed: " << serialPort->error(); + } + } +} + +void PortControl::selectStopBits(int stopBits) +{ + if (serialPort->isOpen()) + { + if(!serialPort->setStopBits((QSerialPort::StopBits) stopBits)) + { + qDebug() << "Set stop bits failed: " << serialPort->error(); + } + } +} + +void PortControl::selectFlowControl(int flowControl) +{ + if (serialPort->isOpen()) + { + if(!serialPort->setFlowControl((QSerialPort::FlowControl) flowControl)) + { + qDebug() << "Set flow control failed: " << serialPort->error(); + } + } +} + +void PortControl::togglePort() +{ + if (serialPort->isOpen()) + { + serialPort->close(); + qDebug() << "Port closed, " << serialPort->portName(); + emit portToggled(false); + } + else + { + serialPort->setPortName(ui->cbPortList->currentText()); + + // open port + if (serialPort->open(QIODevice::ReadWrite)) + { + qDebug() << "Port opened, " << serialPort->portName(); + emit portToggled(true); + + // set baud rate + if (!serialPort->setBaudRate(ui->cbBaudRate->currentText().toInt())) + { + qDebug() << "Set baud rate failed during port opening: " + << serialPort->error(); + } + } + else + { + qDebug() << "Port open error: " << serialPort->error(); + } + } + ui->pbOpenPort->setChecked(serialPort->isOpen()); +} + +void PortControl::selectPort(QString portName) +{ + // has selection actually changed + if (portName != serialPort->portName()) + { + // if another port is already open, close it by toggling + if (serialPort->isOpen()) + { + togglePort(); + + // open new selection by toggling + togglePort(); + } + } +} + +void PortControl::enableSkipByte(bool enabled) +{ + ui->pbSkipByte->setDisabled(enabled); +} diff --git a/portcontrol.h b/portcontrol.h new file mode 100644 --- /dev/null +++ b/portcontrol.h @@ -0,0 +1,67 @@ +/* + Copyright © 2015 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + serialplot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with serialplot. If not, see . +*/ + +#ifndef PORTCONTROL_H +#define PORTCONTROL_H + +#include +#include +#include + +namespace Ui { +class PortControl; +} + +class PortControl : public QWidget +{ + Q_OBJECT + +public: + explicit PortControl(QSerialPort* port, QWidget* parent = 0); + ~PortControl(); + + QSerialPort* serialPort; + +private: + Ui::PortControl *ui; + + QButtonGroup parityButtons; + QButtonGroup dataBitsButtons; + QButtonGroup stopBitsButtons; + QButtonGroup flowControlButtons; + +public slots: + void loadPortList(); + void loadBaudRateList(); + void togglePort(); + void selectPort(QString portName); + void enableSkipByte(bool enabled = true); + + void selectBaudRate(QString baudRate); + void selectParity(int parity); // parity must be one of QSerialPort::Parity + void selectDataBits(int dataBits); // bits must be one of QSerialPort::DataBits + void selectStopBits(int stopBits); // stopBits must be one of QSerialPort::StopBits + void selectFlowControl(int flowControl); // flowControl must be one of QSerialPort::FlowControl + +signals: + void skipByteRequested(); + void portToggled(bool open); +}; + +#endif // PORTCONTROL_H diff --git a/portcontrol.ui b/portcontrol.ui new file mode 100644 --- /dev/null +++ b/portcontrol.ui @@ -0,0 +1,324 @@ + + + PortControl + + + + 0 + 0 + 631 + 213 + + + + Form + + + + + + + + + + Port: + + + + + + + + 0 + 0 + + + + true + + + + + + + Qt::ImhPreferNumbers + + + true + + + + + + + Baud Rate: + + + + + + + + 0 + 0 + + + + + 30 + 16777215 + + + + + + + + + + + + + + + QFrame::NoFrame + + + + + + No Parity + + + true + + + + + + + Odd Parity + + + + + + + Even Parity + + + + + + + Qt::Vertical + + + + 20 + 2 + + + + + + + + + + + + + + 8 bits + + + true + + + + + + + 7 bits + + + + + + + 6 bits + + + + + + + 5 bits + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + + + 1 Stop Bit + + + true + + + + + + + 2 Stop Bit + + + + + + + Qt::Vertical + + + + 20 + 2 + + + + + + + + + + + + + + No Flow Control + + + true + + + + + + + Hardware Control + + + + + + + Software Control + + + + + + + Qt::Vertical + + + + 20 + 2 + + + + + + + + + + + + + + + Qt::Horizontal + + + + 10000 + 20 + + + + + + + + + + + 0 + 50 + + + + Open + + + true + + + + + + + Skip Byte + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + CustomCheckableButton + QPushButton +
customcheckablebutton.h
+
+
+ + +
diff --git a/serialplot.pro b/serialplot.pro --- a/serialplot.pro +++ b/serialplot.pro @@ -35,14 +35,17 @@ CONFIG += qwt SOURCES += main.cpp\ mainwindow.cpp \ - customcheckablebutton.cpp + customcheckablebutton.cpp \ + portcontrol.cpp HEADERS += mainwindow.h \ utils.h \ - customcheckablebutton.h + customcheckablebutton.h \ + portcontrol.h FORMS += mainwindow.ui \ - about_dialog.ui + about_dialog.ui \ + portcontrol.ui INCLUDEPATH += qmake/