]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/QNEPort.cxx
7b71975d3eb636981a46523ecf454f6fd5a62814
[cpPlugins.git] / appli / cpPipelineEditor / QNEPort.cxx
1 /* Copyright (c) 2012, STANISLAW ADASZEWSKI\r
2    All rights reserved.\r
3 \r
4    Redistribution and use in source and binary forms, with or without\r
5    modification, are permitted provided that the following conditions are met:\r
6    * Redistributions of source code must retain the above copyright\r
7    notice, this list of conditions and the following disclaimer.\r
8    * Redistributions in binary form must reproduce the above copyright\r
9    notice, this list of conditions and the following disclaimer in the\r
10    documentation and/or other materials provided with the distribution.\r
11    * Neither the name of STANISLAW ADASZEWSKI nor the\r
12    names of its contributors may be used to endorse or promote products\r
13    derived from this software without specific prior written permission.\r
14 \r
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
16    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
17    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
18    DISCLAIMED. IN NO EVENT SHALL STANISLAW ADASZEWSKI BE LIABLE FOR ANY\r
19    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
20    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
21    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
22    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
23    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
24    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
25 */\r
26 \r
27 #include "QNEPort.h"\r
28 #include "QNEConnection.h"\r
29 \r
30 #include <QGraphicsScene>\r
31 #include <QFontMetrics>\r
32 #include <QPainter>\r
33 #include <QPen>\r
34 \r
35 // -------------------------------------------------------------------------\r
36 PipelineEditor::QNEPort::\r
37 QNEPort( QGraphicsItem* parent, QGraphicsScene* scene )\r
38   : Superclass( parent, scene ),\r
39     m_Radius( 5 ),\r
40     m_Margin( 2 )\r
41 {\r
42   this->m_Label = new QGraphicsTextItem( this );\r
43   this->m_ExtendedLabel = new QGraphicsTextItem( this );\r
44   this->setExtend( false );\r
45 \r
46   QPainterPath p;\r
47   p.addEllipse( 0, 0, 2 * this->m_Radius, 2 * this->m_Radius );\r
48 \r
49   this->setPath( p );\r
50   this->setPen( QPen( Qt::darkRed ) );\r
51   this->setBrush( Qt::red );\r
52   this->setFlag( QGraphicsItem::ItemSendsScenePositionChanges );\r
53 }\r
54 \r
55 // -------------------------------------------------------------------------\r
56 PipelineEditor::QNEPort::\r
57 ~QNEPort( )\r
58 {\r
59 }\r
60 \r
61 // -------------------------------------------------------------------------\r
62 void PipelineEditor::QNEPort::\r
63 setBlock( QNEBlock* b )\r
64 {\r
65   this->m_Block = b;\r
66 }\r
67 \r
68 // -------------------------------------------------------------------------\r
69 void PipelineEditor::QNEPort::\r
70 setName( const QString& n )\r
71 {\r
72   this->m_Label->setPlainText( n );\r
73   this->_updateLabels( );\r
74 }\r
75 \r
76 // -------------------------------------------------------------------------\r
77 void PipelineEditor::QNEPort::\r
78 setExtendedName( const QString& n )\r
79 {\r
80   this->m_ExtendedLabel->setPlainText( n );\r
81   this->_updateLabels( );\r
82 }\r
83 \r
84 // -------------------------------------------------------------------------\r
85 void PipelineEditor::QNEPort::\r
86 setExtend( bool extend )\r
87 {\r
88   // Do nothing!\r
89   this->m_IsExtended = false;\r
90   this->m_ExtendedLabel->setVisible( false );\r
91 }\r
92 \r
93 // -------------------------------------------------------------------------\r
94 void PipelineEditor::QNEPort::\r
95 paint(\r
96   QPainter* painter,\r
97   const QStyleOptionGraphicsItem* option,\r
98   QWidget* widget\r
99   )\r
100 {\r
101   Q_UNUSED( option );\r
102   Q_UNUSED( widget );\r
103 \r
104   if( this->isExtended( ) )\r
105   {\r
106     painter->setPen( QPen( Qt::darkBlue ) );\r
107     painter->setBrush( Qt::blue );\r
108   }\r
109   else\r
110   {\r
111     painter->setPen( QPen( Qt::darkRed ) );\r
112     painter->setBrush( Qt::red );\r
113 \r
114   } // fi\r
115   painter->drawPath( this->path( ) );\r
116 }\r
117 \r
118 // -------------------------------------------------------------------------\r
119 PipelineEditor::QNENamePort::\r
120 QNENamePort( QGraphicsItem* parent, QGraphicsScene* scene )\r
121   : Superclass( parent, scene )\r
122 {\r
123 }\r
124 \r
125 // -------------------------------------------------------------------------\r
126 PipelineEditor::QNENamePort::\r
127 ~QNENamePort( )\r
128 {\r
129 }\r
130 \r
131 // -------------------------------------------------------------------------\r
132 void PipelineEditor::QNENamePort::\r
133 _updateLabels( )\r
134 {\r
135   QFont font( this->scene( )->font( ) );\r
136   font.setBold( true );\r
137   this->m_Label->setFont( font );\r
138   this->m_ExtendedLabel->setFont( font );\r
139   this->setPath( QPainterPath( ) );\r
140 }\r
141 \r
142 // -------------------------------------------------------------------------\r
143 bool PipelineEditor::QNENamePort::\r
144 isConnected( QNEPort* other )\r
145 {\r
146   return( false );\r
147 }\r
148 \r
149 // -------------------------------------------------------------------------\r
150 PipelineEditor::QNETypePort::\r
151 QNETypePort( QGraphicsItem* parent, QGraphicsScene* scene )\r
152   : Superclass( parent, scene )\r
153 {\r
154 }\r
155 \r
156 // -------------------------------------------------------------------------\r
157 PipelineEditor::QNETypePort::\r
158 ~QNETypePort( )\r
159 {\r
160 }\r
161 \r
162 // -------------------------------------------------------------------------\r
163 void PipelineEditor::QNETypePort::\r
164 _updateLabels( )\r
165 {\r
166   QFont font( this->scene( )->font( ) );\r
167   font.setItalic( true );\r
168   this->m_Label->setFont( font );\r
169   this->m_ExtendedLabel->setFont( font );\r
170   this->setPath( QPainterPath( ) );\r
171 }\r
172 \r
173 // -------------------------------------------------------------------------\r
174 bool PipelineEditor::QNETypePort::\r
175 isConnected( QNEPort* other )\r
176 {\r
177   return( false );\r
178 }\r
179 \r
180 // -------------------------------------------------------------------------\r
181 PipelineEditor::QNEInputPort::\r
182 QNEInputPort( QGraphicsItem* parent, QGraphicsScene* scene )\r
183   : Superclass( parent, scene ),\r
184     m_Connection( NULL )\r
185 {\r
186 }\r
187 \r
188 // -------------------------------------------------------------------------\r
189 PipelineEditor::QNEInputPort::\r
190 ~QNEInputPort( )\r
191 {\r
192   if( this->m_Connection != NULL )\r
193     delete this->m_Connection;\r
194 }\r
195 \r
196 // -------------------------------------------------------------------------\r
197 void PipelineEditor::QNEInputPort::\r
198 _updateLabels( )\r
199 {\r
200   QFontMetrics fm( this->scene( )->font( ) );\r
201   this->m_Label->setPos( this->m_Radius * 2, -fm.height( ) / 2 );\r
202   this->m_ExtendedLabel->setPos(\r
203     -fm.width( this->extendedName( ) ) - this->m_Radius * 2,\r
204     -fm.height( ) / 2\r
205     );\r
206 }\r
207 \r
208 // -------------------------------------------------------------------------\r
209 void PipelineEditor::QNEInputPort::\r
210 setExtend( bool extend )\r
211 {\r
212   if( this->m_Connection == NULL )\r
213   {\r
214     this->m_IsExtended = extend;\r
215     this->m_ExtendedLabel->setVisible( extend );\r
216   }\r
217   else\r
218     this->Superclass::setExtend( false );\r
219 }\r
220 \r
221 // -------------------------------------------------------------------------\r
222 bool PipelineEditor::QNEInputPort::\r
223 isConnected( QNEPort* other )\r
224 {\r
225   if( this->m_Connection != NULL )\r
226     return(\r
227       this->m_Connection->port1( ) == other &&\r
228       this->m_Connection->port2( ) == this\r
229       );\r
230   else\r
231     return( false );\r
232 }\r
233 \r
234 // -------------------------------------------------------------------------\r
235 void PipelineEditor::QNEInputPort::\r
236 setConnection( QNEConnection* c )\r
237 {\r
238   this->m_Connection = c;\r
239 }\r
240 \r
241 // -------------------------------------------------------------------------\r
242 QVariant PipelineEditor::QNEInputPort::\r
243 itemChange( GraphicsItemChange change, const QVariant& value )\r
244 {\r
245   if( change == ItemScenePositionHasChanged )\r
246   {\r
247     if( this->m_Connection != NULL )\r
248     {\r
249       this->m_Connection->updatePosFromPorts( );\r
250       this->m_Connection->updatePath( );\r
251 \r
252     } // fi\r
253 \r
254   } // fi\r
255   return( value );\r
256 }\r
257 \r
258 // -------------------------------------------------------------------------\r
259 PipelineEditor::QNEOutputPort::\r
260 QNEOutputPort( QGraphicsItem* parent, QGraphicsScene* scene )\r
261   : Superclass( parent, scene )\r
262 {\r
263 }\r
264 \r
265 // -------------------------------------------------------------------------\r
266 PipelineEditor::QNEOutputPort::\r
267 ~QNEOutputPort( )\r
268 {\r
269   foreach( QNEConnection* conn, this->m_Connections )\r
270     delete conn;\r
271 }\r
272 \r
273 // -------------------------------------------------------------------------\r
274 void PipelineEditor::QNEOutputPort::\r
275 _updateLabels( )\r
276 {\r
277   QFontMetrics fm( this->scene( )->font( ) );\r
278   this->m_Label->setPos(\r
279     -fm.width( this->name( ) ) - this->m_Radius * 2, -fm.height( ) / 2\r
280     );\r
281   this->m_ExtendedLabel->setPos( this->m_Radius * 2, -fm.height( ) / 2 );\r
282 \r
283 }\r
284 \r
285 // -------------------------------------------------------------------------\r
286 void PipelineEditor::QNEOutputPort::\r
287 setExtend( bool extend )\r
288 {\r
289   this->m_IsExtended = extend;\r
290   this->m_ExtendedLabel->setVisible( extend );\r
291 }\r
292 \r
293 // -------------------------------------------------------------------------\r
294 bool PipelineEditor::QNEOutputPort::\r
295 isConnected( QNEPort* other )\r
296 {\r
297   auto i = this->m_Connections.begin( );\r
298   bool conn = false;\r
299   for( ; i != this->m_Connections.end( ) && !conn; ++i )\r
300     conn |= ( ( *i )->port1( ) == this && ( *i )->port2( ) == other );\r
301   return( conn );\r
302 }\r
303 \r
304 // -------------------------------------------------------------------------\r
305 QVariant PipelineEditor::QNEOutputPort::\r
306 itemChange( GraphicsItemChange change, const QVariant& value )\r
307 {\r
308   if( change == ItemScenePositionHasChanged )\r
309   {\r
310     foreach( QNEConnection* conn, this->m_Connections )\r
311     {\r
312       conn->updatePosFromPorts( );\r
313       conn->updatePath( );\r
314 \r
315     } // rof\r
316 \r
317   } // fi\r
318   return( value );\r
319 }\r
320 \r
321 // eof - $RCSfile$\r