Added cancel to dialogs

This commit is contained in:
Borna Rajković 2022-06-12 11:51:57 +02:00
parent 4d4cf136fb
commit 6dfe86335d
6 changed files with 110 additions and 129 deletions

View File

@ -9,59 +9,8 @@
namespace display { namespace display {
void AttributeDialog::onUpdate() {
auto oldValue = attributeValue->value;
attributeValue->value = value;
domain::ComdelValidator validator(domain::getSupportedValidators());
domain::ValidationContext context;
for (auto &addressSpace: Application::instance()->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;
}
}
}
AttributeDialog::AttributeDialog(domain::InstanceAttribute *attribute, bool updating) { AttributeDialog::AttributeDialog(domain::InstanceAttribute *attribute, bool updating) {
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
attributeValue = attribute; attributeValue = attribute;
@ -142,10 +91,68 @@ namespace display {
layout->addWidget(group); layout->addWidget(group);
} }
auto button = new QPushButton(updating ? "Ažuriraj" : "Postavi");
connect(button, &QPushButton::clicked, this, &AttributeDialog::onUpdate);
layout->addWidget(button); auto buttonLayout = new QHBoxLayout(this);
auto okButton = new QPushButton(updating ? "Ažuriraj" : "Postavi", this);
auto cancelButton = new QPushButton("Odustani", this);
connect(okButton, &QPushButton::clicked, this, &AttributeDialog::onUpdate);
connect(cancelButton, &QPushButton::clicked, [this]() { reject(); });
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
layout->addLayout(buttonLayout);
}
void AttributeDialog::onUpdate() {
auto oldValue = attributeValue->value;
attributeValue->value = value;
domain::ComdelValidator validator(domain::getSupportedValidators());
domain::ValidationContext context;
for (auto &addressSpace: Application::instance()->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;
}
}
} }
void AttributeDialog::onTextChanged(const QString &string) { void AttributeDialog::onTextChanged(const QString &string) {
@ -165,12 +172,6 @@ namespace display {
value = attributeValue->attribute.getPopup()->getEnumeration()[index].getValue(); value = attributeValue->attribute.getPopup()->getEnumeration()[index].getValue();
} }
void MemoryDialog::onUpdate() {
attributeValue->value = value;
accept();
}
MemoryDialog::MemoryDialog(domain::InstanceAttribute *attribute, MemoryDialog::MemoryDialog(domain::InstanceAttribute *attribute,
std::vector<std::shared_ptr<domain::ComponentInstance>> instances, bool updating) { std::vector<std::shared_ptr<domain::ComponentInstance>> instances, bool updating) {
memoryInstances = std::vector<std::string>(); memoryInstances = std::vector<std::string>();
@ -216,10 +217,23 @@ namespace display {
} }
} }
auto button = new QPushButton(updating ? "Ažuriraj" : "Postavi"); auto buttonLayout = new QHBoxLayout(this);
connect(button, &QPushButton::clicked, this, &MemoryDialog::onUpdate);
layout->addWidget(button); auto okButton = new QPushButton(updating ? "Ažuriraj" : "Postavi");
auto cancelButton = new QPushButton("Odustani", this);
connect(okButton, &QPushButton::clicked, this, &MemoryDialog::onUpdate);
connect(cancelButton, &QPushButton::clicked, [this]() { reject(); });
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
layout->addLayout(buttonLayout);
}
void MemoryDialog::onUpdate() {
attributeValue->value = value;
accept();
} }
void MemoryDialog::onMemoryChanged(int index) { void MemoryDialog::onMemoryChanged(int index) {
@ -264,6 +278,6 @@ namespace display {
buttonLayout->addWidget(okButton); buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton); buttonLayout->addWidget(cancelButton);
layout->addItem(buttonLayout); layout->addLayout(buttonLayout);
} }
} }

View File

