X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=appli%2FcpPipelineEditor%2FQNodesEditor.cxx;h=039b45ebc1a01cdeaf4eadf896e0ac30ffd8acbe;hb=f4aeff15ebb41183d4b89f41b29ec26043848722;hp=9669eea49704045325fb3dddb2dcdfa9f49b86fb;hpb=ef8b6e12859181d3faa8019ce7319c539c0f86ec;p=cpPlugins.git diff --git a/appli/cpPipelineEditor/QNodesEditor.cxx b/appli/cpPipelineEditor/QNodesEditor.cxx index 9669eea..039b45e 100644 --- a/appli/cpPipelineEditor/QNodesEditor.cxx +++ b/appli/cpPipelineEditor/QNodesEditor.cxx @@ -28,7 +28,14 @@ #include #include +#include +#include +#include +#include #include +#include +#include +#include #include "QNEPort.h" #include "QNEConnection.h" @@ -37,9 +44,10 @@ // ------------------------------------------------------------------------- PipelineEditor::QNodesEditor:: QNodesEditor( QObject* parent ) - : Superclass( parent ) + : Superclass( parent ), + m_Conn( NULL ), + m_Workspace( NULL ) { - this->m_Conn = NULL; } // ------------------------------------------------------------------------- @@ -48,6 +56,92 @@ 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 ) +{ + if( this->m_Workspace == ws ) + return; + this->m_Workspace = ws; + this->m_Graph = TGraph::New( ); + + /* TODO + QGraphicsScene* scene = this->scene( ); + // Add vertices and keep track of ports + std::map< std::string, std::map< std::string, QNEPort* > > + in_ports, out_ports; + 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( ) ) ); + + } // rof + */ + + // Add edges + /* TODO + auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( ); + auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( ); + for( ; rIt != rIt_end; ++rIt ) + { + auto cIt = rIt->second.begin( ); + for( ; cIt != rIt->second.end( ); ++cIt ) + { + auto eIt = cIt->second.begin( ); + for( ; eIt != cIt->second.end( ); ++eIt ) + { + QNEPort* p1 = out_ports[ rIt->first ][ eIt->first ]; + QNEPort* p2 = in_ports[ cIt->first ][ eIt->second ]; + if( p1 != NULL && p2 != NULL ) + { + QNEConnection* conn = new QNEConnection( 0, scene ); + conn->setPort1( p1 ); + conn->setPort2( p2 ); + this->m_Graph->AddConnection( rIt->first, cIt->first, conn ); + + } // fi + + } // 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 ) @@ -69,132 +163,385 @@ itemAt( const QPointF& pos ) return( NULL ); } +#include + +// ------------------------------------------------------------------------- +void PipelineEditor::QNodesEditor:: +_CreateBlock( TFilter* f, const QPointF& pnt ) +{ + if( f == NULL ) + return; + + // Add block + QNEBlock* b = new QNEBlock( 0, this->m_Scene ); + b->setNamePort( f->GetName( ) ); + b->setTypePort( f->GetClassName( ).c_str( ) ); + // TODO: b->setScenePos( pnt ); + + // Add input ports + std::set< std::string > inputs; + f->GetInputsNames( inputs ); + for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt ) + b->addInputPort( iIt->c_str( ) ); + + // Add output ports + std::set< std::string > outputs; + f->GetOutputsNames( outputs ); + for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt ) + b->addOutputPort( oIt->c_str( ) ); + + // Keep a trace of this visual graph + this->m_Graph->InsertVertex( f->GetName( ), b ); +} + +// ------------------------------------------------------------------------- +void PipelineEditor::QNodesEditor:: +_DoubleClick( QGraphicsSceneMouseEvent* evt, QGraphicsItem* item ) +{ + switch( evt->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 ) + { + /* TODO + auto ports = block->ports( ); + std::string name = ""; + for( + auto pIt = ports.begin( ); + pIt != ports.end( ) && name == ""; + ++pIt + ) + if( + ( *pIt )->portFlags( ) && QNEPort::NamePort == QNEPort::NamePort + ) + name = ( *pIt )->portName( ).toStdString( ); + if( name == "" ) + return; + TFilter* filter = this->m_Workspace->GetFilter( name ); + if( filter != NULL ) + { + } // fi + */ + } + else if( port != NULL ) + { + } + else if( conn != NULL ) + { + } // fi + } + break; + /* TODO: + case Qt::RightButton: + { + } + break; + case Qt::MiddleButton: + { + } + break; + */ + default: + break; + } // hctiws +} + // ------------------------------------------------------------------------- bool PipelineEditor::QNodesEditor:: eventFilter( QObject* o, QEvent* e ) { - QGraphicsSceneMouseEvent* me = ( QGraphicsSceneMouseEvent* ) e; - - switch ( ( int ) e->type( ) ) + // Event type + switch( int( e->type( ) ) ) { - case QEvent::GraphicsThis->M_SceneMousePress: + case QEvent::GraphicsSceneContextMenu: { - switch ( ( int ) me->button( ) ) + QGraphicsSceneContextMenuEvent* evt = + dynamic_cast< QGraphicsSceneContextMenuEvent* >( e ); + if( evt != NULL ) { - case Qt::LeftButton: + } // fi + } + break; + case QEvent::GraphicsSceneDragEnter: + { + QGraphicsSceneDragDropEvent* evt = + dynamic_cast< QGraphicsSceneDragDropEvent* >( e ); + if( evt != NULL ) { - QGraphicsItem* item = this->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 ) { - 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( ); - + this->_DoubleClick( evt, item ); return( true ); - } - else if( item && item->type( ) == QNEBlock::Type ) - { - /* if( selBlock ) - selBlock->setSelected( ); */ - // selBlock = ( QNEBlock* ) item; } // fi - break; - } - case Qt::RightButton: - { - 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; - } - } + + } // fi } - case QEvent::GraphicsThis->M_SceneMouseMove: + break; + case QEvent::GraphicsSceneMouseMove: { - if( this->m_Conn ) + QGraphicsSceneMouseEvent* evt = + dynamic_cast< QGraphicsSceneMouseEvent* >( e ); + if( evt != NULL ) { - this->m_Conn->setPos2( me->this->m_ScenePos( ) ); - this->m_Conn->updatePath( ); - return( true ); - } - break; + if( this->m_Conn ) + { + this->m_Conn->setPos2( evt->scenePos( ) ); + this->m_Conn->updatePath( ); + return( true ); + + } // fi + + } // fi } - case QEvent::GraphicsThis->M_SceneMouseRelease: + break; + case QEvent::GraphicsSceneMousePress: { - if( this->m_Conn && me->button( ) == Qt::LeftButton ) + QGraphicsSceneMouseEvent* evt = + dynamic_cast< QGraphicsSceneMouseEvent* >( e ); + if( evt != NULL ) { - QGraphicsItem* item = itemAt( me->this->m_ScenePos( ) ); - if( item && item->type( ) == QNEPort::Type ) + switch( evt->button( ) ) { - QNEPort* port1 = this->m_Conn->port1( ); - QNEPort* port2 = ( QNEPort* ) item; - - if( port1->block( ) != port2->block( ) && port1->isOutput( ) != port2->isOutput( ) && !port1->isThis->M_Connected( port2 ) ) + 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 ); + 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( ); - 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( )->name( ).toStdString( ), + port2->block( )->namePort( )->name( ).toStdString( ), + port1->name( ).toStdString( ), + port2->name( ).toStdString( ) + ); + this->m_Graph->AddConnection( + port1->block( )->namePort( )->name( ).toStdString( ), + port2->block( )->namePort( )->name( ).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 ); + + } // 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$