From 46711ec6b79a2567c201c2054d1ee7e3afbd3107 Mon Sep 17 00:00:00 2001 From: Martin Hanzik <martin@hanzik.com> Date: Wed, 28 Mar 2018 22:54:11 +0200 Subject: [PATCH] Rework connecting --- agui2/Graphics/Connection.cpp | 11 ++++++----- agui2/Graphics/Connection.h | 5 +++-- agui2/Graphics/ConnectionBox.cpp | 28 +++++++++++++++++----------- agui2/Graphics/ConnectionBox.h | 18 ++++++++++++------ agui2/Graphics/GraphicsBox.cpp | 4 ++-- agui2/Graphics/GraphicsBox.h | 4 +++- 6 files changed, 43 insertions(+), 27 deletions(-) diff --git a/agui2/Graphics/Connection.cpp b/agui2/Graphics/Connection.cpp index c343ce6158..6382a8dc41 100644 --- a/agui2/Graphics/Connection.cpp +++ b/agui2/Graphics/Connection.cpp @@ -4,17 +4,23 @@ #include <QPainter> #include <Graphics/ConnectionBox.h> +#include <Graphics/GraphicsBox.h> #include <Utils.h> Connection::Connection(ConnectionBox* origin, ConnectionBox* target) : originConnectionBox(origin) , targetConnectionBox(target) { + Q_ASSERT(origin->getType() == ConnectionBox::Type::Output); + Q_ASSERT(target->getType() == ConnectionBox::Type::Input); this->setZValue(2); this->boundRect = Utils::pointsToRect(origin->pos(), target->pos()); this->boundRect.adjust(-1, -1, 1, 1); origin->scene()->addItem(this); origin->scene()->update(); + + origin->getWrapperBox()->addOutput(target->getWrapperBox(), nullptr); + target->getWrapperBox()->addInput(origin->getWrapperBox(), nullptr); } QRectF Connection::boundingRect() const { @@ -45,8 +51,3 @@ void Connection::recalculateBoundingRect(const QPointF& a, const QPointF& b) { this->boundRect = Utils::pointsToRect(a, b); this->boundRect.adjust(-1, -1, 1, 1); } - -void Connection::destroy() { - this->originConnectionBox->connection.reset(); - this->targetConnectionBox->connection.reset(); -} diff --git a/agui2/Graphics/Connection.h b/agui2/Graphics/Connection.h index 89cf91ac3d..0b10090a67 100644 --- a/agui2/Graphics/Connection.h +++ b/agui2/Graphics/Connection.h @@ -11,8 +11,9 @@ public: QRectF boundingRect() const override; void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - - void destroy(); + + ConnectionBox* getOriginConnectionBox() const { return this->originConnectionBox; } + ConnectionBox* getTargetConnectionBox() const { return this->targetConnectionBox; } private: void recalculateBoundingRect(const QPointF& topLeft, const QPointF& bottomRight); diff --git a/agui2/Graphics/ConnectionBox.cpp b/agui2/Graphics/ConnectionBox.cpp index bc9ffb6590..ad016f9fc5 100644 --- a/agui2/Graphics/ConnectionBox.cpp +++ b/agui2/Graphics/ConnectionBox.cpp @@ -10,19 +10,15 @@ const QColor ConnectionBox::defaultColor = Qt::white; -ConnectionBox::ConnectionBox(GraphicsBox* parent, bool isInput) +ConnectionBox::ConnectionBox(GraphicsBox* parent, Type type) : QGraphicsRectItem(-8, -8, 16, 16, parent) - , isInput(isInput) + , _type(type) { this->setBrush(ConnectionBox::defaultColor); this->setAcceptedMouseButtons(Qt::LeftButton); } -GraphicsBox* ConnectionBox::getParent() const { - return dynamic_cast<GraphicsBox*>(this->parentObject()); -} - void ConnectionBox::mousePressEvent(QGraphicsSceneMouseEvent* event) { this->setCursor(Qt::UpArrowCursor); @@ -53,7 +49,7 @@ void ConnectionBox::mouseMoveEvent(QGraphicsSceneMouseEvent* event) { } } - if (other && other->isInput == !this->isInput) + if (other && other->getType() != this->getType()) { other->setColor(Qt::yellow); } @@ -67,7 +63,7 @@ void ConnectionBox::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) { this->tempLine = nullptr; auto* other = dynamic_cast<ConnectionBox*>(this->scene()->itemAt(event->scenePos(), {})); - if (other && other->isInput == !this->isInput) + if (other && other->getType() != this->getType()) { this->setColor(ConnectionBox::defaultColor); other->setColor(ConnectionBox::defaultColor); @@ -76,10 +72,10 @@ void ConnectionBox::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) { } void ConnectionBox::connect(ConnectionBox* a, ConnectionBox* b) { - Q_ASSERT(a->isInput != b->isInput); + Q_ASSERT(a->getType() != b->getType()); auto* origin = a; auto* target = b; - if (origin->isInput) + if (origin->getType() == ConnectionBox::Type::Input) std::swap(origin, target); origin->connection = target->connection = std::make_shared<Connection>(origin, target); @@ -94,7 +90,13 @@ void ConnectionBox::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) { } void ConnectionBox::on_Disconnect() { - this->connection->destroy(); + auto* origin = this->connection->getOriginConnectionBox(); + auto* target = this->connection->getTargetConnectionBox(); + origin->getWrapperBox()->removeOutput(); + target->getWrapperBox()->removeInput(origin->getWrapperBox(), nullptr); + + origin->connection.reset(); + target->connection.reset(); } void ConnectionBox::setColor(QColor color) { @@ -102,3 +104,7 @@ void ConnectionBox::setColor(QColor color) { brush.setColor(color); this->setBrush(brush); } + +WrapperBox* ConnectionBox::getWrapperBox() const { + return dynamic_cast<GraphicsBox*>(this->parentObject())->getWrapperBox(); +} diff --git a/agui2/Graphics/ConnectionBox.h b/agui2/Graphics/ConnectionBox.h index c4fafe4357..c560aae739 100644 --- a/agui2/Graphics/ConnectionBox.h +++ b/agui2/Graphics/ConnectionBox.h @@ -6,17 +6,25 @@ #include <Graphics/Connection.h> class GraphicsBox; +class WrapperBox; class ConnectionBox : public QObject, public QGraphicsRectItem { Q_OBJECT -friend class Connection; - public: - ConnectionBox(GraphicsBox* parent, bool isInput); + enum class Type + { + Input, + Output + }; + + ConnectionBox(GraphicsBox* parent, Type type); static const QColor defaultColor; + Type getType() const { return this->_type; } + WrapperBox* getWrapperBox() const; + protected: void mousePressEvent(QGraphicsSceneMouseEvent* event) override; void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override; @@ -29,11 +37,9 @@ protected: private: void setColor(QColor color); - GraphicsBox* getParent() const; - std::shared_ptr<Connection> connection; QGraphicsLineItem* tempLine = nullptr; - bool isInput; + Type _type; private slots: void on_Disconnect(); diff --git a/agui2/Graphics/GraphicsBox.cpp b/agui2/Graphics/GraphicsBox.cpp index da59c5c9b4..18fa0b1446 100644 --- a/agui2/Graphics/GraphicsBox.cpp +++ b/agui2/Graphics/GraphicsBox.cpp @@ -28,7 +28,7 @@ GraphicsBox::GraphicsBox(QString text, qreal x, qreal y, uint8_t inputs, uint8_t this->inputConnections.reserve(inputs); for (uint8_t i = 0; i < inputs; ++i) { - auto* box = new ConnectionBox(this, true); + auto* box = new ConnectionBox(this, ConnectionBox::Type::Input); box->setPos(this->m_boundRect.left(), this->m_boundRect.top() + ((i + 1) * this->m_boundRect.height()) / float(inputs + 1)); this->inputConnections.push_back(box); } @@ -36,7 +36,7 @@ GraphicsBox::GraphicsBox(QString text, qreal x, qreal y, uint8_t inputs, uint8_t this->outputConnections.reserve(outputs); for (uint8_t i = 0; i < outputs; ++i) { - auto* box = new ConnectionBox(this, false); + auto* box = new ConnectionBox(this, ConnectionBox::Type::Output); box->setPos(this->m_boundRect.right(), this->m_boundRect.top() + ((i + 1) * this->m_boundRect.height()) / float(outputs + 1)); this->outputConnections.push_back(box); } diff --git a/agui2/Graphics/GraphicsBox.h b/agui2/Graphics/GraphicsBox.h index 10870b613d..b47fecfa73 100644 --- a/agui2/Graphics/GraphicsBox.h +++ b/agui2/Graphics/GraphicsBox.h @@ -19,6 +19,8 @@ class GraphicsBox : public QGraphicsObject { Q_OBJECT public: + friend class WrapperBox; + explicit GraphicsBox(QString text, qreal x = 0, qreal y = 0, uint8_t inputs = 1, uint8_t outputs = 1); ~GraphicsBox() override; @@ -30,7 +32,7 @@ public: QRectF boundingRect() const override; QPointF getConnectionOrigin(); virtual QPointF getConnectionTarget( GraphicsConnection* connection ); - friend class WrapperBox; + WrapperBox* getWrapperBox() const { return this->m_wrapper; } protected: QFont m_font; QColor m_color; -- GitLab