From 0982955739c6b98b9dbe63efb6575cbf01aee1ec Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Fri, 21 Aug 2015 01:03:47 +0300 Subject: Refactor the UI of CAN example application * The SettingsDialog class is added * The menu-bar actions and icons are added * All widgets are wrapped with layouts * A code related to device accessing is modified Change-Id: I45d056951163441195f61df411669d1130a41867 Reviewed-by: Alex Blasche --- examples/serialbus/can/can.pro | 11 +- examples/serialbus/can/can.qrc | 9 + examples/serialbus/can/images/application-exit.png | Bin 0 -> 11200 bytes examples/serialbus/can/images/clear.png | Bin 0 -> 12543 bytes examples/serialbus/can/images/connect.png | Bin 0 -> 15374 bytes examples/serialbus/can/images/disconnect.png | Bin 0 -> 15092 bytes examples/serialbus/can/images/settings.png | Bin 0 -> 16039 bytes examples/serialbus/can/mainwindow.cpp | 180 ++++---- examples/serialbus/can/mainwindow.h | 32 +- examples/serialbus/can/mainwindow.ui | 474 +++++++++------------ examples/serialbus/can/settingsdialog.cpp | 93 ++++ examples/serialbus/can/settingsdialog.h | 89 ++++ examples/serialbus/can/settingsdialog.ui | 145 +++++++ 13 files changed, 667 insertions(+), 366 deletions(-) create mode 100644 examples/serialbus/can/can.qrc create mode 100644 examples/serialbus/can/images/application-exit.png create mode 100644 examples/serialbus/can/images/clear.png create mode 100644 examples/serialbus/can/images/connect.png create mode 100644 examples/serialbus/can/images/disconnect.png create mode 100644 examples/serialbus/can/images/settings.png create mode 100644 examples/serialbus/can/settingsdialog.cpp create mode 100644 examples/serialbus/can/settingsdialog.h create mode 100644 examples/serialbus/can/settingsdialog.ui (limited to 'examples/serialbus/can') diff --git a/examples/serialbus/can/can.pro b/examples/serialbus/can/can.pro index 3e85168..a5e2b19 100644 --- a/examples/serialbus/can/can.pro +++ b/examples/serialbus/can/can.pro @@ -4,8 +4,13 @@ TARGET = can TEMPLATE = app SOURCES += main.cpp \ - mainwindow.cpp + mainwindow.cpp \ + settingsdialog.cpp -HEADERS += mainwindow.h +HEADERS += mainwindow.h \ + settingsdialog.h -FORMS += mainwindow.ui +FORMS += mainwindow.ui \ + settingsdialog.ui + +RESOURCES += can.qrc diff --git a/examples/serialbus/can/can.qrc b/examples/serialbus/can/can.qrc new file mode 100644 index 0000000..0b49879 --- /dev/null +++ b/examples/serialbus/can/can.qrc @@ -0,0 +1,9 @@ + + + images/connect.png + images/disconnect.png + images/application-exit.png + images/settings.png + images/clear.png + + diff --git a/examples/serialbus/can/images/application-exit.png b/examples/serialbus/can/images/application-exit.png new file mode 100644 index 0000000..32be6b3 Binary files /dev/null and b/examples/serialbus/can/images/application-exit.png differ diff --git a/examples/serialbus/can/images/clear.png b/examples/serialbus/can/images/clear.png new file mode 100644 index 0000000..aa612f1 Binary files /dev/null and b/examples/serialbus/can/images/clear.png differ diff --git a/examples/serialbus/can/images/connect.png b/examples/serialbus/can/images/connect.png new file mode 100644 index 0000000..dd5a51e Binary files /dev/null and b/examples/serialbus/can/images/connect.png differ diff --git a/examples/serialbus/can/images/disconnect.png b/examples/serialbus/can/images/disconnect.png new file mode 100644 index 0000000..fd58f7a Binary files /dev/null and b/examples/serialbus/can/images/disconnect.png differ diff --git a/examples/serialbus/can/images/settings.png b/examples/serialbus/can/images/settings.png new file mode 100644 index 0000000..3d1042e Binary files /dev/null and b/examples/serialbus/can/images/settings.png differ diff --git a/examples/serialbus/can/mainwindow.cpp b/examples/serialbus/can/mainwindow.cpp index 2320c30..8ea4701 100644 --- a/examples/serialbus/can/mainwindow.cpp +++ b/examples/serialbus/can/mainwindow.cpp @@ -40,9 +40,10 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include "settingsdialog.h" -#include #include +#include #include #include @@ -50,53 +51,49 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - deviceName(QStringLiteral("")), - ui(new Ui::MainWindow) + m_ui(new Ui::MainWindow), + m_canDevice(Q_NULLPTR) { - ui->setupUi(this); - init(); -} + m_ui->setupUi(this); -MainWindow::~MainWindow() -{ - delete ui; -} + m_settings = new SettingsDialog; + m_status = new QLabel; + m_ui->statusBar->addWidget(m_status); -void MainWindow::init() -{ - QPointer canBus = QCanBus::instance(); + m_ui->sendMessagesBox->setEnabled(false); + + initActionsConnections(); - plugins = canBus->plugins(); - for (int i = 0; i < plugins.size(); i++) - ui->pluginBox->insertItem(i, plugins.at(i)); - connectDevice(ui->pluginBox->currentIndex()); + connect(m_ui->sendButton, &QPushButton::clicked, this, &MainWindow::sendMessage); } -void MainWindow::connectDevice(int pluginIndex) +MainWindow::~MainWindow() { - QPointer canBus = QCanBus::instance(); - - canDevice = canBus->createDevice(plugins.at(pluginIndex), deviceName); - - if (canDevice.isNull()) - return; - connect(canDevice.data(), &QCanBusDevice::errorOccurred, this, &MainWindow::receiveError); + if (m_canDevice) + delete m_canDevice; - if (!canDevice->connectDevice()) { - canDevice.clear(); - return; - } - canDevice->setConfigurationParameter(QCanBusDevice::ReceiveOwnKey, QVariant(true)); - canDevice->setConfigurationParameter( - QCanBusDevice::ErrorFilterKey, - QVariant::fromValue(QCanBusFrame::FrameErrors(QCanBusFrame::AnyError))); - connect(canDevice.data(), &QCanBusDevice::frameReceived, - this, &MainWindow::checkMessages); + delete m_settings; + delete m_ui; +} - ui->deviceLabel->setText("Connected to: " + deviceName); +void MainWindow::showStatusMessage(const QString &message) +{ + m_status->setText(message); +} - checkMessages(); +void MainWindow::initActionsConnections() +{ + m_ui->actionConnect->setEnabled(true); + m_ui->actionDisconnect->setEnabled(false); + m_ui->actionQuit->setEnabled(true); + m_ui->actionConfigure->setEnabled(true); + + connect(m_ui->actionConnect, &QAction::triggered, this, &MainWindow::connectDevice); + connect(m_ui->actionDisconnect, &QAction::triggered, this, &MainWindow::disconnectDevice); + connect(m_ui->actionQuit, SIGNAL(triggered()), this, SLOT(close())); + connect(m_ui->actionConfigure, &QAction::triggered, m_settings, &SettingsDialog::show); + connect(m_ui->actionAboutQt, &QAction::triggered, qApp, &QApplication::aboutQt); } void MainWindow::receiveError(QCanBusDevice::CanBusError error) const @@ -107,18 +104,71 @@ void MainWindow::receiveError(QCanBusDevice::CanBusError error) const case QCanBusDevice::ConnectionError: case QCanBusDevice::ConfigurationError: case QCanBusDevice::UnknownError: - qWarning() << canDevice->errorString(); + qWarning() << m_canDevice->errorString(); default: break; } } +void MainWindow::connectDevice() +{ + const SettingsDialog::Settings p = m_settings->settings(); + + m_canDevice = QCanBus::instance()->createDevice(p.backendName.toLocal8Bit(), p.deviceInterfaceName); + if (!m_canDevice) { + showStatusMessage(tr("Connection error")); + return; + } + + connect(m_canDevice, &QCanBusDevice::errorOccurred, this, &MainWindow::receiveError); + connect(m_canDevice, &QCanBusDevice::frameReceived, this, &MainWindow::checkMessages); + + if (p.useConfigurationEnabled) { + foreach (const SettingsDialog::ConfigurationItem &item, p.configurations) + m_canDevice->setConfigurationParameter(item.first, item.second); + } + + if (!m_canDevice->connectDevice()) { + delete m_canDevice; + m_canDevice = Q_NULLPTR; + + showStatusMessage(tr("Connection error")); + } else { + m_ui->actionConnect->setEnabled(false); + m_ui->actionDisconnect->setEnabled(true); + m_ui->actionConfigure->setEnabled(false); + + m_ui->sendMessagesBox->setEnabled(true); + + showStatusMessage(tr("Backend: %1, Connected to: %2") + .arg(p.backendName).arg(p.deviceInterfaceName)); + } +} + +void MainWindow::disconnectDevice() +{ + if (!m_canDevice) + return; + + m_canDevice->disconnectDevice(); + delete m_canDevice; + m_canDevice = Q_NULLPTR; + + m_ui->actionConnect->setEnabled(true); + m_ui->actionDisconnect->setEnabled(false); + m_ui->actionConfigure->setEnabled(true); + + m_ui->sendMessagesBox->setEnabled(false); + + showStatusMessage(tr("Disconnected")); +} + void MainWindow::checkMessages() { - if (canDevice.isNull()) + if (!m_canDevice) return; - const QCanBusFrame frame = canDevice->readFrame(); + const QCanBusFrame frame = m_canDevice->readFrame(); if (frame.payload().isEmpty()) return; @@ -140,68 +190,52 @@ void MainWindow::checkMessages() } if (frame.frameType() == QCanBusFrame::RemoteRequestFrame) { - ui->requestList->addItem(view); + m_ui->requestList->addItem(view); } else if (frame.frameType() == QCanBusFrame::ErrorFrame) { - ui->errorList->addItem(view); + m_ui->errorList->addItem(view); } else { - ui->listWidget->addItem(view); + m_ui->listWidget->addItem(view); } } -void MainWindow::on_sendButton_clicked() const +void MainWindow::sendMessage() const { - if (canDevice.isNull()) + if (!m_canDevice) return; - QByteArray writings = ui->lineEdit->displayText().toUtf8(); - ui->lineEdit->clear(); + QByteArray writings = m_ui->lineEdit->displayText().toUtf8(); + m_ui->lineEdit->clear(); QCanBusFrame frame; - const int maxPayload = ui->fdBox->checkState() ? 64 : 8; + const int maxPayload = m_ui->fdBox->checkState() ? 64 : 8; int size = writings.size(); if (size > maxPayload) size = maxPayload; writings = writings.left(size); frame.setPayload(writings); - qint32 id = ui->idEdit->displayText().toInt(); - ui->idEdit->clear(); - if (!ui->EFF->checkState() && id > 2047) //11 bits + qint32 id = m_ui->idEdit->displayText().toInt(); + m_ui->idEdit->clear(); + if (!m_ui->EFF->checkState() && id > 2047) //11 bits id = 2047; frame.setFrameId(id); - frame.setExtendedFrameFormat(ui->EFF->checkState()); + frame.setExtendedFrameFormat(m_ui->EFF->checkState()); - if (ui->remoteFrame->isChecked()) + if (m_ui->remoteFrame->isChecked()) frame.setFrameType(QCanBusFrame::RemoteRequestFrame); - else if (ui->errorFrame->isChecked()) + else if (m_ui->errorFrame->isChecked()) frame.setFrameType(QCanBusFrame::ErrorFrame); else frame.setFrameType(QCanBusFrame::DataFrame); - canDevice->writeFrame(frame); -} - -void MainWindow::on_connectButton_clicked() -{ - canDevice.clear(); - deviceName = ui->deviceEdit->text(); - ui->deviceEdit->clear(); - ui->deviceLabel->setText("Connected to:"); - connectDevice(ui->pluginBox->currentIndex()); -} - -void MainWindow::on_pluginBox_activated(int index) -{ - canDevice.clear(); - ui->deviceLabel->setText("Connected to:"); - connectDevice(index); + m_canDevice->writeFrame(frame); } void MainWindow::interpretError(QString &view, const QCanBusFrame &frame) { - if (canDevice.isNull()) + if (!m_canDevice) return; - view = canDevice->interpretErrorFrame(frame); + view = m_canDevice->interpretErrorFrame(frame); } diff --git a/examples/serialbus/can/mainwindow.h b/examples/serialbus/can/mainwindow.h index 876c679..fe7e63c 100644 --- a/examples/serialbus/can/mainwindow.h +++ b/examples/serialbus/can/mainwindow.h @@ -41,17 +41,17 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include +#include // for CanBusError #include -#include QT_BEGIN_NAMESPACE -class QSerialBusBackend; -class QCanBus; -class QSerialBusDevice; +class QLabel; + class QCanBusFrame; +class SettingsDialog; + namespace Ui { class MainWindow; } @@ -68,23 +68,21 @@ public: ~MainWindow(); private Q_SLOTS: - void checkMessages(); - void on_sendButton_clicked() const; + void sendMessage() const; void receiveError(QCanBusDevice::CanBusError) const; - void on_connectButton_clicked(); - void on_pluginBox_activated(int index); + void connectDevice(); + void disconnectDevice(); private: - void init(); - void connectDevice(int pluginIndex); - void interpretError(QString&, const QCanBusFrame&); - - QPointer canDevice; - QString deviceName; - QList plugins; + void showStatusMessage(const QString &message); + void initActionsConnections(); + void interpretError(QString &, const QCanBusFrame &); - Ui::MainWindow *ui; + Ui::MainWindow *m_ui; + QLabel *m_status; + SettingsDialog *m_settings; + QCanBusDevice *m_canDevice; }; #endif // MAINWINDOW_H diff --git a/examples/serialbus/can/mainwindow.ui b/examples/serialbus/can/mainwindow.ui index fe32841..19d8188 100644 --- a/examples/serialbus/can/mainwindow.ui +++ b/examples/serialbus/can/mainwindow.ui @@ -6,292 +6,174 @@ 0 0 - 1254 - 852 + 551 + 481 MainWindow - - - - 180 - 40 - 191 - 27 - - - - - - - 190 - 130 - 191 - 61 - - - - send - - - - - - 20 - 250 - 391 - 541 - - - - - - - 40 - 40 - 113 - 27 - - - - - - - 40 - 20 - 67 - 17 - - - - Id - - - - - - 190 - 20 - 67 - 17 - - - - Data - - - - - - 320 - 100 - 97 - 22 - - - - EFF - - - - - - 420 - 250 - 411 - 541 - - - - - - - 840 - 250 - 401 - 541 - - - - - - - 20 - 230 - 67 - 17 - - - - Messages - - - - - - 430 - 227 - 67 - 20 - - - - Requests - - - - - - 860 - 230 - 67 - 17 - - - - Errors - - - - - - 190 - 100 - 161 - 22 - - - - Flexible Data-Rate - - - - - - 530 - 130 - 131 - 61 - - - - Connect - - - - - - 530 - 40 - 231 - 27 - - - - - - - 530 - 20 - 67 - 17 - - - - Device - - - - - - 530 - 70 - 441 - 17 - - - - Connected to: - - - - - - 960 - 40 - 241 - 27 - - - - - - - 960 - 20 - 67 - 17 - - - - Plugin - - - - - - 40 - 80 - 141 - 111 - - - - Frame Type - - - false - - - - - - Data - - - true - - - - - - - Remote Request - - - - - - - Error - - - - - + + + + + Send CAN message + + + + + + + + Id + + + + + + + + + + Frame Type + + + false + + + + + + Data + + + true + + + + + + + Re&mote Request + + + + + + + Error + + + + + + + + + + Flexible Data-Rate + + + + + + + EFF + + + + + + + send + + + + + + + + + + Data + + + + + + + + + + + + + + Messages + + + + + + + Requests + + + + + + + Errors + + + + + + + + + + + + + + + 0 0 - 1254 - 20 + 551 + 23 + + + &Calls + + + + + + + + + Too&ls + + + + + + Help + + + + + + @@ -300,10 +182,56 @@ false + + + + + + + :/images/connect.png:/images/connect.png + + + &Connect + + + + + + :/images/disconnect.png:/images/disconnect.png + + + &Disconnect + + + + + + :/images/application-exit.png:/images/application-exit.png + + + &Quit + + + + + + :/images/settings.png:/images/settings.png + + + &Configure + + + + + &About Qt + + - + + + diff --git a/examples/serialbus/can/settingsdialog.cpp b/examples/serialbus/can/settingsdialog.cpp new file mode 100644 index 0000000..f082689 --- /dev/null +++ b/examples/serialbus/can/settingsdialog.cpp @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the QtSerialBus module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "settingsdialog.h" +#include "ui_settingsdialog.h" + +#include + +QT_USE_NAMESPACE + +SettingsDialog::SettingsDialog(QWidget *parent) : + QDialog(parent), + m_ui(new Ui::SettingsDialog) +{ + m_ui->setupUi(this); + + connect(m_ui->applyButton, &QPushButton::clicked, this, &SettingsDialog::apply); + connect(m_ui->useConfigurationBox, &QCheckBox::clicked, m_ui->configurationBox, &QGroupBox::setEnabled); + + fillBackends(); + + updateSettings(); +} + +SettingsDialog::~SettingsDialog() +{ + delete m_ui; +} + +SettingsDialog::Settings SettingsDialog::settings() const +{ + return m_currentSettings; +} + +void SettingsDialog::apply() +{ + updateSettings(); + hide(); +} + +void SettingsDialog::updateSettings() +{ + m_currentSettings.backendName = m_ui->backendListBox->currentText(); + m_currentSettings.deviceInterfaceName = m_ui->interfaceNameEdit->text(); + m_currentSettings.useConfigurationEnabled = m_ui->useConfigurationBox->isChecked(); + + if (m_currentSettings.useConfigurationEnabled) { + // FIXME: update configuration + } +} + +void SettingsDialog::fillBackends() +{ + foreach (const QByteArray &backend, QCanBus::instance()->plugins()) + m_ui->backendListBox->addItem(backend); +} diff --git a/examples/serialbus/can/settingsdialog.h b/examples/serialbus/can/settingsdialog.h new file mode 100644 index 0000000..0b62b3a --- /dev/null +++ b/examples/serialbus/can/settingsdialog.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the QtSerialBus module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SETTINGSDIALOG_H +#define SETTINGSDIALOG_H + +#include + +#include + +QT_BEGIN_NAMESPACE + +namespace Ui { +class SettingsDialog; +} + +QT_END_NAMESPACE + +QT_USE_NAMESPACE + +class SettingsDialog : public QDialog +{ + Q_OBJECT + +public: + typedef QPair ConfigurationItem; + + struct Settings { + QString backendName; + QString deviceInterfaceName; + QList configurations; + bool useConfigurationEnabled; + }; + + explicit SettingsDialog(QWidget *parent = 0); + ~SettingsDialog(); + + Settings settings() const; + +private slots: + void apply(); + +private: + void updateSettings(); + void fillBackends(); + +private: + Ui::SettingsDialog *m_ui; + Settings m_currentSettings; +}; + +#endif // SETTINGSDIALOG_H diff --git a/examples/serialbus/can/settingsdialog.ui b/examples/serialbus/can/settingsdialog.ui new file mode 100644 index 0000000..deebda0 --- /dev/null +++ b/examples/serialbus/can/settingsdialog.ui @@ -0,0 +1,145 @@ + + + SettingsDialog + + + + 0 + 0 + 406 + 246 + + + + Settings + + + + + + Select CAN backend + + + + + + + + + + + + false + + + Specify Configuration + + + + + + RAW Filter + + + + + + + + + + Error Filter + + + + + + + + + + Loopback + + + + + + + + + + Receive Own + + + + + + + + + + false + + + Speed + + + + + + + false + + + + + + + + + + Specify CAN interface name + + + + + + + + + + + + Use configuration + + + + + + + + + Qt::Horizontal + + + + 96 + 20 + + + + + + + + Apply + + + + + + + + + + -- cgit v1.2.3