]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/QNEBlock.cxx
d1e01094d1bb535845c708d0cfe498c9c4abbc55
[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( QGraphicsItem* parent, QGraphicsScene* scene )\r
40   : Superclass( parent, scene ),\r
41     m_HorzMargin( 20 ),\r
42     m_VertMargin( 5 )\r
43 {\r
44   QPainterPath p;\r
45   p.addRoundedRect( -50, -15, 100, 30, 5, 5 );\r
46   this->setPath( p );\r
47   this->setPen( QPen( Qt::darkGreen ) );\r
48   this->setBrush( Qt::green );\r
49   this->setFlag( QGraphicsItem::ItemIsMovable );\r
50   this->setFlag( QGraphicsItem::ItemIsSelectable );\r
51   this->m_Width = this->m_HorzMargin;\r
52   this->m_Height = this->m_VertMargin;\r
53 }\r
54 \r
55 // -------------------------------------------------------------------------\r
56 PipelineEditor::QNEBlock::\r
57 ~QNEBlock( )\r
58 {\r
59 }\r
60 \r
61 // -------------------------------------------------------------------------\r
62 PipelineEditor::QNEPort* PipelineEditor::QNEBlock::\r
63 addPort( const QString& name, bool isOutput, int flags, int ptr )\r
64 {\r
65   QNEPort* port = new QNEPort( this );\r
66   port->setName( name );\r
67   port->setIsOutput( isOutput );\r
68   port->setNEBlock( this );\r
69   port->setPortFlags( flags );\r
70   port->setPtr( ptr );\r
71 \r
72   QFontMetrics fm( this->scene( )->font( ) );\r
73   int w = fm.width( name );\r
74   int h = fm.height( );\r
75   if( w > this->m_Width - this->m_HorzMargin )\r
76     this->m_Width = w + this->m_HorzMargin;\r
77   this->m_Height += h;\r
78 \r
79   QPainterPath p;\r
80   p.addRoundedRect(\r
81     -this->m_Width / 2,\r
82     -this->m_Height / 2,\r
83     this->m_Width,\r
84     this->m_Height, 5, 5\r
85     );\r
86   this->setPath( p );\r
87 \r
88   int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( );\r
89   foreach( QGraphicsItem* port_, children( ) )\r
90   {\r
91     if( port_->type( ) != QNEPort::Type )\r
92       continue;\r
93 \r
94     QNEPort* port = ( QNEPort* ) port_;\r
95     if( port->isOutput( ) )\r
96       port->setPos( this->m_Width/2 + port->radius( ), y );\r
97     else\r
98       port->setPos( -this->m_Width/2 - port->radius( ), y );\r
99     y += h;\r
100 \r
101   } // rof\r
102   return( port );\r
103 }\r
104 \r
105 // -------------------------------------------------------------------------\r
106 void PipelineEditor::QNEBlock::\r
107 addInputPort( const QString& name )\r
108 {\r
109   this->addPort( name, false );\r
110 }\r
111 \r
112 // -------------------------------------------------------------------------\r
113 void PipelineEditor::QNEBlock::\r
114 addOutputPort( const QString& name )\r
115 {\r
116   this->addPort( name, true );\r
117 }\r
118 \r
119 // -------------------------------------------------------------------------\r
120 void PipelineEditor::QNEBlock::\r
121 addInputPorts( const QStringList& names )\r
122 {\r
123   foreach( QString n, names )\r
124     this->addInputPort( n );\r
125 }\r
126 \r
127 // -------------------------------------------------------------------------\r
128 void PipelineEditor::QNEBlock::\r
129 addOutputPorts( const QStringList& names )\r
130 {\r
131   foreach( QString n, names )\r
132     this->addOutputPort( n );\r
133 }\r
134 \r
135 // -------------------------------------------------------------------------\r
136 void PipelineEditor::QNEBlock::\r
137 save( QDataStream& ds )\r
138 {\r
139   ds << pos( );\r
140 \r
141   int count( 0 );\r
142 \r
143   foreach( QGraphicsItem* port_, children( ) )\r
144   {\r
145     if( port_->type( ) != QNEPort::Type )\r
146       continue;\r
147     count++;\r
148 \r
149   } // rof\r
150 \r
151   ds << count;\r
152 \r
153   foreach( QGraphicsItem* port_, children( ) )\r
154   {\r
155     if( port_->type( ) != QNEPort::Type )\r
156       continue;\r
157 \r
158     QNEPort* port = ( QNEPort* ) port_;\r
159     ds << ( quint64 ) port;\r
160     ds << port->portName( );\r
161     ds << port->isOutput( );\r
162     ds << port->portFlags( );\r
163 \r
164   } // rof\r
165 }\r
166 \r
167 // -------------------------------------------------------------------------\r
168 void PipelineEditor::QNEBlock::\r
169 load( QDataStream& ds, QMap<quint64, QNEPort*>& portMap )\r
170 {\r
171   QPointF p;\r
172   ds >> p;\r
173   this->setPos( p );\r
174   int count;\r
175   ds >> count;\r
176   for( int i = 0; i < count; i++ )\r
177   {\r
178     QString name;\r
179     bool output;\r
180     int flags;\r
181     quint64 ptr;\r
182 \r
183     ds >> ptr;\r
184     ds >> name;\r
185     ds >> output;\r
186     ds >> flags;\r
187     portMap[ptr] = this->addPort( name, output, flags, ptr );\r
188 \r
189   } // rof\r
190 }\r
191 \r
192 // -------------------------------------------------------------------------\r
193 void PipelineEditor::QNEBlock::\r
194 paint(\r
195   QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget\r
196   )\r
197 {\r
198   Q_UNUSED( option );\r
199   Q_UNUSED( widget );\r
200 \r
201   if( this->isSelected( ) )\r
202   {\r
203     painter->setPen( QPen( Qt::darkYellow ) );\r
204     painter->setBrush( Qt::yellow );\r
205   }\r
206   else\r
207   {\r
208     painter->setPen( QPen( Qt::darkGreen ) );\r
209     painter->setBrush( Qt::green );\r
210 \r
211   } // fi\r
212   painter->drawPath( this->path( ) );\r
213 }\r
214 \r
215 // -------------------------------------------------------------------------\r
216 PipelineEditor::QNEBlock* PipelineEditor::QNEBlock::\r
217 clone( )\r
218 {\r
219   QNEBlock* b = new QNEBlock( 0, this->scene( ) );\r
220 \r
221   foreach( QGraphicsItem* port_, childItems( ) )\r
222   {\r
223     if( port_->type( ) == QNEPort::Type )\r
224     {\r
225       QNEPort* port = ( QNEPort* ) port_;\r
226       b->addPort(\r
227         port->portName( ),\r
228         port->isOutput( ),\r
229         port->portFlags( ),\r
230         port->ptr( )\r
231         );\r
232 \r
233     } // fi\r
234 \r
235   } // rof\r
236   return( b );\r
237 }\r
238 \r
239 // -------------------------------------------------------------------------\r
240 QVector< PipelineEditor::QNEPort* > PipelineEditor::QNEBlock::\r
241 ports( )\r
242 {\r
243   QVector< PipelineEditor::QNEPort* > res;\r
244   foreach( QGraphicsItem* port_, childItems( ) )\r
245   {\r
246     if( port_->type( ) == QNEPort::Type )\r
247       res.append( ( QNEPort* ) port_ );\r
248 \r
249   } // rof\r
250   return( res );\r
251 }\r
252 \r
253 // -------------------------------------------------------------------------\r
254 QVariant PipelineEditor::QNEBlock::\r
255 itemChange( GraphicsItemChange change, const QVariant& value )\r
256 {\r
257   return( value );\r
258 }\r
259 \r
260 // eof - $RCSfile$\r