]> Creatis software - cpPlugins.git/blob - lib/cpPipelineEditor/Port.cxx
b0278eaecf1524078fc92462ed445170953c42e7
[cpPlugins.git] / lib / cpPipelineEditor / Port.cxx
1 #include "Port.h"\r
2 #include "Connection.h"\r
3 #include "Block.h"\r
4 #include "Editor.h"\r
5 \r
6 #include <cpPlugins/DataObjectVisualizationQtDialog.h>\r
7 \r
8 #include <QGraphicsScene>\r
9 #include <QGraphicsSceneContextMenuEvent>\r
10 #include <QFontMetrics>\r
11 #include <QMenu>\r
12 #include <QPainter>\r
13 #include <QPen>\r
14 \r
15 // -------------------------------------------------------------------------\r
16 cpPipelineEditor::Port::\r
17 Port( QGraphicsItem* parent, QGraphicsScene* scene )\r
18   : Superclass( parent, scene ),\r
19     m_Radius( 5 ),\r
20     m_Margin( 2 )\r
21 {\r
22   this->m_Label = new QGraphicsTextItem( this );\r
23   this->m_ExtendedLabel = new QGraphicsTextItem( this );\r
24   this->setExtend( false );\r
25 \r
26   QPainterPath p;\r
27   p.addEllipse( 0, 0, 2 * this->m_Radius, 2 * this->m_Radius );\r
28 \r
29   this->setPath( p );\r
30   this->setPen( QPen( Qt::darkRed ) );\r
31   this->setBrush( Qt::red );\r
32   this->setFlag( QGraphicsItem::ItemSendsScenePositionChanges );\r
33 }\r
34 \r
35 // -------------------------------------------------------------------------\r
36 cpPipelineEditor::Port::\r
37 ~Port( )\r
38 {\r
39 }\r
40 \r
41 // -------------------------------------------------------------------------\r
42 void cpPipelineEditor::Port::\r
43 setBlock( Block* b )\r
44 {\r
45   this->m_Block = b;\r
46 }\r
47 \r
48 // -------------------------------------------------------------------------\r
49 void cpPipelineEditor::Port::\r
50 setName( const QString& n )\r
51 {\r
52   this->m_Label->setPlainText( n );\r
53   this->_updateLabels( );\r
54 }\r
55 \r
56 // -------------------------------------------------------------------------\r
57 void cpPipelineEditor::Port::\r
58 setExtendedName( const QString& n )\r
59 {\r
60   this->m_ExtendedLabel->setPlainText( n );\r
61   this->_updateLabels( );\r
62 }\r
63 \r
64 // -------------------------------------------------------------------------\r
65 void cpPipelineEditor::Port::\r
66 setExtend( bool extend )\r
67 {\r
68   // Do nothing!\r
69   this->m_IsExtended = false;\r
70   this->m_ExtendedLabel->setVisible( false );\r
71 }\r
72 \r
73 // -------------------------------------------------------------------------\r
74 void cpPipelineEditor::Port::\r
75 paint(\r
76   QPainter* painter,\r
77   const QStyleOptionGraphicsItem* option,\r
78   QWidget* widget\r
79   )\r
80 {\r
81   Q_UNUSED( option );\r
82   Q_UNUSED( widget );\r
83 \r
84   if( this->isExtended( ) )\r
85   {\r
86     painter->setPen( QPen( Qt::darkBlue ) );\r
87     painter->setBrush( Qt::blue );\r
88   }\r
89   else\r
90   {\r
91     painter->setPen( QPen( Qt::darkRed ) );\r
92     painter->setBrush( Qt::red );\r
93 \r
94   } // fi\r
95   painter->drawPath( this->path( ) );\r
96 }\r
97 \r
98 // -------------------------------------------------------------------------\r
99 cpPipelineEditor::NamePort::\r
100 NamePort( QGraphicsItem* parent, QGraphicsScene* scene )\r
101   : Superclass( parent, scene )\r
102 {\r
103 }\r
104 \r
105 // -------------------------------------------------------------------------\r
106 cpPipelineEditor::NamePort::\r
107 ~NamePort( )\r
108 {\r
109 }\r
110 \r
111 // -------------------------------------------------------------------------\r
112 void cpPipelineEditor::NamePort::\r
113 _updateLabels( )\r
114 {\r
115   QFont font( this->scene( )->font( ) );\r
116   font.setBold( true );\r
117   this->m_Label->setFont( font );\r
118   this->m_ExtendedLabel->setFont( font );\r
119   this->setPath( QPainterPath( ) );\r
120 }\r
121 \r
122 // -------------------------------------------------------------------------\r
123 bool cpPipelineEditor::NamePort::\r
124 isConnected( Port* other )\r
125 {\r
126   return( false );\r
127 }\r
128 \r
129 // -------------------------------------------------------------------------\r
130 cpPipelineEditor::InputPort::\r
131 InputPort( QGraphicsItem* parent, QGraphicsScene* scene )\r
132   : Superclass( parent, scene ),\r
133     m_Connection( NULL )\r
134 {\r
135 }\r
136 \r
137 // -------------------------------------------------------------------------\r
138 cpPipelineEditor::InputPort::\r
139 ~InputPort( )\r
140 {\r
141   if( this->m_Connection != NULL )\r
142     delete this->m_Connection;\r
143 }\r
144 \r
145 // -------------------------------------------------------------------------\r
146 void cpPipelineEditor::InputPort::\r
147 _updateLabels( )\r
148 {\r
149   QFontMetrics fm( this->scene( )->font( ) );\r
150   this->m_Label->setPos( this->m_Radius * 2, -fm.height( ) / 2 );\r
151   this->m_ExtendedLabel->setPos(\r
152     -fm.width( this->extendedName( ) ) - this->m_Radius * 2,\r
153     -fm.height( ) / 2\r
154     );\r
155 }\r
156 \r
157 // -------------------------------------------------------------------------\r
158 void cpPipelineEditor::InputPort::\r
159 setExtend( bool extend )\r
160 {\r
161   if( this->m_Connection == NULL )\r
162   {\r
163     this->m_IsExtended = extend;\r
164     this->m_ExtendedLabel->setVisible( extend );\r
165   }\r
166   else\r
167     this->Superclass::setExtend( false );\r
168 }\r
169 \r
170 // -------------------------------------------------------------------------\r
171 bool cpPipelineEditor::InputPort::\r
172 isConnected( Port* other )\r
173 {\r
174   if( this->m_Connection != NULL )\r
175     return(\r
176       this->m_Connection->port1( ) == other &&\r
177       this->m_Connection->port2( ) == this\r
178       );\r
179   else\r
180     return( false );\r
181 }\r
182 \r
183 // -------------------------------------------------------------------------\r
184 void cpPipelineEditor::InputPort::\r
185 setConnection( Connection* c )\r
186 {\r
187   this->m_Connection = c;\r
188 }\r
189 \r
190 // -------------------------------------------------------------------------\r
191 QVariant cpPipelineEditor::InputPort::\r
192 itemChange( GraphicsItemChange change, const QVariant& value )\r
193 {\r
194   if( change == ItemScenePositionHasChanged )\r
195   {\r
196     if( this->m_Connection != NULL )\r
197     {\r
198       this->m_Connection->updatePosFromPorts( );\r
199       this->m_Connection->updatePath( );\r
200 \r
201     } // fi\r
202 \r
203   } // fi\r
204   return( value );\r
205 }\r
206 \r
207 // -------------------------------------------------------------------------\r
208 cpPipelineEditor::OutputPort::\r
209 OutputPort( QGraphicsItem* parent, QGraphicsScene* scene )\r
210   : Superclass( parent, scene )\r
211 {\r
212 }\r
213 \r
214 // -------------------------------------------------------------------------\r
215 cpPipelineEditor::OutputPort::\r
216 ~OutputPort( )\r
217 {\r
218   foreach( Connection* conn, this->m_Connections )\r
219     delete conn;\r
220 }\r
221 \r
222 // -------------------------------------------------------------------------\r
223 void cpPipelineEditor::OutputPort::\r
224 _updateLabels( )\r
225 {\r
226   QFontMetrics fm( this->scene( )->font( ) );\r
227   this->m_Label->setPos(\r
228     -fm.width( this->name( ) ) - this->m_Radius * 2, -fm.height( ) / 2\r
229     );\r
230   this->m_ExtendedLabel->setPos( this->m_Radius * 2, -fm.height( ) / 2 );\r
231 \r
232 }\r
233 \r
234 // -------------------------------------------------------------------------\r
235 void cpPipelineEditor::OutputPort::\r
236 setExtend( bool extend )\r
237 {\r
238   this->m_IsExtended = extend;\r
239   this->m_ExtendedLabel->setVisible( extend );\r
240 }\r
241 \r
242 // -------------------------------------------------------------------------\r
243 bool cpPipelineEditor::OutputPort::\r
244 isConnected( Port* other )\r
245 {\r
246   auto i = this->m_Connections.begin( );\r
247   bool conn = false;\r
248   for( ; i != this->m_Connections.end( ) && !conn; ++i )\r
249     conn |= ( ( *i )->port1( ) == this && ( *i )->port2( ) == other );\r
250   return( conn );\r
251 }\r
252 \r
253 // -------------------------------------------------------------------------\r
254 QVariant cpPipelineEditor::OutputPort::\r
255 itemChange( GraphicsItemChange change, const QVariant& value )\r
256 {\r
257   if( change == ItemScenePositionHasChanged )\r
258   {\r
259     foreach( Connection* conn, this->m_Connections )\r
260     {\r
261       conn->updatePosFromPorts( );\r
262       conn->updatePath( );\r
263 \r
264     } // rof\r
265 \r
266   } // fi\r
267   return( value );\r
268 }\r
269 \r
270 // -------------------------------------------------------------------------\r
271 void cpPipelineEditor::OutputPort::\r
272 contextMenuEvent( QGraphicsSceneContextMenuEvent* evt )\r
273 {\r
274   if( this->m_Block == NULL )\r
275     return;\r
276   \r
277   QMenu menu;\r
278   QAction* showAction = menu.addAction( "Show" );\r
279   QAction* hideAction = menu.addAction( "Hide" );\r
280   QAction* propertiesAction = menu.addAction( "Properties" );\r
281   QAction* selectedAction = menu.exec( evt->screenPos( ) );\r
282 \r
283   if( selectedAction == showAction )\r
284   {\r
285     this->m_Block->editor( )->showOutputData(\r
286       this->m_Block->namePort( ).toStdString( ),\r
287       this->name( ).toStdString( )\r
288       );\r
289   }\r
290   else if( selectedAction == hideAction )\r
291   {\r
292     this->m_Block->editor( )->hideOutputData(\r
293       this->m_Block->namePort( ).toStdString( ),\r
294       this->name( ).toStdString( )\r
295       );\r
296   }\r
297   else if( selectedAction == propertiesAction )\r
298   {\r
299     auto filter = this->m_Block->filter( );\r
300     auto name = this->name( ).toStdString( );\r
301     if( filter != NULL )\r
302     {\r
303       auto output = filter->GetOutput( name );\r
304       if( output != NULL )\r
305       {\r
306         auto dlg = output->CreateQtDialog( );\r
307         dlg->exec( );\r
308 \r
309       } // fi\r
310 \r
311     } // fi\r
312 \r
313   } // fi\r
314 }\r
315 \r
316 // eof - $RCSfile$\r