From 636d14a81f3e46fb7775cdac82bd3a21952c1f02 Mon Sep 17 00:00:00 2001
From: Vaclav Mares <maresva2@fit.cvut.cz>
Date: Fri, 7 Apr 2017 20:15:28 +0200
Subject: [PATCH] Connection basics

Add and remove connection
- propagation from GraphicsConnection
- add and remove in Wrapper, Model and Graphics boxes
---
 agui2/graphics/graphicsbox.cpp | 25 ++++++++++++++++++++-----
 agui2/graphics/graphicsbox.h   |  6 ++++--
 agui2/modelbox.cpp             | 10 ++++++++++
 agui2/modelbox.h               |  2 ++
 agui2/wrapperbox.cpp           | 12 ++++++++++++
 agui2/wrapperbox.h             |  4 ++--
 6 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/agui2/graphics/graphicsbox.cpp b/agui2/graphics/graphicsbox.cpp
index b84986f0d0..711e3a765c 100644
--- a/agui2/graphics/graphicsbox.cpp
+++ b/agui2/graphics/graphicsbox.cpp
@@ -49,11 +49,31 @@ void GraphicsBox::addOutput(GraphicsConnection *connection)
     m_OutConnection = connection;
 }
 
+void GraphicsBox::addInput(GraphicsConnection *connection)
+{
+    m_InConnections.append( connection );
+}
+
 void GraphicsBox::removeOutput()
 {
     m_OutConnection = NULL;
 }
 
+void GraphicsBox::removeInput(GraphicsConnection *connection)
+{
+    m_InConnections.removeOne( connection );
+}
+
+void GraphicsBox::disconnectInputEvent( WrapperBox* origin, GraphicsConnection *connection)
+{
+    m_wrapper->removeInput( origin, connection );
+}
+
+void GraphicsBox::disconnectOutputEvent()
+{
+    m_wrapper->removeOutput();
+}
+
 QPointF GraphicsBox::getConnectionOrigin()
 {
     return QPointF(m_boundRect.right(), m_boundRect.center().y());
@@ -64,11 +84,6 @@ QPointF GraphicsBox::getConnectionTarget()
     return QPointF(m_boundRect.left(), m_boundRect.center().y());
 }
 
-WrapperBox *GraphicsBox::wrapper()
-{
-    return m_wrapper;
-}
-
 void GraphicsBox::setWrapper(WrapperBox *wrapper)
 {
     m_wrapper = wrapper;
diff --git a/agui2/graphics/graphicsbox.h b/agui2/graphics/graphicsbox.h
index c6e758ac0b..859e4e96dc 100644
--- a/agui2/graphics/graphicsbox.h
+++ b/agui2/graphics/graphicsbox.h
@@ -23,19 +23,21 @@ public:
     virtual ~GraphicsBox(){}
     void paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget);
     void addOutput( GraphicsConnection* connection );
-    void addInput( GraphicsConnection* connection ) {}
+    void addInput( GraphicsConnection* connection );
     void removeOutput();
     void removeInput( GraphicsConnection* connection );
+    void disconnectInputEvent(WrapperBox *origin, GraphicsConnection* connection );
+    void disconnectOutputEvent();
     QRectF boundingRect() const;
     QPointF getConnectionOrigin();
     QPointF getConnectionTarget();
-    WrapperBox* wrapper();
     void setWrapper( WrapperBox* wrapper );
 protected:
     QFont m_font;
     QString m_text;
     QRectF m_boundRect;
     GraphicsConnection* m_OutConnection;
+    QList<GraphicsConnection*> m_InConnections;
     WrapperBox* m_wrapper;
     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
     virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) = 0;
diff --git a/agui2/modelbox.cpp b/agui2/modelbox.cpp
index d830f8a62a..ceed5758d8 100644
--- a/agui2/modelbox.cpp
+++ b/agui2/modelbox.cpp
@@ -24,6 +24,16 @@ bool ModelBox::addOutput(ModelBox *target)
     return true;
 }
 
+void ModelBox::removeInput(ModelBox *origin)
+{
+    m_InConnections.removeOne( origin );
+}
+
+void ModelBox::removeOutput()
+{
+    m_OutConnection = NULL;
+}
+
 bool ModelBox::isInputAvailable()
 {
     return ( m_InConnections.count() < m_maxInConnections );
diff --git a/agui2/modelbox.h b/agui2/modelbox.h
index 7afcfefa7e..ccbad54793 100644
--- a/agui2/modelbox.h
+++ b/agui2/modelbox.h
@@ -15,6 +15,8 @@ public:
     ModelBox(int maxInConnections, int maxOutConnections );
     bool addInput( ModelBox * origin );
     bool addOutput( ModelBox * target );
+    void removeInput( ModelBox * origin );
+    void removeOutput();
     bool isInputAvailable();
     bool isOutputAvailable();
     int availableInputs();
diff --git a/agui2/wrapperbox.cpp b/agui2/wrapperbox.cpp
index 5c0a7faca9..adc13e10b5 100644
--- a/agui2/wrapperbox.cpp
+++ b/agui2/wrapperbox.cpp
@@ -29,6 +29,18 @@ bool WrapperBox::addOutput(WrapperBox *target, GraphicsConnection *connection)
     return true;
 }
 
+bool WrapperBox::removeInput(WrapperBox *origin, GraphicsConnection *connection)
+{
+    m_model->removeInput( origin->getModel() );
+    m_graphics->removeInput( connection );
+}
+
+bool WrapperBox::removeOutput()
+{
+    m_model->removeOutput();
+    m_graphics->removeOutput();
+}
+
 ModelBox *WrapperBox::getModel()
 {
     return m_model;
diff --git a/agui2/wrapperbox.h b/agui2/wrapperbox.h
index 477fa47b1b..a65f30b1fb 100644
--- a/agui2/wrapperbox.h
+++ b/agui2/wrapperbox.h
@@ -16,8 +16,8 @@ public:
     ~WrapperBox();
     bool addInput( WrapperBox * origin, GraphicsConnection * connection );
     bool addOutput( WrapperBox * target, GraphicsConnection * connection );
-    bool removeInput( WrapperBox * origin, GraphicsConnection * connection ) {}
-    bool removeOutput() {}
+    bool removeInput( WrapperBox * origin, GraphicsConnection * connection );
+    bool removeOutput();
     ModelBox *getModel();
     GraphicsBox *getGraphics();
 private:
-- 
GitLab