From 4cf4440a97c49545df464f2caffdc02a5d7e012f Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Sat, 13 Sep 2014 22:26:11 +0200 Subject: [PATCH] Connection slots are now managed by their own object, and linking is now done to their positions. --HG-- branch : dfighter-tools --- .../plugins/gui_editor/expression_link.cpp | 2 +- .../plugins/gui_editor/expression_node.cpp | 152 ++++++++++++------ .../src/plugins/gui_editor/expression_node.h | 11 +- 3 files changed, 118 insertions(+), 47 deletions(-) diff --git a/code/studio/src/plugins/gui_editor/expression_link.cpp b/code/studio/src/plugins/gui_editor/expression_link.cpp index 1e7f7cf5f..5050a9339 100644 --- a/code/studio/src/plugins/gui_editor/expression_link.cpp +++ b/code/studio/src/plugins/gui_editor/expression_link.cpp @@ -53,7 +53,7 @@ void ExpressionLink::unlink() 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 ) diff --git a/code/studio/src/plugins/gui_editor/expression_node.cpp b/code/studio/src/plugins/gui_editor/expression_node.cpp index fae024096..06616c1db 100644 --- a/code/studio/src/plugins/gui_editor/expression_node.cpp +++ b/code/studio/src/plugins/gui_editor/expression_node.cpp @@ -22,19 +22,92 @@ #include #include + + +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 ) : QGraphicsItem( parent ) { m_link = NULL; + + m_w = 100; + m_h = 100; + + createSlots(); } ExpressionNode::~ExpressionNode() { + qDeleteAll( m_slots ); + m_slots.clear(); } 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 ) @@ -74,10 +147,20 @@ void ExpressionNode::paint( QPainter *painter, const QStyleOptionGraphicsItem *o painter->drawRect( rect ); 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 ) { if( m_link != NULL ) @@ -86,54 +169,33 @@ void ExpressionNode::mouseMoveEvent( QGraphicsSceneMouseEvent *e ) QGraphicsItem::mouseMoveEvent( e ); } -void ExpressionNode::paintConnections( QPainter *painter ) +void ExpressionNode::createSlots() { - QRectF rect = boundingRect(); - QBrush boxBrush; - QPen p; - - boxBrush.setColor( Qt::black ); - boxBrush.setStyle( Qt::SolidPattern ); - 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" ); - - + // First create the "Out" slot + qreal x = 0.0; + qreal y = m_h * 0.5; + qreal tx = 10; + qreal ty = m_h * 0.5 - 2; + m_slots.push_back( new NodeSlot( QPoint( x, y ), QPoint( tx, ty ), "Out" ) ); + + // Then the rest of them for( int i = 0; i < 3; i++ ) { - qreal x = rect.width() - wh; - qreal y = 30 + i * 20; - qreal tx = x - 5 - tw; - qreal ty = y - 2; - - box.setTopLeft( QPoint( x, y ) ); - box.setHeight( wh ); - box.setWidth( wh ); + x = m_w - 10; + y = 30 + i * 20.0; + tx = x - 5 - 25.0; + ty = y - 2; - painter->fillRect( box, boxBrush ); - - tbox.setTopLeft( QPoint( tx, ty ) ); - tbox.setHeight( th ); - tbox.setWidth( tw ); + m_slots.push_back( new NodeSlot( QPoint( x, y ), QPoint( tx, ty ), QString( 'A' + i ) ) ); + } +} - QString text = 'A' + i; - painter->drawText( tbox, Qt::AlignRight, text ); +void ExpressionNode::paintSlots( QPainter *painter ) +{ + for( int i = 0; i < 4; i++ ) + { + NodeSlot *slot = m_slots[ i ]; + slot->paint( painter ); } } diff --git a/code/studio/src/plugins/gui_editor/expression_node.h b/code/studio/src/plugins/gui_editor/expression_node.h index 6eebcb8a4..c189bd0fc 100644 --- a/code/studio/src/plugins/gui_editor/expression_node.h +++ b/code/studio/src/plugins/gui_editor/expression_node.h @@ -21,8 +21,10 @@ #define EXPRESSION_NODE #include +#include class ExpressionLink; +class NodeSlot; class ExpressionNode : public QGraphicsItem { @@ -36,14 +38,21 @@ public: void setLink( ExpressionLink *link ){ m_link = link; } ExpressionLink* link() const{ return m_link; } + QPointF slotPos( int slot ) const; + protected: void mouseMoveEvent( QGraphicsSceneMouseEvent *e ); private: - void paintConnections( QPainter *painter ); + void createSlots(); + void paintSlots( QPainter *painter ); ExpressionLink *m_link; + qreal m_w; + qreal m_h; + + QList< NodeSlot* > m_slots; }; #endif