Skip to content

Commit

Permalink
Options dialog window
Browse files Browse the repository at this point in the history
  • Loading branch information
BugLight committed Nov 16, 2020
1 parent a08d96c commit 4bbba2e
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 2 deletions.
4 changes: 4 additions & 0 deletions can-diagnostic-tool.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\hexvalidator.cpp" />
<ClCompile Include="src\jsonerrorsreader.cpp" />
<ClCompile Include="src\diagnosticprotocol.cpp" />
<ClCompile Include="src\canbusview.cpp" />
<ClCompile Include="src\candiagnostictool.cpp" />
<ClCompile Include="src\deviceerrorview.cpp" />
<ClCompile Include="src\diagnosticview.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\optionsdialog.cpp" />
</ItemGroup>
<ItemGroup>
<QtMoc Include="src\candiagnostictool.h" />
Expand All @@ -32,10 +34,12 @@
<QtMoc Include="src\canbusview.h" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\hexvalidator.h" />
<ClInclude Include="src\jsonerrorsreader.h" />
<ClInclude Include="src\diagnosticprotocol.h" />
<ClInclude Include="src\deviceerror.h" />
<ClInclude Include="src\deviceinfo.h" />
<QtMoc Include="src\optionsdialog.h" />
<QtMoc Include="src\diagnosticview.h" />
</ItemGroup>
<ItemGroup>
Expand Down
16 changes: 14 additions & 2 deletions src/candiagnostictool.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "candiagnostictool.h"

#include <QMenuBar>

#include <deviceerrorview.h>
#include <optionsdialog.h>

#include <QMenuBar>

CanDiagnosticTool::CanDiagnosticTool(QWidget* parent) : QMainWindow(parent) {
setFont(QFont("Arial", 12));
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
4 changes: 4 additions & 0 deletions src/candiagnostictool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/diagnosticview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/diagnosticview.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class DiagnosticView : public QWidget {
public slots:
void SetCanBusDevice(std::shared_ptr<QCanBusDevice>);

void SetProtocolOptions(const DiagnosticProtocolOptions&);

void SearchDevice();

void ReadErrors();
Expand Down
11 changes: 11 additions & 0 deletions src/hexvalidator.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions src/hexvalidator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <QString>
#include <QValidator>

class HexValidator final : public QValidator {
public:
explicit HexValidator(QObject* parent) : QValidator(parent) {}

QValidator::State validate(QString& input, int& pos) const override;
};
54 changes: 54 additions & 0 deletions src/optionsdialog.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
35 changes: 35 additions & 0 deletions src/optionsdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <QDialog>
#include <QFormLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>
#include <QVBoxLayout>

#include <hexvalidator.h>
#include <diagnosticprotocol.h>

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;
};

0 comments on commit 4bbba2e

Please sign in to comment.