Compare commits

...

2 Commits

Author SHA1 Message Date
Borna Rajković 6dfe86335d Added cancel to dialogs 2022-06-12 11:51:57 +02:00
Borna Rajković 4d4cf136fb Added pin connection editing 2022-06-09 23:42:45 +02:00
10 changed files with 229 additions and 152 deletions

View File

@ -288,6 +288,30 @@ bool Application::removeComponent(std::string componentName) {
return true;
}
void Application::removeConnection(domain::ConnectionInstance *connectionInstance) {
if(auto directConnection = dynamic_cast<domain::DirectConnectionInstance*>(connectionInstance)) {
schema->busInstances.erase(
std::remove_if(
schema->busInstances.begin(),
schema->busInstances.end(),
[directConnection](const std::shared_ptr<domain::BusInstance> &bus) {
return directConnection->bus == bus.get();
}),
schema->busInstances.end()
);
}
schema->connections.erase(
std::remove_if(
schema->connections.begin(),
schema->connections.end(),
[connectionInstance](const std::shared_ptr<domain::ConnectionInstance> &conn) {
return connectionInstance == conn.get();
}),
schema->connections.end()
);
}
bool Application::removeBus(std::string busName) {
auto bus = findBusByName(busName);
if (bus == nullptr) {
@ -367,4 +391,4 @@ void Application::renameBus(std::string currentName, std::string newName) {
if(bus) {
bus->name = newName;
}
}
}

View File

@ -52,6 +52,8 @@ public:
void renameComponent(std::string currentName, std::string newName);
void renameBus(std::string currentName, std::string newName);
void removeConnection(domain::ConnectionInstance *pInstance);
};

View File

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

View File

