schema_editor/comdel/display/component_display.cpp

81 lines
2.6 KiB
C++

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