1 /* Copyright (c) 2012, STANISLAW ADASZEWSKI
\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
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
27 #include "QNEBlock.h"
\r
30 #include <QGraphicsScene>
\r
31 #include <QFontMetrics>
\r
33 #include <QStyleOptionGraphicsItem>
\r
35 #include "QNEPort.h"
\r
36 #include "QNEConnection.h"
\r
38 // -------------------------------------------------------------------------
\r
39 PipelineEditor::QNEBlock::
\r
40 QNEBlock( TFilter* filter, QGraphicsItem* parent, QGraphicsScene* scene )
\r
41 : Superclass( parent, scene ),
\r
49 p.addRoundedRect( -50, -15, 100, 30, 5, 5 );
\r
52 this->setPen( QPen( Qt::darkGreen ) );
\r
53 this->setBrush( Qt::green );
\r
54 this->setFlag( QGraphicsItem::ItemIsMovable );
\r
55 this->setFlag( QGraphicsItem::ItemIsSelectable );
\r
57 this->m_Width = this->m_HorzMargin;
\r
58 this->m_Height = this->m_VertMargin;
\r
61 this->setNamePort( this->m_Filter->GetName( ) );
\r
62 this->_setTypePort( this->m_Filter->GetClassName( ) );
\r
65 std::set< std::string > inputs;
\r
66 this->m_Filter->GetInputsNames( inputs );
\r
67 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
\r
68 this->addInputPort( iIt->c_str( ) );
\r
71 std::set< std::string > outputs;
\r
72 this->m_Filter->GetOutputsNames( outputs );
\r
73 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
\r
74 this->addOutputPort( oIt->c_str( ) );
\r
77 // -------------------------------------------------------------------------
\r
78 PipelineEditor::QNEBlock::
\r
83 // -------------------------------------------------------------------------
\r
84 void PipelineEditor::QNEBlock::
\r
85 setNamePort( const QString& txt )
\r
87 if( this->m_NamePort == NULL )
\r
88 this->m_NamePort = new QNENamePort( this );
\r
89 this->m_NamePort->setName( txt );
\r
90 this->m_Filter->SetName( txt.toStdString( ) );
\r
91 this->_configPort( this->m_NamePort );
\r
94 // -------------------------------------------------------------------------
\r
95 PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock::
\r
96 addInputPort( const QString& txt )
\r
98 QNEInputPort* ip = new QNEInputPort( this );
\r
99 ip->setExtendedName(
\r
101 txt.toStdString( ) +
\r
102 std::string( "@" ) +
\r
103 this->namePort( ).toStdString( )
\r
106 ip->setName( txt );
\r
107 this->m_InputPorts[ txt.toStdString( ) ] = ip;
\r
108 this->_configPort( ip );
\r
112 // -------------------------------------------------------------------------
\r
113 PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock::
\r
114 addOutputPort( const QString& txt )
\r
116 QNEOutputPort* op = new QNEOutputPort( this );
\r
117 op->setExtendedName(
\r
119 txt.toStdString( ) +
\r
120 std::string( "@" ) +
\r
121 this->namePort( ).toStdString( )
\r
124 op->setName( txt );
\r
125 this->m_OutputPorts[ txt.toStdString( ) ] = op;
\r
126 this->_configPort( op );
\r
130 // -------------------------------------------------------------------------
\r
131 bool PipelineEditor::QNEBlock::
\r
132 extendInputPort( const QString& txt, QNEConnection* conn )
\r
134 auto p = this->m_InputPorts.find( txt.toStdString( ) );
\r
135 auto i = this->m_ExtInputPorts.find( txt.toStdString( ) );
\r
136 if( p != this->m_InputPorts.end( ) && i == this->m_ExtInputPorts.end( ) )
\r
138 this->m_ExtInputPorts[ txt.toStdString( ) ] = conn;
\r
145 // -------------------------------------------------------------------------
\r
146 bool PipelineEditor::QNEBlock::
\r
147 extendOutputPort( const QString& txt, QNEConnection* conn )
\r
149 auto p = this->m_OutputPorts.find( txt.toStdString( ) );
\r
150 auto i = this->m_ExtOutputPorts.find( txt.toStdString( ) );
\r
151 if( p != this->m_OutputPorts.end( ) && i == this->m_ExtOutputPorts.end( ) )
\r
153 this->m_ExtOutputPorts[ txt.toStdString( ) ] = conn;
\r
160 // -------------------------------------------------------------------------
\r
161 PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock::
\r
162 inputPort( const QString& txt )
\r
164 auto i = this->m_InputPorts.find( txt.toStdString( ) );
\r
165 if( i != this->m_InputPorts.end( ) )
\r
166 return( i->second );
\r
171 // -------------------------------------------------------------------------
\r
172 PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock::
\r
173 outputPort( const QString& txt )
\r
175 auto o = this->m_OutputPorts.find( txt.toStdString( ) );
\r
176 if( o != this->m_OutputPorts.end( ) )
\r
177 return( o->second );
\r
182 // -------------------------------------------------------------------------
\r
183 QString PipelineEditor::QNEBlock::
\r
186 return( this->m_NamePort->name( ) );
\r
189 // -------------------------------------------------------------------------
\r
190 const PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock::
\r
191 inputPort( const QString& txt ) const
\r
193 auto i = this->m_InputPorts.find( txt.toStdString( ) );
\r
194 if( i != this->m_InputPorts.end( ) )
\r
195 return( i->second );
\r
200 // -------------------------------------------------------------------------
\r
201 const PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock::
\r
202 outputPort( const QString& txt ) const
\r
204 auto o = this->m_OutputPorts.find( txt.toStdString( ) );
\r
205 if( o != this->m_OutputPorts.end( ) )
\r
206 return( o->second );
\r
211 // -------------------------------------------------------------------------
\r
212 const PipelineEditor::QNEConnection* PipelineEditor::QNEBlock::
\r
213 extendedInputPort( const QString& txt ) const
\r
215 auto i = this->m_ExtInputPorts.find( txt.toStdString( ) );
\r
216 if( i != this->m_ExtInputPorts.end( ) )
\r
217 return( i->second );
\r
222 // -------------------------------------------------------------------------
\r
223 const PipelineEditor::QNEConnection* PipelineEditor::QNEBlock::
\r
224 extendedOutputPort( const QString& txt ) const
\r
226 auto i = this->m_ExtOutputPorts.find( txt.toStdString( ) );
\r
227 if( i != this->m_ExtOutputPorts.end( ) )
\r
228 return( i->second );
\r
233 // -------------------------------------------------------------------------
\r
234 void PipelineEditor::QNEBlock::
\r
236 QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget
\r
239 Q_UNUSED( option );
\r
240 Q_UNUSED( widget );
\r
242 if( this->isSelected( ) )
\r
244 painter->setPen( QPen( Qt::darkYellow ) );
\r
245 painter->setBrush( Qt::yellow );
\r
249 painter->setPen( QPen( Qt::darkGreen ) );
\r
250 painter->setBrush( Qt::green );
\r
253 painter->drawPath( this->path( ) );
\r
256 // -------------------------------------------------------------------------
\r
257 QVariant PipelineEditor::QNEBlock::
\r
258 itemChange( GraphicsItemChange change, const QVariant& value )
\r
263 // -------------------------------------------------------------------------
\r
264 void PipelineEditor::QNEBlock::
\r
265 _setTypePort( const QString& txt )
\r
267 if( this->m_TypePort == NULL )
\r
268 this->m_TypePort = new QNETypePort( this );
\r
269 this->m_TypePort->setName( txt );
\r
270 this->_configPort( this->m_TypePort );
\r
273 // -------------------------------------------------------------------------
\r
274 void PipelineEditor::QNEBlock::
\r
275 _configPort( QNEPort* port )
\r
277 port->setBlock( this );
\r
279 QFontMetrics fm( this->scene( )->font( ) );
\r
280 int w = fm.width( port->name( ) ) + ( 4 * port->radius( ) );
\r
281 int h = fm.height( );
\r
282 if( w > this->m_Width - this->m_HorzMargin )
\r
283 this->m_Width = w + this->m_HorzMargin;
\r
284 this->m_Height = this->m_InputPorts.size( ) + this->m_OutputPorts.size( );
\r
285 this->m_Height += 4;
\r
286 this->m_Height *= h;
\r
289 pth.addRoundedRect(
\r
290 -this->m_Width / 2,
\r
291 -this->m_Height / 2,
\r
293 this->m_Height, 5, 5
\r
295 this->setPath( pth );
\r
297 int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( );
\r
298 foreach( QGraphicsItem* i, this->children( ) )
\r
300 QNEPort* p = dynamic_cast< QNEPort* >( i );
\r
304 if( dynamic_cast< QNENamePort* >( i ) != NULL )
\r
305 i->setPos( -this->m_Width / 2 + port->radius( ), y );
\r
306 else if( dynamic_cast< QNETypePort* >( i ) != NULL )
\r
308 i->setPos( -this->m_Width / 2 + port->radius( ), y );
\r
311 else if( dynamic_cast< QNEInputPort* >( i ) != NULL )
\r
312 i->setPos( -this->m_Width / 2 - 2 * port->radius( ), y );
\r
313 else if( dynamic_cast< QNEOutputPort* >( i ) != NULL )
\r
314 i->setPos( this->m_Width / 2, y );
\r
317 QNEPort* p = dynamic_cast< QNEPort* >( i );
\r
321 if( dynamic_cast< QNEOutputPort* >( p ) != NULL )
\r
322 p->setPos( this->m_Width / 2 + port->radius( ), y );
\r
324 p->setPos( -this->m_Width / 2 - port->radius( ), y );
\r
331 // -------------------------------------------------------------------------
\r
332 void PipelineEditor::QNEBlock::
\r
333 mouseReleaseEvent( QGraphicsSceneMouseEvent* evt )
\r
335 if( this->m_Filter.IsNotNull( ) )
\r
336 this->m_Filter->SetViewCoords(
\r
337 this->scenePos( ).x( ), this->scenePos( ).y( )
\r
339 this->Superclass::mouseReleaseEvent( evt );
\r