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 "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 <QLabel>
#include <QVBoxLayout>
#include <utility>
#include <comdel/domain/instanceattribute.h>
#include <comdel/domain/value.h>
#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<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

View File

@ -10,15 +10,20 @@
#include <iostream>
#include <QPlainTextEdit>
#include <sstream>
#include <comdel/domain/comdelvalidator.h>
#include <fstream>
#include <comdel/domain/comdel_generator.h>
std::optional<domain::Library> 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();

View File

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