]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/Node.cxx
More on PipelineEditor
[cpPlugins.git] / appli / cpPipelineEditor / Node.cxx
1 #include "Node.h"
2 #include "Edge.h"
3 #include "GraphCanvas.h"
4
5 #include <QFontMetricsF>
6 #include <QGraphicsWidget>
7 #include <QGraphicsSceneHoverEvent>
8
9 #include <cpPlugins/Interface/Object.h>
10 #include <cpPlugins/Interface/ProcessObject.h>
11
12 #define PORT_SIZE 10
13
14 // -------------------------------------------------------------------------
15 PipelineEditor::Node::
16 Node( GraphCanvas* canvas, cpPlugins::Interface::Object* object )
17   : QGraphicsItem( NULL ),
18     m_Canvas( canvas ),
19     m_Object( object ),
20     m_UpdatedBounds( false )
21 {
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( ) );
28 }
29
30 // -------------------------------------------------------------------------
31 PipelineEditor::Node::
32 ~Node( )
33 {
34 }
35
36 // -------------------------------------------------------------------------
37 void PipelineEditor::Node::
38 addEdge( PipelineEditor::Edge* edge )
39 {
40   this->m_Edges << edge;
41   edge->adjust( );
42 }
43
44 // -------------------------------------------------------------------------
45 QList< PipelineEditor::Edge* > PipelineEditor::Node::
46 edges( ) const
47 {
48   return( this->m_Edges );
49 }
50
51 // -------------------------------------------------------------------------
52 QRectF PipelineEditor::Node::
53 boundingRect( ) const
54 {
55   typedef cpPlugins::Interface::ProcessObject _TFilter;
56   if( !this->m_UpdatedBounds )
57   {
58     // Text bounding box
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( );
63
64     // Ports
65     this->m_Bounds = fm.boundingRect( this->m_Label );
66     const _TFilter* f = dynamic_cast< const _TFilter* >( this->m_Object );
67     if( f != NULL )
68     {
69       unsigned int nIn = f->GetNumberOfInputs( );
70       unsigned int nOut = f->GetNumberOfOutputs( );
71       qreal n =
72         qreal( ( ( ( ( nIn > nOut )? nIn: nOut ) << 1 ) + 1 ) * PORT_SIZE );
73       qreal h = this->m_Bounds.height( );
74       if( n > h )
75         this->m_Bounds.setHeight( n );
76
77       // Let some space for ports
78       this->m_Bounds.setLeft(
79         this->m_Bounds.left( ) - qreal( PORT_SIZE )
80         );
81       this->m_Bounds.setTop(
82         this->m_Bounds.top( ) - qreal( PORT_SIZE )
83         );
84       this->m_Bounds.setRight(
85         this->m_Bounds.right( ) + qreal( PORT_SIZE )
86         );
87       this->m_Bounds.setBottom(
88         this->m_Bounds.bottom( ) + qreal( PORT_SIZE )
89         );
90
91     } // fi
92     this->m_UpdatedBounds = true;
93
94   } // fi
95   return( this->m_Bounds );
96 }
97
98 // -------------------------------------------------------------------------
99 QPainterPath PipelineEditor::Node::
100 shape( ) const
101 {
102   QPainterPath path;
103   path.addRect( this->boundingRect( ) );
104   return( path );
105 }
106
107 // -------------------------------------------------------------------------
108 void PipelineEditor::Node::
109 paint(
110   QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget
111   )
112 {
113   typedef cpPlugins::Interface::ProcessObject _TFilter;
114
115   QRectF rect = this->boundingRect( );
116   painter->drawRect( rect );
117   painter->drawText( rect, Qt::AlignCenter, this->m_Label );
118
119   // Show ports
120   const _TFilter* f = dynamic_cast< const _TFilter* >( this->m_Object );
121   if( f != NULL )
122   {
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( );
128
129     std::set< std::string > inputs, outputs;
130     f->GetInputsNames( inputs );
131     f->GetOutputsNames( outputs );
132
133     qreal oh = qreal( ( ( inputs.size( ) << 1 ) + 1 ) * PORT_SIZE );
134     qreal off = qreal( PORT_SIZE );
135     if( rh > oh )
136       off += ( rh - oh ) / qreal( 2 );
137     for( auto it = inputs.begin( ); it != inputs.end( ); ++it )
138     {
139       painter->drawRect( QRectF( QPointF( rl, rt + off ), port_size ) );
140       off += qreal( PORT_SIZE < 1 );
141
142     } // rof
143
144     oh = qreal( ( ( outputs.size( ) << 1 ) + 1 ) * PORT_SIZE );
145     off = qreal( PORT_SIZE );
146     if( rh > oh )
147       off += ( rh - oh ) / qreal( 2 );
148     for( auto it = outputs.begin( ); it != outputs.end( ); ++it )
149     {
150       painter->drawRect(
151         QRectF( QPointF( rr - qreal( PORT_SIZE ), rt + off ), port_size )
152         );
153       off += qreal( PORT_SIZE < 1 );
154
155     } // rof
156
157   } // fi
158 }
159
160 // -------------------------------------------------------------------------
161 QVariant PipelineEditor::Node::
162 itemChange( GraphicsItemChange change, const QVariant& value )
163 {
164   /* TODO
165      switch( change )
166      {
167      case QGraphicsItem::ItemPositionHasChanged:
168      foreach( Edge* edge, this->m_Edges )
169      edge->adjust( );
170      this->m_Canvas->itemMoved( );
171      break;
172      default:
173      break;
174      } // hctiws
175   */
176   return( this->QGraphicsItem::itemChange( change, value ) );
177 }
178
179 // -------------------------------------------------------------------------
180 void PipelineEditor::Node::
181 mousePressEvent( QGraphicsSceneMouseEvent* event )
182 {
183   this->update( );
184   this->QGraphicsItem::mousePressEvent( event );
185 }
186
187 // -------------------------------------------------------------------------
188 void PipelineEditor::Node::
189 mouseReleaseEvent( QGraphicsSceneMouseEvent* event )
190 {
191   this->update( );
192   this->QGraphicsItem::mouseReleaseEvent( event );
193 }
194
195 // -------------------------------------------------------------------------
196 void PipelineEditor::Node::
197 mouseDoubleClickEvent( QGraphicsSceneMouseEvent* event )
198 {
199 }
200
201 // -------------------------------------------------------------------------
202 void PipelineEditor::Node::
203 hoverMoveEvent( QGraphicsSceneHoverEvent* event )
204 {
205   QPointF pos = event->pos( );
206 }
207
208 /* TODO
209    private:
210    GraphCanvas*   m_Canvas;
211    QList< Edge* > m_Edges;
212    std::string    m_Label;
213    };
214
215    } // ecapseman
216
217    #endif // __PIPELINEEDITOR__NODE__H__
218 */
219
220 // eof - $RCSfile$
221
222
223 /*
224 QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
225 {
226   switch (change) {
227   case ItemPositionHasChanged:
228     foreach (Edge *edge, edgeList)
229       edge->adjust();
230     graph->itemMoved();
231     break;
232   default:
233     break;
234   };
235
236   return QGraphicsItem::itemChange(change, value);
237 }
238
239 void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
240 {
241   update();
242   QGraphicsItem::mousePressEvent(event);
243 }
244
245 void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
246 {
247   update();
248   QGraphicsItem::mouseReleaseEvent(event);
249 }
250
251 // eof - $RCSfile$
252 */