From d2c396bc84d1d13befab3d77f2cc591996b2bf86 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Sat, 13 Sep 2014 00:43:00 +0200 Subject: [PATCH] Link nodes and remove and delete the link too when deleting one of the nodes. --HG-- branch : dfighter-tools --- .../plugins/gui_editor/expression_editor.cpp | 24 ++++++++++++++-- .../plugins/gui_editor/expression_link.cpp | 28 +++++++++++++++++-- .../src/plugins/gui_editor/expression_link.h | 14 ++++++---- .../plugins/gui_editor/expression_node.cpp | 12 ++++++++ .../src/plugins/gui_editor/expression_node.h | 11 ++++++++ 5 files changed, 77 insertions(+), 12 deletions(-) diff --git a/code/studio/src/plugins/gui_editor/expression_editor.cpp b/code/studio/src/plugins/gui_editor/expression_editor.cpp index 5fad4f054..48f807696 100644 --- a/code/studio/src/plugins/gui_editor/expression_editor.cpp +++ b/code/studio/src/plugins/gui_editor/expression_editor.cpp @@ -81,7 +81,24 @@ void ExpressionEditor::onDeleteSelection() QList< QGraphicsItem* > l = m_scene->selectedItems(); QListIterator< QGraphicsItem* > itr( l ); while( itr.hasNext() ) - m_scene->removeItem( itr.next() ); + { + QGraphicsItem *item = itr.next(); + + ExpressionNode *node = dynamic_cast< ExpressionNode* >( item ); + if( node != NULL ) + { + ExpressionLink *link = node->link(); + if( link != NULL ) + { + link->unlink(); + m_scene->removeItem( link ); + delete link; + } + } + + m_scene->removeItem( item ); + delete item; + } } void ExpressionEditor::onSelectionChanged() @@ -93,11 +110,12 @@ void ExpressionEditor::onSelectionChanged() void ExpressionEditor::onLinkItems() { QList< QGraphicsItem* > l = m_scene->selectedItems(); - QGraphicsItem *from = l[ 0 ]; - QGraphicsItem *to = l[ 1 ]; + ExpressionNode *from = static_cast< ExpressionNode* >( l[ 0 ] ); + ExpressionNode *to = static_cast< ExpressionNode* >( l[ 1 ] ); ExpressionLink *link = new ExpressionLink(); link->link( from, to ); + m_scene->addItem( link ); } diff --git a/code/studio/src/plugins/gui_editor/expression_link.cpp b/code/studio/src/plugins/gui_editor/expression_link.cpp index 55f0f8421..9fa6aa536 100644 --- a/code/studio/src/plugins/gui_editor/expression_link.cpp +++ b/code/studio/src/plugins/gui_editor/expression_link.cpp @@ -17,6 +17,7 @@ // along with this program. If not, see . #include "expression_link.h" +#include "expression_node.h" #include #include @@ -25,17 +26,38 @@ QGraphicsLineItem( parent ) { m_from = NULL; m_to = NULL; + + setFlags( QGraphicsItem::ItemIsSelectable ); } ExpressionLink::~ExpressionLink() { + unlink(); +} + +void ExpressionLink::link( ExpressionNode *from, ExpressionNode *to ) +{ + m_from = from; + m_to = to; + m_from->setLink( this ); + m_to->setLink( this ); + + nodeMoved(); +} + +void ExpressionLink::unlink() +{ + m_from->setLink( NULL ); + m_to->setLink( NULL ); +} + +void ExpressionLink::nodeMoved() +{ + setLine( QLineF( m_from->pos(), m_to->pos() ) ); } void ExpressionLink::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget ) { - if( m_from != NULL ) - setLine( QLineF( m_from->pos(), m_to->pos() ) ); - setPen( QPen( Qt::darkRed ) ); QGraphicsLineItem::paint( painter, option, widget ); diff --git a/code/studio/src/plugins/gui_editor/expression_link.h b/code/studio/src/plugins/gui_editor/expression_link.h index d45900691..59644980a 100644 --- a/code/studio/src/plugins/gui_editor/expression_link.h +++ b/code/studio/src/plugins/gui_editor/expression_link.h @@ -21,22 +21,24 @@ #include +class ExpressionNode; + class ExpressionLink : public QGraphicsLineItem { public: ExpressionLink( QGraphicsItem *parent = NULL ); ~ExpressionLink(); - void link( QGraphicsItem *from, QGraphicsItem *to ){ - m_from = from; - m_to = to; - } + void link( ExpressionNode *from, ExpressionNode *to ); + void unlink(); + + void nodeMoved(); void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget ); private: - QGraphicsItem *m_from; - QGraphicsItem *m_to; + ExpressionNode *m_from; + ExpressionNode *m_to; }; diff --git a/code/studio/src/plugins/gui_editor/expression_node.cpp b/code/studio/src/plugins/gui_editor/expression_node.cpp index 3fc0d7d2b..8e92b6705 100644 --- a/code/studio/src/plugins/gui_editor/expression_node.cpp +++ b/code/studio/src/plugins/gui_editor/expression_node.cpp @@ -18,12 +18,14 @@ #include "expression_node.h" +#include "expression_link.h" #include #include ExpressionNode::ExpressionNode( QGraphicsItem *parent ) : QGraphicsItem( parent ) { + m_link = NULL; } ExpressionNode::~ExpressionNode() @@ -48,3 +50,13 @@ void ExpressionNode::paint( QPainter *painter, const QStyleOptionGraphicsItem *o painter->drawRect( boundingRect() ); } + +void ExpressionNode::mouseMoveEvent( QGraphicsSceneMouseEvent *e ) +{ + if( m_link != NULL ) + m_link->nodeMoved(); + + QGraphicsItem::mouseMoveEvent( e ); +} + + diff --git a/code/studio/src/plugins/gui_editor/expression_node.h b/code/studio/src/plugins/gui_editor/expression_node.h index 354905c3e..a3bf701fd 100644 --- a/code/studio/src/plugins/gui_editor/expression_node.h +++ b/code/studio/src/plugins/gui_editor/expression_node.h @@ -22,6 +22,8 @@ #include +class ExpressionLink; + class ExpressionNode : public QGraphicsItem { public: @@ -31,6 +33,15 @@ public: QRectF boundingRect() const; void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget ); + void setLink( ExpressionLink *link ){ m_link = link; } + ExpressionLink* link() const{ return m_link; } + +protected: + void mouseMoveEvent( QGraphicsSceneMouseEvent *e ); + +private: + ExpressionLink *m_link; + }; #endif