schema_editor/comdel/display/component_display.cpp

156 lines
4.7 KiB
C++

#include "component_display.h"
#include "attribute_dialog.h"
#include "name_dialog.h"
#include <QMenu>
#include <QGraphicsSceneContextMenuEvent>
#include <QGraphicsScene>
#include <iostream>
namespace display {
void Component::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
menu.addAction("Izmjeni ime", [this](){});
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]() {
auto dialog = new AttributeDialog(attr);
dialog->exec();
});
action->setEnabled(enabled);
}
menu.exec(event->screenPos());
}
void Bus::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
menu.addAction("Izmjeni ime", [this](){});
menu.exec(event->screenPos());
}
/*
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::clear() {
for(auto item: childItems()) {
this->removeFromGroup(item);
item->scene()->removeItem(item);
delete item;
}
if(componentInstance) {
componentItem->clear();
for(auto pinItem: pinItems) {
pinItem->clear();
}
}
}
void ComponentWrapper::redraw() {
if(componentInstance) {
addToGroup(componentItem);
componentItem->redraw();
for(auto pinItem: pinItems) {
addToGroup(pinItem);
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::clear() {
for(auto item: childItems()) {
this->removeFromGroup(item);
item->scene()->removeItem(item);
delete item;
}
}
void ComponentItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
auto instance = this->componentInstance;
menu.addAction("Izmjeni ime", [instance, this](){
DialogManager::updateName(instance);
auto *wrapper = static_cast<ComponentWrapper*>(this->parentItem());
wrapper->clear();
wrapper->redraw();
});
menu.addSeparator();
for(auto attr: componentInstance->attributes) {
bool enabled = attr.attribute.getPopup().has_value();
auto action = menu.addAction(QString::fromStdString("Izmjeni " + attr.name),
[&attr, this]() {
DialogManager::updateAttribute(&attr);
this->redraw();
});
action->setEnabled(enabled);
}
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);
}
void PinItem::clear() {
for(auto item: childItems()) {
this->removeFromGroup(item);
item->scene()->removeItem(item);
delete item;
}
}
*/
} // namespace display