X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=appli%2FcpPipelineEditor%2FQNodesEditor.cxx;h=99db8a6becab35b054a29b09592da756009c1df6;hb=f8c546490b7168b5c75d877bf66d27f83aa9412c;hp=9669eea49704045325fb3dddb2dcdfa9f49b86fb;hpb=ef8b6e12859181d3faa8019ce7319c539c0f86ec;p=cpPlugins.git diff --git a/appli/cpPipelineEditor/QNodesEditor.cxx b/appli/cpPipelineEditor/QNodesEditor.cxx index 9669eea..99db8a6 100644 --- a/appli/cpPipelineEditor/QNodesEditor.cxx +++ b/appli/cpPipelineEditor/QNodesEditor.cxx @@ -28,7 +28,15 @@ #include #include +#include +#include +#include +#include #include +#include +#include +#include +#include #include "QNEPort.h" #include "QNEConnection.h" @@ -37,9 +45,10 @@ // ------------------------------------------------------------------------- PipelineEditor::QNodesEditor:: QNodesEditor( QObject* parent ) - : Superclass( parent ) + : Superclass( parent ), + m_Conn( NULL ), + m_Workspace( NULL ) { - this->m_Conn = NULL; } // ------------------------------------------------------------------------- @@ -48,6 +57,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 +162,462 @@ 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 ); + + // Keep a trace of this visual graph + this->m_Graph->SetVertex( f->GetName( ), b ); +} - switch ( ( int ) e->type( ) ) +// ------------------------------------------------------------------------- +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; + /* TODO: + 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 ) + case QEvent::GraphicsSceneContextMenu: + { + QGraphicsSceneContextMenuEvent* evt = + dynamic_cast< QGraphicsSceneContextMenuEvent* >( e ); + if( evt != NULL ) { - this->m_Conn->setPos2( me->this->m_ScenePos( ) ); - this->m_Conn->updatePath( ); - return( true ); - } - break; + } // fi } - case QEvent::GraphicsThis->M_SceneMouseRelease: + break; + case QEvent::GraphicsSceneDragEnter: { - if( this->m_Conn && me->button( ) == Qt::LeftButton ) + QGraphicsSceneDragDropEvent* evt = + dynamic_cast< QGraphicsSceneDragDropEvent* >( e ); + if( evt != NULL ) { - QGraphicsItem* item = itemAt( me->this->m_ScenePos( ) ); - if( item && item->type( ) == QNEPort::Type ) + } // fi + } + break; + case QEvent::GraphicsSceneDragLeave: + { + QGraphicsSceneDragDropEvent* evt = + dynamic_cast< QGraphicsSceneDragDropEvent* >( e ); + if( evt != NULL ) + { + } // fi + } + break; + case QEvent::GraphicsSceneDragMove: + { + QGraphicsSceneDragDropEvent* evt = + dynamic_cast< QGraphicsSceneDragDropEvent* >( e ); + if( evt != NULL ) + { + } // fi + } + break; + case QEvent::GraphicsSceneDrop: + { + QGraphicsSceneDragDropEvent* evt = + dynamic_cast< QGraphicsSceneDragDropEvent* >( e ); + if( evt != NULL ) + { + } // fi + } + break; + case QEvent::GraphicsSceneHelp: + { + QGraphicsSceneHelpEvent* evt = + dynamic_cast< QGraphicsSceneHelpEvent* >( e ); + if( evt != NULL ) + { + } // fi + } + break; + case QEvent::GraphicsSceneHoverEnter: + { + QGraphicsSceneHoverEvent* evt = + dynamic_cast< QGraphicsSceneHoverEvent* >( e ); + if( evt != NULL ) + { + } // fi + } + break; + case QEvent::GraphicsSceneHoverLeave: + { + QGraphicsSceneHoverEvent* evt = + dynamic_cast< QGraphicsSceneHoverEvent* >( e ); + if( evt != NULL ) + { + } // fi + } + break; + case QEvent::GraphicsSceneHoverMove: + { + QGraphicsSceneHoverEvent* evt = + dynamic_cast< QGraphicsSceneHoverEvent* >( e ); + if( evt != NULL ) + { + } // fi + } + break; + case QEvent::GraphicsSceneMouseDoubleClick: + { + QGraphicsSceneMouseEvent* evt = + dynamic_cast< QGraphicsSceneMouseEvent* >( e ); + if( evt != NULL ) + { + QGraphicsItem* item = this->itemAt( evt->scenePos( ) ); + if( item != NULL ) { - QNEPort* port1 = this->m_Conn->port1( ); - QNEPort* port2 = ( QNEPort* ) item; + this->_DoubleClick( evt, item ); + return( true ); - if( port1->block( ) != port2->block( ) && port1->isOutput( ) != port2->isOutput( ) && !port1->isThis->M_Connected( port2 ) ) + } // fi + + } // fi + } + break; + case QEvent::GraphicsSceneMouseMove: + { + QGraphicsSceneMouseEvent* evt = + dynamic_cast< QGraphicsSceneMouseEvent* >( e ); + if( evt != NULL ) + { + if( this->m_Conn ) + { + 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( ); + return( true ); + + } // fi + + } // fi + } + break; + case QEvent::GraphicsSceneMousePress: + { + QGraphicsSceneMouseEvent* evt = + dynamic_cast< QGraphicsSceneMouseEvent* >( e ); + if( evt != NULL ) + { + switch( evt->button( ) ) + { + case Qt::LeftButton: + { + QNEOutputPort* port = + dynamic_cast< QNEOutputPort* >( this->itemAt( evt->scenePos( ) ) ); + if( port != NULL ) { - this->m_Conn->setPos2( port2->this->m_ScenePos( ) ); - this->m_Conn->setPort2( port2 ); + if( port->block( ) != NULL ) + { + this->m_Conn = new QNEConnection( 0, this->m_Scene ); + this->m_Conn->setPort1( port ); + this->m_Conn->setPos1( port->scenePos( ) ); + this->m_Conn->setPos2( evt->scenePos( ) ); + this->m_Conn->updatePath( ); + return( true ); + + } // fi + + } // fi + } + break; + case Qt::RightButton: + { + QNEInputPort* in_port = + dynamic_cast< QNEInputPort* >( this->itemAt( evt->scenePos( ) ) ); + QNEOutputPort* out_port = + dynamic_cast< QNEOutputPort* >( this->itemAt( evt->scenePos( ) ) ); + if( in_port != NULL ) + { + if( in_port->connection( ) == NULL && in_port->block( ) != NULL ) + { + this->m_Conn = new QNEConnection( 0, this->m_Scene ); + this->m_Conn->setPort2( in_port ); + this->m_Conn->setPos1( evt->scenePos( ) ); + this->m_Conn->setPos2( in_port->scenePos( ) ); + this->m_Conn->updatePath( ); + return( true ); + + } // fi + } + else if( out_port != NULL && out_port->block( ) != NULL ) + { + 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->updatePath( ); - this->m_Conn = NULL; return( true ); - } + + } // fi } + break; + default: + break; - delete this->m_Conn; - this->m_Conn = NULL; - return( true ); - } - break; - } - } - return( this->Superclass::eventFilter( o, e ) ); -} + } // hctiws -// ------------------------------------------------------------------------- -void PipelineEditor::QNodesEditor:: -save( QDataStream& ds ) -{ - foreach( QGraphicsItem* item, this->m_Scene->items( ) ) - if( item->type( ) == QNEBlock::Type ) + } // fi + } + break; + case QEvent::GraphicsSceneMouseRelease: + { + QGraphicsSceneMouseEvent* evt = + dynamic_cast< QGraphicsSceneMouseEvent* >( e ); + if( evt != NULL ) { - ds << item->type( ); - ( ( QNEBlock* ) item )->save( ds ); - } + if( this->m_Conn != NULL && evt->button( ) == Qt::LeftButton ) + { + QNEInputPort* port2 = + dynamic_cast< QNEInputPort* >( this->itemAt( evt->scenePos( ) ) ); + if( port2 != NULL ) + { + QNEOutputPort* port1 = + dynamic_cast< QNEOutputPort* >( this->m_Conn->port1( ) ); + if( port1 != NULL ) + { + if( + port1->block( ) != port2->block( ) && + !port2->hasConnection( ) && + !port1->isConnected( port2 ) + ) + { + this->m_Conn->setPos2( port2->scenePos( ) ); + this->m_Conn->setPort2( port2 ); + this->m_Conn->updatePath( ); - foreach( QGraphicsItem* item, this->m_Scene->items( ) ) - if( item->type( ) == QNEConnection::Type ) - { - ds << item->type( ); - ( ( QNEConnection* ) item )->save( ds ); - } -} + 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 + ); -// ------------------------------------------------------------------------- -void PipelineEditor::QNodesEditor:: -load( QDataStream& ds ) -{ - this->m_Scene->clear( ); + this->m_Conn = NULL; + return( true ); - QMap portMap; + } // fi - while ( !ds.atEnd( ) ) + } // fi + + } // fi + delete this->m_Conn; + this->m_Conn = NULL; + return( true ); + } + else if( this->m_Conn != NULL && evt->button( ) == Qt::RightButton ) + { + QNEOutputPort* port1 = this->m_Conn->port1( ); + QNEInputPort* port2 = this->m_Conn->port2( ); + + if( port1 != NULL && port2 == NULL ) + { + if( + dynamic_cast< QNEInputPort* >( + this->itemAt( evt->scenePos( ) ) + ) == NULL + ) + { + port2 = new QNEInputPort( NULL, this->m_Scene ); + port2->setName( port1->name( ) ); + port2->setPos( evt->scenePos( ) ); + this->m_Conn->setPos2( evt->scenePos( ) ); + this->m_Conn->setPort2( port2 ); + this->m_Conn->updatePath( ); + } + else + delete this->m_Conn; + this->m_Conn = NULL; + return( true ); + } + else if( port1 == NULL && port2 != NULL ) + { + if( + dynamic_cast< QNEOutputPort* >( + this->itemAt( evt->scenePos( ) ) + ) == NULL + ) + { + port1 = new QNEOutputPort( NULL, this->m_Scene ); + port1->setName( port2->name( ) ); + port1->setPos( evt->scenePos( ) ); + this->m_Conn->setPos1( evt->scenePos( ) ); + this->m_Conn->setPort1( port1 ); + this->m_Conn->updatePath( ); + } + else + delete this->m_Conn; + this->m_Conn = NULL; + return( true ); + + } // fi + + } // fi + + } // fi + } + break; + case QEvent::GraphicsSceneMove: { - int type; - ds >> type; - if( type == QNEBlock::Type ) + QGraphicsSceneMoveEvent* evt = + dynamic_cast< QGraphicsSceneMoveEvent* >( e ); + if( evt != NULL ) { - QNEBlock* block = new QNEBlock( 0, this->m_Scene ); - block->load( ds, portMap ); - } else if( type == QNEConnection::Type ) + } // fi + } + break; + case QEvent::GraphicsSceneResize: + { + QGraphicsSceneResizeEvent* evt = + dynamic_cast< QGraphicsSceneResizeEvent* >( e ); + if( evt != NULL ) { - QNEConnection* this->m_Conn = new QNEConnection( 0, this->m_Scene ); - this->m_Conn->load( ds, portMap ); - } + } // fi } + break; + case QEvent::GraphicsSceneWheel: + { + QGraphicsSceneWheelEvent* evt = + dynamic_cast< QGraphicsSceneWheelEvent* >( e ); + if( evt != NULL ) + { + } // fi + } + break; + default: + break; + } // hctiws + + // Mouse event + /* + QGraphicsSceneMouseEvent* me = + dynamic_cast< QGraphicsSceneMouseEvent* >( e ); + if( me != NULL ) + { + } // fi + */ + + /* TODO + switch( ( int ) e->type( ) ) + { + case QEvent::GraphicsSceneMouseMove: + { + if( this->m_Conn ) + { + this->m_Conn->setPos2( me->scenePos( ) ); + this->m_Conn->updatePath( ); + return( true ); + } + break; + } + case QEvent::GraphicsSceneMouseRelease: + { + if( this->m_Conn && me->button( ) == Qt::LeftButton ) + { + QGraphicsItem* item = itemAt( me->scenePos( ) ); + if( item && item->type( ) == QNEPort::Type ) + { + QNEPort* port1 = this->m_Conn->port1( ); + QNEPort* port2 = ( QNEPort* ) item; + if( port1->block( ) != port2->block( ) && port1->isOutput( ) != port2->isOutput( ) && !port1->isConnected( port2 ) ) + { + this->m_Conn->setPos2( port2->scenePos( ) ); + this->m_Conn->setPort2( port2 ); + this->m_Conn->updatePath( ); + this->m_Conn = NULL; + return( true ); + } + } + + delete this->m_Conn; + this->m_Conn = NULL; + return( true ); + } + break; + } + } // hctiws + */ + + return( this->Superclass::eventFilter( o, e ) ); } // eof - $RCSfile$