diff options
author | Andre Hartmann <aha_1980@gmx.de> | 2017-01-08 17:47:31 +0100 |
---|---|---|
committer | André Hartmann <aha_1980@gmx.de> | 2017-01-16 16:10:42 +0000 |
commit | 52967158da2c02efceea610e83786bbd6b5a888a (patch) | |
tree | da66251af3c827489e74d9d9e36953f5082fae6c | |
parent | a0cc6c88fd990b89848a78606e8547f814213a46 (diff) |
CAN-Example: Introduce bitrate selection combox box
Factor the bitrate selection logic out of connect dialog to allow
re-using the logic for CAN FD data bitrate selection later.
Change-Id: I0a509c6200dddbd6b254a5e7f0702e6b0c99d1a3
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r-- | examples/serialbus/can/bitratebox.cpp | 111 | ||||
-rw-r--r-- | examples/serialbus/can/bitratebox.h | 69 | ||||
-rw-r--r-- | examples/serialbus/can/can.pro | 12 | ||||
-rw-r--r-- | examples/serialbus/can/connectdialog.cpp | 47 | ||||
-rw-r--r-- | examples/serialbus/can/connectdialog.h | 5 | ||||
-rw-r--r-- | examples/serialbus/can/connectdialog.ui | 13 |
6 files changed, 202 insertions, 55 deletions
diff --git a/examples/serialbus/can/bitratebox.cpp b/examples/serialbus/can/bitratebox.cpp new file mode 100644 index 0000000..07aad9c --- /dev/null +++ b/examples/serialbus/can/bitratebox.cpp @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Andre Hartmann <aha_1980@gmx.de> +** 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 "bitratebox.h" + +#include <QLineEdit> + +BitRateBox::BitRateBox(QWidget *parent) : + QComboBox(parent), + m_customSpeedValidator(new QIntValidator(0, 1000000, this)) +{ + fillBitRates(); + + connect(this, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), + this, &BitRateBox::checkCustomSpeedPolicy); +} + +BitRateBox::~BitRateBox() +{ + delete m_customSpeedValidator; +} + +int BitRateBox::bitRate() const +{ + if (currentIndex() == (count() - 1)) + return currentText().toInt(); + + return itemData(currentIndex()).toInt(); +} + +bool BitRateBox::isFlexibleDataRateEnabled() const +{ + return m_isFlexibleDataRateEnabled; +} + +void BitRateBox::setFlexibleDateRateEnabled(bool enabled) +{ + m_isFlexibleDataRateEnabled = enabled; + m_customSpeedValidator->setTop(enabled ? 10000000 : 1000000); + fillBitRates(); +} + +void BitRateBox::checkCustomSpeedPolicy(int idx) +{ + const bool isCustomSpeed = !itemData(idx).isValid(); + setEditable(isCustomSpeed); + if (isCustomSpeed) { + clearEditText(); + lineEdit()->setValidator(m_customSpeedValidator); + } +} + +void BitRateBox::fillBitRates() +{ + const QList<int> rates = { + 10000, 20000, 50000, 100000, 125000, 250000, 500000, 800000, 1000000 + }; + const QList<int> dataRates = { + 2000000, 4000000, 8000000 + }; + + clear(); + + for (int rate : rates) + addItem(QString::number(rate), rate); + + if (isFlexibleDataRateEnabled()) { + for (int rate : dataRates) + addItem(QString::number(rate), rate); + } + + addItem(tr("Custom")); + setCurrentIndex(6); // default is 500000 bits/sec +} diff --git a/examples/serialbus/can/bitratebox.h b/examples/serialbus/can/bitratebox.h new file mode 100644 index 0000000..e9df635 --- /dev/null +++ b/examples/serialbus/can/bitratebox.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Andre Hartmann <aha_1980@gmx.de> +** 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 BITRATEBOX_H +#define BITRATEBOX_H + +#include <QComboBox> + +class QIntValidator; + +class BitRateBox : public QComboBox +{ +public: + BitRateBox(QWidget *parent = nullptr); + ~BitRateBox(); + + int bitRate() const; + + bool isFlexibleDataRateEnabled() const; + void setFlexibleDateRateEnabled(bool enabled); + +private slots: + void checkCustomSpeedPolicy(int idx); + +private: + void fillBitRates(); + + int m_isFlexibleDataRateEnabled = false; + QIntValidator *m_customSpeedValidator = nullptr; +}; + +#endif // BITRATEBOX_H diff --git a/examples/serialbus/can/can.pro b/examples/serialbus/can/can.pro index aa2234f..c667dbb 100644 --- a/examples/serialbus/can/can.pro +++ b/examples/serialbus/can/can.pro @@ -3,12 +3,16 @@ QT += serialbus widgets TARGET = can TEMPLATE = app -SOURCES += main.cpp \ +SOURCES += \ + bitratebox.cpp \ + connectdialog.cpp \ + main.cpp \ mainwindow.cpp \ - connectdialog.cpp -HEADERS += mainwindow.h \ - connectdialog.h +HEADERS += \ + bitratebox.h \ + connectdialog.h \ + mainwindow.h \ FORMS += mainwindow.ui \ connectdialog.ui diff --git a/examples/serialbus/can/connectdialog.cpp b/examples/serialbus/can/connectdialog.cpp index 90b74b3..5d9e33d 100644 --- a/examples/serialbus/can/connectdialog.cpp +++ b/examples/serialbus/can/connectdialog.cpp @@ -49,7 +49,6 @@ ConnectDialog::ConnectDialog(QWidget *parent) : { 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()); @@ -67,8 +66,6 @@ ConnectDialog::ConnectDialog(QWidget *parent) : 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<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), - this, &ConnectDialog::checkCustomSpeedPolicy); connect(m_ui->backendListBox, &QComboBox::currentTextChanged, this, &ConnectDialog::backendChanged); connect(m_ui->interfaceListBox, &QComboBox::currentTextChanged, @@ -77,7 +74,6 @@ ConnectDialog::ConnectDialog(QWidget *parent) : m_ui->rawFilterLabel->hide(); m_ui->backendListBox->addItems(QCanBus::instance()->plugins()); - fillSpeeds(); updateSettings(); } @@ -92,17 +88,6 @@ 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::backendChanged(const QString &backend) { m_ui->interfaceListBox->clear(); @@ -173,7 +158,7 @@ void ConnectDialog::revertSettings() m_ui->errorFilterEdit->setText(value); value = configurationValue(QCanBusDevice::BitRateKey); - m_ui->speedBox->setCurrentText(value); + m_ui->bitrateBox->setCurrentText(value); value = configurationValue(QCanBusDevice::CanFdKey); m_ui->canFdBox->setCurrentText(value); @@ -222,17 +207,9 @@ void ConnectDialog::updateSettings() } // 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); + const int bitrate = m_ui->bitrateBox->bitRate(); + if (bitrate > 0) { + const ConfigurationItem item(QCanBusDevice::BitRateKey, QVariant(bitrate)); m_currentSettings.configurations.append(item); } @@ -243,19 +220,3 @@ void ConnectDialog::updateSettings() m_currentSettings.configurations.append(fdItem); } } - -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 index 23ba501..0411269 100644 --- a/examples/serialbus/can/connectdialog.h +++ b/examples/serialbus/can/connectdialog.h @@ -52,8 +52,6 @@ namespace Ui { class ConnectDialog; } -class QIntValidator; - QT_END_NAMESPACE class ConnectDialog : public QDialog @@ -76,7 +74,6 @@ public: Settings settings() const; private slots: - void checkCustomSpeedPolicy(int idx); void backendChanged(const QString &backend); void interfaceChanged(const QString &interface); void ok(); @@ -86,10 +83,8 @@ private: QString configurationValue(QCanBusDevice::ConfigurationKey key); void revertSettings(); void updateSettings(); - void fillSpeeds(); Ui::ConnectDialog *m_ui = nullptr; - QIntValidator *m_customSpeedValidator = nullptr; Settings m_currentSettings; QList<QCanBusDeviceInfo> m_interfaces; }; diff --git a/examples/serialbus/can/connectdialog.ui b/examples/serialbus/can/connectdialog.ui index f5cc53e..345ff64 100644 --- a/examples/serialbus/can/connectdialog.ui +++ b/examples/serialbus/can/connectdialog.ui @@ -127,14 +127,14 @@ <widget class="QComboBox" name="receiveOwnBox"/> </item> <item row="4" column="0"> - <widget class="QLabel" name="speedLabel"> + <widget class="QLabel" name="bitrateLabel"> <property name="text"> - <string>Speed</string> + <string>Bitrate</string> </property> </widget> </item> <item row="4" column="1"> - <widget class="QComboBox" name="speedBox"/> + <widget class="BitRateBox" name="bitrateBox"/> </item> <item row="5" column="0"> <widget class="QLabel" name="canFdLabel"> @@ -200,6 +200,13 @@ </item> </layout> </widget> + <customwidgets> + <customwidget> + <class>BitRateBox</class> + <extends>QComboBox</extends> + <header>bitratebox.h</header> + </customwidget> + </customwidgets> <resources/> <connections/> </ui> |