Added pin connection editing

This commit is contained in:
Borna Rajković 2022-06-09 23:42:45 +02:00
parent f66fc1db26
commit 4d4cf136fb
6 changed files with 119 additions and 23 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) {

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

@ -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.

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;
};

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