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