From 9e3af4a3a828a0c632ef07969005f46fd30bee63 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Thu, 28 Apr 2016 09:02:51 +0200 Subject: CAN-Example: Change SettingsDialog to ConnectDialog This simplifies the handling and makes it more like how other CAN monitors behave. * After startup, the connect dialog is shown * If the dialog is accepted, the selected device is connected instantly * To interrupt a connection, the disconnect action can be used * To connect again, the action connect can be used Change-Id: I47db596658e90cc0393981fba135cd6d89191f1a Reviewed-by: Alex Blasche --- examples/serialbus/can/can.pro | 6 +- examples/serialbus/can/can.qrc | 1 - examples/serialbus/can/connectdialog.cpp | 244 +++++++++++++++++++++++++++++ examples/serialbus/can/connectdialog.h | 95 +++++++++++ examples/serialbus/can/connectdialog.ui | 175 +++++++++++++++++++++ examples/serialbus/can/images/settings.png | Bin 16039 -> 0 bytes examples/serialbus/can/mainwindow.cpp | 19 ++- examples/serialbus/can/mainwindow.h | 4 +- examples/serialbus/can/mainwindow.ui | 19 +-- examples/serialbus/can/settingsdialog.cpp | 192 ----------------------- examples/serialbus/can/settingsdialog.h | 92 ----------- examples/serialbus/can/settingsdialog.ui | 170 -------------------- src/serialbus/doc/images/can-example.png | Bin 44567 -> 14937 bytes src/serialbus/doc/src/examples/can.qdoc | 4 +- 14 files changed, 531 insertions(+), 490 deletions(-) create mode 100644 examples/serialbus/can/connectdialog.cpp create mode 100644 examples/serialbus/can/connectdialog.h create mode 100644 examples/serialbus/can/connectdialog.ui delete mode 100644 examples/serialbus/can/images/settings.png delete mode 100644 examples/serialbus/can/settingsdialog.cpp delete mode 100644 examples/serialbus/can/settingsdialog.h delete mode 100644 examples/serialbus/can/settingsdialog.ui diff --git a/examples/serialbus/can/can.pro b/examples/serialbus/can/can.pro index bf22fc5..aa2234f 100644 --- a/examples/serialbus/can/can.pro +++ b/examples/serialbus/can/can.pro @@ -5,13 +5,13 @@ TEMPLATE = app SOURCES += main.cpp \ mainwindow.cpp \ - settingsdialog.cpp + connectdialog.cpp HEADERS += mainwindow.h \ - settingsdialog.h + connectdialog.h FORMS += mainwindow.ui \ - settingsdialog.ui + connectdialog.ui RESOURCES += can.qrc diff --git a/examples/serialbus/can/can.qrc b/examples/serialbus/can/can.qrc index 0b49879..1b8d697 100644 --- a/examples/serialbus/can/can.qrc +++ b/examples/serialbus/can/can.qrc @@ -3,7 +3,6 @@ images/connect.png images/disconnect.png images/application-exit.png - images/settings.png images/clear.png diff --git a/examples/serialbus/can/connectdialog.cpp b/examples/serialbus/can/connectdialog.cpp new file mode 100644 index 0000000..7495832 --- /dev/null +++ b/examples/serialbus/can/connectdialog.cpp @@ -0,0 +1,244 @@ +/**************************************************************************** +** +** 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 "connectdialog.h" +#include "ui_connectdialog.h" + +#include +#include + +ConnectDialog::ConnectDialog(QWidget *parent) : + QDialog(parent), + m_ui(new Ui::ConnectDialog), + m_customSpeedValidator(0) +{ + m_ui->setupUi(this); + + m_customSpeedValidator = new QIntValidator(0, 1000000, this); + m_ui->errorFilterEdit->setValidator(new QIntValidator(0, 0x1FFFFFFFU, this)); + + m_ui->loopbackBox->addItem(tr("unspecified"), QVariant()); + m_ui->loopbackBox->addItem(tr("false"), QVariant(false)); + m_ui->loopbackBox->addItem(tr("true"), QVariant(true)); + + m_ui->receiveOwnBox->addItem(tr("unspecified"), QVariant()); + m_ui->receiveOwnBox->addItem(tr("false"), QVariant(false)); + m_ui->receiveOwnBox->addItem(tr("true"), QVariant(true)); + + m_ui->canFdBox->addItem(tr("false"), QVariant(false)); + m_ui->canFdBox->addItem(tr("true"), QVariant(true)); + + connect(m_ui->okButton, &QPushButton::clicked, this, &ConnectDialog::ok); + connect(m_ui->cancelButton, &QPushButton::clicked, this, &ConnectDialog::cancel); + connect(m_ui->useConfigurationBox, &QCheckBox::clicked, m_ui->configurationBox, &QGroupBox::setEnabled); + + connect(m_ui->speedBox, static_cast(&QComboBox::currentIndexChanged), + this, &ConnectDialog::checkCustomSpeedPolicy); + + m_ui->rawFilterEdit->hide(); + m_ui->rawFilterLabel->hide(); + + fillBackends(); + fillSpeeds(); + + updateSettings(); +} + +ConnectDialog::~ConnectDialog() +{ + delete m_ui; +} + +ConnectDialog::Settings ConnectDialog::settings() const +{ + return m_currentSettings; +} + +void ConnectDialog::checkCustomSpeedPolicy(int idx) +{ + const bool isCustomSpeed = !m_ui->speedBox->itemData(idx).isValid(); + m_ui->speedBox->setEditable(isCustomSpeed); + if (isCustomSpeed) { + m_ui->speedBox->clearEditText(); + QLineEdit *edit = m_ui->speedBox->lineEdit(); + edit->setValidator(m_customSpeedValidator); + } +} + +void ConnectDialog::ok() +{ + updateSettings(); + accept(); +} + +void ConnectDialog::cancel() +{ + revertSettings(); + reject(); +} + +QString ConnectDialog::configurationValue(QCanBusDevice::ConfigurationKey key) +{ + QVariant result; + + foreach (const ConfigurationItem &item, m_currentSettings.configurations) { + if (item.first == key) { + result = item.second; + break; + } + } + + if (result.isNull() && ( + key == QCanBusDevice::LoopbackKey || + key == QCanBusDevice::ReceiveOwnKey)) { + return tr("unspecified"); + } + + return result.toString(); +} + +void ConnectDialog::revertSettings() +{ + m_ui->backendListBox->setCurrentText(m_currentSettings.backendName); + m_ui->interfaceNameEdit->setText(m_currentSettings.deviceInterfaceName); + m_ui->useConfigurationBox->setChecked(m_currentSettings.useConfigurationEnabled); + + QString value = configurationValue(QCanBusDevice::LoopbackKey); + m_ui->loopbackBox->setCurrentText(value); + + value = configurationValue(QCanBusDevice::ReceiveOwnKey); + m_ui->receiveOwnBox->setCurrentText(value); + + value = configurationValue(QCanBusDevice::ErrorFilterKey); + m_ui->errorFilterEdit->setText(value); + + value = configurationValue(QCanBusDevice::BitRateKey); + m_ui->speedBox->setCurrentText(value); + + value = configurationValue(QCanBusDevice::CanFdKey); + m_ui->canFdBox->setCurrentText(value); +} + +void ConnectDialog::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) { + m_currentSettings.configurations.clear(); + // process LoopBack + if (m_ui->loopbackBox->currentIndex() != 0) { + ConfigurationItem item; + item.first = QCanBusDevice::LoopbackKey; + item.second = m_ui->loopbackBox->currentData(); + m_currentSettings.configurations.append(item); + } + + // process ReceiveOwnKey + if (m_ui->receiveOwnBox->currentIndex() != 0) { + ConfigurationItem item; + item.first = QCanBusDevice::ReceiveOwnKey; + item.second = m_ui->receiveOwnBox->currentData(); + m_currentSettings.configurations.append(item); + } + + // process error filter + if (!m_ui->errorFilterEdit->text().isEmpty()) { + QString value = m_ui->errorFilterEdit->text(); + bool ok = false; + int dec = value.toInt(&ok); + if (ok) { + ConfigurationItem item; + item.first = QCanBusDevice::ErrorFilterKey; + item.second = QVariant::fromValue(QCanBusFrame::FrameErrors(dec)); + m_currentSettings.configurations.append(item); + } + } + + // process raw filter list + if (!m_ui->rawFilterEdit->text().isEmpty()) { + //TODO current ui not sfficient to reflect this param + } + + // process bitrate + bool ok = false; + int bitrate = 0; + if (m_ui->speedBox->currentIndex() == (m_ui->speedBox->count() - 1)) + bitrate = m_ui->speedBox->currentText().toInt(&ok); + else + bitrate = m_ui->speedBox->itemData(m_ui->speedBox->currentIndex()).toInt(&ok); + + if (ok && (bitrate > 0)) { + ConfigurationItem item; + item.first = QCanBusDevice::BitRateKey; + item.second = QVariant(bitrate); + m_currentSettings.configurations.append(item); + } + + // process CAN FD setting + ConfigurationItem fdItem; + fdItem.first = QCanBusDevice::CanFdKey; + fdItem.second = m_ui->canFdBox->currentData(); + m_currentSettings.configurations.append(fdItem); + } +} + +void ConnectDialog::fillBackends() +{ + foreach (const QByteArray &backend, QCanBus::instance()->plugins()) + m_ui->backendListBox->addItem(backend); +} + +void ConnectDialog::fillSpeeds() +{ + m_ui->speedBox->addItem(QStringLiteral("10000"), 10000); + m_ui->speedBox->addItem(QStringLiteral("20000"), 20000); + m_ui->speedBox->addItem(QStringLiteral("50000"), 50000); + m_ui->speedBox->addItem(QStringLiteral("100000"), 100000); + m_ui->speedBox->addItem(QStringLiteral("125000"), 125000); + m_ui->speedBox->addItem(QStringLiteral("250000"), 250000); + m_ui->speedBox->addItem(QStringLiteral("500000"), 500000); + m_ui->speedBox->addItem(QStringLiteral("800000"), 800000); + m_ui->speedBox->addItem(QStringLiteral("1000000"), 1000000); + m_ui->speedBox->addItem(tr("Custom")); + + m_ui->speedBox->setCurrentIndex(6); // setup 500000 bits/sec by default +} diff --git a/examples/serialbus/can/connectdialog.h b/examples/serialbus/can/connectdialog.h new file mode 100644 index 0000000..7a46c86 --- /dev/null +++ b/examples/serialbus/can/connectdialog.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** 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 CONNECTDIALOG_H +#define CONNECTDIALOG_H + +#include + +#include + +QT_BEGIN_NAMESPACE + +namespace Ui { +class ConnectDialog; +} + +class QIntValidator; + +QT_END_NAMESPACE + +class ConnectDialog : public QDialog +{ + Q_OBJECT + +public: + typedef QPair ConfigurationItem; + + struct Settings { + QString backendName; + QString deviceInterfaceName; + QList configurations; + bool useConfigurationEnabled; + }; + + explicit ConnectDialog(QWidget *parent = nullptr); + ~ConnectDialog(); + + Settings settings() const; + +private slots: + void checkCustomSpeedPolicy(int idx); + void ok(); + void cancel(); + +private: + QString configurationValue(QCanBusDevice::ConfigurationKey key); + void revertSettings(); + void updateSettings(); + void fillBackends(); + void fillSpeeds(); + +private: + Ui::ConnectDialog *m_ui; + QIntValidator *m_customSpeedValidator; + Settings m_currentSettings; +}; + +#endif // CONNECTDIALOG_H diff --git a/examples/serialbus/can/connectdialog.ui b/examples/serialbus/can/connectdialog.ui new file mode 100644 index 0000000..5f35b14 --- /dev/null +++ b/examples/serialbus/can/connectdialog.ui @@ -0,0 +1,175 @@ + + + ConnectDialog + + + + 0 + 0 + 441 + 281 + + + + Connect + + + + + + + + Qt::Horizontal + + + + 96 + 20 + + + + + + + + Cancel + + + false + + + + + + + OK + + + false + + + true + + + + + + + + + Select CAN backend + + + + + + + + + + + + false + + + Specify Configuration + + + + + + RAW Filter + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Error Filter + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + FrameError bits + + + + + + + Loopback + + + + + + + + + + Receive Own + + + + + + + + + + Speed + + + + + + + + + + CAN FD + + + + + + + + + + + + + Specify CAN interface name + + + + + + + + + + + + Custom configuration + + + + + + + + diff --git a/examples/serialbus/can/images/settings.png b/examples/serialbus/can/images/settings.png deleted file mode 100644 index 3d1042e..0000000 Binary files a/examples/serialbus/can/images/settings.png and /dev/null differ diff --git a/examples/serialbus/can/mainwindow.cpp b/examples/serialbus/can/mainwindow.cpp index 07ef95b..4ac7ca7 100644 --- a/examples/serialbus/can/mainwindow.cpp +++ b/examples/serialbus/can/mainwindow.cpp @@ -40,10 +40,11 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -#include "settingsdialog.h" +#include "connectdialog.h" #include #include +#include #include #include @@ -56,7 +57,7 @@ MainWindow::MainWindow(QWidget *parent) : { m_ui->setupUi(this); - m_settings = new SettingsDialog; + m_connectDialog = new ConnectDialog; m_status = new QLabel; m_ui->statusBar->addWidget(m_status); @@ -64,6 +65,7 @@ MainWindow::MainWindow(QWidget *parent) : m_ui->sendMessagesBox->setEnabled(false); initActionsConnections(); + QTimer::singleShot(50, m_connectDialog, &ConnectDialog::show); connect(m_ui->sendButton, &QPushButton::clicked, this, &MainWindow::sendMessage); } @@ -72,7 +74,7 @@ MainWindow::~MainWindow() { delete m_canDevice; - delete m_settings; + delete m_connectDialog; delete m_ui; } @@ -86,12 +88,11 @@ 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->actionConnect, &QAction::triggered, m_connectDialog, &ConnectDialog::show); + connect(m_connectDialog, &QDialog::accepted, this, &MainWindow::connectDevice); connect(m_ui->actionDisconnect, &QAction::triggered, this, &MainWindow::disconnectDevice); connect(m_ui->actionQuit, &QAction::triggered, this, &QWidget::close); - connect(m_ui->actionConfigure, &QAction::triggered, m_settings, &SettingsDialog::show); connect(m_ui->actionAboutQt, &QAction::triggered, qApp, &QApplication::aboutQt); } @@ -111,7 +112,7 @@ void MainWindow::receiveError(QCanBusDevice::CanBusError error) const void MainWindow::connectDevice() { - const SettingsDialog::Settings p = m_settings->settings(); + const ConnectDialog::Settings p = m_connectDialog->settings(); m_canDevice = QCanBus::instance()->createDevice(p.backendName.toLocal8Bit(), p.deviceInterfaceName); if (!m_canDevice) { @@ -127,7 +128,7 @@ void MainWindow::connectDevice() this, &MainWindow::framesWritten); if (p.useConfigurationEnabled) { - foreach (const SettingsDialog::ConfigurationItem &item, p.configurations) + foreach (const ConnectDialog::ConfigurationItem &item, p.configurations) m_canDevice->setConfigurationParameter(item.first, item.second); } @@ -139,7 +140,6 @@ void MainWindow::connectDevice() } else { m_ui->actionConnect->setEnabled(false); m_ui->actionDisconnect->setEnabled(true); - m_ui->actionConfigure->setEnabled(false); m_ui->sendMessagesBox->setEnabled(true); @@ -159,7 +159,6 @@ void MainWindow::disconnectDevice() m_ui->actionConnect->setEnabled(true); m_ui->actionDisconnect->setEnabled(false); - m_ui->actionConfigure->setEnabled(true); m_ui->sendMessagesBox->setEnabled(false); diff --git a/examples/serialbus/can/mainwindow.h b/examples/serialbus/can/mainwindow.h index 8e1b142..aff6c94 100644 --- a/examples/serialbus/can/mainwindow.h +++ b/examples/serialbus/can/mainwindow.h @@ -45,7 +45,7 @@ #include -class SettingsDialog; +class ConnectDialog; QT_BEGIN_NAMESPACE @@ -81,7 +81,7 @@ private: Ui::MainWindow *m_ui; QLabel *m_status; - SettingsDialog *m_settings; + ConnectDialog *m_connectDialog; QCanBusDevice *m_canDevice; }; diff --git a/examples/serialbus/can/mainwindow.ui b/examples/serialbus/can/mainwindow.ui index cd3eec7..9700024 100644 --- a/examples/serialbus/can/mainwindow.ui +++ b/examples/serialbus/can/mainwindow.ui @@ -161,7 +161,7 @@ Otherwise, the standard format with 11 bit idenfier is used. 0 0 551 - 23 + 25 @@ -173,12 +173,6 @@ Otherwise, the standard format with 11 bit idenfier is used. - - - Too&ls - - - Help @@ -186,7 +180,6 @@ Otherwise, the standard format with 11 bit idenfier is used. - @@ -198,7 +191,6 @@ Otherwise, the standard format with 11 bit idenfier is used. - @@ -228,15 +220,6 @@ Otherwise, the standard format with 11 bit idenfier is used. &Quit - - - - :/images/settings.png:/images/settings.png - - - &Configure - - &About Qt diff --git a/examples/serialbus/can/settingsdialog.cpp b/examples/serialbus/can/settingsdialog.cpp deleted file mode 100644 index d61f4c6..0000000 --- a/examples/serialbus/can/settingsdialog.cpp +++ /dev/null @@ -1,192 +0,0 @@ -/**************************************************************************** -** -** 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 -#include - -SettingsDialog::SettingsDialog(QWidget *parent) : - QDialog(parent), - m_ui(new Ui::SettingsDialog), - m_customSpeedValidator(0) -{ - m_ui->setupUi(this); - - m_customSpeedValidator = new QIntValidator(0, 1000000, this); - m_ui->errorFilterEdit->setValidator(new QIntValidator(0, 0x1FFFFFFFU, this)); - - m_ui->loopbackBox->addItem(tr("unspecified"), QVariant()); - m_ui->loopbackBox->addItem(tr("false"), QVariant(false)); - m_ui->loopbackBox->addItem(tr("true"), QVariant(true)); - - m_ui->receiveOwnBox->addItem(tr("unspecified"), QVariant()); - m_ui->receiveOwnBox->addItem(tr("false"), QVariant(false)); - m_ui->receiveOwnBox->addItem(tr("true"), QVariant(true)); - - connect(m_ui->applyButton, &QPushButton::clicked, this, &SettingsDialog::apply); - connect(m_ui->useConfigurationBox, &QCheckBox::clicked, m_ui->configurationBox, &QGroupBox::setEnabled); - - connect(m_ui->speedBox, static_cast(&QComboBox::currentIndexChanged), - this, &SettingsDialog::checkCustomSpeedPolicy); - - m_ui->rawFilterEdit->hide(); - m_ui->rawFilterLabel->hide(); - - fillBackends(); - fillSpeeds(); - - updateSettings(); -} - -SettingsDialog::~SettingsDialog() -{ - delete m_ui; -} - -SettingsDialog::Settings SettingsDialog::settings() const -{ - return m_currentSettings; -} - -void SettingsDialog::checkCustomSpeedPolicy(int idx) -{ - const bool isCustomSpeed = !m_ui->speedBox->itemData(idx).isValid(); - m_ui->speedBox->setEditable(isCustomSpeed); - if (isCustomSpeed) { - m_ui->speedBox->clearEditText(); - QLineEdit *edit = m_ui->speedBox->lineEdit(); - edit->setValidator(m_customSpeedValidator); - } -} - -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) { - m_currentSettings.configurations.clear(); - // process LoopBack - if (m_ui->loopbackBox->currentIndex() != 0) { - ConfigurationItem item; - item.first = QCanBusDevice::LoopbackKey; - item.second = m_ui->loopbackBox->currentData(); - m_currentSettings.configurations.append(item); - } - - // process ReceiveOwnKey - if (m_ui->receiveOwnBox->currentIndex() != 0) { - ConfigurationItem item; - item.first = QCanBusDevice::ReceiveOwnKey; - item.second = m_ui->receiveOwnBox->currentData(); - m_currentSettings.configurations.append(item); - } - - // process error filter - if (!m_ui->errorFilterEdit->text().isEmpty()) { - QString value = m_ui->errorFilterEdit->text(); - bool ok = false; - int dec = value.toInt(&ok); - if (ok) { - ConfigurationItem item; - item.first = QCanBusDevice::ErrorFilterKey; - item.second = QVariant::fromValue(QCanBusFrame::FrameErrors(dec)); - m_currentSettings.configurations.append(item); - } - } - - // process raw filter list - if (!m_ui->rawFilterEdit->text().isEmpty()) { - //TODO current ui not sfficient to reflect this param - } - - // process bitrate - bool ok = false; - int bitrate = 0; - if (m_ui->speedBox->currentIndex() == (m_ui->speedBox->count() - 1)) - bitrate = m_ui->speedBox->currentText().toInt(&ok); - else - bitrate = m_ui->speedBox->itemData(m_ui->speedBox->currentIndex()).toInt(&ok); - - if (ok && (bitrate > 0)) { - ConfigurationItem item; - item.first = QCanBusDevice::BitRateKey; - item.second = QVariant(bitrate); - m_currentSettings.configurations.append(item); - } - - // process CAN FD setting - ConfigurationItem fdItem; - fdItem.first = QCanBusDevice::CanFdKey; - fdItem.second = QVariant(m_ui->canFdBox->currentIndex() == 1); // 0 -> no, 1 - yes - m_currentSettings.configurations.append(fdItem); - } -} - -void SettingsDialog::fillBackends() -{ - foreach (const QByteArray &backend, QCanBus::instance()->plugins()) - m_ui->backendListBox->addItem(backend); -} - -void SettingsDialog::fillSpeeds() -{ - m_ui->speedBox->addItem(QStringLiteral("10000"), 10000); - m_ui->speedBox->addItem(QStringLiteral("20000"), 20000); - m_ui->speedBox->addItem(QStringLiteral("50000"), 50000); - m_ui->speedBox->addItem(QStringLiteral("100000"), 100000); - m_ui->speedBox->addItem(QStringLiteral("125000"), 125000); - m_ui->speedBox->addItem(QStringLiteral("250000"), 250000); - m_ui->speedBox->addItem(QStringLiteral("500000"), 500000); - m_ui->speedBox->addItem(QStringLiteral("800000"), 800000); - m_ui->speedBox->addItem(QStringLiteral("1000000"), 1000000); - m_ui->speedBox->addItem(tr("Custom")); - - m_ui->speedBox->setCurrentIndex(6); // setup 500000 bits/sec by default -} diff --git a/examples/serialbus/can/settingsdialog.h b/examples/serialbus/can/settingsdialog.h deleted file mode 100644 index 2b51f09..0000000 --- a/examples/serialbus/can/settingsdialog.h +++ /dev/null @@ -1,92 +0,0 @@ -/**************************************************************************** -** -** 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; -} - -class QIntValidator; - -QT_END_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 = nullptr); - ~SettingsDialog(); - - Settings settings() const; - -private slots: - void checkCustomSpeedPolicy(int idx); - void apply(); - -private: - void updateSettings(); - void fillBackends(); - void fillSpeeds(); - -private: - Ui::SettingsDialog *m_ui; - QIntValidator *m_customSpeedValidator; - Settings m_currentSettings; -}; - -#endif // SETTINGSDIALOG_H diff --git a/examples/serialbus/can/settingsdialog.ui b/examples/serialbus/can/settingsdialog.ui deleted file mode 100644 index 37491a2..0000000 --- a/examples/serialbus/can/settingsdialog.ui +++ /dev/null @@ -1,170 +0,0 @@ - - - SettingsDialog - - - - 0 - 0 - 406 - 254 - - - - Settings - - - - - - - - Qt::Horizontal - - - - 96 - 20 - - - - - - - - Apply - - - - - - - - - Select CAN backend - - - - - - - - - - - - false - - - Specify Configuration - - - - - - RAW Filter - - - - - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - - - - - Error Filter - - - - - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - FrameError bits - - - - - - - Loopback - - - - - - - - - - Receive Own - - - - - - - - - - Speed - - - - - - - - - - CAN FD - - - - - - - - no - - - - - yes - - - - - - - - - - - Specify CAN interface name - - - - - - - - - - - - Custom configuration - - - - - - - - diff --git a/src/serialbus/doc/images/can-example.png b/src/serialbus/doc/images/can-example.png index f53df41..861218f 100644 Binary files a/src/serialbus/doc/images/can-example.png and b/src/serialbus/doc/images/can-example.png differ diff --git a/src/serialbus/doc/src/examples/can.qdoc b/src/serialbus/doc/src/examples/can.qdoc index dc3401a..0020823 100644 --- a/src/serialbus/doc/src/examples/can.qdoc +++ b/src/serialbus/doc/src/examples/can.qdoc @@ -31,8 +31,8 @@ \brief The example sends and received CAN bus frames. The example sends and receives CAN bus frames. Incoming frames - are ordered according to their type. A configuration - dialog is provided to adjust CAN Bus connection parameter. + are ordered according to their type. A connect dialog is + provided to adjust the CAN Bus connection parameters. \ingroup qtserialbus-examples -- cgit v1.2.3