Link nodes and remove and delete the link too when deleting one of the nodes.

This commit is contained in:
dfighter1985 2014-09-13 00:43:00 +02:00
parent 7c0bbf71f9
commit 4b7e1c95a6
5 changed files with 77 additions and 12 deletions

View file

@ -81,7 +81,24 @@ void ExpressionEditor::onDeleteSelection()
QList< QGraphicsItem* > l = m_scene->selectedItems(); QList< QGraphicsItem* > l = m_scene->selectedItems();
QListIterator< QGraphicsItem* > itr( l ); QListIterator< QGraphicsItem* > itr( l );
while( itr.hasNext() ) 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() void ExpressionEditor::onSelectionChanged()
@ -93,11 +110,12 @@ void ExpressionEditor::onSelectionChanged()
void ExpressionEditor::onLinkItems() void ExpressionEditor::onLinkItems()
{ {
QList< QGraphicsItem* > l = m_scene->selectedItems(); QList< QGraphicsItem* > l = m_scene->selectedItems();
QGraphicsItem *from = l[ 0 ]; ExpressionNode *from = static_cast< ExpressionNode* >( l[ 0 ] );
QGraphicsItem *to = l[ 1 ]; ExpressionNode *to = static_cast< ExpressionNode* >( l[ 1 ] );
ExpressionLink *link = new ExpressionLink(); ExpressionLink *link = new ExpressionLink();
link->link( from, to ); link->link( from, to );
m_scene->addItem( link ); m_scene->addItem( link );
} }

View file

@ -17,6 +17,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "expression_link.h" #include "expression_link.h"
#include "expression_node.h"
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QPen> #include <QPen>
@ -25,17 +26,38 @@ QGraphicsLineItem( parent )
{ {
m_from = NULL; m_from = NULL;
m_to = NULL; m_to = NULL;
setFlags( QGraphicsItem::ItemIsSelectable );
} }
ExpressionLink::~ExpressionLink() 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 ) 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 ) ); setPen( QPen( Qt::darkRed ) );
QGraphicsLineItem::paint( painter, option, widget ); QGraphicsLineItem::paint( painter, option, widget );

View file

@ -21,22 +21,24 @@
#include <QGraphicsItem> #include <QGraphicsItem>
class ExpressionNode;
class ExpressionLink : public QGraphicsLineItem class ExpressionLink : public QGraphicsLineItem
{ {
public: public:
ExpressionLink( QGraphicsItem *parent = NULL ); ExpressionLink( QGraphicsItem *parent = NULL );
~ExpressionLink(); ~ExpressionLink();
void link( QGraphicsItem *from, QGraphicsItem *to ){ void link( ExpressionNode *from, ExpressionNode *to );
m_from = from; void unlink();
m_to = to;
} void nodeMoved();
void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget ); void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget );
private: private:
QGraphicsItem *m_from; ExpressionNode *m_from;
QGraphicsItem *m_to; ExpressionNode *m_to;
}; };

View file

@ -18,12 +18,14 @@
#include "expression_node.h" #include "expression_node.h"
#include "expression_link.h"
#include <QPainter> #include <QPainter>
#include <QStyleOption> #include <QStyleOption>
ExpressionNode::ExpressionNode( QGraphicsItem *parent ) : ExpressionNode::ExpressionNode( QGraphicsItem *parent ) :
QGraphicsItem( parent ) QGraphicsItem( parent )
{ {
m_link = NULL;
} }
ExpressionNode::~ExpressionNode() ExpressionNode::~ExpressionNode()
@ -48,3 +50,13 @@ void ExpressionNode::paint( QPainter *painter, const QStyleOptionGraphicsItem *o
painter->drawRect( boundingRect() ); painter->drawRect( boundingRect() );
} }
void ExpressionNode::mouseMoveEvent( QGraphicsSceneMouseEvent *e )
{
if( m_link != NULL )
m_link->nodeMoved();
QGraphicsItem::mouseMoveEvent( e );
}

View file

@ -22,6 +22,8 @@
#include <QGraphicsItem> #include <QGraphicsItem>
class ExpressionLink;
class ExpressionNode : public QGraphicsItem class ExpressionNode : public QGraphicsItem
{ {
public: public:
@ -31,6 +33,15 @@ public:
QRectF boundingRect() const; QRectF boundingRect() const;
void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget ); 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 #endif