From 9e2b0f6c3caa3bae96907675e5f646f1fc347ada Mon Sep 17 00:00:00 2001 From: Borna Rajkovic Date: Wed, 20 Apr 2022 21:50:39 +0200 Subject: [PATCH] WIP --- comdel/display/component_display.cpp | 50 +++++++++++++++++++++++++--- comdel/display/component_display.h | 19 +++++++++++ comdel/display/name_dialog.h | 36 ++++++++++++++++---- 3 files changed, 94 insertions(+), 11 deletions(-) diff --git a/comdel/display/component_display.cpp b/comdel/display/component_display.cpp index 9fc2b10..5a3b4fe 100644 --- a/comdel/display/component_display.cpp +++ b/comdel/display/component_display.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "dialogmanager.h" @@ -34,10 +35,27 @@ ComponentWrapper *ComponentWrapper::ofBus(domain::BusInstance *instance) { component->redraw(); return component; } -void ComponentWrapper::redraw() { +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))); @@ -60,17 +78,33 @@ ComponentItem::ComponentItem(domain::ComponentInstance *instance, QGraphicsItem 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 componentInstance = this->componentInstance; - menu.addAction("Izmjeni ime", [componentInstance](){DialogManager::updateName(componentInstance);}); + auto instance = this->componentInstance; + menu.addAction("Izmjeni ime", [instance, this](){ + DialogManager::updateName(instance); + auto *wrapper = static_cast(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](){DialogManager::updateAttribute(attr);}); + [attr, this]() { + DialogManager::updateAttribute(attr); + + this->redraw(); + }); action->setEnabled(enabled); } menu.exec(event->screenPos()); @@ -84,4 +118,12 @@ PinItem::PinItem(domain::Pin pin, QGraphicsItem *parent): pin(pin) { 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 diff --git a/comdel/display/component_display.h b/comdel/display/component_display.h index b2151d5..6259656 100644 --- a/comdel/display/component_display.h +++ b/comdel/display/component_display.h @@ -13,6 +13,7 @@ class ComponentItem: public QGraphicsItemGroup public: ComponentItem(domain::ComponentInstance *instance, QGraphicsItem *parent); void redraw(); + void clear(); private: domain::ComponentInstance *componentInstance; @@ -25,6 +26,7 @@ class PinItem: public QGraphicsItemGroup public: PinItem(domain::Pin pin, QGraphicsItem *parent); void redraw(); + void clear(); private: domain::Pin pin; }; @@ -40,6 +42,23 @@ public: this->setHandlesChildEvents(false); } void redraw(); + void clear(); + + + QVariant itemChange( GraphicsItemChange change, const QVariant &value ) override + { + auto response = QGraphicsItem::itemChange(change, value); + if (change == ItemPositionChange){ + if(componentInstance != nullptr) { + componentInstance->position.first = (int)scenePos().x(); + componentInstance->position.second = (int)scenePos().y(); + } else if(busInstance != nullptr) { + busInstance->position.first = (int)scenePos().x(); + busInstance->position.second = (int)scenePos().y(); + } + } + return response; + } private: domain::ComponentInstance *componentInstance = nullptr; diff --git a/comdel/display/name_dialog.h b/comdel/display/name_dialog.h index f73ccd7..ba18eb6 100644 --- a/comdel/display/name_dialog.h +++ b/comdel/display/name_dialog.h @@ -1,23 +1,45 @@ -// -// Created by bbr on 18. 04. 2022.. -// - #ifndef NAME_DIALOG_H #define NAME_DIALOG_H #include +#include +#include +#include +#include + #include namespace display { class NameDialog: public QDialog { -public: - NameDialog(domain::ComponentInstance *instance) { + QLineEdit *edit; + domain::ComponentInstance *instance; + +public: + + NameDialog(domain::ComponentInstance *instance): instance(instance) { + auto *layout = new QVBoxLayout(this); + layout->addWidget(new QLabel("Izmjeni ime", this)); + + edit = new QLineEdit(this); + edit->insert(instance->name.c_str()); + layout->addWidget(edit); + this->setWindowTitle("Izmjeni ime"); + auto *button = new QPushButton("Ažuriraj", this); + connect(button, &QPushButton::clicked, this, &NameDialog::onNameChange); + layout->addWidget(button); + this->setLayout(layout); + } + +public slots: + void onNameChange() { + instance->name = this->edit->text().toStdString(); + this->close(); } }; } -#endif //sNAME_DIALOG_H +#endif //NAME_DIALOG_H