]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/QNEBlock.cxx
...
[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 \r
60 // -------------------------------------------------------------------------\r
61 PipelineEditor::QNEBlock::\r
62 ~QNEBlock( )\r
63 {\r
64 }\r
65 \r
66 // -------------------------------------------------------------------------\r
67 void PipelineEditor::QNEBlock::\r
68 setNamePort( const QString& txt )\r
69 {\r
70   if( this->m_NamePort == NULL )\r
71     this->m_NamePort = new QNENamePort( this );\r
72   this->m_NamePort->setName( txt );\r
73   this->m_Filter->SetName( txt.toStdString( ) );\r
74   this->_configPort( this->m_NamePort );\r
75 }\r
76 \r
77 // -------------------------------------------------------------------------\r
78 void PipelineEditor::QNEBlock::\r
79 setTypePort( const QString& txt )\r
80 {\r
81   if( this->m_TypePort == NULL )\r
82     this->m_TypePort = new QNETypePort( this );\r
83   this->m_TypePort->setName( txt );\r
84   this->_configPort( this->m_TypePort );\r
85 }\r
86 \r
87 // -------------------------------------------------------------------------\r
88 void PipelineEditor::QNEBlock::\r
89 addInputPort( const QString& txt )\r
90 {\r
91   QNEInputPort* ip = new QNEInputPort( this );\r
92   ip->setName( txt );\r
93   this->m_InputPorts.push_back( ip );\r
94   this->_configPort( ip );\r
95 }\r
96 \r
97 // -------------------------------------------------------------------------\r
98 void PipelineEditor::QNEBlock::\r
99 addOutputPort( const QString& txt )\r
100 {\r
101   QNEOutputPort* op = new QNEOutputPort( this );\r
102   op->setName( txt );\r
103   this->m_OutputPorts.push_back( op );\r
104   this->_configPort( op );\r
105 }\r
106 \r
107 // -------------------------------------------------------------------------\r
108 QVector< PipelineEditor::QNEPort* > PipelineEditor::QNEBlock::\r
109 ports( )\r
110 {\r
111   QVector< QNEPort* > res;\r
112   foreach( QGraphicsItem* i, this->childItems( ) )\r
113   {\r
114     QNEPort* p = dynamic_cast< QNEPort* >( i );\r
115     if( p != NULL )\r
116       res.append( p );\r
117 \r
118   } // rof\r
119   return( res );\r
120 }\r
121 \r
122 // -------------------------------------------------------------------------\r
123 PipelineEditor::QNEBlock* PipelineEditor::QNEBlock::\r
124 clone( )\r
125 {\r
126   QNEBlock* b = new QNEBlock( this->m_Filter, 0, this->scene( ) );\r
127   foreach( QGraphicsItem* i, this->childItems( ) )\r
128   {\r
129     QNENamePort* np = dynamic_cast< QNENamePort* >( i );\r
130     if( np != NULL )\r
131       b->setNamePort( np->name( ) );\r
132 \r
133     QNETypePort* tp = dynamic_cast< QNETypePort* >( i );\r
134     if( tp != NULL )\r
135       b->setTypePort( tp->name( ) );\r
136 \r
137     QNEInputPort* ip = dynamic_cast< QNEInputPort* >( i );\r
138     if( ip != NULL )\r
139       b->addInputPort( ip->name( ) );\r
140 \r
141     QNEOutputPort* op = dynamic_cast< QNEOutputPort* >( i );\r
142     if( op != NULL )\r
143       b->addOutputPort( op->name( ) );\r
144 \r
145   } // rof\r
146   return( b );\r
147 }\r
148 \r
149 // -------------------------------------------------------------------------\r
150 void PipelineEditor::QNEBlock::\r
151 paint(\r
152   QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget\r
153   )\r
154 {\r
155   Q_UNUSED( option );\r
156   Q_UNUSED( widget );\r
157 \r
158   if( this->isSelected( ) )\r
159   {\r
160     painter->setPen( QPen( Qt::darkYellow ) );\r
161     painter->setBrush( Qt::yellow );\r
162   }\r
163   else\r
164   {\r
165     painter->setPen( QPen( Qt::darkGreen ) );\r
166     painter->setBrush( Qt::green );\r
167 \r
168   } // fi\r
169   painter->drawPath( this->path( ) );\r
170 }\r
171 \r
172 // -------------------------------------------------------------------------\r
173 QVariant PipelineEditor::QNEBlock::\r
174 itemChange( GraphicsItemChange change, const QVariant& value )\r
175 {\r
176   return( value );\r
177 }\r
178 \r
179 // -------------------------------------------------------------------------\r
180 void PipelineEditor::QNEBlock::\r
181 _configPort( QNEPort* port )\r
182 {\r
183   port->setBlock( this );\r
184 \r
185   QFontMetrics fm( this->scene( )->font( ) );\r
186   int w = fm.width( port->name( ) );\r
187   int h = fm.height( );\r
188   if( w > this->m_Width - this->m_HorzMargin )\r
189     this->m_Width = w + this->m_HorzMargin;\r
190   this->m_Height += h;\r
191 \r
192   QPainterPath pth;\r
193   pth.addRoundedRect(\r
194     -this->m_Width / 2,\r
195     -this->m_Height / 2,\r
196     this->m_Width,\r
197     this->m_Height, 5, 5\r
198     );\r
199   this->setPath( pth );\r
200 \r
201   int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( );\r
202   foreach( QGraphicsItem* i, this->children( ) )\r
203   {\r
204     QNEPort* p = dynamic_cast< QNEPort* >( i );\r
205     if( p == NULL )\r
206       continue;\r
207 \r
208     if( dynamic_cast< QNEOutputPort* >( p ) != NULL )\r
209       p->setPos( this->m_Width / 2 + port->radius( ), y );\r
210     else\r
211       p->setPos( -this->m_Width / 2 - port->radius( ), y );\r
212     y += h;\r
213 \r
214   } // rof\r
215 }\r
216 \r
217 // eof - $RCSfile$\r