#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // ------------------------------------------------------------------------- #define cpBaseQtApplication_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 cpBaseQtApplication_Editor_Callback_CODE( E ) \ void cpBaseQtApplication::Editor::_##E##_cbk( QGraphicsScene##E##Event* evt ) // ------------------------------------------------------------------------- cpBaseQtApplication::Editor:: Editor( QObject* parent ) : Superclass( parent ), m_ActualConnection( NULL ), m_Workspace( NULL ) { } // ------------------------------------------------------------------------- cpBaseQtApplication::Editor:: ~Editor( ) { } // ------------------------------------------------------------------------- cpBaseQtApplication::Editor:: TWorkspace* cpBaseQtApplication::Editor:: workspace( ) { return( this->m_Workspace ); } // ------------------------------------------------------------------------- const cpBaseQtApplication::Editor:: TWorkspace* cpBaseQtApplication::Editor:: workspace( ) const { return( this->m_Workspace ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Editor:: setWorkspace( TWorkspace* ws ) { this->m_Workspace = ws; this->redrawWorkspace( ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Editor:: redrawWorkspace( ) { if( this->m_Workspace == NULL ) return; // Create blocks std::map< std::string, Block* > blocks; auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( ); auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( ); for( ; vIt != vIt_end; ++vIt ) { auto b = this->_createBlock( dynamic_cast< TFilter* >( vIt->second.GetPointer( ) ), vIt->first.c_str( ), QPointF( vIt->second->GetViewX( ), vIt->second->GetViewY( ) ) ); blocks[ vIt->first ] = b; } // rof // Add edges auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( ); auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( ); for( ; rIt != rIt_end; ++rIt ) { Block* orig = blocks[ rIt->first ]; auto cIt = rIt->second.begin( ); for( ; cIt != rIt->second.end( ); ++cIt ) { Block* dest = blocks[ 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( ); } // rof } // rof } // rof } // ------------------------------------------------------------------------- std::string cpBaseQtApplication::Editor:: createFilter( const std::string& category, 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( category, filter, name ) ) { auto b = this->_createBlock( this->m_Workspace->GetFilter( name ), name.c_str( ), pnt ); return( name ); } else return( "" ); } // ------------------------------------------------------------------------- bool cpBaseQtApplication::Editor:: deleteFilter( const std::string& name ) { if( this->m_Workspace != NULL ) return( this->m_Workspace->RemoveFilter( name ) ); else return( false ); } // ------------------------------------------------------------------------- bool cpBaseQtApplication::Editor:: deleteConnection( const std::string& src, const std::string& des, const std::string& in, const std::string& out ) { if( this->m_Workspace != NULL ) return( this->m_Workspace->Disconnect( src, des, in, out ) ); else return( false ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Editor:: clear( ) { std::cout << "Editor: clear" << std::endl; /* TODO auto vIt = this->m_Graph.BeginVertices( ); for( ; vIt != this->m_Graph.EndVertices( ); ++vIt ) { } // rof */ } // ------------------------------------------------------------------------- void cpBaseQtApplication::Editor:: install( QGraphicsScene* s ) { s->installEventFilter( this ); this->m_Scene = s; } // ------------------------------------------------------------------------- QGraphicsItem* cpBaseQtApplication::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 ); } // ------------------------------------------------------------------------- cpBaseQtApplication::Block* cpBaseQtApplication::Editor:: _createBlock( TFilter* f, const QString& name, const QPointF& pnt ) { if( f == NULL ) return( NULL ); // Add block Block* b = new Block( f, name, 0, this->m_Scene ); b->setEditor( this ); b->setPos( pnt ); // Mark exposed inputs auto& e_in = this->m_Workspace->GetExposedInputPorts( ); auto f_in = f->GetInputsNames( ); for( auto iIt = f_in.begin( ); iIt != f_in.end( ); ++iIt ) { auto eIt = e_in.begin( ); auto fIt = e_in.end( ); for( ; eIt != e_in.end( ) && fIt == e_in.end( ); ++eIt ) if( eIt->second.second == *iIt ) fIt = eIt; if( fIt == e_in.end( ) ) continue; auto port = b->inputPort( iIt->c_str( ) ); port->setExtendedName( fIt->first.c_str( ) ); port->setExtend( true ); } // rof // Mark exposed outputs auto& e_out = this->m_Workspace->GetExposedOutputPorts( ); auto f_out = f->GetOutputsNames( ); for( auto iIt = f_out.begin( ); iIt != f_out.end( ); ++iIt ) { auto eIt = e_out.begin( ); auto fIt = e_out.end( ); for( ; eIt != e_out.end( ) && fIt == e_out.end( ); ++eIt ) if( eIt->second.second == *iIt ) fIt = eIt; if( fIt == e_out.end( ) ) continue; auto port = b->outputPort( iIt->c_str( ) ); port->setExtendedName( fIt->first.c_str( ) ); port->setExtend( true ); } // rof return( b ); } // ------------------------------------------------------------------------- bool cpBaseQtApplication::Editor:: eventFilter( QObject* o, QEvent* e ) { // Event type switch( int( e->type( ) ) ) { cpBaseQtApplication_Editor_Callback_SWITCH( ContextMenu, e ); cpBaseQtApplication_Editor_Callback_SWITCH( DragEnter, e ); cpBaseQtApplication_Editor_Callback_SWITCH( DragLeave, e ); cpBaseQtApplication_Editor_Callback_SWITCH( DragMove, e ); cpBaseQtApplication_Editor_Callback_SWITCH( Drop, e ); cpBaseQtApplication_Editor_Callback_SWITCH( Help, e ); cpBaseQtApplication_Editor_Callback_SWITCH( HoverEnter, e ); cpBaseQtApplication_Editor_Callback_SWITCH( HoverLeave, e ); cpBaseQtApplication_Editor_Callback_SWITCH( HoverMove, e ); cpBaseQtApplication_Editor_Callback_SWITCH( MouseDoubleClick, e ); cpBaseQtApplication_Editor_Callback_SWITCH( MouseMove, e ); cpBaseQtApplication_Editor_Callback_SWITCH( MousePress, e ); cpBaseQtApplication_Editor_Callback_SWITCH( MouseRelease, e ); cpBaseQtApplication_Editor_Callback_SWITCH( Move, e ); cpBaseQtApplication_Editor_Callback_SWITCH( Resize, e ); cpBaseQtApplication_Editor_Callback_SWITCH( Wheel, e ); default: break; } // hctiws return( this->Superclass::eventFilter( o, e ) ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Editor:: updateFilter( const std::string& filter_name ) { emit execFilter( filter_name ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Editor:: showOutputData( const std::string& filter_name, const std::string& output_name ) { emit showFilterOutput( filter_name, output_name ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Editor:: hideOutputData( const std::string& filter_name, const std::string& output_name ) { emit hideFilterOutput( filter_name, output_name ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::Editor:: visualPropertiesOutputData( const std::string& filter_name, const std::string& output_name ) { emit visualPropertiesFilterOutput( filter_name, output_name ); } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( ContextMenu ) { } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( DragEnter ) { } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( DragLeave ) { } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( DragMove ) { } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( Drop ) { } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( Help ) { } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( HoverEnter ) { } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( HoverLeave ) { } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( HoverMove ) { } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( MouseDoubleClick ) { /* TODO 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( ) ); block->setNamePort( new_name ); this->m_Workspace->RenameFilter( old_name.toStdString( ), new_name.toStdString( ) ); } // 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; case Qt::RightButton: { } break; case Qt::MiddleButton: { } break; default: break; } // hctiws */ } // ------------------------------------------------------------------------- cpBaseQtApplication_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 } // ------------------------------------------------------------------------- cpBaseQtApplication_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 } // ------------------------------------------------------------------------- cpBaseQtApplication_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( ) ); } else delete this->m_ActualConnection; } else delete this->m_ActualConnection; } else delete this->m_ActualConnection; this->m_ActualConnection = NULL; } break; default: break; } // hctisw } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( Move ) { } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( Resize ) { } // ------------------------------------------------------------------------- cpBaseQtApplication_Editor_Callback_CODE( Wheel ) { } // eof - $RCSfile$