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