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 PipelineEditor::QNEPort* PipelineEditor::QNEBlock::
\r
107 addInputPort( const QString& name )
\r
109 return( this->addPort( name, false ) );
\r
112 // -------------------------------------------------------------------------
\r
113 PipelineEditor::QNEPort* PipelineEditor::QNEBlock::
\r
114 addOutputPort( const QString& name )
\r
116 return( 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
138 QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget
\r
141 Q_UNUSED( option );
\r
142 Q_UNUSED( widget );
\r
144 if( this->isSelected( ) )
\r
146 painter->setPen( QPen( Qt::darkYellow ) );
\r
147 painter->setBrush( Qt::yellow );
\r
151 painter->setPen( QPen( Qt::darkGreen ) );
\r
152 painter->setBrush( Qt::green );
\r
155 painter->drawPath( this->path( ) );
\r
158 // -------------------------------------------------------------------------
\r
159 PipelineEditor::QNEBlock* PipelineEditor::QNEBlock::
\r
162 QNEBlock* b = new QNEBlock( 0, this->scene( ) );
\r
164 foreach( QGraphicsItem* port_, childItems( ) )
\r
166 if( port_->type( ) == QNEPort::Type )
\r
168 QNEPort* port = ( QNEPort* ) port_;
\r
172 port->portFlags( ),
\r
182 // -------------------------------------------------------------------------
\r
183 QVector< PipelineEditor::QNEPort* > PipelineEditor::QNEBlock::
\r
186 QVector< PipelineEditor::QNEPort* > res;
\r
187 foreach( QGraphicsItem* port_, childItems( ) )
\r
189 if( port_->type( ) == QNEPort::Type )
\r
190 res.append( ( QNEPort* ) port_ );
\r
196 // -------------------------------------------------------------------------
\r
197 QVariant PipelineEditor::QNEBlock::
\r
198 itemChange( GraphicsItemChange change, const QVariant& value )
\r