X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2FcpPlugins%2FInterface%2FWorkspace.cxx;h=8eca62080671d2108b9bb02ec3c3faf6335bd201;hb=4985f4ba986f600298676866c5099b2f84bee50b;hp=d1d2c0daa7b412935ce6c4909b1ccdfc4b879c2d;hpb=3393941bf8f26babc7b592db434b40c1b747a687;p=cpPlugins.git diff --git a/lib/cpPlugins/Interface/Workspace.cxx b/lib/cpPlugins/Interface/Workspace.cxx index d1d2c0d..8eca620 100644 --- a/lib/cpPlugins/Interface/Workspace.cxx +++ b/lib/cpPlugins/Interface/Workspace.cxx @@ -1,13 +1,40 @@ #include #include +// ------------------------------------------------------------------------- +cpPlugins::Interface::Workspace:: +Pointer cpPlugins::Interface::Workspace:: +New( ) +{ + Pointer smartPtr = new Self; + smartPtr->UnRegister( ); + return( smartPtr ); +} + +// ------------------------------------------------------------------------- +itk::LightObject::Pointer cpPlugins::Interface::Workspace:: +CreateAnother( ) const +{ + itk::LightObject::Pointer smartPtr; + smartPtr = Self::New( ).GetPointer( ); + return( smartPtr ); +} + +// ------------------------------------------------------------------------- +cpPlugins::Interface::Workspace:: +Pointer cpPlugins::Interface::Workspace:: +Clone( ) const +{ + Pointer rval = + dynamic_cast< Self* >( this->InternalClone( ).GetPointer( ) ); + return( rval ); +} + // ------------------------------------------------------------------------- void cpPlugins::Interface::Workspace:: Clear( ) { this->m_Filters.clear( ); - this->m_ExposedInputs.clear( ); - this->m_ExposedOutputs.clear( ); } // ------------------------------------------------------------------------- @@ -15,14 +42,14 @@ std::vector< std::string > cpPlugins::Interface::Workspace:: GetFiltersNames( ) const { std::vector< std::string > n; - for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i ) - n.push_back( i->first ); + for( auto i : this->m_Filters ) + n.push_back( i.first ); return( n ); } // ------------------------------------------------------------------------- cpPlugins::Interface::Workspace:: -TProcess* cpPlugins::Interface::Workspace:: +TFilter* cpPlugins::Interface::Workspace:: GetFilter( const std::string& name ) { auto i = this->m_Filters.find( name ); @@ -34,7 +61,7 @@ GetFilter( const std::string& name ) // ------------------------------------------------------------------------- const cpPlugins::Interface::Workspace:: -TProcess* cpPlugins::Interface::Workspace:: +TFilter* cpPlugins::Interface::Workspace:: GetFilter( const std::string& name ) const { auto i = this->m_Filters.find( name ); @@ -44,6 +71,24 @@ GetFilter( const std::string& name ) const return( NULL ); } +// ------------------------------------------------------------------------- +cpPlugins::Interface::Workspace:: +TWidget* cpPlugins::Interface::Workspace:: +GetWidget( const std::string& name ) +{ + TFilter* process = this->GetFilter( name ); + return( dynamic_cast< TWidget* >( process ) ); +} + +// ------------------------------------------------------------------------- +const cpPlugins::Interface::Workspace:: +TWidget* cpPlugins::Interface::Workspace:: +GetWidget( const std::string& name ) const +{ + const TFilter* process = this->GetFilter( name ); + return( dynamic_cast< const TWidget* >( process ) ); +} + // ------------------------------------------------------------------------- bool cpPlugins::Interface::Workspace:: HasFilter( const std::string& name ) const @@ -51,39 +96,41 @@ HasFilter( const std::string& name ) const return( this->m_Filters.find( name ) != this->m_Filters.end( ) ); } +// ------------------------------------------------------------------------- +bool cpPlugins::Interface::Workspace:: +HasWidget( const std::string& name ) const +{ + const TWidget* wdg = this->GetWidget( name ); + return( wdg != NULL ); +} + // ------------------------------------------------------------------------- cpPlugins::Interface::Workspace:: -TProcess* cpPlugins::Interface::Workspace:: -CreateFilter( - const std::string& category, - const std::string& filter, - const std::string& name - ) +TFilter* cpPlugins::Interface::Workspace:: +CreateFilter( const std::string& category, const std::string& filter ) { typedef cpPlugins::BaseObjects::Widget _TWidget; - TInterface::Pointer interface = TInterface::New( ); - TProcess::Pointer o = this->GetFilter( name ); - if( o.IsNull( ) ) + TFilter::Pointer o = this->m_Plugins->CreateFilter( category, filter ); + if( o.IsNotNull( ) ) { - o = interface->CreateProcessObject( category, filter ); - if( o.IsNotNull( ) ) - { - o->SetPrintExecution( this->m_PrintExecution ); - o->SetName( name ); - - // Interactors - for( - auto i = this->m_Interactors.begin( ); - i != this->m_Interactors.end( ); - ++i - ) - o->AddInteractor( *i ); - - // Finish association - this->m_Filters[ name ] = o; - - } // fi + // Choose a name + std::string name = filter; + while( this->GetFilter( name ) != NULL ) + name += std::string( "_" ); + o->SetPrintExecution( this->m_PrintExecution ); + o->SetName( name ); + + // Interactors + for( + auto i = this->m_Interactors.begin( ); + i != this->m_Interactors.end( ); + ++i + ) + o->AddInteractor( *i ); + + // Finish association + this->m_Filters[ name ] = o; } // fi return( o.GetPointer( ) ); @@ -103,14 +150,16 @@ RenameFilter( const std::string& old_name, const std::string& new_name ) this->m_Filters.erase( o ); // Rename exposed ports - auto e = this->m_ExposedInputs.begin( ); - for( ; e != this->m_ExposedInputs.end( ); ++e ) - if( e->second.first == old_name ) - e->second.first = new_name; - e = this->m_ExposedOutputs.begin( ); - for( ; e != this->m_ExposedOutputs.end( ); ++e ) - if( e->second.first == old_name ) - e->second.first = new_name; + /* TODO + auto e = this->m_ExposedInputs.begin( ); + for( ; e != this->m_ExposedInputs.end( ); ++e ) + if( e->second.first == old_name ) + e->second.first = new_name; + e = this->m_ExposedOutputs.begin( ); + for( ; e != this->m_ExposedOutputs.end( ); ++e ) + if( e->second.first == old_name ) + e->second.first = new_name; + */ return( true ); } @@ -161,10 +210,141 @@ void cpPlugins::Interface::Workspace:: AddInteractor( vtkRenderWindowInteractor* iren ) { if( iren != NULL ) + { this->m_Interactors.insert( iren ); + for( auto f : this->m_Filters ) + f.second->AddInteractor( iren ); + + } // fi +} + +// ------------------------------------------------------------------------- +bool cpPlugins::Interface::Workspace:: +Connect( + const std::string& origin_filter, + const std::string& origin_output, + const std::string& destination_filter, + const std::string& destination_input + ) +{ + // Get filters and check pertinence + TFilter* origin = this->GetFilter( origin_filter ); + TFilter* destination = this->GetFilter( destination_filter ); + if( origin == NULL || destination == NULL ) + return( false ); + if( !( destination->HasInput( destination_input ) ) ) + return( false ); + if( !( origin->HasOutput( origin_output ) ) ) + return( false ); + + // Check if there is room for a new connection + bool ok = true; + if( destination->IsInputMultiple( destination_input ) ) + { + for( + unsigned int i = 0; + i < destination->GetInputSize( destination_input ); + ++i + ) + if( + destination->GetInput( destination_input, i )->GetSource( ) == origin + ) + ok = false; + } + else + ok = ( destination->GetInput( destination_input ) == NULL ); + if( ok ) + destination->AddInput( + destination_input, + origin->GetOutput( origin_output ) + ); + return( ok ); +} + +// ------------------------------------------------------------------------- +bool cpPlugins::Interface::Workspace:: +Connect( + TDataObject* input, + const std::string& destination_filter, + const std::string& destination_input + ) +{ + // Get filters and check pertinence + if( input == NULL ) + return( false ); + TFilter* destination = this->GetFilter( destination_filter ); + if( destination == NULL ) + return( false ); + if( !( destination->HasInput( destination_input ) ) ) + return( false ); + + // Check if there is room for a new connection + bool ok = true; + if( destination->IsInputMultiple( destination_input ) ) + { + for( + unsigned int i = 0; + i < destination->GetInputSize( destination_input ); + ++i + ) + if( + destination->GetInput( destination_input, i )->GetSource( ) == + input->GetSource( ) + ) + ok = false; + } + else + ok = ( destination->GetInput( destination_input ) == NULL ); + if( ok ) + destination->AddInput( + destination_input, + input + ); + return( ok ); +} + +// ------------------------------------------------------------------------- +bool cpPlugins::Interface::Workspace:: +Disconnect( + const std::string& origin_filter, + const std::string& origin_output, + const std::string& destination_filter, + const std::string& destination_input + ) +{ + // Get filters and check pertinence + TFilter* origin = this->GetFilter( origin_filter ); + TFilter* destination = this->GetFilter( destination_filter ); + if( origin == NULL || destination == NULL ) + return( false ); + if( !( destination->HasInput( destination_input ) ) ) + return( false ); + if( !( origin->HasOutput( origin_output ) ) ) + return( false ); + + // Check if there is room for a new connection + bool ok = false; + unsigned int del_id = 0; + for( + unsigned int i = 0; + i < destination->GetInputSize( destination_input ); + ++i + ) + if( + destination->GetInput( destination_input, i )->GetSource( ) == origin + ) + { + ok = true; + del_id = i; + + } // fi + if( ok ) + destination->DisconnectInput( destination_input, del_id ); + return( ok ); } // ------------------------------------------------------------------------- +/* const cpPlugins::Interface::Workspace:: TExposedPorts& cpPlugins::Interface::Workspace:: GetExposedInputs( ) const @@ -324,12 +504,17 @@ GetConnections( { for( auto i = ins.begin( ); i != ins.end( ); ++i ) { - auto od = orig->GetOutput( *o ); - auto id = dest->GetInput( *i ); - if( od != NULL && od == id ) - conns.push_back( - std::pair< std::string, std::string >( *o, *i ) - ); + unsigned int nInputs = dest->GetInputSize( *i ); + for( unsigned j = 0; j < nInputs; ++j ) + { + auto od = orig->GetOutput( *o ); + auto id = dest->GetInput( *i, j ); + if( od != NULL && od == id ) + conns.push_back( + std::pair< std::string, std::string >( *o, *i ) + ); + + } // rof } // rof @@ -430,10 +615,11 @@ Disconnect( const std::string& dest_filter ) { throw std::logic_error( "Disconnect 2" ); } +*/ // ------------------------------------------------------------------------- void cpPlugins::Interface::Workspace:: -Execute( ) +Update( ) { for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f ) f->second->Update( ); @@ -441,7 +627,7 @@ Execute( ) // ------------------------------------------------------------------------- void cpPlugins::Interface::Workspace:: -Execute( const std::string& name ) +Update( const std::string& name ) { auto filter = this->GetFilter( name ); if( filter != NULL ) @@ -454,14 +640,17 @@ Workspace( ) : Superclass( ), m_PrintExecution( false ) { + this->m_Plugins = TPlugins::New( ); } // ------------------------------------------------------------------------- cpPlugins::Interface::Workspace:: ~Workspace( ) { - this->m_ExposedOutputs.clear( ); - this->m_ExposedInputs.clear( ); + /* TODO + this->m_ExposedOutputs.clear( ); + this->m_ExposedInputs.clear( ); + */ this->m_Filters.clear( ); }