Separated components from pins

This commit is contained in:
Borna Rajkovic 2022-04-10 17:24:15 +02:00
parent e8505f9cef
commit 389113722e
3 changed files with 90 additions and 25 deletions

View File

@ -1,31 +1,42 @@
#include "component_display.h" #include "component_display.h"
#include <QMenu>
#include <QGraphicsSceneContextMenuEvent>
#include <iostream>
namespace display { namespace display {
Component *Component::ofWire(domain::WireInstance *wire) { ComponentWrapper *ComponentWrapper::ofWire(domain::WireInstance *wire) {
auto component = new Component(); auto component = new ComponentWrapper();
component->wireInstance = wire; component->wireInstance = wire;
component->redraw(); component->redraw();
return component; return component;
} }
Component *Component::ofComponent(domain::ComponentInstance *instance) { ComponentWrapper *ComponentWrapper::ofComponent(domain::ComponentInstance *instance) {
auto component = new Component(); auto component = new ComponentWrapper();
component->componentInstance = instance; component->componentInstance = instance;
component->redraw(); component->setFlag(QGraphicsItem::ItemIsMovable, true);
return component; component->componentItem = new ComponentItem(instance, component);
}
Component *Component::ofBus(domain::BusInstance *instance) { for(auto& pin: instance->component.getPins()) {
auto component = new Component(); component->pinItems.push_back(new PinItem(pin, component));
component->busInstance = instance;
component->redraw();
return component;
} }
void Component::redraw() { 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) { if(componentInstance) {
componentInstance->component.getDisplay().render(this); componentItem->redraw();
for(auto &pin: componentInstance->component.getPins()) { for(auto pinItem: pinItems) {
pin.getDisplay().render(this); pinItem->redraw();
} }
this->addToGroup(new QGraphicsSimpleTextItem(QString::fromStdString(componentInstance->name))); 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 } // namespace display

View File

@ -8,19 +8,46 @@
namespace display { namespace display {
class Component: public QGraphicsItemGroup class ComponentItem: public QGraphicsItemGroup
{ {
public: public:
static Component *ofComponent(domain::ComponentInstance *instance); ComponentItem(domain::ComponentInstance *instance, QGraphicsItem *parent);
static Component *ofBus(domain::BusInstance *instance); void redraw();
static Component *ofWire(domain::WireInstance *wire); 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(); void redraw();
private: private:
domain::ComponentInstance *componentInstance = nullptr; domain::ComponentInstance *componentInstance = nullptr;
domain::BusInstance *busInstance = nullptr; domain::BusInstance *busInstance = nullptr;
domain::WireInstance *wireInstance = nullptr; domain::WireInstance *wireInstance = nullptr;
ComponentItem *componentItem;
std::vector<PinItem*> pinItems;
}; };
} // namespace display } // namespace display

View File

@ -15,14 +15,14 @@ void Schema::setSchema(std::optional<domain::Schema> schema)
this->schema = schema; this->schema = schema;
if(schema.has_value()) { if(schema.has_value()) {
for(auto &instance: schema->instances) { for(auto &instance: schema->instances) {
Component *group = nullptr; ComponentWrapper *group = nullptr;
auto component = dynamic_cast<domain::ComponentInstance*>(instance); auto component = dynamic_cast<domain::ComponentInstance*>(instance);
if(component) { if(component) {
group = Component::ofComponent(component); group = ComponentWrapper::ofComponent(component);
} }
auto bus = dynamic_cast<domain::BusInstance*>(instance); auto bus = dynamic_cast<domain::BusInstance*>(instance);
if(bus) { if(bus) {
group = Component::ofBus(bus); group = ComponentWrapper::ofBus(bus);
} }
if(group != nullptr) { if(group != nullptr) {
group->setPos(instance->position.first, instance->position.second); group->setPos(instance->position.first, instance->position.second);
@ -30,7 +30,7 @@ void Schema::setSchema(std::optional<domain::Schema> schema)
} }
} }
for(auto &wire: schema->wires) { for(auto &wire: schema->wires) {
auto group = Component::ofWire(wire); auto group = ComponentWrapper::ofWire(wire);
group->setPos(wire->position.first, wire->position.second); group->setPos(wire->position.first, wire->position.second);
scene.addItem(group); scene.addItem(group);
} }