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 <QMenu>
#include <QGraphicsSceneContextMenuEvent>
#include <iostream>
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

View File

@ -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<PinItem*> pinItems;
};
} // namespace display

View File

@ -15,14 +15,14 @@ void Schema::setSchema(std::optional<domain::Schema> schema)
this->schema = schema;
if(schema.has_value()) {
for(auto &instance: schema->instances) {
Component *group = nullptr;
ComponentWrapper *group = nullptr;
auto component = dynamic_cast<domain::ComponentInstance*>(instance);
if(component) {
group = Component::ofComponent(component);
group = ComponentWrapper::ofComponent(component);
}
auto bus = dynamic_cast<domain::BusInstance*>(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<domain::Schema> 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);
}