]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/QNEPort.cxx
6aa653b1a1987f14f1b8efcabd5397e3ae5a8445
[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 <QPen>\r
33 \r
34 // -------------------------------------------------------------------------\r
35 PipelineEditor::QNEPort::\r
36 QNEPort( QGraphicsItem* parent, QGraphicsScene* scene )\r
37   : Superclass( parent, scene ),\r
38     m_Radius( 5 ),\r
39     m_Margin( 2 )\r
40 {\r
41   this->m_Label = new QGraphicsTextItem( this );\r
42 \r
43   QPainterPath p;\r
44   p.addEllipse(\r
45     -this->m_Radius, -this->m_Radius,\r
46     2 * this->m_Radius, 2 * this->m_Radius\r
47     );\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_Name = n;\r
73   this->m_Label->setPlainText( n );\r
74 }\r
75 \r
76 // -------------------------------------------------------------------------\r
77 void PipelineEditor::QNEPort::\r
78 setPtr( quint64 p )\r
79 {\r
80   this->m_Ptr = p;\r
81 }\r
82 \r
83 // -------------------------------------------------------------------------\r
84 PipelineEditor::QNENamePort::\r
85 QNENamePort( QGraphicsItem* parent, QGraphicsScene* scene )\r
86   : Superclass( parent, scene )\r
87 {\r
88 }\r
89 \r
90 // -------------------------------------------------------------------------\r
91 PipelineEditor::QNENamePort::\r
92 ~QNENamePort( )\r
93 {\r
94 }\r
95 \r
96 // -------------------------------------------------------------------------\r
97 void PipelineEditor::QNENamePort::\r
98 setName( const QString& n )\r
99 {\r
100   this->Superclass::setName( n );\r
101 \r
102   QFont font( this->scene( )->font( ) );\r
103   font.setBold( true );\r
104   this->m_Label->setFont( font );\r
105   this->setPath( QPainterPath( ) );\r
106 }\r
107 \r
108 // -------------------------------------------------------------------------\r
109 bool PipelineEditor::QNENamePort::\r
110 isConnected( QNEPort* other )\r
111 {\r
112   return( false );\r
113 }\r
114 \r
115 // -------------------------------------------------------------------------\r
116 PipelineEditor::QNETypePort::\r
117 QNETypePort( QGraphicsItem* parent, QGraphicsScene* scene )\r
118   : Superclass( parent, scene )\r
119 {\r
120 }\r
121 \r
122 // -------------------------------------------------------------------------\r
123 PipelineEditor::QNETypePort::\r
124 ~QNETypePort( )\r
125 {\r
126 }\r
127 \r
128 // -------------------------------------------------------------------------\r
129 void PipelineEditor::QNETypePort::\r
130 setName( const QString& n )\r
131 {\r
132   this->Superclass::setName( n );\r
133 \r
134   QFont font( this->scene( )->font( ) );\r
135   font.setItalic( true );\r
136   this->m_Label->setFont( font );\r
137   this->setPath( QPainterPath( ) );\r
138 }\r
139 \r
140 // -------------------------------------------------------------------------\r
141 bool PipelineEditor::QNETypePort::\r
142 isConnected( QNEPort* other )\r
143 {\r
144   return( false );\r
145 }\r
146 \r
147 // -------------------------------------------------------------------------\r
148 PipelineEditor::QNEInputPort::\r
149 QNEInputPort( QGraphicsItem* parent, QGraphicsScene* scene )\r
150   : Superclass( parent, scene ),\r
151     m_Connection( NULL )\r
152 {\r
153 }\r
154 \r
155 // -------------------------------------------------------------------------\r
156 PipelineEditor::QNEInputPort::\r
157 ~QNEInputPort( )\r
158 {\r
159   if( this->m_Connection != NULL )\r
160     delete this->m_Connection;\r
161 }\r
162 \r
163 // -------------------------------------------------------------------------\r
164 void PipelineEditor::QNEInputPort::\r
165 setName( const QString& n )\r
166 {\r
167   this->Superclass::setName( n );\r
168 \r
169   QFontMetrics fm( this->scene( )->font( ) );\r
170   QRect r = fm.boundingRect( this->m_Name );\r
171   int rm = this->m_Radius + this->m_Margin;\r
172   int h = -this->m_Label->boundingRect( ).height( ) / 2;\r
173   this->m_Label->setPos( rm, h );\r
174 }\r
175 \r
176 // -------------------------------------------------------------------------\r
177 bool PipelineEditor::QNEInputPort::\r
178 isConnected( QNEPort* other )\r
179 {\r
180   if( this->m_Connection != NULL )\r
181     return(\r
182       this->m_Connection->port1( ) == other &&\r
183       this->m_Connection->port2( ) == this\r
184       );\r
185   else\r
186     return( false );\r
187 }\r
188 \r
189 // -------------------------------------------------------------------------\r
190 void PipelineEditor::QNEInputPort::\r
191 setConnection( QNEConnection* c )\r
192 {\r
193   this->m_Connection = c;\r
194 }\r
195 \r
196 // -------------------------------------------------------------------------\r
197 QVariant PipelineEditor::QNEInputPort::\r
198 itemChange( GraphicsItemChange change, const QVariant& value )\r
199 {\r
200   if( change == ItemScenePositionHasChanged )\r
201   {\r
202     if( this->m_Connection != NULL )\r
203     {\r
204       this->m_Connection->updatePosFromPorts( );\r
205       this->m_Connection->updatePath( );\r
206 \r
207     } // fi\r
208 \r
209   } // fi\r
210   return( value );\r
211 }\r
212 \r
213 // -------------------------------------------------------------------------\r
214 PipelineEditor::QNEOutputPort::\r
215 QNEOutputPort( QGraphicsItem* parent, QGraphicsScene* scene )\r
216   : Superclass( parent, scene )\r
217 {\r
218 }\r
219 \r
220 // -------------------------------------------------------------------------\r
221 PipelineEditor::QNEOutputPort::\r
222 ~QNEOutputPort( )\r
223 {\r
224   foreach( QNEConnection* conn, this->m_Connections )\r
225     delete conn;\r
226 }\r
227 \r
228 // -------------------------------------------------------------------------\r
229 void PipelineEditor::QNEOutputPort::\r
230 setName( const QString& n )\r
231 {\r
232   this->Superclass::setName( n );\r
233 \r
234   QFontMetrics fm( this->scene( )->font( ) );\r
235   QRect r = fm.boundingRect( this->m_Name );\r
236   int rm = this->m_Radius + this->m_Margin;\r
237   int h = -this->m_Label->boundingRect( ).height( ) / 2;\r
238   this->m_Label->setPos(\r
239     -rm - this->m_Label->boundingRect( ).width( ), h\r
240     );\r
241 }\r
242 \r
243 // -------------------------------------------------------------------------\r
244 bool PipelineEditor::QNEOutputPort::\r
245 isConnected( QNEPort* other )\r
246 {\r
247   auto i = this->m_Connections.begin( );\r
248   bool conn = false;\r
249   for( ; i != this->m_Connections.end( ) && !conn; ++i )\r
250     conn |= ( ( *i )->port1( ) == this && ( *i )->port2( ) == other );\r
251   return( conn );\r
252 }\r
253 \r
254 // -------------------------------------------------------------------------\r
255 QVariant PipelineEditor::QNEOutputPort::\r
256 itemChange( GraphicsItemChange change, const QVariant& value )\r
257 {\r
258   if( change == ItemScenePositionHasChanged )\r
259   {\r
260     foreach( QNEConnection* conn, this->m_Connections )\r
261     {\r
262       conn->updatePosFromPorts( );\r
263       conn->updatePath( );\r
264 \r
265     } // rof\r
266 \r
267   } // fi\r
268   return( value );\r
269 }\r
270 \r
271 // eof - $RCSfile$\r