| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the QtSerialBus module. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "connectdialog.h" |
| 52 | #include "ui_connectdialog.h" |
| 53 | |
| 54 | #include <QCanBus> |
| 55 | |
| 56 | ConnectDialog::ConnectDialog(QWidget *parent) : |
| 57 | QDialog(parent), |
| 58 | m_ui(new Ui::ConnectDialog) |
| 59 | { |
| 60 | m_ui->setupUi(this); |
| 61 | |
| 62 | m_ui->errorFilterEdit->setValidator(new QIntValidator(0, 0x1FFFFFFFU, this)); |
| 63 | |
| 64 | m_ui->loopbackBox->addItem(atext: tr(s: "unspecified" ), auserData: QVariant()); |
| 65 | m_ui->loopbackBox->addItem(atext: tr(s: "false" ), auserData: QVariant(false)); |
| 66 | m_ui->loopbackBox->addItem(atext: tr(s: "true" ), auserData: QVariant(true)); |
| 67 | |
| 68 | m_ui->receiveOwnBox->addItem(atext: tr(s: "unspecified" ), auserData: QVariant()); |
| 69 | m_ui->receiveOwnBox->addItem(atext: tr(s: "false" ), auserData: QVariant(false)); |
| 70 | m_ui->receiveOwnBox->addItem(atext: tr(s: "true" ), auserData: QVariant(true)); |
| 71 | |
| 72 | m_ui->canFdBox->addItem(atext: tr(s: "false" ), auserData: QVariant(false)); |
| 73 | m_ui->canFdBox->addItem(atext: tr(s: "true" ), auserData: QVariant(true)); |
| 74 | |
| 75 | m_ui->dataBitrateBox->setFlexibleDateRateEnabled(true); |
| 76 | |
| 77 | connect(sender: m_ui->okButton, signal: &QPushButton::clicked, receiver: this, slot: &ConnectDialog::ok); |
| 78 | connect(sender: m_ui->cancelButton, signal: &QPushButton::clicked, receiver: this, slot: &ConnectDialog::cancel); |
| 79 | connect(sender: m_ui->useConfigurationBox, signal: &QCheckBox::clicked, |
| 80 | receiver: m_ui->configurationBox, slot: &QGroupBox::setEnabled); |
| 81 | connect(sender: m_ui->pluginListBox, signal: &QComboBox::currentTextChanged, |
| 82 | receiver: this, slot: &ConnectDialog::pluginChanged); |
| 83 | connect(sender: m_ui->interfaceListBox, signal: &QComboBox::currentTextChanged, |
| 84 | receiver: this, slot: &ConnectDialog::interfaceChanged); |
| 85 | m_ui->rawFilterEdit->hide(); |
| 86 | m_ui->rawFilterLabel->hide(); |
| 87 | |
| 88 | m_ui->pluginListBox->addItems(texts: QCanBus::instance()->plugins()); |
| 89 | |
| 90 | updateSettings(); |
| 91 | } |
| 92 | |
| 93 | ConnectDialog::~ConnectDialog() |
| 94 | { |
| 95 | delete m_ui; |
| 96 | } |
| 97 | |
| 98 | ConnectDialog::Settings ConnectDialog::settings() const |
| 99 | { |
| 100 | return m_currentSettings; |
| 101 | } |
| 102 | |
| 103 | void ConnectDialog::pluginChanged(const QString &plugin) |
| 104 | { |
| 105 | m_ui->interfaceListBox->clear(); |
| 106 | m_interfaces = QCanBus::instance()->availableDevices(plugin); |
| 107 | for (const QCanBusDeviceInfo &info : qAsConst(t&: m_interfaces)) |
| 108 | m_ui->interfaceListBox->addItem(atext: info.name()); |
| 109 | } |
| 110 | |
| 111 | void ConnectDialog::interfaceChanged(const QString &interface) |
| 112 | { |
| 113 | m_ui->isVirtual->setChecked(false); |
| 114 | m_ui->isFlexibleDataRateCapable->setChecked(false); |
| 115 | |
| 116 | for (const QCanBusDeviceInfo &info : qAsConst(t&: m_interfaces)) { |
| 117 | if (info.name() == interface) { |
| 118 | m_ui->descriptionLabel->setText(info.description()); |
| 119 | QString serialNumber = info.serialNumber(); |
| 120 | if (serialNumber.isEmpty()) |
| 121 | serialNumber = tr(s: "n/a" ); |
| 122 | m_ui->serialNumberLabel->setText(tr(s: "Serial: %1" ).arg(a: serialNumber)); |
| 123 | m_ui->channelLabel->setText(tr(s: "Channel: %1" ).arg(a: info.channel())); |
| 124 | m_ui->isVirtual->setChecked(info.isVirtual()); |
| 125 | m_ui->isFlexibleDataRateCapable->setChecked(info.hasFlexibleDataRate()); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void ConnectDialog::ok() |
| 132 | { |
| 133 | updateSettings(); |
| 134 | accept(); |
| 135 | } |
| 136 | |
| 137 | void ConnectDialog::cancel() |
| 138 | { |
| 139 | revertSettings(); |
| 140 | reject(); |
| 141 | } |
| 142 | |
| 143 | QString ConnectDialog::configurationValue(QCanBusDevice::ConfigurationKey key) |
| 144 | { |
| 145 | QVariant result; |
| 146 | |
| 147 | for (const ConfigurationItem &item : qAsConst(t&: m_currentSettings.configurations)) { |
| 148 | if (item.first == key) { |
| 149 | result = item.second; |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (result.isNull() && ( |
| 155 | key == QCanBusDevice::LoopbackKey || |
| 156 | key == QCanBusDevice::ReceiveOwnKey)) { |
| 157 | return tr(s: "unspecified" ); |
| 158 | } |
| 159 | |
| 160 | return result.toString(); |
| 161 | } |
| 162 | |
| 163 | void ConnectDialog::revertSettings() |
| 164 | { |
| 165 | m_ui->pluginListBox->setCurrentText(m_currentSettings.pluginName); |
| 166 | m_ui->interfaceListBox->setCurrentText(m_currentSettings.deviceInterfaceName); |
| 167 | m_ui->useConfigurationBox->setChecked(m_currentSettings.useConfigurationEnabled); |
| 168 | |
| 169 | QString value = configurationValue(key: QCanBusDevice::LoopbackKey); |
| 170 | m_ui->loopbackBox->setCurrentText(value); |
| 171 | |
| 172 | value = configurationValue(key: QCanBusDevice::ReceiveOwnKey); |
| 173 | m_ui->receiveOwnBox->setCurrentText(value); |
| 174 | |
| 175 | value = configurationValue(key: QCanBusDevice::ErrorFilterKey); |
| 176 | m_ui->errorFilterEdit->setText(value); |
| 177 | |
| 178 | value = configurationValue(key: QCanBusDevice::BitRateKey); |
| 179 | m_ui->bitrateBox->setCurrentText(value); |
| 180 | |
| 181 | value = configurationValue(key: QCanBusDevice::CanFdKey); |
| 182 | m_ui->canFdBox->setCurrentText(value); |
| 183 | |
| 184 | value = configurationValue(key: QCanBusDevice::DataBitRateKey); |
| 185 | m_ui->dataBitrateBox->setCurrentText(value); |
| 186 | } |
| 187 | |
| 188 | void ConnectDialog::updateSettings() |
| 189 | { |
| 190 | m_currentSettings.pluginName = m_ui->pluginListBox->currentText(); |
| 191 | m_currentSettings.deviceInterfaceName = m_ui->interfaceListBox->currentText(); |
| 192 | m_currentSettings.useConfigurationEnabled = m_ui->useConfigurationBox->isChecked(); |
| 193 | |
| 194 | if (m_currentSettings.useConfigurationEnabled) { |
| 195 | m_currentSettings.configurations.clear(); |
| 196 | // process LoopBack |
| 197 | if (m_ui->loopbackBox->currentIndex() != 0) { |
| 198 | ConfigurationItem item; |
| 199 | item.first = QCanBusDevice::LoopbackKey; |
| 200 | item.second = m_ui->loopbackBox->currentData(); |
| 201 | m_currentSettings.configurations.append(t: item); |
| 202 | } |
| 203 | |
| 204 | // process ReceiveOwnKey |
| 205 | if (m_ui->receiveOwnBox->currentIndex() != 0) { |
| 206 | ConfigurationItem item; |
| 207 | item.first = QCanBusDevice::ReceiveOwnKey; |
| 208 | item.second = m_ui->receiveOwnBox->currentData(); |
| 209 | m_currentSettings.configurations.append(t: item); |
| 210 | } |
| 211 | |
| 212 | // process error filter |
| 213 | if (!m_ui->errorFilterEdit->text().isEmpty()) { |
| 214 | QString value = m_ui->errorFilterEdit->text(); |
| 215 | bool ok = false; |
| 216 | int dec = value.toInt(ok: &ok); |
| 217 | if (ok) { |
| 218 | ConfigurationItem item; |
| 219 | item.first = QCanBusDevice::ErrorFilterKey; |
| 220 | item.second = QVariant::fromValue(value: QCanBusFrame::FrameErrors(dec)); |
| 221 | m_currentSettings.configurations.append(t: item); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // process raw filter list |
| 226 | if (!m_ui->rawFilterEdit->text().isEmpty()) { |
| 227 | //TODO current ui not sfficient to reflect this param |
| 228 | } |
| 229 | |
| 230 | // process bitrate |
| 231 | const int bitrate = m_ui->bitrateBox->bitRate(); |
| 232 | if (bitrate > 0) { |
| 233 | const ConfigurationItem item(QCanBusDevice::BitRateKey, QVariant(bitrate)); |
| 234 | m_currentSettings.configurations.append(t: item); |
| 235 | } |
| 236 | |
| 237 | // process CAN FD setting |
| 238 | ConfigurationItem fdItem; |
| 239 | fdItem.first = QCanBusDevice::CanFdKey; |
| 240 | fdItem.second = m_ui->canFdBox->currentData(); |
| 241 | m_currentSettings.configurations.append(t: fdItem); |
| 242 | |
| 243 | // process data bitrate |
| 244 | const int dataBitrate = m_ui->dataBitrateBox->bitRate(); |
| 245 | if (dataBitrate > 0) { |
| 246 | const ConfigurationItem item(QCanBusDevice::DataBitRateKey, QVariant(dataBitrate)); |
| 247 | m_currentSettings.configurations.append(t: item); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |