From 6ca89f09950459129b717c65b6d17829f4e80028 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 3 Dec 2020 14:55:03 +0100 Subject: [PATCH] Wires now move when the component is dragged --- include/Component.hpp | 13 ++++++++++++- include/Window.hpp | 2 +- src/Component.cpp | 28 +++++++++++++++++++++++++++- src/Window.cpp | 22 ++++++++++++++-------- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/include/Component.hpp b/include/Component.hpp index d98e7bc..cc780d3 100644 --- a/include/Component.hpp +++ b/include/Component.hpp @@ -6,17 +6,27 @@ namespace Ui { class Component; } +typedef struct { + QLine* line; + enum class eType { + SOURCE, + TARGET + } type; +} Wire; + class Component : public QFrame { public: explicit Component(QWidget* parent); explicit Component(QWidget* parent, const QString& resource); + ~Component(); virtual void mouseMoveEvent(QMouseEvent* event) override; QPoint CenterPos(); - void Connect(Component* component); + void Connect(Component* component, QLine* wire); const QString& Type(); + void UpdateWires(); virtual bool Action() = 0; @@ -26,4 +36,5 @@ protected: QString type; std::vector connections; + std::vector wires; }; \ No newline at end of file diff --git a/include/Window.hpp b/include/Window.hpp index 29f8e22..a5d0ed0 100644 --- a/include/Window.hpp +++ b/include/Window.hpp @@ -50,5 +50,5 @@ private: } componentType; std::vector components; - std::vector wires; + std::vector wires; }; \ No newline at end of file diff --git a/src/Component.cpp b/src/Component.cpp index 9d39cc3..f79f91e 100644 --- a/src/Component.cpp +++ b/src/Component.cpp @@ -19,6 +19,15 @@ Component::Component(QWidget* parent, const QString& resource) : ui->label->setPixmap(QPixmap(resource)); } +Component::~Component() +{ + for (Wire* w : wires) + { + delete w; + w = nullptr; + } +} + void Component::mouseMoveEvent(QMouseEvent* event) { event->ignore(); @@ -29,12 +38,29 @@ QPoint Component::CenterPos() return pos() + QPoint(size().width() / 2, size().height() / 2); } -void Component::Connect(Component* component) +void Component::Connect(Component* component, QLine* wire) { connections.push_back(component); + this->wires.push_back(new Wire{ wire, Wire::eType::TARGET}); + component->wires.push_back(new Wire{ wire, Wire::eType::SOURCE}); } const QString& Component::Type() { return type; } + +void Component::UpdateWires() +{ + for (Wire* wire : wires) + { + if (wire->type == Wire::eType::SOURCE) + { + wire->line->setP1(this->CenterPos()); + } + else + { + wire->line->setP2(this->CenterPos()); + } + } +} diff --git a/src/Window.cpp b/src/Window.cpp index e41a0d2..b36f165 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -76,6 +76,12 @@ Window::~Window() delete c; c = nullptr; } + + for (QLine* line : wires) + { + delete line; + line = nullptr; + } } void Window::mousePressEvent(QMouseEvent* event) @@ -101,7 +107,7 @@ void Window::mousePressEvent(QMouseEvent* event) QPoint mousePos = ui->centralwidget->mapFromParent(event->pos()); mousePos.setY(mousePos.y() + ui->toolBar->height()); - wires.push_back(QLine(child->CenterPos(), mousePos)); + wires.push_back(new QLine(child->CenterPos(), mousePos)); // std::cout << "(" << child->x() << ", " << child->y() << ") --> (" << mousePos.x() << ", " << mousePos.y() << ")" << std::endl; } @@ -121,7 +127,7 @@ void Window::mouseMoveEvent(QMouseEvent* event) if (dragInfo.component != nullptr) { dragInfo.component->move(event->pos() - dragInfo.relativePos); - // Move wires too lol + dragInfo.component->UpdateWires(); } if (ghostImage != nullptr) @@ -131,7 +137,7 @@ void Window::mouseMoveEvent(QMouseEvent* event) { QPoint mousePos = ui->centralwidget->mapFromParent(event->pos()); mousePos.setY(mousePos.y() + ui->toolBar->height()); - wires.back().setP2(mousePos); + wires.back()->setP2(mousePos); } event->accept(); @@ -144,7 +150,7 @@ void Window::mouseReleaseEvent(QMouseEvent* event) if (componentGroup->checkedAction() == ui->actionWiring) { - Component* connectFrom = componentAt(wires.back().p1()); + Component* connectFrom = componentAt(wires.back()->p1()); Component* connectTo = componentAt(event->pos()); if (connectTo == nullptr || connectTo == connectFrom) { @@ -152,8 +158,8 @@ void Window::mouseReleaseEvent(QMouseEvent* event) } else { - wires.back().setP2(connectTo->CenterPos()); - connectTo->Connect(connectFrom); + wires.back()->setP2(connectTo->CenterPos()); + connectTo->Connect(connectFrom, wires.back()); } } else if(componentGroup->checkedAction() == ui->actionCursor) @@ -172,8 +178,8 @@ void Window::paintEvent(QPaintEvent* event) QPainter painter(this); painter.setPen(pen); - for(QLine line : wires) - painter.drawLine(line); + for(QLine* line : wires) + painter.drawLine(*line); update(); }