#include "Editor.h" #include #include #include #include #include #include #include #include #include #include #include #include "Port.h" #include "Connection.h" #include "Block.h" // ------------------------------------------------------------------------- #define cpPipelineEditor_Editor_Callback_SWITCH( E, e ) \ case QEvent::GraphicsScene##E: \ { \ QGraphicsScene##E##Event* evt = \ dynamic_cast< QGraphicsScene##E##Event* >( e ); \ if( evt != NULL ) \ this->_##E##_cbk( evt ); \ } \ break; // ------------------------------------------------------------------------- #define cpPipelineEditor_Editor_Callback_CODE( E ) \ void cpPipelineEditor::Editor::_##E##_cbk( QGraphicsScene##E##Event* evt ) // ------------------------------------------------------------------------- cpPipelineEditor::Editor:: Editor( QObject* parent ) : Superclass( parent ), m_ActualConnection( NULL ), m_Workspace( NULL ) { } // ------------------------------------------------------------------------- cpPipelineEditor::Editor:: ~Editor( ) { } // ------------------------------------------------------------------------- cpPipelineEditor::Editor:: TWorkspace* cpPipelineEditor::Editor:: workspace( ) { return( this->m_Workspace ); } // ------------------------------------------------------------------------- const cpPipelineEditor::Editor:: TWorkspace* cpPipelineEditor::Editor:: workspace( ) const { return( this->m_Workspace ); } // ------------------------------------------------------------------------- void cpPipelineEditor::Editor:: 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( ) ) ); // Add edges auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( ); auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( ); for( ; rIt != rIt_end; ++rIt ) { Block* orig = this->m_Graph->GetVertex( rIt->first ); auto cIt = rIt->second.begin( ); for( ; cIt != rIt->second.end( ); ++cIt ) { Block* dest = this->m_Graph->GetVertex( cIt->first ); auto eIt = cIt->second.begin( ); for( ; eIt != cIt->second.end( ); ++eIt ) { OutputPort* op = orig->outputPort( eIt->first.c_str( ) ); InputPort* ip = dest->inputPort( eIt->second.c_str( ) ); if( op == NULL || ip == NULL ) continue; Connection* c = new Connection( 0, this->m_Scene ); c->setPort1( op ); c->setPort2( ip ); c->updatePosFromPorts( ); c->updatePath( ); this->m_Graph->AddEdge( rIt->first, cIt->first, c ); } // rof } // rof } // rof } // ------------------------------------------------------------------------- std::string cpPipelineEditor::Editor:: 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 cpPipelineEditor::Editor:: install( QGraphicsScene* s ) { s->installEventFilter( this ); this->m_Scene = s; } // ------------------------------------------------------------------------- QGraphicsItem* cpPipelineEditor::Editor:: itemAt( const QPointF& pos ) { QList< QGraphicsItem* > items = this->m_Scene->items( QRectF( pos - QPointF( 1, 1 ), QSize( 3, 3 ) ) ); foreach( QGraphicsItem* item, items ) if( item->type( ) > QGraphicsItem::UserType ) return( item ); return( NULL ); } // ------------------------------------------------------------------------- void cpPipelineEditor::Editor:: _createBlock( TFilter* f, const QPointF& pnt ) { if( f == NULL ) return; // Add block Block* b = new Block( f, 0, this->m_Scene ); b->setPos( pnt ); // Keep a trace of this visual graph this->m_Graph->SetVertex( f->GetName( ), b ); } // ------------------------------------------------------------------------- bool cpPipelineEditor::Editor:: eventFilter( QObject* o, QEvent* e ) { // Event type switch( int( e->type( ) ) ) { cpPipelineEditor_Editor_Callback_SWITCH( ContextMenu, e ); cpPipelineEditor_Editor_Callback_SWITCH( DragEnter, e ); cpPipelineEditor_Editor_Callback_SWITCH( DragLeave, e ); cpPipelineEditor_Editor_Callback_SWITCH( DragMove, e ); cpPipelineEditor_Editor_Callback_SWITCH( Drop, e ); cpPipelineEditor_Editor_Callback_SWITCH( Help, e ); cpPipelineEditor_Editor_Callback_SWITCH( HoverEnter, e ); cpPipelineEditor_Editor_Callback_SWITCH( HoverLeave, e ); cpPipelineEditor_Editor_Callback_SWITCH( HoverMove, e ); cpPipelineEditor_Editor_Callback_SWITCH( MouseDoubleClick, e ); cpPipelineEditor_Editor_Callback_SWITCH( MouseMove, e ); cpPipelineEditor_Editor_Callback_SWITCH( MousePress, e ); cpPipelineEditor_Editor_Callback_SWITCH( MouseRelease, e ); cpPipelineEditor_Editor_Callback_SWITCH( Move, e ); cpPipelineEditor_Editor_Callback_SWITCH( Resize, e ); cpPipelineEditor_Editor_Callback_SWITCH( Wheel, e ); default: break; } // hctiws return( this->Superclass::eventFilter( o, e ) ); } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( ContextMenu ) { } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( DragEnter ) { } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( DragLeave ) { } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( DragMove ) { } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( Drop ) { } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( Help ) { } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( HoverEnter ) { } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( HoverLeave ) { } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( HoverMove ) { } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( MouseDoubleClick ) { QGraphicsItem* item = this->itemAt( evt->scenePos( ) ); if( item == NULL ) return; switch( evt->button( ) ) { case Qt::LeftButton: { Block* block = dynamic_cast< Block* >( item ); Port* port = dynamic_cast< Port* >( item ); Connection* conn = dynamic_cast< Connection* >( item ); if( block != NULL ) { 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 ) { ok = this->m_Graph->RenameVertex( old_name.toStdString( ), new_name.toStdString( ) ); if( ok ) { block->setNamePort( new_name ); this->m_Workspace->RenameFilter( old_name.toStdString( ), new_name.toStdString( ) ); } // fi } // fi } else if( port != NULL ) { if( evt->modifiers( ) == Qt::ControlModifier ) { port->setExtend( !( port->isExtended( ) ) ); InputPort* in_port = dynamic_cast< InputPort* >( port ); OutputPort* out_port = dynamic_cast< OutputPort* >( port ); if( port->isExtended( ) ) { if( in_port != NULL ) { this->m_Workspace->ExposeInputPort( in_port->extendedName( ).toStdString( ), in_port->block( )->namePort( ).toStdString( ), in_port->name( ).toStdString( ) ); } else if( out_port != NULL ) { this->m_Workspace->ExposeOutputPort( out_port->extendedName( ).toStdString( ), out_port->block( )->namePort( ).toStdString( ), out_port->name( ).toStdString( ) ); } // fi } else { if( in_port != NULL ) this->m_Workspace->HideInputPort( in_port->extendedName( ).toStdString( ) ); else if( out_port != NULL ) this->m_Workspace->HideOutputPort( out_port->extendedName( ).toStdString( ) ); } // 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 ) { port->setExtendedName( new_name ); InputPort* in_port = dynamic_cast< InputPort* >( port ); OutputPort* out_port = dynamic_cast< OutputPort* >( port ); if( in_port != NULL ) this->m_Workspace-> RenameExposedInputPort( old_name.toStdString( ), new_name.toStdString( ) ); else if( out_port != NULL ) this->m_Workspace-> RenameExposedOutputPort( old_name.toStdString( ), new_name.toStdString( ) ); this->m_Scene->update( ); } // fi } // fi } // fi } else if( conn != NULL ) { } // fi } break; /* TODO: case Qt::RightButton: { } break; case Qt::MiddleButton: { } break; */ default: break; } // hctiws } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( MouseMove ) { if( this->m_ActualConnection != NULL ) { if( this->m_ActualConnection->port1( ) == NULL ) this->m_ActualConnection->setPos1( evt->scenePos( ) ); else if( this->m_ActualConnection->port2( ) == NULL ) this->m_ActualConnection->setPos2( evt->scenePos( ) ); this->m_ActualConnection->updatePath( ); } // fi } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( MousePress ) { InputPort* in_port = dynamic_cast< InputPort* >( this->itemAt( evt->scenePos( ) ) ); OutputPort* out_port = dynamic_cast< OutputPort* >( this->itemAt( evt->scenePos( ) ) ); if( in_port == NULL && out_port == NULL ) return; switch( evt->button( ) ) { case Qt::LeftButton: { if( out_port != NULL ) { if( out_port->block( ) != NULL ) { // Start new connection this->m_ActualConnection = new Connection( 0, this->m_Scene ); this->m_ActualConnection->setPort1( out_port ); this->m_ActualConnection->setPos1( out_port->scenePos( ) ); this->m_ActualConnection->setPos2( evt->scenePos( ) ); this->m_ActualConnection->updatePosFromPorts( ); this->m_ActualConnection->updatePath( ); } // fi } // fi } break; default: break; } // hctiws } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( MouseRelease ) { if( this->m_ActualConnection == NULL ) return; switch( evt->button( ) ) { case Qt::LeftButton: { InputPort* port2 = dynamic_cast< InputPort* >( this->itemAt( evt->scenePos( ) ) ); if( port2 != NULL ) { OutputPort* port1 = dynamic_cast< OutputPort* >( this->m_ActualConnection->port1( ) ); if( port1 != NULL ) { if( port1->block( ) != port2->block( ) && !port2->hasConnection( ) && !port1->isConnected( port2 ) && !port2->isExtended( ) ) { this->m_ActualConnection->setPos2( port2->scenePos( ) ); this->m_ActualConnection->setPort2( port2 ); this->m_ActualConnection->updatePosFromPorts( ); this->m_ActualConnection->updatePath( ); this->m_Workspace->Connect( port1->block( )->namePort( ).toStdString( ), port2->block( )->namePort( ).toStdString( ), port1->name( ).toStdString( ), port2->name( ).toStdString( ) ); this->m_Graph->AddEdge( port1->block( )->namePort( ).toStdString( ), port2->block( )->namePort( ).toStdString( ), this->m_ActualConnection ); } else delete this->m_ActualConnection; } else delete this->m_ActualConnection; } else delete this->m_ActualConnection; this->m_ActualConnection = NULL; } break; default: break; } // hctisw } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( Move ) { } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( Resize ) { } // ------------------------------------------------------------------------- cpPipelineEditor_Editor_Callback_CODE( Wheel ) { } // eof - $RCSfile$