]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/Port.cxx
bdd9aea75819aad334ad54894dda32e894a2add4
[cpPlugins.git] / lib / cpBaseQtApplication / Port.cxx
1 #include <cpBaseQtApplication/Port.h>\r
2 #include <cpBaseQtApplication/Connection.h>\r
3 #include <cpBaseQtApplication/Block.h>\r
4 #include <cpBaseQtApplication/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 cpBaseQtApplication::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 cpBaseQtApplication::Port::\r
35 ~Port( )\r
36 {\r
37 }\r
38 \r
39 // -------------------------------------------------------------------------\r
40 void cpBaseQtApplication::Port::\r
41 setBlock( Block* b )\r
42 {\r
43   this->m_Block = b;\r
44 }\r
45 \r
46 // -------------------------------------------------------------------------\r
47 void cpBaseQtApplication::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 cpBaseQtApplication::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 cpBaseQtApplication::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 cpBaseQtApplication::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 cpBaseQtApplication::NamePort::\r
98 NamePort( QGraphicsItem* parent, QGraphicsScene* scene )\r
99   : Superclass( parent, scene )\r
100 {\r
101 }\r
102 \r
103 // -------------------------------------------------------------------------\r
104 cpBaseQtApplication::NamePort::\r
105 ~NamePort( )\r
106 {\r
107 }\r
108 \r
109 // -------------------------------------------------------------------------\r
110 void cpBaseQtApplication::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 cpBaseQtApplication::NamePort::\r
122 isConnected( Port* other )\r
123 {\r
124   return( false );\r
125 }\r
126 \r
127 // -------------------------------------------------------------------------\r
128 cpBaseQtApplication::InputPort::\r
129 InputPort( QGraphicsItem* parent, QGraphicsScene* scene )\r
130   : Superclass( parent, scene ),\r
131     m_Connection( NULL )\r
132 {\r
133 }\r
134 \r
135 // -------------------------------------------------------------------------\r
136 cpBaseQtApplication::InputPort::\r
137 ~InputPort( )\r
138 {\r
139   if( this->m_Connection != NULL )\r
140     delete this->m_Connection;\r
141 }\r
142 \r
143 // -------------------------------------------------------------------------\r
144 void cpBaseQtApplication::InputPort::\r
145 _updateLabels( )\r
146 {\r
147   QFontMetrics fm( this->scene( )->font( ) );\r
148   this->m_Label->setPos( this->m_Radius * 2, -fm.height( ) / 2 );\r
149   this->m_ExtendedLabel->setPos(\r
150     -fm.width( this->extendedName( ) ) - this->m_Radius * 2,\r
151     -fm.height( ) / 2\r
152     );\r
153 }\r
154 \r
155 // -------------------------------------------------------------------------\r
156 void cpBaseQtApplication::InputPort::\r
157 setExtend( bool extend )\r
158 {\r
159   if( this->m_Connection == NULL )\r
160   {\r
161     this->m_IsExtended = extend;\r
162     this->m_ExtendedLabel->setVisible( extend );\r
163   }\r
164   else\r
165     this->Superclass::setExtend( false );\r
166 }\r
167 \r
168 // -------------------------------------------------------------------------\r
169 bool cpBaseQtApplication::InputPort::\r
170 isConnected( Port* other )\r
171 {\r
172   if( this->m_Connection != NULL )\r
173     return(\r
174       this->m_Connection->port1( ) == other &&\r
175       this->m_Connection->port2( ) == this\r
176       );\r
177   else\r
178     return( false );\r
179 }\r
180 \r
181 // -------------------------------------------------------------------------\r
182 void cpBaseQtApplication::InputPort::\r
183 setConnection( Connection* c )\r
184 {\r
185   this->m_Connection = c;\r
186 }\r
187 \r
188 // -------------------------------------------------------------------------\r
189 QVariant cpBaseQtApplication::InputPort::\r
190 itemChange( GraphicsItemChange change, const QVariant& value )\r
191 {\r
192   if( change == ItemScenePositionHasChanged )\r
193   {\r
194     if( this->m_Connection != NULL )\r
195     {\r
196       this->m_Connection->updatePosFromPorts( );\r
197       this->m_Connection->updatePath( );\r
198 \r
199     } // fi\r
200 \r
201   } // fi\r
202   return( value );\r
203 }\r
204 \r
205 // -------------------------------------------------------------------------\r
206 cpBaseQtApplication::OutputPort::\r
207 OutputPort( QGraphicsItem* parent, QGraphicsScene* scene )\r
208   : Superclass( parent, scene )\r
209 {\r
210 }\r
211 \r
212 // -------------------------------------------------------------------------\r
213 cpBaseQtApplication::OutputPort::\r
214 ~OutputPort( )\r
215 {\r
216   foreach( Connection* conn, this->m_Connections )\r
217     delete conn;\r
218 }\r
219 \r
220 // -------------------------------------------------------------------------\r
221 void cpBaseQtApplication::OutputPort::\r
222 _updateLabels( )\r
223 {\r
224   QFontMetrics fm( this->scene( )->font( ) );\r
225   this->m_Label->setPos(\r
226     -fm.width( this->name( ) ) - this->m_Radius * 2, -fm.height( ) / 2\r
227     );\r
228   this->m_ExtendedLabel->setPos( this->m_Radius * 2, -fm.height( ) / 2 );\r
229 \r
230 }\r
231 \r
232 // -------------------------------------------------------------------------\r
233 void cpBaseQtApplication::OutputPort::\r
234 setExtend( bool extend )\r
235 {\r
236   this->m_IsExtended = extend;\r
237   this->m_ExtendedLabel->setVisible( extend );\r
238 }\r
239 \r
240 // -------------------------------------------------------------------------\r
241 bool cpBaseQtApplication::OutputPort::\r
242 isConnected( Port* other )\r
243 {\r
244   auto i = this->m_Connections.begin( );\r
245   bool conn = false;\r
246   for( ; i != this->m_Connections.end( ) && !conn; ++i )\r
247     conn |= ( ( *i )->port1( ) == this && ( *i )->port2( ) == other );\r
248   return( conn );\r
249 }\r
250 \r
251 // -------------------------------------------------------------------------\r
252 QVariant cpBaseQtApplication::OutputPort::\r
253 itemChange( GraphicsItemChange change, const QVariant& value )\r
254 {\r
255   if( change == ItemScenePositionHasChanged )\r
256   {\r
257     foreach( Connection* conn, this->m_Connections )\r
258     {\r
259       conn->updatePosFromPorts( );\r
260       conn->updatePath( );\r
261 \r
262     } // rof\r
263 \r
264   } // fi\r
265   return( value );\r
266 }\r
267 \r
268 // -------------------------------------------------------------------------\r
269 void cpBaseQtApplication::OutputPort::\r
270 contextMenuEvent( QGraphicsSceneContextMenuEvent* evt )\r
271 {\r
272   if( this->m_Block == NULL )\r
273     return;\r
274   \r
275   QMenu menu;\r
276   QAction* showAction = menu.addAction( "Show" );\r
277   QAction* hideAction = menu.addAction( "Hide" );\r
278   QAction* propertiesAction = menu.addAction( "Properties" );\r
279   QAction* selectedAction = menu.exec( evt->screenPos( ) );\r
280 \r
281   if( selectedAction == showAction )\r
282   {\r
283     this->m_Block->editor( )->showData(\r
284       this->m_Block->namePort( ).toStdString( ),\r
285       this->name( ).toStdString( )\r
286       );\r
287   }\r
288   else if( selectedAction == hideAction )\r
289   {\r
290     this->m_Block->editor( )->hideData(\r
291       this->m_Block->namePort( ).toStdString( ),\r
292       this->name( ).toStdString( )\r
293       );\r
294   }\r
295   else if( selectedAction == propertiesAction )\r
296   {\r
297     this->m_Block->editor( )->dataProperties(\r
298       this->m_Block->namePort( ).toStdString( ),\r
299       this->name( ).toStdString( )\r
300       );\r
301 \r
302   } // fi\r
303 }\r
304 \r
305 // eof - $RCSfile$\r