From 389113722ea6e87d00cb7d496fd5deca20a8c183 Mon Sep 17 00:00:00 2001 From: Borna Rajkovic Date: Sun, 10 Apr 2022 17:24:15 +0200 Subject: [PATCH] Separated components from pins --- comdel/display/component_display.cpp | 72 +++++++++++++++++++++------- comdel/display/component_display.h | 35 ++++++++++++-- comdel/display/schema_display.cpp | 8 ++-- 3 files changed, 90 insertions(+), 25 deletions(-) diff --git a/comdel/display/component_display.cpp b/comdel/display/component_display.cpp index ce95660..2fa6483 100644 --- a/comdel/display/component_display.cpp +++ b/comdel/display/component_display.cpp @@ -1,31 +1,42 @@ #include "component_display.h" +#include +#include +#include + namespace display { -Component *Component::ofWire(domain::WireInstance *wire) { - auto component = new Component(); +ComponentWrapper *ComponentWrapper::ofWire(domain::WireInstance *wire) { + auto component = new ComponentWrapper(); component->wireInstance = wire; component->redraw(); return component; } -Component *Component::ofComponent(domain::ComponentInstance *instance) { - auto component = new Component(); +ComponentWrapper *ComponentWrapper::ofComponent(domain::ComponentInstance *instance) { + auto component = new ComponentWrapper(); component->componentInstance = instance; - component->redraw(); - return component; -} -Component *Component::ofBus(domain::BusInstance *instance) { - auto component = new Component(); - component->busInstance = instance; - component->redraw(); - return component; -} + component->setFlag(QGraphicsItem::ItemIsMovable, true); + component->componentItem = new ComponentItem(instance, component); -void Component::redraw() { + for(auto& pin: instance->component.getPins()) { + component->pinItems.push_back(new PinItem(pin, component)); + } + + component->redraw(); + return component; +} +ComponentWrapper *ComponentWrapper::ofBus(domain::BusInstance *instance) { + auto component = new ComponentWrapper(); + component->busInstance = instance; + component->setFlag(QGraphicsItem::ItemIsMovable, true); + component->redraw(); + return component; +} +void ComponentWrapper::redraw() { if(componentInstance) { - componentInstance->component.getDisplay().render(this); - for(auto &pin: componentInstance->component.getPins()) { - pin.getDisplay().render(this); + componentItem->redraw(); + for(auto pinItem: pinItems) { + pinItem->redraw(); } this->addToGroup(new QGraphicsSimpleTextItem(QString::fromStdString(componentInstance->name))); } @@ -39,4 +50,31 @@ void Component::redraw() { } } +ComponentItem::ComponentItem(domain::ComponentInstance *instance, QGraphicsItem *parent): componentInstance(instance) { + setParentItem(parent); + redraw(); + setToolTip(QString::fromStdString(this->componentInstance->component.getTooltip())); +} +void ComponentItem::redraw() { + componentInstance->component.getDisplay().render(this); +} + +void ComponentItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { + QMenu menu; + menu.addAction("Izmjeni ime"); + menu.addSeparator(); + for(auto attr: componentInstance->attributes) { + menu.addAction(QString::fromStdString("Izmjeni " + attr->name)); + } + menu.exec(event->screenPos()); +} + +PinItem::PinItem(domain::Pin pin, QGraphicsItem *parent): pin(pin) { + setParentItem(parent); + redraw(); + setToolTip(QString::fromStdString(pin.getTooltip())); +} +void PinItem::redraw() { + pin.getDisplay().render(this); +} } // namespace display diff --git a/comdel/display/component_display.h b/comdel/display/component_display.h index b299185..b2151d5 100644 --- a/comdel/display/component_display.h +++ b/comdel/display/component_display.h @@ -8,19 +8,46 @@ namespace display { -class Component: public QGraphicsItemGroup +class ComponentItem: public QGraphicsItemGroup { public: - static Component *ofComponent(domain::ComponentInstance *instance); - static Component *ofBus(domain::BusInstance *instance); - static Component *ofWire(domain::WireInstance *wire); + ComponentItem(domain::ComponentInstance *instance, QGraphicsItem *parent); + void redraw(); +private: + domain::ComponentInstance *componentInstance; +public: + void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override; +}; + +class PinItem: public QGraphicsItemGroup +{ +public: + PinItem(domain::Pin pin, QGraphicsItem *parent); + void redraw(); +private: + domain::Pin pin; +}; + +class ComponentWrapper: public QGraphicsItemGroup +{ +public: + static ComponentWrapper *ofComponent(domain::ComponentInstance *instance); + static ComponentWrapper *ofBus(domain::BusInstance *instance); + static ComponentWrapper *ofWire(domain::WireInstance *wire); + + ComponentWrapper() { + this->setHandlesChildEvents(false); + } void redraw(); private: domain::ComponentInstance *componentInstance = nullptr; domain::BusInstance *busInstance = nullptr; domain::WireInstance *wireInstance = nullptr; + + ComponentItem *componentItem; + std::vector pinItems; }; } // namespace display diff --git a/comdel/display/schema_display.cpp b/comdel/display/schema_display.cpp index f415130..7b18878 100644 --- a/comdel/display/schema_display.cpp +++ b/comdel/display/schema_display.cpp @@ -15,14 +15,14 @@ void Schema::setSchema(std::optional schema) this->schema = schema; if(schema.has_value()) { for(auto &instance: schema->instances) { - Component *group = nullptr; + ComponentWrapper *group = nullptr; auto component = dynamic_cast(instance); if(component) { - group = Component::ofComponent(component); + group = ComponentWrapper::ofComponent(component); } auto bus = dynamic_cast(instance); if(bus) { - group = Component::ofBus(bus); + group = ComponentWrapper::ofBus(bus); } if(group != nullptr) { group->setPos(instance->position.first, instance->position.second); @@ -30,7 +30,7 @@ void Schema::setSchema(std::optional schema) } } for(auto &wire: schema->wires) { - auto group = Component::ofWire(wire); + auto group = ComponentWrapper::ofWire(wire); group->setPos(wire->position.first, wire->position.second); scene.addItem(group); }