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( QGraphicsItem* parent, QGraphicsScene* scene )
\r
40 : Superclass( parent, scene ),
\r
45 p.addRoundedRect( -50, -15, 100, 30, 5, 5 );
\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
55 // -------------------------------------------------------------------------
\r
56 PipelineEditor::QNEBlock::
\r
61 // -------------------------------------------------------------------------
\r
62 PipelineEditor::QNEPort* PipelineEditor::QNEBlock::
\r
63 addPort( const QString& name, bool isOutput, int flags, int ptr )
\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
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
82 -this->m_Height / 2,
\r
84 this->m_Height, 5, 5
\r
88 int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( );
\r
89 foreach( QGraphicsItem* port_, children( ) )
\r
91 if( port_->type( ) != QNEPort::Type )
\r
94 QNEPort* port = ( QNEPort* ) port_;
\r
95 if( port->isOutput( ) )
\r
96 port->setPos( this->m_Width/2 + port->radius( ), y );
\r
98 port->setPos( -this->m_Width/2 - port->radius( ), y );
\r
105 // -------------------------------------------------------------------------
\r
106 void PipelineEditor::QNEBlock::
\r
107 addInputPort( const QString& name )
\r
109 this->addPort( name, false );
\r
112 // -------------------------------------------------------------------------
\r
113 void PipelineEditor::QNEBlock::
\r
114 addOutputPort( const QString& name )
\r
116 this->addPort( name, true );
\r
119 // -------------------------------------------------------------------------
\r
120 void PipelineEditor::QNEBlock::
\r
121 addInputPorts( const QStringList& names )
\r
123 foreach( QString n, names )
\r
124 this->addInputPort( n );
\r
127 // -------------------------------------------------------------------------
\r
128 void PipelineEditor::QNEBlock::
\r
129 addOutputPorts( const QStringList& names )
\r
131 foreach( QString n, names )
\r
132 this->addOutputPort( n );
\r
135 // -------------------------------------------------------------------------
\r
136 void PipelineEditor::QNEBlock::
\r
137 save( QDataStream& ds )
\r
143 foreach( QGraphicsItem* port_, children( ) )
\r
145 if( port_->type( ) != QNEPort::Type )
\r
153 foreach( QGraphicsItem* port_, children( ) )
\r
155 if( port_->type( ) != QNEPort::Type )
\r
158 QNEPort* port = ( QNEPort* ) port_;
\r
159 ds << ( quint64 ) port;
\r
160 ds << port->portName( );
\r
161 ds << port->isOutput( );
\r
162 ds << port->portFlags( );
\r
167 // -------------------------------------------------------------------------
\r
168 void PipelineEditor::QNEBlock::
\r
169 load( QDataStream& ds, QMap<quint64, QNEPort*>& portMap )
\r
176 for( int i = 0; i < count; i++ )
\r
187 portMap[ptr] = this->addPort( name, output, flags, ptr );
\r
192 // -------------------------------------------------------------------------
\r
193 void PipelineEditor::QNEBlock::
\r
195 QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget
\r
198 Q_UNUSED( option );
\r
199 Q_UNUSED( widget );
\r
201 if( this->isSelected( ) )
\r
203 painter->setPen( QPen( Qt::darkYellow ) );
\r
204 painter->setBrush( Qt::yellow );
\r
208 painter->setPen( QPen( Qt::darkGreen ) );
\r
209 painter->setBrush( Qt::green );
\r
212 painter->drawPath( this->path( ) );
\r
215 // -------------------------------------------------------------------------
\r
216 PipelineEditor::QNEBlock* PipelineEditor::QNEBlock::
\r
219 QNEBlock* b = new QNEBlock( 0, this->scene( ) );
\r
221 foreach( QGraphicsItem* port_, childItems( ) )
\r
223 if( port_->type( ) == QNEPort::Type )
\r
225 QNEPort* port = ( QNEPort* ) port_;
\r
229 port->portFlags( ),
\r
239 // -------------------------------------------------------------------------
\r
240 QVector< PipelineEditor::QNEPort* > PipelineEditor::QNEBlock::
\r
243 QVector< PipelineEditor::QNEPort* > res;
\r
244 foreach( QGraphicsItem* port_, childItems( ) )
\r
246 if( port_->type( ) == QNEPort::Type )
\r
247 res.append( ( QNEPort* ) port_ );
\r
253 // -------------------------------------------------------------------------
\r
254 QVariant PipelineEditor::QNEBlock::
\r
255 itemChange( GraphicsItemChange change, const QVariant& value )
\r