@ -16,6 +16,12 @@ namespace display {
QPen connectionPen(QColor::fromRgb(150, 150, 250));
Component::Component(const std::shared_ptr<domain::ComponentInstance> &instance): instance(instance) {
setFlag(ItemSendsGeometryChanges, true);
setToolTip(QString::fromStdString(instance->name + "::" + instance->component.getName()));
instance->component.getDisplay().render(this);
}
void Component::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
menu.addAction("Izmjeni ime", [this]() {
@ -30,6 +36,7 @@ namespace display {
auto newName = dialog->getName();
Application::instance()->renameComponent(currentName, newName);
setToolTip(QString::fromStdString(instance->name + "::" + instance->component.getName()));
});
menu.addSeparator();
for (int i = 0; i < this->instance->attributes.size(); i++) {
@ -61,14 +68,57 @@ namespace display {
menu.exec(event->screenPos());
}
Component::Component(const std::shared_ptr<domain::ComponentInstance> &instance): instance(instance) {
setFlag(ItemSendsGeometryChanges, true);
instance->component.getDisplay().render(this);
}
void Pin::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
menu.addAction("Poveži pin", [&]() {});
auto pinConnections = Application::instance()->getSchema()->getConnections(componentInstance->name, pin.getName());
if(isSingleAutomatic(pinConnections)) {
auto *update = menu.addMenu("Izmjeni");
auto *remove = menu.addMenu("Ukloni");
for (auto pinConnection: pinConnections) {
// this always must be true as only directConnections can be connected multiple times
if (auto directConnection = dynamic_cast<domain::DirectConnectionInstance *>(pinConnection)) {
if (directConnection->bus->bus.getType() == domain::Bus::SINGLE_AUTOMATIC) {
auto connectionName = directConnection->attributes[0].value.stringify() + "-" +
directConnection->attributes[1].value.stringify();
update->addAction(QString::fromStdString("Izmjeni " + connectionName), [directConnection]() {
auto dialog = new SingleAutomaticDialog(directConnection->attributes);
dialog->exec();
});
remove->addAction(QString::fromStdString("Ukloni " + connectionName),
[this, directConnection]() {
Application::instance()->removeConnection(directConnection);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
});
}
}
}
} else {
auto pinConnection = pinConnections[0];
if(auto busConnection = dynamic_cast<domain::BusConnectionInstance*>(pinConnection)) {
menu.addSection(QString::fromStdString(busConnection->bus->name));
} else if(auto directConnection = dynamic_cast<domain::DirectConnectionInstance*>(pinConnection)) {
if(directConnection->instance == componentInstance.get()) {
menu.addSection(QString::fromStdString(directConnection->secondInstance->name + "." + directConnection->connection.getSecondComponent()->pin));
} else {
menu.addSection(QString::fromStdString(directConnection->instance->name + "." + directConnection->connection.getComponent().pin));
}
}
for (int i = 0; i < pinConnection->attributes.size(); i++) {
auto *attr = &pinConnection->attributes[i];
menu.addAction(QString::fromStdString("Izmjeni '" + attr->name + "'"),[attr]() {
auto dialog = new AttributeDialog(attr);
dialog->exec();
});
}
menu.addAction("Ukloni poveznicu", [this, pinConnection]() {
Application::instance()->removeConnection(pinConnection);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
});
}
menu.exec(event->screenPos());
}
@ -116,6 +166,23 @@ namespace display {
return componentInstance.get();
}
bool Pin::isSingleAutomatic(std::vector<domain::ConnectionInstance *> pinConnections) {
if(pinConnections.empty()) {
return false;
}
auto pinConnection = pinConnections[0];
if(auto directConnection = dynamic_cast<domain::DirectConnectionInstance*>(pinConnection)) {
return directConnection->bus->bus.getType() == domain::Bus::SINGLE_AUTOMATIC;
} else {
return false;
}
}
Bus::Bus(const std::shared_ptr<domain::BusInstance> &instance): busInstance(instance) {
instance->bus.getDisplayBus()->render(this, instance->size);
setToolTip(QString::fromStdString(busInstance->name + "::" + busInstance->bus.getName()));
}
void Bus::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
menu.addAction("Izmjeni ime", [this]() {
@ -130,6 +197,7 @@ namespace display {
auto newName = dialog->getName();
Application::instance()->renameBus(currentName, newName);
setToolTip(QString::fromStdString(busInstance->name + "::" + busInstance->bus.getName()));
});
menu.addSeparator();
menu.addAction(QString::fromStdString("Ukloni " + this->busInstance->name), [this]() {
@ -140,14 +208,6 @@ namespace display {
menu.exec(event->screenPos());
}
Bus::Bus(const std::shared_ptr<domain::BusInstance> &instance): busInstance(instance) {
instance->bus.getDisplayBus()->render(this, instance->size);
}
domain::BusInstance *Bus::getBusInstance() {
{ return busInstance.get(); }
}
QVariant BusGroup::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) {
if (change == ItemPositionChange && scene()) {
// value is the new position.
@ -212,24 +272,6 @@ namespace display {
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(
connection), component(component), bus(bus) {
updateConnection();
@ -254,36 +296,6 @@ namespace display {
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,
ComponentGroup *second): connection(connection), first(first), second(second) {
updateConnection();

View File

@ -17,9 +17,7 @@ namespace display {
std::shared_ptr<domain::ComponentInstance> componentInstance;
public:
Pin(domain::Pin pin, std::shared_ptr<domain::ComponentInstance> componentInstance) : pin(pin),
componentInstance(std::move(
componentInstance)) {
Pin(domain::Pin pin, std::shared_ptr<domain::ComponentInstance> componentInstance) : pin(pin), componentInstance(std::move(componentInstance)) {
pin.getDisplayPin().render(this);
this->setToolTip(QString::fromStdString(pin.getTooltip()));
}
@ -35,6 +33,8 @@ namespace display {
domain::Pin &getPin();
domain::ComponentInstance *getComponentInstance();
bool isSingleAutomatic(std::vector<domain::ConnectionInstance *> pinConnections);
};
class Component : public QGraphicsItemGroup {
@ -52,8 +52,6 @@ namespace display {
public:
explicit Bus(const std::shared_ptr<domain::BusInstance> &instance);
domain::BusInstance *getBusInstance();
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
};
@ -97,8 +95,6 @@ namespace display {
BusConnection(domain::BusConnectionInstance *connection, ComponentGroup *component, BusGroup *bus);
void updateConnection();
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
};
@ -112,8 +108,6 @@ namespace display {
DirectConnection(domain::DirectConnectionInstance *connection, ComponentGroup *first, ComponentGroup *second);
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);
layout->addWidget(edit);
setWindowTitle("Izmjeni ime");
button = new QPushButton("Ažuriraj", this);
connect(button, &QPushButton::clicked, this, &NameDialog::onNameChange);
layout->addWidget(button);
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) {

View File

@ -29,9 +29,18 @@ namespace display {
setupValues(firstLayout, values[0], &SingleAutomaticDialog::onFirstEnumerationChanged);
setupValues(secondLayout, values[1], &SingleAutomaticDialog::onSecondEnumerationChanged);
auto button = new QPushButton(updating ? "Ažuriraj" : "Postavi");
connect(button, &QPushButton::clicked, this, &SingleAutomaticDialog::onUpdate);
parentLayout->addWidget(button);
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);
parentLayout->addLayout(buttonLayout);
}
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) {
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);
errors.emplace_back(Action::ERROR, message);
} else if (count > comp.getCount().second) {
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);
}
}
@ -64,11 +64,11 @@ namespace domain {
if (count < bus.getCount().first) {
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);
} else if (count > bus.getCount().second) {
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);
}
}
@ -193,7 +193,7 @@ namespace domain {
if(names.find(component->name) != names.end()) {
context.attributes["componentName"] = Value::fromString(component->name);
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);
}
names.insert(component->name);
@ -234,11 +234,11 @@ namespace domain {
auto directConnection = dynamic_cast<DirectConnectionInstance *>(conn.get());
if (directConnection != nullptr) {
if (directConnection->instance->name == component->name &&
busConnection->connection.getComponent().pin == pin.getName()) {
directConnection->connection.getComponent().pin == pin.getName()) {
return true;
}
if (directConnection->secondInstance->name == component->name &&
busConnection->connection.getSecondComponent()->pin == pin.getName()) {
directConnection->connection.getSecondComponent()->pin == pin.getName()) {
return true;
}
}

View File

@ -7,18 +7,28 @@ namespace domain {
}
ConnectionInstance *Schema::getConnection(string &component, string &pin) {
auto pinConnections = getConnections(component, pin);
if(pinConnections.empty()) {
return nullptr;
} else {
return pinConnections[0];
}
}
std::vector<ConnectionInstance*> Schema::getConnections(string &component, string &pin) {
std::vector<ConnectionInstance*> pinConnections;
for (auto &conn: connections) {
if (conn->instance->name == component && conn->connection.getComponent().pin == pin) {
return conn.get();
pinConnections.push_back(conn.get());
}
auto dirConn = dynamic_cast<DirectConnectionInstance *>(conn.get());
if (dirConn != nullptr) {
if (dirConn->secondInstance->name == component && conn->connection.getSecondComponent()->pin == pin) {
return dirConn;
pinConnections.push_back(conn.get());
}
}
}
return nullptr;
return pinConnections;
}
BusInstance *Schema::getBusInstance(string &name) {

View File

@ -42,6 +42,8 @@ namespace domain {
ConnectionInstance *getConnection(string &component, string &pin);
std::vector<ConnectionEntry> availableConnections(std::string instance, std::string pin, bool onlyConnectable);
vector<ConnectionInstance *> getConnections(string &component, string &pin);
};
} // namespace domain