]> 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( 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 PipelineEditor::QNEPort* PipelineEditor::QNEBlock::\r
107 addInputPort( const QString& name )\r
108 {\r
109   return( this->addPort( name, false ) );\r
110 }\r
111 \r
112 // -------------------------------------------------------------------------\r
113 PipelineEditor::QNEPort* PipelineEditor::QNEBlock::\r
114 addOutputPort( const QString& name )\r
115 {\r
116   return( 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 paint(\r
138   QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget\r
139   )\r
140 {\r
141   Q_UNUSED( option );\r
142   Q_UNUSED( widget );\r
143 \r
144   if( this->isSelected( ) )\r
145   {\r
146     painter->setPen( QPen( Qt::darkYellow ) );\r
147     painter->setBrush( Qt::yellow );\r
148   }\r
149   else\r
150   {\r
151     painter->setPen( QPen( Qt::darkGreen ) );\r
152     painter->setBrush( Qt::green );\r
153 \r
154   } // fi\r
155   painter->drawPath( this->path( ) );\r
156 }\r
157 \r
158 // -------------------------------------------------------------------------\r
159 PipelineEditor::QNEBlock* PipelineEditor::QNEBlock::\r
160 clone( )\r
161 {\r
162   QNEBlock* b = new QNEBlock( 0, this->scene( ) );\r
163 \r
164   foreach( QGraphicsItem* port_, childItems( ) )\r
165   {\r
166     if( port_->type( ) == QNEPort::Type )\r
167     {\r
168       QNEPort* port = ( QNEPort* ) port_;\r
169       b->addPort(\r
170         port->portName( ),\r
171         port->isOutput( ),\r
172         port->portFlags( ),\r
173         port->ptr( )\r
174         );\r
175 \r
176     } // fi\r
177 \r
178   } // rof\r
179   return( b );\r
180 }\r
181 \r
182 // -------------------------------------------------------------------------\r
183 QVector< PipelineEditor::QNEPort* > PipelineEditor::QNEBlock::\r
184 ports( )\r
185 {\r
186   QVector< PipelineEditor::QNEPort* > res;\r
187   foreach( QGraphicsItem* port_, childItems( ) )\r
188   {\r
189     if( port_->type( ) == QNEPort::Type )\r
190       res.append( ( QNEPort* ) port_ );\r
191 \r
192   } // rof\r
193   return( res );\r
194 }\r
195 \r
196 // -------------------------------------------------------------------------\r
197 QVariant PipelineEditor::QNEBlock::\r
198 itemChange( GraphicsItemChange change, const QVariant& value )\r
199 {\r
200   return( value );\r
201 }\r
202 \r
203 // eof - $RCSfile$\r