From bb423c21849d4223d1dd69bcacacd2da7dc5b1d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borna=20Rajkovi=C4=87?= Date: Sun, 15 May 2022 18:38:17 +0200 Subject: [PATCH] Added warning and error dialogs to attribute change --- comdel/display/attribute_dialog.cpp | 55 ++++++++++++++++++++++++++++ comdel/display/attribute_dialog.h | 56 ++++++++++++++++++++++++++--- mainwindow.cpp | 7 +++- mainwindow.h | 13 +++++-- 4 files changed, 124 insertions(+), 7 deletions(-) diff --git a/comdel/display/attribute_dialog.cpp b/comdel/display/attribute_dialog.cpp index 1ae0aee..71d86e1 100644 --- a/comdel/display/attribute_dialog.cpp +++ b/comdel/display/attribute_dialog.cpp @@ -3,3 +3,58 @@ // #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 errors; + std::vector 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; + } + } + +} + +} \ No newline at end of file diff --git a/comdel/display/attribute_dialog.h b/comdel/display/attribute_dialog.h index e098145..dcbab51 100644 --- a/comdel/display/attribute_dialog.h +++ b/comdel/display/attribute_dialog.h @@ -10,9 +10,11 @@ #include #include #include +#include #include #include +#include "comdel/domain/comdelvalidator.h" namespace display { @@ -138,13 +140,59 @@ public slots: value = attributeValue->attribute.getPopup()->getEnumeration()[index].getValue(); } - void onUpdate() { - attributeValue->value = value; - accept(); - } + void onUpdate(); }; + class ErrorDialog: public QDialog { + + public: + ErrorDialog(std::vector 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 diff --git a/mainwindow.cpp b/mainwindow.cpp index 4020308..127de2d 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -10,15 +10,20 @@ #include #include -#include #include #include #include +std::optional MainWindow::library; +domain::Schema* MainWindow::schema; + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { + schema = nullptr; + library = std::nullopt; + ui->setupUi(this); setupUi(); diff --git a/mainwindow.h b/mainwindow.h index 02a5164..2966bd0 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -27,14 +27,23 @@ public: display::Library *libraryDisplay; display::Schema *schemaDisplay; - std::optional library = std::nullopt; - domain::Schema* schema = nullptr; + static std::optional library; + static domain::Schema* schema; std::vector validators; void setupUi(); void clear(); + static domain::Schema* getSchema() { + return schema; + } + + static std::optional getLibrary() { + return library; + } + + private slots: void onLoadLibrary(); void onLoadSchema();