]> Creatis software - cpPlugins.git/blob - lib/cpPipelineEditor/Block.cxx
Now it's broken :-(
[cpPlugins.git] / lib / cpPipelineEditor / Block.cxx
1 #include "Block.h"\r
2 \r
3 #include <QPen>\r
4 #include <QGraphicsScene>\r
5 #include <QFontMetrics>\r
6 #include <QPainter>\r
7 #include <QStyleOptionGraphicsItem>\r
8 \r
9 #include "Port.h"\r
10 #include "Connection.h"\r
11 \r
12 // -------------------------------------------------------------------------\r
13 cpPipelineEditor::Block::\r
14 Block( TFilter* filter, QGraphicsItem* parent, QGraphicsScene* scene )\r
15   : Superclass( parent, scene ),\r
16     m_HorzMargin( 20 ),\r
17     m_VertMargin( 5 ),\r
18     m_NamePort( NULL ),\r
19     m_TypePort( NULL ),\r
20     m_Filter( filter )\r
21 {\r
22   QPainterPath p;\r
23   p.addRoundedRect( -50, -15, 100, 30, 5, 5 );\r
24 \r
25   this->setPath( p );\r
26   this->setPen( QPen( Qt::darkGreen ) );\r
27   this->setBrush( Qt::green );\r
28   this->setFlag( QGraphicsItem::ItemIsMovable );\r
29   this->setFlag( QGraphicsItem::ItemIsSelectable );\r
30 \r
31   this->m_Width = this->m_HorzMargin;\r
32   this->m_Height = this->m_VertMargin;\r
33 \r
34   // Configure names\r
35   this->setNamePort( this->m_Filter->GetName( ) );\r
36   this->_setTypePort( this->m_Filter->GetClassName( ) );\r
37 \r
38   // Add input ports\r
39   std::set< std::string > inputs;\r
40   this->m_Filter->GetInputsNames( inputs );\r
41   for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )\r
42     this->addInputPort( iIt->c_str( ) );\r
43 \r
44   // Add output ports\r
45   std::set< std::string > outputs;\r
46   this->m_Filter->GetOutputsNames( outputs );\r
47   for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )\r
48     this->addOutputPort( oIt->c_str( ) );\r
49 }\r
50 \r
51 // -------------------------------------------------------------------------\r
52 cpPipelineEditor::Block::\r
53 ~Block( )\r
54 {\r
55 }\r
56 \r
57 // -------------------------------------------------------------------------\r
58 void cpPipelineEditor::Block::\r
59 setNamePort( const QString& txt )\r
60 {\r
61   if( this->m_NamePort == NULL )\r
62     this->m_NamePort = new NamePort( this );\r
63   this->m_NamePort->setName( txt );\r
64   this->_configPort( this->m_NamePort );\r
65 }\r
66 \r
67 // -------------------------------------------------------------------------\r
68 cpPipelineEditor::InputPort* cpPipelineEditor::Block::\r
69 addInputPort( const QString& txt )\r
70 {\r
71   InputPort* ip = new InputPort( this );\r
72   ip->setExtendedName(\r
73     (\r
74       txt.toStdString( ) +\r
75       std::string( "@" ) +\r
76       this->namePort( ).toStdString( )\r
77       ).c_str( )\r
78     );\r
79   ip->setName( txt );\r
80   this->m_InputPorts[ txt.toStdString( ) ] = ip;\r
81   this->_configPort( ip );\r
82   return( ip );\r
83 }\r
84 \r
85 // -------------------------------------------------------------------------\r
86 cpPipelineEditor::OutputPort* cpPipelineEditor::Block::\r
87 addOutputPort( const QString& txt )\r
88 {\r
89   OutputPort* op = new OutputPort( this );\r
90   op->setExtendedName(\r
91     (\r
92       txt.toStdString( ) +\r
93       std::string( "@" ) +\r
94       this->namePort( ).toStdString( )\r
95       ).c_str( )\r
96     );\r
97   op->setName( txt );\r
98   this->m_OutputPorts[ txt.toStdString( ) ] = op;\r
99   this->_configPort( op );\r
100   return( op );\r
101 }\r
102 \r
103 // -------------------------------------------------------------------------\r
104 cpPipelineEditor::InputPort* cpPipelineEditor::Block::\r
105 inputPort( const QString& txt )\r
106 {\r
107   auto i = this->m_InputPorts.find( txt.toStdString( ) );\r
108   if( i != this->m_InputPorts.end( ) )\r
109     return( i->second );\r
110   else\r
111     return( NULL );\r
112 }\r
113 \r
114 // -------------------------------------------------------------------------\r
115 cpPipelineEditor::OutputPort* cpPipelineEditor::Block::\r
116 outputPort( const QString& txt )\r
117 {\r
118   auto o = this->m_OutputPorts.find( txt.toStdString( ) );\r
119   if( o != this->m_OutputPorts.end( ) )\r
120     return( o->second );\r
121   else\r
122     return( NULL );\r
123 }\r
124 \r
125 // -------------------------------------------------------------------------\r
126 QString cpPipelineEditor::Block::\r
127 namePort( ) const\r
128 {\r
129   return( this->m_NamePort->name( ) );\r
130 }\r
131 \r
132 // -------------------------------------------------------------------------\r
133 const cpPipelineEditor::InputPort* cpPipelineEditor::Block::\r
134 inputPort( const QString& txt ) const\r
135 {\r
136   auto i = this->m_InputPorts.find( txt.toStdString( ) );\r
137   if( i != this->m_InputPorts.end( ) )\r
138     return( i->second );\r
139   else\r
140     return( NULL );\r
141 }\r
142 \r
143 // -------------------------------------------------------------------------\r
144 const cpPipelineEditor::OutputPort* cpPipelineEditor::Block::\r
145 outputPort( const QString& txt ) const\r
146 {\r
147   auto o = this->m_OutputPorts.find( txt.toStdString( ) );\r
148   if( o != this->m_OutputPorts.end( ) )\r
149     return( o->second );\r
150   else\r
151     return( NULL );\r
152 }\r
153 \r
154 // -------------------------------------------------------------------------\r
155 void cpPipelineEditor::Block::\r
156 paint(\r
157   QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget\r
158   )\r
159 {\r
160   Q_UNUSED( option );\r
161   Q_UNUSED( widget );\r
162 \r
163   if( this->isSelected( ) )\r
164   {\r
165     painter->setPen( QPen( Qt::darkYellow ) );\r
166     painter->setBrush( Qt::yellow );\r
167   }\r
168   else\r
169   {\r
170     painter->setPen( QPen( Qt::darkGreen ) );\r
171     painter->setBrush( Qt::green );\r
172 \r
173   } // fi\r
174   painter->drawPath( this->path( ) );\r
175 }\r
176 \r
177 // -------------------------------------------------------------------------\r
178 QVariant cpPipelineEditor::Block::\r
179 itemChange( GraphicsItemChange change, const QVariant& value )\r
180 {\r
181   return( value );\r
182 }\r
183 \r
184 // -------------------------------------------------------------------------\r
185 void cpPipelineEditor::Block::\r
186 _setTypePort( const QString& txt )\r
187 {\r
188   if( this->m_TypePort == NULL )\r
189     this->m_TypePort = new TypePort( this );\r
190   this->m_TypePort->setName( txt );\r
191   this->_configPort( this->m_TypePort );\r
192 }\r
193 \r
194 // -------------------------------------------------------------------------\r
195 void cpPipelineEditor::Block::\r
196 _configPort( Port* port )\r
197 {\r
198   port->setBlock( this );\r
199 \r
200   QFontMetrics fm( this->scene( )->font( ) );\r
201   int w = fm.width( port->name( ) ) + ( 4 * port->radius( ) );\r
202   int h = fm.height( );\r
203   if( w > this->m_Width - this->m_HorzMargin )\r
204     this->m_Width = w + this->m_HorzMargin;\r
205   this->m_Height = this->m_InputPorts.size( ) + this->m_OutputPorts.size( );\r
206   this->m_Height += 4;\r
207   this->m_Height *= h;\r
208 \r
209   QPainterPath pth;\r
210   pth.addRoundedRect(\r
211     -this->m_Width / 2,\r
212     -this->m_Height / 2,\r
213     this->m_Width,\r
214     this->m_Height, 5, 5\r
215     );\r
216   this->setPath( pth );\r
217 \r
218   int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( );\r
219   foreach( QGraphicsItem* i, this->children( ) )\r
220   {\r
221     Port* p = dynamic_cast< Port* >( i );\r
222     if( p == NULL )\r
223       continue;\r
224 \r
225     if( dynamic_cast< NamePort* >( i ) != NULL )\r
226       i->setPos( -this->m_Width / 2 + port->radius( ), y );\r
227     else if( dynamic_cast< TypePort* >( i ) != NULL )\r
228     {\r
229       i->setPos( -this->m_Width / 2 + port->radius( ), y );\r
230       y += h;\r
231     }\r
232     else if( dynamic_cast< InputPort* >( i ) != NULL )\r
233       i->setPos( -this->m_Width / 2 - 2 * port->radius( ), y );\r
234     else if( dynamic_cast< OutputPort* >( i ) != NULL )\r
235       i->setPos( this->m_Width / 2, y );\r
236 \r
237     /* TODO\r
238        Port* p = dynamic_cast< Port* >( i );\r
239        if( p == NULL )\r
240        continue;\r
241 \r
242        if( dynamic_cast< OutputPort* >( p ) != NULL )\r
243        p->setPos( this->m_Width / 2 + port->radius( ), y );\r
244        else\r
245        p->setPos( -this->m_Width / 2 - port->radius( ), y );\r
246     */\r
247     y += h;\r
248 \r
249   } // rof\r
250 }\r
251 \r
252 // -------------------------------------------------------------------------\r
253 void cpPipelineEditor::Block::\r
254 mouseReleaseEvent( QGraphicsSceneMouseEvent* evt )\r
255 {\r
256   if( this->m_Filter != NULL )\r
257     this->m_Filter->SetViewCoords(\r
258       this->scenePos( ).x( ), this->scenePos( ).y( )\r
259       );\r
260   this->Superclass::mouseReleaseEvent( evt );\r
261 }\r
262 \r
263 // eof - $RCSfile$\r