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
47 p.addRoundedRect( -50, -15, 100, 30, 5, 5 );
\r
50 this->setPen( QPen( Qt::darkGreen ) );
\r
51 this->setBrush( Qt::green );
\r
52 this->setFlag( QGraphicsItem::ItemIsMovable );
\r
53 this->setFlag( QGraphicsItem::ItemIsSelectable );
\r
55 this->m_Width = this->m_HorzMargin;
\r
56 this->m_Height = this->m_VertMargin;
\r
59 // -------------------------------------------------------------------------
\r
60 PipelineEditor::QNEBlock::
\r
65 // -------------------------------------------------------------------------
\r
66 void PipelineEditor::QNEBlock::
\r
67 setNamePort( const QString& txt )
\r
69 if( this->m_NamePort == NULL )
\r
70 this->m_NamePort = new QNENamePort( this );
\r
71 this->m_NamePort->setName( txt );
\r
72 this->_configPort( this->m_NamePort );
\r
75 // -------------------------------------------------------------------------
\r
76 void PipelineEditor::QNEBlock::
\r
77 setTypePort( const QString& txt )
\r
79 if( this->m_TypePort == NULL )
\r
80 this->m_TypePort = new QNETypePort( this );
\r
81 this->m_TypePort->setName( txt );
\r
82 this->_configPort( this->m_TypePort );
\r
85 // -------------------------------------------------------------------------
\r
86 void PipelineEditor::QNEBlock::
\r
87 addInputPort( const QString& txt )
\r
89 QNEInputPort* ip = new QNEInputPort( this );
\r
91 this->m_InputPorts.push_back( ip );
\r
92 this->_configPort( ip );
\r
95 // -------------------------------------------------------------------------
\r
96 void PipelineEditor::QNEBlock::
\r
97 addOutputPort( const QString& txt )
\r
99 QNEOutputPort* op = new QNEOutputPort( this );
\r
100 op->setName( txt );
\r
101 this->m_OutputPorts.push_back( op );
\r
102 this->_configPort( op );
\r
105 // -------------------------------------------------------------------------
\r
106 QVector< PipelineEditor::QNEPort* > PipelineEditor::QNEBlock::
\r
109 QVector< QNEPort* > res;
\r
110 foreach( QGraphicsItem* i, this->childItems( ) )
\r
112 QNEPort* p = dynamic_cast< QNEPort* >( i );
\r
120 // -------------------------------------------------------------------------
\r
121 PipelineEditor::QNEBlock* PipelineEditor::QNEBlock::
\r
124 QNEBlock* b = new QNEBlock( 0, this->scene( ) );
\r
125 foreach( QGraphicsItem* i, this->childItems( ) )
\r
127 QNENamePort* np = dynamic_cast< QNENamePort* >( i );
\r
129 b->setNamePort( np->name( ) );
\r
131 QNETypePort* tp = dynamic_cast< QNETypePort* >( i );
\r
133 b->setTypePort( tp->name( ) );
\r
135 QNEInputPort* ip = dynamic_cast< QNEInputPort* >( i );
\r
137 b->addInputPort( ip->name( ) );
\r
139 QNEOutputPort* op = dynamic_cast< QNEOutputPort* >( i );
\r
141 b->addOutputPort( op->name( ) );
\r
147 // -------------------------------------------------------------------------
\r
148 void PipelineEditor::QNEBlock::
\r
150 QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget
\r
153 Q_UNUSED( option );
\r
154 Q_UNUSED( widget );
\r
156 if( this->isSelected( ) )
\r
158 painter->setPen( QPen( Qt::darkYellow ) );
\r
159 painter->setBrush( Qt::yellow );
\r
163 painter->setPen( QPen( Qt::darkGreen ) );
\r
164 painter->setBrush( Qt::green );
\r
167 painter->drawPath( this->path( ) );
\r
170 // -------------------------------------------------------------------------
\r
171 QVariant PipelineEditor::QNEBlock::
\r
172 itemChange( GraphicsItemChange change, const QVariant& value )
\r
177 // -------------------------------------------------------------------------
\r
178 void PipelineEditor::QNEBlock::
\r
179 _configPort( QNEPort* port )
\r
181 port->setBlock( this );
\r
183 QFontMetrics fm( this->scene( )->font( ) );
\r
184 int w = fm.width( port->name( ) );
\r
185 int h = fm.height( );
\r
186 if( w > this->m_Width - this->m_HorzMargin )
\r
187 this->m_Width = w + this->m_HorzMargin;
\r
188 this->m_Height += h;
\r
191 pth.addRoundedRect(
\r
192 -this->m_Width / 2,
\r
193 -this->m_Height / 2,
\r
195 this->m_Height, 5, 5
\r
197 this->setPath( pth );
\r
199 int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( );
\r
200 foreach( QGraphicsItem* i, this->children( ) )
\r
202 QNEPort* p = dynamic_cast< QNEPort* >( i );
\r
206 if( dynamic_cast< QNEOutputPort* >( p ) != NULL )
\r
207 p->setPos( this->m_Width / 2 + port->radius( ), y );
\r
209 p->setPos( -this->m_Width / 2 - port->radius( ), y );
\r