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