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
37 // -------------------------------------------------------------------------
\r
38 PipelineEditor::QNEBlock::
\r
39 QNEBlock( TFilter* filter, QGraphicsItem* parent, QGraphicsScene* scene )
\r
40 : Superclass( parent, scene ),
\r
48 p.addRoundedRect( -50, -15, 100, 30, 5, 5 );
\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
56 this->m_Width = this->m_HorzMargin;
\r
57 this->m_Height = this->m_VertMargin;
\r
60 this->setNamePort( this->m_Filter->GetName( ) );
\r
61 this->_setTypePort( this->m_Filter->GetClassName( ) );
\r
64 std::set< std::string > inputs;
\r
65 this->m_Filter->GetInputsNames( inputs );
\r
66 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
\r
67 this->addInputPort( iIt->c_str( ) );
\r
70 std::set< std::string > outputs;
\r
71 this->m_Filter->GetOutputsNames( outputs );
\r
72 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
\r
73 this->addOutputPort( oIt->c_str( ) );
\r
76 // -------------------------------------------------------------------------
\r
77 PipelineEditor::QNEBlock::
\r
82 // -------------------------------------------------------------------------
\r
83 void PipelineEditor::QNEBlock::
\r
84 setNamePort( const QString& txt )
\r
86 if( this->m_NamePort == NULL )
\r
87 this->m_NamePort = new QNENamePort( this );
\r
88 this->m_NamePort->setName( txt );
\r
89 this->m_Filter->SetName( txt.toStdString( ) );
\r
90 this->_configPort( this->m_NamePort );
\r
93 // -------------------------------------------------------------------------
\r
94 PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock::
\r
95 addInputPort( const QString& txt )
\r
97 QNEInputPort* ip = new QNEInputPort( this );
\r
99 this->m_InputPorts[ txt.toStdString( ) ] = ip;
\r
100 this->_configPort( ip );
\r
104 // -------------------------------------------------------------------------
\r
105 PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock::
\r
106 addOutputPort( const QString& txt )
\r
108 QNEOutputPort* op = new QNEOutputPort( this );
\r
109 op->setName( txt );
\r
110 this->m_OutputPorts[ txt.toStdString( ) ] = op;
\r
111 this->_configPort( op );
\r
115 // -------------------------------------------------------------------------
\r
116 PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock::
\r
117 inputPort( const QString& txt )
\r
119 auto i = this->m_InputPorts.find( txt.toStdString( ) );
\r
120 if( i != this->m_InputPorts.end( ) )
\r
121 return( i->second );
\r
126 // -------------------------------------------------------------------------
\r
127 PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock::
\r
128 outputPort( const QString& txt )
\r
130 auto o = this->m_OutputPorts.find( txt.toStdString( ) );
\r
131 if( o != this->m_OutputPorts.end( ) )
\r
132 return( o->second );
\r
137 // -------------------------------------------------------------------------
\r
138 const PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock::
\r
139 inputPort( const QString& txt ) const
\r
141 auto i = this->m_InputPorts.find( txt.toStdString( ) );
\r
142 if( i != this->m_InputPorts.end( ) )
\r
143 return( i->second );
\r
148 // -------------------------------------------------------------------------
\r
149 const PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock::
\r
150 outputPort( const QString& txt ) const
\r
152 auto o = this->m_OutputPorts.find( txt.toStdString( ) );
\r
153 if( o != this->m_OutputPorts.end( ) )
\r
154 return( o->second );
\r
159 // -------------------------------------------------------------------------
\r
160 const QString& PipelineEditor::QNEBlock::
\r
163 return( this->m_NamePort->name( ) );
\r
166 // -------------------------------------------------------------------------
\r
167 void PipelineEditor::QNEBlock::
\r
169 QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget
\r
172 Q_UNUSED( option );
\r
173 Q_UNUSED( widget );
\r
175 if( this->isSelected( ) )
\r
177 painter->setPen( QPen( Qt::darkYellow ) );
\r
178 painter->setBrush( Qt::yellow );
\r
182 painter->setPen( QPen( Qt::darkGreen ) );
\r
183 painter->setBrush( Qt::green );
\r
186 painter->drawPath( this->path( ) );
\r
189 // -------------------------------------------------------------------------
\r
190 QVariant PipelineEditor::QNEBlock::
\r
191 itemChange( GraphicsItemChange change, const QVariant& value )
\r
196 // -------------------------------------------------------------------------
\r
197 void PipelineEditor::QNEBlock::
\r
198 _setTypePort( const QString& txt )
\r
200 if( this->m_TypePort == NULL )
\r
201 this->m_TypePort = new QNETypePort( this );
\r
202 this->m_TypePort->setName( txt );
\r
203 this->_configPort( this->m_TypePort );
\r
206 // -------------------------------------------------------------------------
\r
207 void PipelineEditor::QNEBlock::
\r
208 _configPort( QNEPort* port )
\r
210 port->setBlock( this );
\r
212 QFontMetrics fm( this->scene( )->font( ) );
\r
213 int w = fm.width( port->name( ) );
\r
214 int h = fm.height( );
\r
215 if( w > this->m_Width - this->m_HorzMargin )
\r
216 this->m_Width = w + this->m_HorzMargin;
\r
217 this->m_Height += h;
\r
220 pth.addRoundedRect(
\r
221 -this->m_Width / 2,
\r
222 -this->m_Height / 2,
\r
224 this->m_Height, 5, 5
\r
226 this->setPath( pth );
\r
228 int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( );
\r
229 foreach( QGraphicsItem* i, this->children( ) )
\r
231 QNEPort* p = dynamic_cast< QNEPort* >( i );
\r
235 if( dynamic_cast< QNEOutputPort* >( p ) != NULL )
\r
236 p->setPos( this->m_Width / 2 + port->radius( ), y );
\r
238 p->setPos( -this->m_Width / 2 - port->radius( ), y );
\r
244 // -------------------------------------------------------------------------
\r
245 void PipelineEditor::QNEBlock::
\r
246 mouseReleaseEvent( QGraphicsSceneMouseEvent* evt )
\r
248 if( this->m_Filter.IsNotNull( ) )
\r
249 this->m_Filter->SetViewCoords(
\r
250 this->scenePos( ).x( ), this->scenePos( ).y( )
\r
252 this->Superclass::mouseReleaseEvent( evt );
\r