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