From 4bbba2e2ac58e83d2f9ce5fd0cfbf63eafcd33f8 Mon Sep 17 00:00:00 2001 From: Daniil Zhukov Date: Mon, 16 Nov 2020 20:14:22 +0300 Subject: [PATCH] Options dialog window --- can-diagnostic-tool.vcxproj | 4 +++ src/candiagnostictool.cpp | 16 +++++++++-- src/candiagnostictool.h | 4 +++ src/diagnosticview.cpp | 6 +++++ src/diagnosticview.h | 2 ++ src/hexvalidator.cpp | 11 ++++++++ src/hexvalidator.h | 11 ++++++++ src/optionsdialog.cpp | 54 +++++++++++++++++++++++++++++++++++++ src/optionsdialog.h | 35 ++++++++++++++++++++++++ 9 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 src/hexvalidator.cpp create mode 100644 src/hexvalidator.h create mode 100644 src/optionsdialog.cpp create mode 100644 src/optionsdialog.h diff --git a/can-diagnostic-tool.vcxproj b/can-diagnostic-tool.vcxproj index e3ca49c..747c0a8 100644 --- a/can-diagnostic-tool.vcxproj +++ b/can-diagnostic-tool.vcxproj @@ -11,6 +11,7 @@ + @@ -18,6 +19,7 @@ + @@ -32,10 +34,12 @@ + + diff --git a/src/candiagnostictool.cpp b/src/candiagnostictool.cpp index 40f531c..ff9dee2 100644 --- a/src/candiagnostictool.cpp +++ b/src/candiagnostictool.cpp @@ -1,8 +1,9 @@ #include "candiagnostictool.h" -#include - #include +#include + +#include CanDiagnosticTool::CanDiagnosticTool(QWidget* parent) : QMainWindow(parent) { setFont(QFont("Arial", 12)); @@ -11,8 +12,10 @@ CanDiagnosticTool::CanDiagnosticTool(QWidget* parent) : QMainWindow(parent) { searchAction = new QAction(u8"Обнаружить устройство", this); readAction = new QAction(u8"Прочитать ошибки", this); resetAction = new QAction(u8"Очистить ошибки", this); + optionsAction = new QAction(u8"Настройки", this); fileMenu = menuBar()->addMenu(u8"Файл"); + fileMenu->addAction(optionsAction); diagnosticMenu = menuBar()->addMenu(u8"Диагностика"); diagnosticMenu->addAction(searchAction); @@ -43,4 +46,13 @@ CanDiagnosticTool::CanDiagnosticTool(QWidget* parent) : QMainWindow(parent) { &DiagnosticView::ReadErrors); connect(resetAction, &QAction::triggered, diagnosticView, &DiagnosticView::ResetErrors); + connect(optionsAction, &QAction::triggered, this, + &CanDiagnosticTool::ExecOptionsDialog); +} + +void CanDiagnosticTool::ExecOptionsDialog() { + OptionsDialog dialog(this); + connect(&dialog, &OptionsDialog::OptionsConfigured, diagnosticView, + &DiagnosticView::SetProtocolOptions); + dialog.exec(); } diff --git a/src/candiagnostictool.h b/src/candiagnostictool.h index 592493c..096dcdb 100644 --- a/src/candiagnostictool.h +++ b/src/candiagnostictool.h @@ -16,10 +16,14 @@ class CanDiagnosticTool : public QMainWindow { public: CanDiagnosticTool(QWidget* parent = Q_NULLPTR); + private slots: + void ExecOptionsDialog(); + private: QAction* readAction; QAction* resetAction; QAction* searchAction; + QAction* optionsAction; CanBusView* canBusView; DiagnosticView* diagnosticView; QGridLayout* mainLayout; diff --git a/src/diagnosticview.cpp b/src/diagnosticview.cpp index 0032d21..df73cc5 100644 --- a/src/diagnosticview.cpp +++ b/src/diagnosticview.cpp @@ -48,6 +48,12 @@ void DiagnosticView::SetCanBusDevice( ReadErrors(); } +void DiagnosticView::SetProtocolOptions(const DiagnosticProtocolOptions& options) { + protocol.SetOptions(options); + SearchDevice(); + ReadErrors(); +} + void DiagnosticView::SearchDevice() { try { auto info = protocol.RequestDeviceInfo(); diff --git a/src/diagnosticview.h b/src/diagnosticview.h index 004e360..d3e0f57 100644 --- a/src/diagnosticview.h +++ b/src/diagnosticview.h @@ -20,6 +20,8 @@ class DiagnosticView : public QWidget { public slots: void SetCanBusDevice(std::shared_ptr); + void SetProtocolOptions(const DiagnosticProtocolOptions&); + void SearchDevice(); void ReadErrors(); diff --git a/src/hexvalidator.cpp b/src/hexvalidator.cpp new file mode 100644 index 0000000..bbb674b --- /dev/null +++ b/src/hexvalidator.cpp @@ -0,0 +1,11 @@ +#include "hexvalidator.h" + +QValidator::State HexValidator::validate(QString& input, int& pos) const { + bool success; + input.toUInt(&success, 16); + if (success) + return QValidator::Acceptable; + if (input.isEmpty()) + return QValidator::Intermediate; + return QValidator::Invalid; +} diff --git a/src/hexvalidator.h b/src/hexvalidator.h new file mode 100644 index 0000000..e7ab6a5 --- /dev/null +++ b/src/hexvalidator.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +class HexValidator final : public QValidator { + public: + explicit HexValidator(QObject* parent) : QValidator(parent) {} + + QValidator::State validate(QString& input, int& pos) const override; +}; diff --git a/src/optionsdialog.cpp b/src/optionsdialog.cpp new file mode 100644 index 0000000..cea0b70 --- /dev/null +++ b/src/optionsdialog.cpp @@ -0,0 +1,54 @@ +#include "optionsdialog.h" + +OptionsDialog::OptionsDialog(QWidget* parent) : QDialog(parent) { + setFont(QFont("Arial", 12)); + setLocale(QLocale(QLocale::Russian, QLocale::Russia)); + setWindowTitle(u8""); + + layout = new QVBoxLayout(this); + + form = new QWidget(this); + layout->addWidget(form); + + formLayout = new QFormLayout(form); + + hexValidator = new HexValidator(this); + + editRequestId = new QLineEdit(this); + editRequestId->setValidator(hexValidator); + formLayout->addRow(u8"CAN ID :", editRequestId); + + editResponseId = new QLineEdit(this); + editResponseId->setValidator(hexValidator); + formLayout->addRow(u8"CAN ID :", editResponseId); + + spinTimeout = new QSpinBox(this); + spinTimeout->setMinimum(100); + spinTimeout->setMaximum(10000); + formLayout->addRow(u8":", spinTimeout); + + spinMaxRetries = new QSpinBox(this); + spinMaxRetries->setMinimum(1); + spinMaxRetries->setMaximum(20); + formLayout->addRow(u8" :", spinMaxRetries); + + buttonCancel = new QPushButton(u8"", this); + layout->addWidget(buttonCancel); + + buttonOk = new QPushButton(u8"", this); + buttonOk->setFocus(); + layout->addWidget(buttonOk); + + connect(buttonCancel, &QPushButton::clicked, this, &OptionsDialog::reject); + connect(buttonOk, &QPushButton::clicked, this, &OptionsDialog::accept); +} + +void OptionsDialog::accept() { + DiagnosticProtocolOptions options; + options.SetRequestCanId(editRequestId->text().toUInt()); + options.SetResponseCanId(editResponseId->text().toUInt()); + options.SetTimeout(spinTimeout->value()); + options.SetMaxRetries(spinMaxRetries->value()); + emit OptionsConfigured(options); + QDialog::accept(); +} diff --git a/src/optionsdialog.h b/src/optionsdialog.h new file mode 100644 index 0000000..8e3d116 --- /dev/null +++ b/src/optionsdialog.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include +#include + +class OptionsDialog : public QDialog { + Q_OBJECT + + public: + OptionsDialog(QWidget *parent = Q_NULLPTR); + + void accept() override; + + signals: + void OptionsConfigured(const DiagnosticProtocolOptions&); + + private: + HexValidator* hexValidator; + QFormLayout* formLayout; + QLineEdit* editRequestId; + QLineEdit* editResponseId; + QPushButton* buttonCancel; + QPushButton* buttonOk; + QSpinBox* spinMaxRetries; + QSpinBox* spinTimeout; + QVBoxLayout* layout; + QWidget* form; +};