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 // -------------------------------------------------------------------------
\r
61 PipelineEditor::QNEBlock::
\r
66 // -------------------------------------------------------------------------
\r
67 void PipelineEditor::QNEBlock::
\r
68 setNamePort( const QString& txt )
\r
70 if( this->m_NamePort == NULL )
\r
71 this->m_NamePort = new QNENamePort( this );
\r
72 this->m_NamePort->setName( txt );
\r
73 this->m_Filter->SetName( txt.toStdString( ) );
\r
74 this->_configPort( this->m_NamePort );
\r
77 // -------------------------------------------------------------------------
\r
78 void PipelineEditor::QNEBlock::
\r
79 setTypePort( const QString& txt )
\r
81 if( this->m_TypePort == NULL )
\r
82 this->m_TypePort = new QNETypePort( this );
\r
83 this->m_TypePort->setName( txt );
\r
84 this->_configPort( this->m_TypePort );
\r
87 // -------------------------------------------------------------------------
\r
88 void PipelineEditor::QNEBlock::
\r
89 addInputPort( const QString& txt )
\r
91 QNEInputPort* ip = new QNEInputPort( this );
\r
93 this->m_InputPorts.push_back( ip );
\r
94 this->_configPort( ip );
\r
97 // -------------------------------------------------------------------------
\r
98 void PipelineEditor::QNEBlock::
\r
99 addOutputPort( const QString& txt )
\r
101 QNEOutputPort* op = new QNEOutputPort( this );
\r
102 op->setName( txt );
\r
103 this->m_OutputPorts.push_back( op );
\r
104 this->_configPort( op );
\r
107 // -------------------------------------------------------------------------
\r
108 QVector< PipelineEditor::QNEPort* > PipelineEditor::QNEBlock::
\r
111 QVector< QNEPort* > res;
\r
112 foreach( QGraphicsItem* i, this->childItems( ) )
\r
114 QNEPort* p = dynamic_cast< QNEPort* >( i );
\r
122 // -------------------------------------------------------------------------
\r
123 PipelineEditor::QNEBlock* PipelineEditor::QNEBlock::
\r
126 QNEBlock* b = new QNEBlock( this->m_Filter, 0, this->scene( ) );
\r
127 foreach( QGraphicsItem* i, this->childItems( ) )
\r
129 QNENamePort* np = dynamic_cast< QNENamePort* >( i );
\r
131 b->setNamePort( np->name( ) );
\r
133 QNETypePort* tp = dynamic_cast< QNETypePort* >( i );
\r
135 b->setTypePort( tp->name( ) );
\r
137 QNEInputPort* ip = dynamic_cast< QNEInputPort* >( i );
\r
139 b->addInputPort( ip->name( ) );
\r
141 QNEOutputPort* op = dynamic_cast< QNEOutputPort* >( i );
\r
143 b->addOutputPort( op->name( ) );
\r
149 // -------------------------------------------------------------------------
\r
150 void PipelineEditor::QNEBlock::
\r
152 QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget
\r
155 Q_UNUSED( option );
\r
156 Q_UNUSED( widget );
\r
158 if( this->isSelected( ) )
\r
160 painter->setPen( QPen( Qt::darkYellow ) );
\r
161 painter->setBrush( Qt::yellow );
\r
165 painter->setPen( QPen( Qt::darkGreen ) );
\r
166 painter->setBrush( Qt::green );
\r
169 painter->drawPath( this->path( ) );
\r
172 // -------------------------------------------------------------------------
\r
173 QVariant PipelineEditor::QNEBlock::
\r
174 itemChange( GraphicsItemChange change, const QVariant& value )
\r
179 // -------------------------------------------------------------------------
\r
180 void PipelineEditor::QNEBlock::
\r
181 _configPort( QNEPort* port )
\r
183 port->setBlock( this );
\r
185 QFontMetrics fm( this->scene( )->font( ) );
\r
186 int w = fm.width( port->name( ) );
\r
187 int h = fm.height( );
\r
188 if( w > this->m_Width - this->m_HorzMargin )
\r
189 this->m_Width = w + this->m_HorzMargin;
\r
190 this->m_Height += h;
\r
193 pth.addRoundedRect(
\r
194 -this->m_Width / 2,
\r
195 -this->m_Height / 2,
\r
197 this->m_Height, 5, 5
\r
199 this->setPath( pth );
\r
201 int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( );
\r
202 foreach( QGraphicsItem* i, this->children( ) )
\r
204 QNEPort* p = dynamic_cast< QNEPort* >( i );
\r
208 if( dynamic_cast< QNEOutputPort* >( p ) != NULL )
\r
209 p->setPos( this->m_Width / 2 + port->radius( ), y );
\r
211 p->setPos( -this->m_Width / 2 - port->radius( ), y );
\r