@ -272,24 +272,6 @@ namespace display {
setPos(instance->position.first, instance->position.second); setPos(instance->position.first, instance->position.second);
} }
void BusConnection::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
menu.addAction("Ukloni poveznicu", [this]() {});
menu.addSeparator();
for (int i = 0; i < this->connection->attributes.size(); i++) {
auto *attr = &this->connection->attributes[i];
bool enabled = attr->attribute.getPopup().has_value();
auto action = menu.addAction(QString::fromStdString("Izmjeni " + attr->name),
[attr]() {
auto dialog = new AttributeDialog(attr);
dialog->exec();
});
action->setEnabled(enabled);
}
menu.exec(event->screenPos());
}
BusConnection::BusConnection(domain::BusConnectionInstance *connection, ComponentGroup *component, BusGroup *bus): connection( BusConnection::BusConnection(domain::BusConnectionInstance *connection, ComponentGroup *component, BusGroup *bus): connection(
connection), component(component), bus(bus) { connection), component(component), bus(bus) {
updateConnection(); updateConnection();
@ -314,36 +296,6 @@ namespace display {
connection->end.second = connection->bus->position.second + busPosition.height() / 2; connection->end.second = connection->bus->position.second + busPosition.height() / 2;
} }
void DirectConnection::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
menu.addAction("Ukloni poveznicu", [this]() {});
menu.addSeparator();
if(connection->bus->bus.getType() == domain::Bus::SINGLE_AUTOMATIC) {
auto &attributes = this->connection->attributes;
menu.addAction(QString::fromStdString("Izmjeni poveznicu"),
[&attributes]() {
auto dialog = new display::SingleAutomaticDialog(attributes);
dialog->exec();
});
} else {
for (int i = 0; i < this->connection->attributes.size(); i++) {
auto *attr = &this->connection->attributes[i];
bool enabled = attr->attribute.getPopup().has_value();
auto action = menu.addAction(QString::fromStdString("Izmjeni " + attr->name),
[attr]() {
auto dialog = new AttributeDialog(attr);
dialog->exec();
});
action->setEnabled(enabled);
}
}
menu.exec(event->screenPos());
}
DirectConnection::DirectConnection(domain::DirectConnectionInstance *connection, ComponentGroup *first, DirectConnection::DirectConnection(domain::DirectConnectionInstance *connection, ComponentGroup *first,
ComponentGroup *second): connection(connection), first(first), second(second) { ComponentGroup *second): connection(connection), first(first), second(second) {
updateConnection(); updateConnection();

View File

@ -95,8 +95,6 @@ namespace display {
BusConnection(domain::BusConnectionInstance *connection, ComponentGroup *component, BusGroup *bus); BusConnection(domain::BusConnectionInstance *connection, ComponentGroup *component, BusGroup *bus);
void updateConnection(); void updateConnection();
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
}; };
@ -110,8 +108,6 @@ namespace display {
DirectConnection(domain::DirectConnectionInstance *connection, ComponentGroup *first, ComponentGroup *second); DirectConnection(domain::DirectConnectionInstance *connection, ComponentGroup *first, ComponentGroup *second);
void updateConnection(); void updateConnection();
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
}; };

View File

