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 f7b360f1cb
commit a6e5f52e17
5 changed files with 77 additions and 12 deletions

View file

@ -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 );
}

View file

@ -17,6 +17,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "expression_link.h"
#include "expression_node.h"
#include <QGraphicsItem>
#include <QPen>
@ -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 );

View file

@ -21,22 +21,24 @@
#include <QGraphicsItem>
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;
};

View file

@ -18,12 +18,14 @@
#include "expression_node.h"
#include "expression_link.h"
#include <QPainter>
#include <QStyleOption>
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 );
}

View file

@ -22,6 +22,8 @@
#include <QGraphicsItem>
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