3 #include "GraphCanvas.h"
5 #include <QFontMetricsF>
6 #include <QGraphicsWidget>
7 #include <QGraphicsSceneHoverEvent>
9 #include <cpPlugins/Interface/Object.h>
10 #include <cpPlugins/Interface/ProcessObject.h>
14 // -------------------------------------------------------------------------
15 PipelineEditor::Node::
16 Node( GraphCanvas* canvas, cpPlugins::Interface::Object* object )
17 : QGraphicsItem( NULL ),
20 m_UpdatedBounds( false )
22 this->setFlag( QGraphicsItem::ItemIsMovable );
23 this->setFlag( QGraphicsItem::ItemSendsGeometryChanges );
24 this->setCacheMode( QGraphicsItem::DeviceCoordinateCache );
25 this->setAcceptHoverEvents( true );
26 this->setZValue( -1 );
27 this->setToolTip( this->m_Object->GetName( ) );
30 // -------------------------------------------------------------------------
31 PipelineEditor::Node::
36 // -------------------------------------------------------------------------
37 void PipelineEditor::Node::
38 addEdge( PipelineEditor::Edge* edge )
40 this->m_Edges << edge;
44 // -------------------------------------------------------------------------
45 QList< PipelineEditor::Edge* > PipelineEditor::Node::
48 return( this->m_Edges );
51 // -------------------------------------------------------------------------
52 QRectF PipelineEditor::Node::
55 typedef cpPlugins::Interface::ProcessObject _TFilter;
56 if( !this->m_UpdatedBounds )
59 QFontMetricsF fm( this->m_Canvas->font( ) );
60 this->m_Label = this->m_Object->GetName( );
61 this->m_Label += "\n";
62 this->m_Label += this->m_Object->GetClassName( ).c_str( );
65 this->m_Bounds = fm.boundingRect( this->m_Label );
66 const _TFilter* f = dynamic_cast< const _TFilter* >( this->m_Object );
69 unsigned int nIn = f->GetNumberOfInputs( );
70 unsigned int nOut = f->GetNumberOfOutputs( );
72 qreal( ( ( ( ( nIn > nOut )? nIn: nOut ) << 1 ) + 1 ) * PORT_SIZE );
73 qreal h = this->m_Bounds.height( );
75 this->m_Bounds.setHeight( n );
77 // Let some space for ports
78 this->m_Bounds.setLeft(
79 this->m_Bounds.left( ) - qreal( PORT_SIZE )
81 this->m_Bounds.setTop(
82 this->m_Bounds.top( ) - qreal( PORT_SIZE )
84 this->m_Bounds.setRight(
85 this->m_Bounds.right( ) + qreal( PORT_SIZE )
87 this->m_Bounds.setBottom(
88 this->m_Bounds.bottom( ) + qreal( PORT_SIZE )
92 this->m_UpdatedBounds = true;
95 return( this->m_Bounds );
98 // -------------------------------------------------------------------------
99 QPainterPath PipelineEditor::Node::
103 path.addRect( this->boundingRect( ) );
107 // -------------------------------------------------------------------------
108 void PipelineEditor::Node::
110 QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget
113 typedef cpPlugins::Interface::ProcessObject _TFilter;
115 QRectF rect = this->boundingRect( );
116 painter->drawRect( rect );
117 painter->drawText( rect, Qt::AlignCenter, this->m_Label );
120 const _TFilter* f = dynamic_cast< const _TFilter* >( this->m_Object );
123 QSizeF port_size( qreal( PORT_SIZE ), qreal( PORT_SIZE ) );
124 qreal rh = rect.height( );
125 qreal rt = rect.top( );
126 qreal rl = rect.left( );
127 qreal rr = rect.right( );
129 std::set< std::string > inputs, outputs;
130 f->GetInputsNames( inputs );
131 f->GetOutputsNames( outputs );
133 qreal oh = qreal( ( ( inputs.size( ) << 1 ) + 1 ) * PORT_SIZE );
134 qreal off = qreal( PORT_SIZE );
136 off += ( rh - oh ) / qreal( 2 );
137 for( auto it = inputs.begin( ); it != inputs.end( ); ++it )
139 painter->drawRect( QRectF( QPointF( rl, rt + off ), port_size ) );
140 off += qreal( PORT_SIZE < 1 );
144 oh = qreal( ( ( outputs.size( ) << 1 ) + 1 ) * PORT_SIZE );
145 off = qreal( PORT_SIZE );
147 off += ( rh - oh ) / qreal( 2 );
148 for( auto it = outputs.begin( ); it != outputs.end( ); ++it )
151 QRectF( QPointF( rr - qreal( PORT_SIZE ), rt + off ), port_size )
153 off += qreal( PORT_SIZE < 1 );
160 // -------------------------------------------------------------------------
161 QVariant PipelineEditor::Node::
162 itemChange( GraphicsItemChange change, const QVariant& value )
167 case QGraphicsItem::ItemPositionHasChanged:
168 foreach( Edge* edge, this->m_Edges )
170 this->m_Canvas->itemMoved( );
176 return( this->QGraphicsItem::itemChange( change, value ) );
179 // -------------------------------------------------------------------------
180 void PipelineEditor::Node::
181 mousePressEvent( QGraphicsSceneMouseEvent* event )
184 this->QGraphicsItem::mousePressEvent( event );
187 // -------------------------------------------------------------------------
188 void PipelineEditor::Node::
189 mouseReleaseEvent( QGraphicsSceneMouseEvent* event )
192 this->QGraphicsItem::mouseReleaseEvent( event );
195 // -------------------------------------------------------------------------
196 void PipelineEditor::Node::
197 mouseDoubleClickEvent( QGraphicsSceneMouseEvent* event )
201 // -------------------------------------------------------------------------
202 void PipelineEditor::Node::
203 hoverMoveEvent( QGraphicsSceneHoverEvent* event )
205 QPointF pos = event->pos( );
210 GraphCanvas* m_Canvas;
211 QList< Edge* > m_Edges;
217 #endif // __PIPELINEEDITOR__NODE__H__
224 QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
227 case ItemPositionHasChanged:
228 foreach (Edge *edge, edgeList)
236 return QGraphicsItem::itemChange(change, value);
239 void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
242 QGraphicsItem::mousePressEvent(event);
245 void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
248 QGraphicsItem::mouseReleaseEvent(event);