Added warning and error dialogs to attribute change

This commit is contained in:
Borna Rajković 2022-05-15 18:38:17 +02:00
parent 65020c2b7f
commit bb423c2184
4 changed files with 124 additions and 7 deletions

View File

@ -3,3 +3,58 @@
// //
#include "attribute_dialog.h" #include "attribute_dialog.h"
#include "mainwindow.h"
namespace display {
void AttributeDialog::onUpdate() {
auto oldValue = attributeValue->value;
attributeValue->value = value;
domain::ComdelValidator validator(domain::getSupportedValidators());
domain::ValidationContext context;
for (auto &addressSpace: MainWindow::getLibrary()->getAddressSpaces()) {
context.addressSpaces.insert(std::make_pair(addressSpace.getName(), addressSpace));
}
auto validationErrors = validator.validateAttribute(attributeValue, context);
if(validationErrors.empty()) {
accept();
} else {
bool canAccept = true;
std::vector<domain::ValidationError> errors;
std::vector<domain::ValidationError> warnings;
for(auto& err: validationErrors) {
if(err.type == domain::Action::ERROR) {
errors.push_back(err);
} else {
warnings.push_back(err);
}
}
if(!errors.empty()) {
canAccept = false;
auto errorDialog = new ErrorDialog(errors);
errorDialog->exec();
}
for(auto& warning: warnings) {
auto warningDialog = new WarningDialog(warning);
int response = warningDialog->exec();
if(response == QDialog::Rejected) {
canAccept = false;
}
}
if(canAccept) {
accept();
} else {
attributeValue->value = oldValue;
}
}
}
}

View File

@ -10,9 +10,11 @@
#include <QLineEdit> #include <QLineEdit>
#include <QLabel> #include <QLabel>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <utility>
#include <comdel/domain/instanceattribute.h> #include <comdel/domain/instanceattribute.h>
#include <comdel/domain/value.h> #include <comdel/domain/value.h>
#include "comdel/domain/comdelvalidator.h"
namespace display { namespace display {
@ -138,13 +140,59 @@ public slots:
value = attributeValue->attribute.getPopup()->getEnumeration()[index].getValue(); value = attributeValue->attribute.getPopup()->getEnumeration()[index].getValue();
} }
void onUpdate() { void onUpdate();
attributeValue->value = value;
accept();
}
}; };
class ErrorDialog: public QDialog {
public:
ErrorDialog(std::vector<domain::ValidationError> errors) {
setAttribute(Qt::WA_DeleteOnClose);
this->setWindowTitle("Greške");
auto layout = new QVBoxLayout(this);
this->setLayout(layout);
for(auto& err: errors) {
layout->addWidget(new QLabel(QString::fromStdString(err.message), this));
}
}
};
class WarningDialog: public QDialog {
public:
WarningDialog(domain::ValidationError error) {
setAttribute(Qt::WA_DeleteOnClose);
this->setWindowTitle("Upozorenje");
auto layout = new QVBoxLayout(this);
this->setLayout(layout);
layout->addWidget(new QLabel(QString::fromStdString(error.message), this));
auto buttonLayout = new QHBoxLayout(this);
auto okButton = new QPushButton("U redu", this);
auto cancelButton = new QPushButton("Odustani", this);
connect(okButton, &QPushButton::clicked, [this]() { accept(); });
connect(cancelButton, &QPushButton::clicked, [this]() { reject(); });
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
layout->addItem(buttonLayout);
}
};
} }
#endif //ATTRIBUTE_DIALOG_H #endif //ATTRIBUTE_DIALOG_H

View File

@ -10,15 +10,20 @@
#include <iostream> #include <iostream>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <sstream>
#include <comdel/domain/comdelvalidator.h> #include <comdel/domain/comdelvalidator.h>
#include <fstream> #include <fstream>
#include <comdel/domain/comdel_generator.h> #include <comdel/domain/comdel_generator.h>
std::optional<domain::Library> MainWindow::library;
domain::Schema* MainWindow::schema;
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
{ {
schema = nullptr;
library = std::nullopt;
ui->setupUi(this); ui->setupUi(this);
setupUi(); setupUi();

View File

@ -27,14 +27,23 @@ public:
display::Library *libraryDisplay; display::Library *libraryDisplay;
display::Schema *schemaDisplay; display::Schema *schemaDisplay;
std::optional<domain::Library> library = std::nullopt; static std::optional<domain::Library> library;
domain::Schema* schema = nullptr; static domain::Schema* schema;
std::vector<domain::FunctionValidator*> validators; std::vector<domain::FunctionValidator*> validators;
void setupUi(); void setupUi();
void clear(); void clear();
static domain::Schema* getSchema() {
return schema;
}
static std::optional<domain::Library> getLibrary() {
return library;
}
private slots: private slots:
void onLoadLibrary(); void onLoadLibrary();
void onLoadSchema(); void onLoadSchema();