1 #include <cpBaseQtApplication/Port.h>
\r
2 #include <cpBaseQtApplication/Connection.h>
\r
3 #include <cpBaseQtApplication/Block.h>
\r
4 #include <cpBaseQtApplication/Editor.h>
\r
6 #include <QGraphicsScene>
\r
7 #include <QGraphicsSceneContextMenuEvent>
\r
8 #include <QFontMetrics>
\r
13 // -------------------------------------------------------------------------
\r
14 cpBaseQtApplication::Port::
\r
15 Port( QGraphicsItem* parent, QGraphicsScene* scene )
\r
16 : Superclass( parent, scene ),
\r
20 this->m_Label = new QGraphicsTextItem( this );
\r
21 this->m_ExtendedLabel = new QGraphicsTextItem( this );
\r
22 this->setExtend( false );
\r
25 p.addEllipse( 0, 0, 2 * this->m_Radius, 2 * this->m_Radius );
\r
28 this->setPen( QPen( Qt::darkRed ) );
\r
29 this->setBrush( Qt::red );
\r
30 this->setFlag( QGraphicsItem::ItemSendsScenePositionChanges );
\r
33 // -------------------------------------------------------------------------
\r
34 cpBaseQtApplication::Port::
\r
39 // -------------------------------------------------------------------------
\r
40 void cpBaseQtApplication::Port::
\r
41 setBlock( Block* b )
\r
46 // -------------------------------------------------------------------------
\r
47 void cpBaseQtApplication::Port::
\r
48 setName( const QString& n )
\r
50 this->m_Label->setPlainText( n );
\r
51 this->_updateLabels( );
\r
54 // -------------------------------------------------------------------------
\r
55 void cpBaseQtApplication::Port::
\r
56 setExtendedName( const QString& n )
\r
58 this->m_ExtendedLabel->setPlainText( n );
\r
59 this->_updateLabels( );
\r
62 // -------------------------------------------------------------------------
\r
63 void cpBaseQtApplication::Port::
\r
64 setExtend( bool extend )
\r
67 this->m_IsExtended = false;
\r
68 this->m_ExtendedLabel->setVisible( false );
\r
71 // -------------------------------------------------------------------------
\r
72 void cpBaseQtApplication::Port::
\r
75 const QStyleOptionGraphicsItem* option,
\r
82 if( this->isExtended( ) )
\r
84 painter->setPen( QPen( Qt::darkBlue ) );
\r
85 painter->setBrush( Qt::blue );
\r
89 painter->setPen( QPen( Qt::darkRed ) );
\r
90 painter->setBrush( Qt::red );
\r
93 painter->drawPath( this->path( ) );
\r
96 // -------------------------------------------------------------------------
\r
97 cpBaseQtApplication::NamePort::
\r
98 NamePort( QGraphicsItem* parent, QGraphicsScene* scene )
\r
99 : Superclass( parent, scene )
\r
103 // -------------------------------------------------------------------------
\r
104 cpBaseQtApplication::NamePort::
\r
109 // -------------------------------------------------------------------------
\r
110 void cpBaseQtApplication::NamePort::
\r
113 QFont font( this->scene( )->font( ) );
\r
114 font.setBold( true );
\r
115 this->m_Label->setFont( font );
\r
116 this->m_ExtendedLabel->setFont( font );
\r
117 this->setPath( QPainterPath( ) );
\r
120 // -------------------------------------------------------------------------
\r
121 bool cpBaseQtApplication::NamePort::
\r
122 isConnected( Port* other )
\r
127 // -------------------------------------------------------------------------
\r
128 cpBaseQtApplication::InputPort::
\r
129 InputPort( QGraphicsItem* parent, bool multiple, QGraphicsScene* scene )
\r
130 : Superclass( parent, scene ),
\r
131 m_IsMultiple( multiple )
\r
135 // -------------------------------------------------------------------------
\r
136 cpBaseQtApplication::InputPort::
\r
139 this->m_Connection.clear( );
\r
142 // -------------------------------------------------------------------------
\r
143 void cpBaseQtApplication::InputPort::
\r
146 QFontMetrics fm( this->scene( )->font( ) );
\r
147 this->m_Label->setPos( this->m_Radius * 2, -fm.height( ) / 2 );
\r
148 this->m_ExtendedLabel->setPos(
\r
149 -fm.width( this->extendedName( ) ) - this->m_Radius * 2,
\r
154 // -------------------------------------------------------------------------
\r
155 void cpBaseQtApplication::InputPort::
\r
156 setExtend( bool extend )
\r
158 if( this->m_Connection.size( ) == 0 )
\r
160 this->m_IsExtended = extend;
\r
161 this->m_ExtendedLabel->setVisible( extend );
\r
164 this->Superclass::setExtend( false );
\r
167 // -------------------------------------------------------------------------
\r
168 bool cpBaseQtApplication::InputPort::
\r
169 isConnected( Port* other )
\r
172 auto i = this->m_Connection.begin( );
\r
173 for( ; i != this->m_Connection.end( ); ++i )
\r
174 conn |= ( ( *i )->port1( ) == other && ( *i )->port2( ) == this );
\r
178 // -------------------------------------------------------------------------
\r
179 void cpBaseQtApplication::InputPort::
\r
180 setConnection( Connection* c )
\r
182 if( this->m_IsMultiple || this->m_Connection.size( ) == 0 )
\r
183 this->m_Connection.push_back( c );
\r
186 // -------------------------------------------------------------------------
\r
187 QVariant cpBaseQtApplication::InputPort::
\r
188 itemChange( GraphicsItemChange change, const QVariant& value )
\r
190 if( change == ItemScenePositionHasChanged )
\r
193 auto i = this->m_Connection.begin( );
\r
194 i != this->m_Connection.end( );
\r
198 ( *i )->updatePosFromPorts( );
\r
199 ( *i )->updatePath( );
\r
207 // -------------------------------------------------------------------------
\r
208 cpBaseQtApplication::OutputPort::
\r
209 OutputPort( QGraphicsItem* parent, QGraphicsScene* scene )
\r
210 : Superclass( parent, scene )
\r
214 // -------------------------------------------------------------------------
\r
215 cpBaseQtApplication::OutputPort::
\r
218 foreach( Connection* conn, this->m_Connections )
\r
222 // -------------------------------------------------------------------------
\r
223 void cpBaseQtApplication::OutputPort::
\r
226 QFontMetrics fm( this->scene( )->font( ) );
\r
227 this->m_Label->setPos(
\r
228 -fm.width( this->name( ) ) - this->m_Radius * 2, -fm.height( ) / 2
\r
230 this->m_ExtendedLabel->setPos( this->m_Radius * 2, -fm.height( ) / 2 );
\r
234 // -------------------------------------------------------------------------
\r
235 void cpBaseQtApplication::OutputPort::
\r
236 setExtend( bool extend )
\r
238 this->m_IsExtended = extend;
\r
239 this->m_ExtendedLabel->setVisible( extend );
\r
242 // -------------------------------------------------------------------------
\r
243 bool cpBaseQtApplication::OutputPort::
\r
244 isConnected( Port* other )
\r
246 auto i = this->m_Connections.begin( );
\r
248 for( ; i != this->m_Connections.end( ) && !conn; ++i )
\r
249 conn |= ( ( *i )->port1( ) == this && ( *i )->port2( ) == other );
\r
253 // -------------------------------------------------------------------------
\r
254 QVariant cpBaseQtApplication::OutputPort::
\r
255 itemChange( GraphicsItemChange change, const QVariant& value )
\r
257 if( change == ItemScenePositionHasChanged )
\r
259 foreach( Connection* conn, this->m_Connections )
\r
261 conn->updatePosFromPorts( );
\r
262 conn->updatePath( );
\r
270 // -------------------------------------------------------------------------
\r
271 void cpBaseQtApplication::OutputPort::
\r
272 contextMenuEvent( QGraphicsSceneContextMenuEvent* evt )
\r
274 if( this->m_Block == NULL )
\r
278 QAction* showAction = menu.addAction( "Show" );
\r
279 QAction* hideAction = menu.addAction( "Hide" );
\r
280 QAction* propertiesAction = menu.addAction( "Properties" );
\r
281 QAction* selectedAction = menu.exec( evt->screenPos( ) );
\r
283 if( selectedAction == showAction )
\r
285 this->m_Block->editor( )->showData(
\r
286 this->m_Block->namePort( ).toStdString( ),
\r
287 this->name( ).toStdString( )
\r
290 else if( selectedAction == hideAction )
\r
292 this->m_Block->editor( )->hideData(
\r
293 this->m_Block->namePort( ).toStdString( ),
\r
294 this->name( ).toStdString( )
\r
297 else if( selectedAction == propertiesAction )
\r
299 this->m_Block->editor( )->dataProperties(
\r
300 this->m_Block->namePort( ).toStdString( ),
\r
301 this->name( ).toStdString( )
\r