#include "component_display.h" #include "attribute_dialog.h" #include "name_dialog.h" #include "mainwindow.h" #include "application.h" #include #include #include namespace display { void Component::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { QMenu menu; menu.addAction("Izmjeni ime", [this]() { auto dialog = new NameDialog(this->instance.get()); dialog->exec(); }); menu.addSeparator(); for (int i = 0; i < this->instance->attributes.size(); i++) { auto *attr = &this->instance->attributes[i]; bool enabled = attr->attribute.getPopup().has_value(); auto action = menu.addAction(QString::fromStdString("Izmjeni " + attr->name), [attr]() { if (attr->value.getType() == domain::Value::MEMORY_REFERENCE) { auto dialog = new MemoryDialog(attr, Application::instance()->getSchema()->componentInstances); dialog->exec(); } else { auto dialog = new AttributeDialog(attr); dialog->exec(); } }); action->setEnabled(enabled); } menu.exec(event->screenPos()); } Component::Component(const std::shared_ptr &instance): instance(instance) { setFlag(ItemSendsGeometryChanges, true); instance->component.getDisplay().render(this); } void Pin::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { QMenu menu; menu.addAction("Connect pin", [&]() {}); menu.exec(event->screenPos()); } void Pin::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::MouseButton::LeftButton) { auto view = dynamic_cast(this->scene()->views()[0]); view->state = Schema::CREATING_CONNECTION; view->context.pin = this; view->context.startingPoint = dynamic_cast(this->parentItem())->pos() + QPointF(pin.getDisplayPin().getConnectionX(), pin.getDisplayPin().getConnectionY()); view->context.line = new QGraphicsLineItem(QLineF(view->context.startingPoint, event->scenePos())); this->scene()->addItem(view->context.line); view->showConnectable(this); } } void Pin::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { auto view = dynamic_cast(this->scene()->views()[0]); if (view->state == Schema::CREATING_CONNECTION) { auto line = view->context.line->line(); line.setP2(event->scenePos()); view->context.line->setLine(line); } } void Pin::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::MouseButton::LeftButton) { auto view = dynamic_cast(this->scene()->views()[0]); view->state = Schema::DEFAULT; this->scene()->removeItem(view->context.line); delete view->context.line; view->removeConnectable(event->scenePos()); } } domain::Pin &Pin::getPin() { return pin; } domain::ComponentInstance *Pin::getComponentInstance() { return componentInstance.get(); } void Bus::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { QMenu menu; menu.addAction("Izmjeni ime", [this]() { auto dialog = new NameDialog(this->busInstance.get()); dialog->exec(); }); menu.exec(event->screenPos()); } Bus::Bus(const std::shared_ptr &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. QPointF newPos = value.toPointF(); busInstance->position.first = newPos.x(); busInstance->position.second = newPos.y(); auto view = dynamic_cast(scene()->views()[0]); view->updateConnections(); } return QGraphicsItem::itemChange(change, value); } BusGroup::BusGroup(const std::shared_ptr &instance) : busInstance(instance) { setFlag(ItemIsMovable, true); setFlag(ItemSendsGeometryChanges, true); setHandlesChildEvents(false); bus = new display::Bus(instance); addToGroup(bus); setPos(instance->position.first, instance->position.second); } QVariant ComponentGroup::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) { if (change == ItemPositionChange && scene()) { // value is the new position. QPointF newPos = value.toPointF(); componentInstance->position.first = newPos.x(); componentInstance->position.second = newPos.y(); auto view = dynamic_cast(scene()->views()[0]); view->updateConnections(); } return QGraphicsItem::itemChange(change, value); } std::shared_ptr ComponentGroup::getComponentInstance() { return componentInstance; } std::vector &ComponentGroup::getPins() { return pins; } ComponentGroup::ComponentGroup(const std::shared_ptr &instance): componentInstance( instance) { setFlag(ItemIsMovable, true); setFlag(ItemSendsGeometryChanges, true); setHandlesChildEvents(false); addToGroup(new display::Component(instance)); for (auto &pin: instance->component.getPins()) { pins.push_back(new display::Pin(pin, componentInstance)); addToGroup(pins.back()); } 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(); setHandlesChildEvents(false); } void BusConnection::updateConnection() { auto busPosition = bus->boundingRect(); auto pin = connection->instance->component.getPin( connection->connection.getComponent().pin).getDisplayPin(); setLine(connection->instance->position.first + pin.getConnectionX(), connection->instance->position.second + pin.getConnectionY(), connection->bus->position.first + busPosition.width() / 2, connection->bus->position.second + busPosition.height() / 2); connection->start.first = connection->instance->position.first + pin.getConnectionX(); connection->start.second = connection->instance->position.second + pin.getConnectionY(); connection->end.first = connection->bus->position.first + busPosition.width() / 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(); 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(); setHandlesChildEvents(false); } void DirectConnection::updateConnection() { auto pin1 = connection->instance->component.getPin( connection->connection.getComponent().pin).getDisplayPin(); auto pin2 = connection->secondInstance->component.getPin( connection->connection.getSecondComponent()->pin).getDisplayPin(); setLine(connection->instance->position.first + pin1.getConnectionX(), connection->instance->position.second + pin1.getConnectionY(), connection->secondInstance->position.first + pin2.getConnectionX(), connection->secondInstance->position.second + pin2.getConnectionY()); connection->start.first = connection->instance->position.first + pin1.getConnectionX(); connection->start.second = connection->instance->position.second + pin1.getConnectionY(); connection->end.first = connection->secondInstance->position.first + pin2.getConnectionX(); connection->end.second = connection->secondInstance->position.second + pin2.getConnectionY(); } } // namespace display