Connection slots are now managed by their own object, and linking is now done to their positions.

This commit is contained in:
dfighter1985 2014-09-13 22:26:11 +02:00
parent ab7c580ce7
commit 4591673dfe
3 changed files with 117 additions and 46 deletions

View file

@ -53,7 +53,7 @@ void ExpressionLink::unlink()
void ExpressionLink::nodeMoved() void ExpressionLink::nodeMoved()
{ {
setLine( QLineF( m_from->pos(), m_to->pos() ) ); setLine( QLineF( m_from->slotPos( 0 ), m_to->slotPos( 0 ) ) );
} }
void ExpressionLink::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget ) void ExpressionLink::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )

View file

@ -22,19 +22,92 @@
#include <QPainter> #include <QPainter>
#include <QStyleOption> #include <QStyleOption>
class NodeSlot
{
public:
NodeSlot( const QPoint &tl, const QPoint &ttl, const QString &text )
{
m_tl = tl;
m_ttl = ttl;
m_text = text;
m_tw = 25.0;
m_th = 12.0;
m_wh = 10.0;
}
~NodeSlot()
{
}
QPointF pos() const{
QPointF p;
p.setX( m_tl.x() + m_wh / 2.0 );
p.setY( m_tl.y() + m_wh / 2.0 );
return p;
}
void paint( QPainter *painter )
{
QBrush boxBrush;
QPen p;
boxBrush.setColor( Qt::black );
boxBrush.setStyle( Qt::SolidPattern );
p.setColor( Qt::black );
painter->setPen( p );
QRectF box;
QRectF tbox;
box.setTopLeft( m_tl );
box.setHeight( m_wh );
box.setWidth( m_wh );
painter->fillRect( box, boxBrush );
tbox.setTopLeft( m_ttl );
tbox.setHeight( m_th );
tbox.setWidth( m_tw );
painter->drawText( tbox, Qt::AlignRight, m_text );
}
private:
QPoint m_tl;
QPoint m_ttl;
QString m_text;
qreal m_th;
qreal m_tw;
qreal m_wh;
};
ExpressionNode::ExpressionNode( QGraphicsItem *parent ) : ExpressionNode::ExpressionNode( QGraphicsItem *parent ) :
QGraphicsItem( parent ) QGraphicsItem( parent )
{ {
m_link = NULL; m_link = NULL;
m_w = 100;
m_h = 100;
createSlots();
} }
ExpressionNode::~ExpressionNode() ExpressionNode::~ExpressionNode()
{ {
qDeleteAll( m_slots );
m_slots.clear();
} }
QRectF ExpressionNode::boundingRect() const QRectF ExpressionNode::boundingRect() const
{ {
return QRectF( 0, 0, 100, 100 ); return QRectF( 0, 0, m_w, m_h );
} }
void ExpressionNode::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget ) void ExpressionNode::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
@ -74,10 +147,20 @@ void ExpressionNode::paint( QPainter *painter, const QStyleOptionGraphicsItem *o
painter->drawRect( rect ); painter->drawRect( rect );
painter->drawRect( header ); painter->drawRect( header );
paintConnections( painter ); paintSlots( painter );
} }
QPointF ExpressionNode::slotPos( int slot ) const
{
const NodeSlot *s = m_slots[ slot ];
QPointF sp = s->pos();
QPointF mp = pos();
mp += sp;
return mp;
}
void ExpressionNode::mouseMoveEvent( QGraphicsSceneMouseEvent *e ) void ExpressionNode::mouseMoveEvent( QGraphicsSceneMouseEvent *e )
{ {
if( m_link != NULL ) if( m_link != NULL )
@ -86,54 +169,33 @@ void ExpressionNode::mouseMoveEvent( QGraphicsSceneMouseEvent *e )
QGraphicsItem::mouseMoveEvent( e ); QGraphicsItem::mouseMoveEvent( e );
} }
void ExpressionNode::paintConnections( QPainter *painter ) void ExpressionNode::createSlots()
{ {
QRectF rect = boundingRect(); // First create the "Out" slot
QBrush boxBrush; qreal x = 0.0;
QPen p; qreal y = m_h * 0.5;
qreal tx = 10;
boxBrush.setColor( Qt::black ); qreal ty = m_h * 0.5 - 2;
boxBrush.setStyle( Qt::SolidPattern ); m_slots.push_back( new NodeSlot( QPoint( x, y ), QPoint( tx, ty ), "Out" ) );
p.setColor( Qt::black );
QRectF box = rect;
QRectF tbox = rect;
qreal wh = 10.0;
qreal tw = 25.0;
qreal th = 12.0;
box.setTopLeft( QPoint( 0, rect.height() * 0.5 ) );
box.setHeight( wh );
box.setWidth( wh );
painter->fillRect( box, boxBrush );
tbox.setTopLeft( QPoint( 15, rect.height() * 0.50 ) );
tbox.setHeight( th );
tbox.setWidth( tw );
painter->setPen( p );
painter->drawText( tbox, Qt::AlignCenter, "Out" );
// Then the rest of them
for( int i = 0; i < 3; i++ ) for( int i = 0; i < 3; i++ )
{ {
qreal x = rect.width() - wh; x = m_w - 10;
qreal y = 30 + i * 20; y = 30 + i * 20.0;
qreal tx = x - 5 - tw; tx = x - 5 - 25.0;
qreal ty = y - 2; ty = y - 2;
box.setTopLeft( QPoint( x, y ) ); m_slots.push_back( new NodeSlot( QPoint( x, y ), QPoint( tx, ty ), QString( 'A' + i ) ) );
box.setHeight( wh ); }
box.setWidth( wh ); }
painter->fillRect( box, boxBrush ); void ExpressionNode::paintSlots( QPainter *painter )
{
tbox.setTopLeft( QPoint( tx, ty ) ); for( int i = 0; i < 4; i++ )
tbox.setHeight( th ); {
tbox.setWidth( tw ); NodeSlot *slot = m_slots[ i ];
slot->paint( painter );
QString text = 'A' + i;
painter->drawText( tbox, Qt::AlignRight, text );
} }
} }

View file

@ -21,8 +21,10 @@
#define EXPRESSION_NODE #define EXPRESSION_NODE
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QList>
class ExpressionLink; class ExpressionLink;
class NodeSlot;
class ExpressionNode : public QGraphicsItem class ExpressionNode : public QGraphicsItem
{ {
@ -36,14 +38,21 @@ public:
void setLink( ExpressionLink *link ){ m_link = link; } void setLink( ExpressionLink *link ){ m_link = link; }
ExpressionLink* link() const{ return m_link; } ExpressionLink* link() const{ return m_link; }
QPointF slotPos( int slot ) const;
protected: protected:
void mouseMoveEvent( QGraphicsSceneMouseEvent *e ); void mouseMoveEvent( QGraphicsSceneMouseEvent *e );
private: private:
void paintConnections( QPainter *painter ); void createSlots();
void paintSlots( QPainter *painter );
ExpressionLink *m_link; ExpressionLink *m_link;
qreal m_w;
qreal m_h;
QList< NodeSlot* > m_slots;
}; };
#endif #endif