X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=appli%2FcpPipelineEditor%2FQNEBlock.cxx;h=67f4a9bd477d873a924b5e357298233797170128;hb=c06908465eb6da50572779f423d1e2c9e03b68dd;hp=0c6900d559cff82ee176e4898e11624841379eeb;hpb=9c75dcecf566cc567583caf4687ce523a2af586d;p=cpPlugins.git diff --git a/appli/cpPipelineEditor/QNEBlock.cxx b/appli/cpPipelineEditor/QNEBlock.cxx index 0c6900d..67f4a9b 100644 --- a/appli/cpPipelineEditor/QNEBlock.cxx +++ b/appli/cpPipelineEditor/QNEBlock.cxx @@ -33,23 +33,45 @@ #include #include "QNEPort.h" +#include "QNEConnection.h" // ------------------------------------------------------------------------- PipelineEditor::QNEBlock:: -QNEBlock( QGraphicsItem* parent, QGraphicsScene* scene ) +QNEBlock( TFilter* filter, QGraphicsItem* parent, QGraphicsScene* scene ) : Superclass( parent, scene ), m_HorzMargin( 20 ), - m_VertMargin( 5 ) + m_VertMargin( 5 ), + m_NamePort( NULL ), + m_TypePort( NULL ), + m_Filter( filter ) { QPainterPath p; p.addRoundedRect( -50, -15, 100, 30, 5, 5 ); + this->setPath( p ); this->setPen( QPen( Qt::darkGreen ) ); this->setBrush( Qt::green ); this->setFlag( QGraphicsItem::ItemIsMovable ); this->setFlag( QGraphicsItem::ItemIsSelectable ); + this->m_Width = this->m_HorzMargin; this->m_Height = this->m_VertMargin; + + // Configure names + this->setNamePort( this->m_Filter->GetName( ) ); + this->_setTypePort( this->m_Filter->GetClassName( ) ); + + // Add input ports + std::set< std::string > inputs; + this->m_Filter->GetInputsNames( inputs ); + for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt ) + this->addInputPort( iIt->c_str( ) ); + + // Add output ports + std::set< std::string > outputs; + this->m_Filter->GetOutputsNames( outputs ); + for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt ) + this->addOutputPort( oIt->c_str( ) ); } // ------------------------------------------------------------------------- @@ -59,77 +81,153 @@ PipelineEditor::QNEBlock:: } // ------------------------------------------------------------------------- -PipelineEditor::QNEPort* PipelineEditor::QNEBlock:: -addPort( const QString& name, bool isOutput, int flags, int ptr ) +void PipelineEditor::QNEBlock:: +setNamePort( const QString& txt ) { - QNEPort* port = new QNEPort( this ); - port->setName( name ); - port->setIsOutput( isOutput ); - port->setNEBlock( this ); - port->setPortFlags( flags ); - port->setPtr( ptr ); + if( this->m_NamePort == NULL ) + this->m_NamePort = new QNENamePort( this ); + this->m_NamePort->setName( txt ); + this->m_Filter->SetName( txt.toStdString( ) ); + this->_configPort( this->m_NamePort ); +} - QFontMetrics fm( this->scene( )->font( ) ); - int w = fm.width( name ); - int h = fm.height( ); - if( w > this->m_Width - this->m_HorzMargin ) - this->m_Width = w + this->m_HorzMargin; - this->m_Height += h; +// ------------------------------------------------------------------------- +PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock:: +addInputPort( const QString& txt ) +{ + QNEInputPort* ip = new QNEInputPort( this ); + ip->setExtendedName( + ( + txt.toStdString( ) + + std::string( "@" ) + + this->namePort( ).toStdString( ) + ).c_str( ) + ); + ip->setName( txt ); + this->m_InputPorts[ txt.toStdString( ) ] = ip; + this->_configPort( ip ); + return( ip ); +} - QPainterPath p; - p.addRoundedRect( - -this->m_Width / 2, - -this->m_Height / 2, - this->m_Width, - this->m_Height, 5, 5 +// ------------------------------------------------------------------------- +PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock:: +addOutputPort( const QString& txt ) +{ + QNEOutputPort* op = new QNEOutputPort( this ); + op->setExtendedName( + ( + txt.toStdString( ) + + std::string( "@" ) + + this->namePort( ).toStdString( ) + ).c_str( ) ); - this->setPath( p ); + op->setName( txt ); + this->m_OutputPorts[ txt.toStdString( ) ] = op; + this->_configPort( op ); + return( op ); +} - int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( ); - foreach( QGraphicsItem* port_, children( ) ) +// ------------------------------------------------------------------------- +bool PipelineEditor::QNEBlock:: +extendInputPort( const QString& txt, QNEConnection* conn ) +{ + auto p = this->m_InputPorts.find( txt.toStdString( ) ); + auto i = this->m_ExtInputPorts.find( txt.toStdString( ) ); + if( p != this->m_InputPorts.end( ) && i == this->m_ExtInputPorts.end( ) ) { - if( port_->type( ) != QNEPort::Type ) - continue; + this->m_ExtInputPorts[ txt.toStdString( ) ] = conn; + return( true ); + } + else + return( false ); +} - QNEPort* port = ( QNEPort* ) port_; - if( port->isOutput( ) ) - port->setPos( this->m_Width/2 + port->radius( ), y ); - else - port->setPos( -this->m_Width/2 - port->radius( ), y ); - y += h; +// ------------------------------------------------------------------------- +bool PipelineEditor::QNEBlock:: +extendOutputPort( const QString& txt, QNEConnection* conn ) +{ + auto p = this->m_OutputPorts.find( txt.toStdString( ) ); + auto i = this->m_ExtOutputPorts.find( txt.toStdString( ) ); + if( p != this->m_OutputPorts.end( ) && i == this->m_ExtOutputPorts.end( ) ) + { + this->m_ExtOutputPorts[ txt.toStdString( ) ] = conn; + return( true ); + } + else + return( false ); +} - } // rof - return( port ); +// ------------------------------------------------------------------------- +PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock:: +inputPort( const QString& txt ) +{ + auto i = this->m_InputPorts.find( txt.toStdString( ) ); + if( i != this->m_InputPorts.end( ) ) + return( i->second ); + else + return( NULL ); } // ------------------------------------------------------------------------- -PipelineEditor::QNEPort* PipelineEditor::QNEBlock:: -addInputPort( const QString& name ) +PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock:: +outputPort( const QString& txt ) { - return( this->addPort( name, false ) ); + auto o = this->m_OutputPorts.find( txt.toStdString( ) ); + if( o != this->m_OutputPorts.end( ) ) + return( o->second ); + else + return( NULL ); } // ------------------------------------------------------------------------- -PipelineEditor::QNEPort* PipelineEditor::QNEBlock:: -addOutputPort( const QString& name ) +QString PipelineEditor::QNEBlock:: +namePort( ) const { - return( this->addPort( name, true ) ); + return( this->m_NamePort->name( ) ); } // ------------------------------------------------------------------------- -void PipelineEditor::QNEBlock:: -addInputPorts( const QStringList& names ) +const PipelineEditor::QNEInputPort* PipelineEditor::QNEBlock:: +inputPort( const QString& txt ) const { - foreach( QString n, names ) - this->addInputPort( n ); + auto i = this->m_InputPorts.find( txt.toStdString( ) ); + if( i != this->m_InputPorts.end( ) ) + return( i->second ); + else + return( NULL ); } // ------------------------------------------------------------------------- -void PipelineEditor::QNEBlock:: -addOutputPorts( const QStringList& names ) +const PipelineEditor::QNEOutputPort* PipelineEditor::QNEBlock:: +outputPort( const QString& txt ) const { - foreach( QString n, names ) - this->addOutputPort( n ); + auto o = this->m_OutputPorts.find( txt.toStdString( ) ); + if( o != this->m_OutputPorts.end( ) ) + return( o->second ); + else + return( NULL ); +} + +// ------------------------------------------------------------------------- +const PipelineEditor::QNEConnection* PipelineEditor::QNEBlock:: +extendedInputPort( const QString& txt ) const +{ + auto i = this->m_ExtInputPorts.find( txt.toStdString( ) ); + if( i != this->m_ExtInputPorts.end( ) ) + return( i->second ); + else + return( NULL ); +} + +// ------------------------------------------------------------------------- +const PipelineEditor::QNEConnection* PipelineEditor::QNEBlock:: +extendedOutputPort( const QString& txt ) const +{ + auto i = this->m_ExtOutputPorts.find( txt.toStdString( ) ); + if( i != this->m_ExtOutputPorts.end( ) ) + return( i->second ); + else + return( NULL ); } // ------------------------------------------------------------------------- @@ -156,48 +254,89 @@ paint( } // ------------------------------------------------------------------------- -PipelineEditor::QNEBlock* PipelineEditor::QNEBlock:: -clone( ) +QVariant PipelineEditor::QNEBlock:: +itemChange( GraphicsItemChange change, const QVariant& value ) { - QNEBlock* b = new QNEBlock( 0, this->scene( ) ); - - foreach( QGraphicsItem* port_, childItems( ) ) - { - if( port_->type( ) == QNEPort::Type ) - { - QNEPort* port = ( QNEPort* ) port_; - b->addPort( - port->portName( ), - port->isOutput( ), - port->portFlags( ), - port->ptr( ) - ); - - } // fi + return( value ); +} - } // rof - return( b ); +// ------------------------------------------------------------------------- +void PipelineEditor::QNEBlock:: +_setTypePort( const QString& txt ) +{ + if( this->m_TypePort == NULL ) + this->m_TypePort = new QNETypePort( this ); + this->m_TypePort->setName( txt ); + this->_configPort( this->m_TypePort ); } // ------------------------------------------------------------------------- -QVector< PipelineEditor::QNEPort* > PipelineEditor::QNEBlock:: -ports( ) +void PipelineEditor::QNEBlock:: +_configPort( QNEPort* port ) { - QVector< PipelineEditor::QNEPort* > res; - foreach( QGraphicsItem* port_, childItems( ) ) + port->setBlock( this ); + + QFontMetrics fm( this->scene( )->font( ) ); + int w = fm.width( port->name( ) ) + ( 4 * port->radius( ) ); + int h = fm.height( ); + if( w > this->m_Width - this->m_HorzMargin ) + this->m_Width = w + this->m_HorzMargin; + this->m_Height = this->m_InputPorts.size( ) + this->m_OutputPorts.size( ); + this->m_Height += 4; + this->m_Height *= h; + + QPainterPath pth; + pth.addRoundedRect( + -this->m_Width / 2, + -this->m_Height / 2, + this->m_Width, + this->m_Height, 5, 5 + ); + this->setPath( pth ); + + int y = -this->m_Height / 2 + this->m_VertMargin + port->radius( ); + foreach( QGraphicsItem* i, this->children( ) ) { - if( port_->type( ) == QNEPort::Type ) - res.append( ( QNEPort* ) port_ ); + QNEPort* p = dynamic_cast< QNEPort* >( i ); + if( p == NULL ) + continue; + + if( dynamic_cast< QNENamePort* >( i ) != NULL ) + i->setPos( -this->m_Width / 2 + port->radius( ), y ); + else if( dynamic_cast< QNETypePort* >( i ) != NULL ) + { + i->setPos( -this->m_Width / 2 + port->radius( ), y ); + y += h; + } + else if( dynamic_cast< QNEInputPort* >( i ) != NULL ) + i->setPos( -this->m_Width / 2 - 2 * port->radius( ), y ); + else if( dynamic_cast< QNEOutputPort* >( i ) != NULL ) + i->setPos( this->m_Width / 2, y ); + + /* TODO + QNEPort* p = dynamic_cast< QNEPort* >( i ); + if( p == NULL ) + continue; + + if( dynamic_cast< QNEOutputPort* >( p ) != NULL ) + p->setPos( this->m_Width / 2 + port->radius( ), y ); + else + p->setPos( -this->m_Width / 2 - port->radius( ), y ); + */ + y += h; } // rof - return( res ); } // ------------------------------------------------------------------------- -QVariant PipelineEditor::QNEBlock:: -itemChange( GraphicsItemChange change, const QVariant& value ) +void PipelineEditor::QNEBlock:: +mouseReleaseEvent( QGraphicsSceneMouseEvent* evt ) { - return( value ); + if( this->m_Filter.IsNotNull( ) ) + this->m_Filter->SetViewCoords( + this->scenePos( ).x( ), this->scenePos( ).y( ) + ); + this->Superclass::mouseReleaseEvent( evt ); } // eof - $RCSfile$