X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=appli%2FcpPipelineEditor%2FQNodesEditor.cxx;h=46508ca6fb92cbfe69c34430d3e1e9260126e8d4;hb=c06908465eb6da50572779f423d1e2c9e03b68dd;hp=9669eea49704045325fb3dddb2dcdfa9f49b86fb;hpb=ef8b6e12859181d3faa8019ce7319c539c0f86ec;p=cpPlugins.git diff --git a/appli/cpPipelineEditor/QNodesEditor.cxx b/appli/cpPipelineEditor/QNodesEditor.cxx index 9669eea..46508ca 100644 --- a/appli/cpPipelineEditor/QNodesEditor.cxx +++ b/appli/cpPipelineEditor/QNodesEditor.cxx @@ -28,18 +28,41 @@ #include #include +#include +#include +#include +#include #include +#include +#include +#include +#include #include "QNEPort.h" #include "QNEConnection.h" #include "QNEBlock.h" +// ------------------------------------------------------------------------- +#define PipelineEditor_QNodesEditor_Callback_SWITCH( E, e ) \ + case QEvent::E: \ + { \ + Q##E##Event* evt = dynamic_cast< Q##E##Event* >( e ); \ + if( evt != NULL ) \ + this->_##E##_cbk( evt ); \ + } \ + break + +// ------------------------------------------------------------------------- +#define PipelineEditor_QNodesEditor_Callback_CODE( E ) \ + void PipelineEditor::QNodesEditor::_##E##_cbk( Q##E##Event* evt ) + // ------------------------------------------------------------------------- PipelineEditor::QNodesEditor:: QNodesEditor( QObject* parent ) - : Superclass( parent ) + : Superclass( parent ), + m_Conn( NULL ), + m_Workspace( NULL ) { - this->m_Conn = NULL; } // ------------------------------------------------------------------------- @@ -48,6 +71,89 @@ PipelineEditor::QNodesEditor:: { } +// ------------------------------------------------------------------------- +PipelineEditor::QNodesEditor:: +TWorkspace* PipelineEditor::QNodesEditor:: +workspace( ) +{ + return( this->m_Workspace ); +} + +// ------------------------------------------------------------------------- +const PipelineEditor::QNodesEditor:: +TWorkspace* PipelineEditor::QNodesEditor:: +workspace( ) const +{ + return( this->m_Workspace ); +} + +// ------------------------------------------------------------------------- +void PipelineEditor::QNodesEditor:: +setWorkspace( TWorkspace* ws ) +{ + this->m_Workspace = ws; + this->m_Graph = TGraph::New( ); + + // Create blocks + auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( ); + auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( ); + for( ; vIt != vIt_end; ++vIt ) + { + this->_CreateBlock( + dynamic_cast< TFilter* >( vIt->second.GetPointer( ) ), + QPointF( vIt->second->GetViewX( ), vIt->second->GetViewY( ) ) + ); + + } // rof + + // Add edges + auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( ); + auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( ); + for( ; rIt != rIt_end; ++rIt ) + { + QNEBlock* orig = this->m_Graph->GetVertex( rIt->first ); + auto cIt = rIt->second.begin( ); + for( ; cIt != rIt->second.end( ); ++cIt ) + { + QNEBlock* dest = this->m_Graph->GetVertex( cIt->first ); + auto eIt = cIt->second.begin( ); + for( ; eIt != cIt->second.end( ); ++eIt ) + { + QNEOutputPort* op = orig->outputPort( eIt->first.c_str( ) ); + QNEInputPort* ip = dest->inputPort( eIt->second.c_str( ) ); + if( op == NULL || ip == NULL ) + continue; + + QNEConnection* c = new QNEConnection( 0, this->m_Scene ); + c->setPort1( op ); + c->setPort2( ip ); + c->updatePosFromPorts( ); + c->updatePath( ); + this->m_Graph->AddConnection( rIt->first, cIt->first, c ); + + } // rof + + } // rof + + } // rof +} + +// ------------------------------------------------------------------------- +std::string PipelineEditor::QNodesEditor:: +createFilter( const std::string& filter, const QPointF& pnt ) +{ + std::string name = filter; + while( this->m_Workspace->HasFilter( name ) ) + name += std::string( "_" ); + if( this->m_Workspace->CreateFilter( filter, name ) ) + { + this->_CreateBlock( this->m_Workspace->GetFilter( name ), pnt ); + return( name ); + } + else + return( "" ); +} + // ------------------------------------------------------------------------- void PipelineEditor::QNodesEditor:: install( QGraphicsScene* s ) @@ -70,131 +176,421 @@ itemAt( const QPointF& pos ) } // ------------------------------------------------------------------------- -bool PipelineEditor::QNodesEditor:: -eventFilter( QObject* o, QEvent* e ) +void PipelineEditor::QNodesEditor:: +_CreateBlock( TFilter* f, const QPointF& pnt ) { - QGraphicsSceneMouseEvent* me = ( QGraphicsSceneMouseEvent* ) e; + if( f == NULL ) + return; + + // Add block + QNEBlock* b = new QNEBlock( f, 0, this->m_Scene ); + b->setPos( pnt ); - switch ( ( int ) e->type( ) ) + // Keep a trace of this visual graph + this->m_Graph->SetVertex( f->GetName( ), b ); +} + +// ------------------------------------------------------------------------- +/* TODO +void PipelineEditor::QNodesEditor:: +_DoubleClick( QGraphicsSceneMouseEvent* evt, QGraphicsItem* item ) +{ + switch( evt->button( ) ) { - case QEvent::GraphicsThis->M_SceneMousePress: + case Qt::LeftButton: { - switch ( ( int ) me->button( ) ) - { - case Qt::LeftButton: + QNEBlock* block = dynamic_cast< QNEBlock* >( item ); + QNEPort* port = dynamic_cast< QNEPort* >( item ); + QNEConnection* conn = dynamic_cast< QNEConnection* >( item ); + + if( block != NULL ) { - QGraphicsItem* item = this->itemAt( me->this->m_ScenePos( ) ); - if( item && item->type( ) == QNEPort::Type ) + QString old_name = block->namePort( ); + bool ok; + QString new_name = + QInputDialog::getText( + dynamic_cast< QWidget* >( this->parent( ) ), + "Change filter name", + "Filter name:", + QLineEdit::Normal, + old_name, + &ok + ); + if( ok && !new_name.isEmpty( ) && old_name != new_name ) { - this->m_Conn = new QNEConnection( 0, this->m_Scene ); - this->m_Conn->setPort1( ( QNEPort* ) item ); - this->m_Conn->setPos1( item->this->m_ScenePos( ) ); - this->m_Conn->setPos2( me->this->m_ScenePos( ) ); - this->m_Conn->updatePath( ); + ok = this->m_Graph->RenameVertex( + old_name.toStdString( ), + new_name.toStdString( ) + ); + if( ok ) + { + block->setNamePort( new_name ); + this->m_Workspace->GetGraph( )->RenameVertex( + old_name.toStdString( ), + new_name.toStdString( ) + ); - return( true ); - } - else if( item && item->type( ) == QNEBlock::Type ) - { - /* if( selBlock ) - selBlock->setSelected( ); */ - // selBlock = ( QNEBlock* ) item; + } // fi } // fi - break; } - case Qt::RightButton: + else if( port != NULL ) { - QGraphicsItem* item = itemAt( me->this->m_ScenePos( ) ); - if( item && ( item->type( ) == QNEConnection::Type || item->type( ) == QNEBlock::Type ) ) - delete item; - // if( selBlock == ( QNEBlock* ) item ) - // selBlock = 0; - break; - } } + else if( conn != NULL ) + { + } // fi } - case QEvent::GraphicsThis->M_SceneMouseMove: + break; + case Qt::RightButton: + { + } + break; + case Qt::MiddleButton: + { + } + break; + default: + break; + } // hctiws +} + */ + +// ------------------------------------------------------------------------- +bool PipelineEditor::QNodesEditor:: +eventFilter( QObject* o, QEvent* e ) +{ + // Event type + switch( int( e->type( ) ) ) { - if( this->m_Conn ) - { - this->m_Conn->setPos2( me->this->m_ScenePos( ) ); - this->m_Conn->updatePath( ); - return( true ); - } + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneContextMenu, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneDragEnter, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneDragLeave, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneDragMove, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneDrop, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneHelp, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneHoverEnter, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneHoverLeave, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneHoverMove, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneMouseDoubleClick, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneMouseMove, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneMousePress, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneMouseRelease, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneMove, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneResize, e ); + PipelineEditor_QNodesEditor_Callback_SWITCH( GraphicsSceneWheel, e ); + default: break; - } - case QEvent::GraphicsThis->M_SceneMouseRelease: + } // hctiws + + return( this->Superclass::eventFilter( o, e ) ); +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneContextMenu ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneDragEnter ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneDragLeave ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneDragMove ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneDrop ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneHelp ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneHoverEnter ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneHoverLeave ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneHoverMove ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneMouseDoubleClick ) +{ + QGraphicsItem* item = this->itemAt( evt->scenePos( ) ); + if( item == NULL ) + return; + + switch( evt->button( ) ) + { + case Qt::LeftButton: { - if( this->m_Conn && me->button( ) == Qt::LeftButton ) + QNEBlock* block = dynamic_cast< QNEBlock* >( item ); + QNEPort* port = dynamic_cast< QNEPort* >( item ); + QNEConnection* conn = dynamic_cast< QNEConnection* >( item ); + + if( block != NULL ) { - QGraphicsItem* item = itemAt( me->this->m_ScenePos( ) ); - if( item && item->type( ) == QNEPort::Type ) + QString old_name = block->namePort( ); + bool ok; + QString new_name = + QInputDialog::getText( + dynamic_cast< QWidget* >( this->parent( ) ), + "Change filter name", + "Filter name:", + QLineEdit::Normal, + old_name, + &ok + ); + if( ok && !new_name.isEmpty( ) && old_name != new_name ) { - QNEPort* port1 = this->m_Conn->port1( ); - QNEPort* port2 = ( QNEPort* ) item; + ok = this->m_Graph->RenameVertex( + old_name.toStdString( ), + new_name.toStdString( ) + ); + if( ok ) + { + block->setNamePort( new_name ); + this->m_Workspace->GetGraph( )->RenameVertex( + old_name.toStdString( ), + new_name.toStdString( ) + ); + + } // fi - if( port1->block( ) != port2->block( ) && port1->isOutput( ) != port2->isOutput( ) && !port1->isThis->M_Connected( port2 ) ) + } // fi + } + else if( port != NULL ) + { + if( evt->modifiers( ) == Qt::ControlModifier ) + { + port->setExtend( !( port->isExtended( ) ) ); + QNEInputPort* in_port = dynamic_cast< QNEInputPort* >( port ); + QNEOutputPort* out_port = dynamic_cast< QNEOutputPort* >( port ); + if( port->isExtended( ) ) { - this->m_Conn->setPos2( port2->this->m_ScenePos( ) ); - this->m_Conn->setPort2( port2 ); - this->m_Conn->updatePath( ); - this->m_Conn = NULL; - return( true ); + if( in_port != NULL ) + { + this->m_Workspace->AddInputPort( + in_port->extendedName( ).toStdString( ), + in_port->block( )->namePort( ).toStdString( ), + in_port->name( ).toStdString( ) + ); + } + else if( out_port != NULL ) + { + } // fi } + else + { + if( in_port != NULL ) + { + } + else if( out_port != NULL ) + { + } // fi + + } // fi + this->m_Scene->update( ); } + else if( evt->modifiers( ) == Qt::NoModifier ) + { + if( port->isExtended( ) ) + { + QString old_name = port->extendedName( ); + bool ok; + QString new_name = + QInputDialog::getText( + dynamic_cast< QWidget* >( this->parent( ) ), + "Change filter name", + "Filter name:", + QLineEdit::Normal, + old_name, + &ok + ); + if( ok && !new_name.isEmpty( ) && old_name != new_name ) + { + // TODO: port->setExtendedName( new_name ); + /* TODO + ok = this->m_Graph->RenameVertex( + old_name.toStdString( ), + new_name.toStdString( ) + ); + if( ok ) + { + block->setNamePort( new_name ); + this->m_Workspace->GetGraph( )->RenameVertex( + old_name.toStdString( ), + new_name.toStdString( ) + ); - delete this->m_Conn; - this->m_Conn = NULL; - return( true ); + } // fi + */ + } // fi + + } // fi + + } // fi } - break; - } + else if( conn != NULL ) + { + } // fi } - return( this->Superclass::eventFilter( o, e ) ); + break; + /* TODO: + case Qt::RightButton: + { + } + break; + case Qt::MiddleButton: + { + } + break; + */ + default: + break; + } // hctiws } // ------------------------------------------------------------------------- -void PipelineEditor::QNodesEditor:: -save( QDataStream& ds ) +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneMouseMove ) { - foreach( QGraphicsItem* item, this->m_Scene->items( ) ) - if( item->type( ) == QNEBlock::Type ) - { - ds << item->type( ); - ( ( QNEBlock* ) item )->save( ds ); - } + if( this->m_Conn != NULL ) + { + if( this->m_Conn->port1( ) == NULL ) + this->m_Conn->setPos1( evt->scenePos( ) ); + else if( this->m_Conn->port2( ) == NULL ) + this->m_Conn->setPos2( evt->scenePos( ) ); + this->m_Conn->updatePath( ); - foreach( QGraphicsItem* item, this->m_Scene->items( ) ) - if( item->type( ) == QNEConnection::Type ) - { - ds << item->type( ); - ( ( QNEConnection* ) item )->save( ds ); - } + } // fi } // ------------------------------------------------------------------------- -void PipelineEditor::QNodesEditor:: -load( QDataStream& ds ) +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneMousePress ) { - this->m_Scene->clear( ); - - QMap portMap; + QNEInputPort* in_port = + dynamic_cast< QNEInputPort* >( this->itemAt( evt->scenePos( ) ) ); + QNEOutputPort* out_port = + dynamic_cast< QNEOutputPort* >( this->itemAt( evt->scenePos( ) ) ); + if( in_port == NULL && out_port == NULL ) + return; - while ( !ds.atEnd( ) ) + switch( evt->button( ) ) + { + case Qt::LeftButton: { - int type; - ds >> type; - if( type == QNEBlock::Type ) + if( out_port != NULL ) { - QNEBlock* block = new QNEBlock( 0, this->m_Scene ); - block->load( ds, portMap ); - } else if( type == QNEConnection::Type ) + if( out_port->block( ) != NULL ) + { + // Start new connection + this->m_Conn = new QNEConnection( 0, this->m_Scene ); + this->m_Conn->setPort1( out_port ); + this->m_Conn->setPos1( out_port->scenePos( ) ); + this->m_Conn->setPos2( evt->scenePos( ) ); + this->m_Conn->updatePosFromPorts( ); + this->m_Conn->updatePath( ); + + } // fi + + } // fi + } + break; + default: + break; + + } // hctiws +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneMouseRelease ) +{ + if( this->m_Conn == NULL ) + return; + + switch( evt->button( ) ) + { + case Qt::LeftButton: + { + QNEInputPort* port2 = + dynamic_cast< QNEInputPort* >( this->itemAt( evt->scenePos( ) ) ); + if( port2 != NULL ) { - QNEConnection* this->m_Conn = new QNEConnection( 0, this->m_Scene ); - this->m_Conn->load( ds, portMap ); + QNEOutputPort* port1 = + dynamic_cast< QNEOutputPort* >( this->m_Conn->port1( ) ); + if( port1 != NULL ) + { + if( + port1->block( ) != port2->block( ) && + !port2->hasConnection( ) && + !port1->isConnected( port2 ) && + !port2->isExtended( ) + ) + { + this->m_Conn->setPos2( port2->scenePos( ) ); + this->m_Conn->setPort2( port2 ); + this->m_Conn->updatePosFromPorts( ); + this->m_Conn->updatePath( ); + + this->m_Workspace->Connect( + port1->block( )->namePort( ).toStdString( ), + port2->block( )->namePort( ).toStdString( ), + port1->name( ).toStdString( ), + port2->name( ).toStdString( ) + ); + this->m_Graph->AddConnection( + port1->block( )->namePort( ).toStdString( ), + port2->block( )->namePort( ).toStdString( ), + this->m_Conn + ); + } + else + delete this->m_Conn; + } + else + delete this->m_Conn; } + else + delete this->m_Conn; + this->m_Conn = NULL; } + break; + default: + break; + } // hctisw +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneMove ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneResize ) +{ +} + +// ------------------------------------------------------------------------- +PipelineEditor_QNodesEditor_Callback_CODE( GraphicsSceneWheel ) +{ } // eof - $RCSfile$