@ -16,10 +16,20 @@ display::NameDialog::NameDialog(std::string currentName, std::set<std::string> &
connect(edit, &QLineEdit::textChanged, this, &NameDialog::onNameUpdate); connect(edit, &QLineEdit::textChanged, this, &NameDialog::onNameUpdate);
layout->addWidget(edit); layout->addWidget(edit);
setWindowTitle("Izmjeni ime"); setWindowTitle("Izmjeni ime");
button = new QPushButton("Ažuriraj", this);
connect(button, &QPushButton::clicked, this, &NameDialog::onNameChange);
layout->addWidget(button);
setLayout(layout); setLayout(layout);
auto buttonLayout = new QHBoxLayout(this);
auto okButton = new QPushButton("Ažuriraj");
auto cancelButton = new QPushButton("Odustani", this);
connect(okButton, &QPushButton::clicked, this, &NameDialog::onNameChange);
connect(cancelButton, &QPushButton::clicked, [this]() { reject(); });
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
layout->addLayout(buttonLayout);
} }
void display::NameDialog::onNameUpdate(const QString &text) { void display::NameDialog::onNameUpdate(const QString &text) {

View File

@ -29,9 +29,18 @@ namespace display {
setupValues(firstLayout, values[0], &SingleAutomaticDialog::onFirstEnumerationChanged); setupValues(firstLayout, values[0], &SingleAutomaticDialog::onFirstEnumerationChanged);
setupValues(secondLayout, values[1], &SingleAutomaticDialog::onSecondEnumerationChanged); setupValues(secondLayout, values[1], &SingleAutomaticDialog::onSecondEnumerationChanged);
auto button = new QPushButton(updating ? "Ažuriraj" : "Postavi"); auto buttonLayout = new QHBoxLayout(this);
connect(button, &QPushButton::clicked, this, &SingleAutomaticDialog::onUpdate);
parentLayout->addWidget(button); 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);
parentLayout->addLayout(buttonLayout);
} }
void SingleAutomaticDialog::setupValues(QVBoxLayout *layout, domain::InstanceAttribute &attribute, void (display::SingleAutomaticDialog::* handler)(int)) { void SingleAutomaticDialog::setupValues(QVBoxLayout *layout, domain::InstanceAttribute &attribute, void (display::SingleAutomaticDialog::* handler)(int)) {

View File

@ -37,12 +37,12 @@ namespace domain {
if (count < comp.getCount().first) { if (count < comp.getCount().first) {
auto message = populateMessage( auto message = populateMessage(
"Not enough instances of component '{componentName}' required at least {min}, found {count}", "Nedovoljno instanci komponente '{componentName}' potrebno barem {min}, pronađeno {count}",
context); context);
errors.emplace_back(Action::ERROR, message); errors.emplace_back(Action::ERROR, message);
} else if (count > comp.getCount().second) { } else if (count > comp.getCount().second) {
auto message = populateMessage( auto message = populateMessage(
"To many instances of component '{componentName}' allow at most {max}, found {count}", context); "Previše insanci komponente '{componentName}' dozvoljeno najviše {max}, pronađeno {count}", context);
errors.emplace_back(Action::ERROR, message); errors.emplace_back(Action::ERROR, message);
} }
} }
@ -64,11 +64,11 @@ namespace domain {
if (count < bus.getCount().first) { if (count < bus.getCount().first) {
auto message = populateMessage( auto message = populateMessage(
"Not enough instances of bus '{busName}' required at least {min}, found {count}", context); "Nedovoljno instanci sabirnice '{busName}' potrebna barem jedna {min}, pronađeno {count}", context);
errors.emplace_back(Action::ERROR, message); errors.emplace_back(Action::ERROR, message);
} else if (count > bus.getCount().second) { } else if (count > bus.getCount().second) {
auto message = populateMessage( auto message = populateMessage(
"To many instances of bus '{busName}' allow at most {max}, found {count}", context); "Previše instanci sabirnice '{busName}' dozvoljeno najviše {max}, pronašeno {count}", context);
errors.emplace_back(Action::ERROR, message); errors.emplace_back(Action::ERROR, message);
} }
} }
@ -193,7 +193,7 @@ namespace domain {
if(names.find(component->name) != names.end()) { if(names.find(component->name) != names.end()) {
context.attributes["componentName"] = Value::fromString(component->name); context.attributes["componentName"] = Value::fromString(component->name);
auto message = populateMessage( auto message = populateMessage(
"There are multiple component instances named '{componentName}'", context); "Pronađeno više instanci sa imenom '{componentName}'", context);
errors.emplace_back(Action::ERROR, message); errors.emplace_back(Action::ERROR, message);
} }
names.insert(component->name); names.insert(component->name);
@ -234,11 +234,11 @@ namespace domain {
auto directConnection = dynamic_cast<DirectConnectionInstance *>(conn.get()); auto directConnection = dynamic_cast<DirectConnectionInstance *>(conn.get());
if (directConnection != nullptr) { if (directConnection != nullptr) {
if (directConnection->instance->name == component->name && if (directConnection->instance->name == component->name &&
busConnection->connection.getComponent().pin == pin.getName()) { directConnection->connection.getComponent().pin == pin.getName()) {
return true; return true;
} }
if (directConnection->secondInstance->name == component->name && if (directConnection->secondInstance->name == component->name &&
busConnection->connection.getSecondComponent()->pin == pin.getName()) { directConnection->connection.getSecondComponent()->pin == pin.getName()) {
return true; return true;
} }
} }