X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2FcpPlugins%2FInterface%2FWorkspace.cxx;h=d1d2c0daa7b412935ce6c4909b1ccdfc4b879c2d;hb=f6e163be0bf2ffeb19239bb31c1df936017cdc8e;hp=3c42481f7bcb6b797ab64e1ab63b1ef82e72eaf2;hpb=6885cd42cb7981803efb5c27c0a1a761084dc31c;p=cpPlugins.git diff --git a/lib/cpPlugins/Interface/Workspace.cxx b/lib/cpPlugins/Interface/Workspace.cxx index 3c42481..d1d2c0d 100644 --- a/lib/cpPlugins/Interface/Workspace.cxx +++ b/lib/cpPlugins/Interface/Workspace.cxx @@ -1,303 +1,468 @@ #include +#include // ------------------------------------------------------------------------- -cpPlugins::Interface::Workspace:: -Workspace( ) - : m_Interface( NULL ) +void cpPlugins::Interface::Workspace:: +Clear( ) +{ + this->m_Filters.clear( ); + this->m_ExposedInputs.clear( ); + this->m_ExposedOutputs.clear( ); +} + +// ------------------------------------------------------------------------- +std::vector< std::string > cpPlugins::Interface::Workspace:: +GetFiltersNames( ) const { - this->m_Graph = TGraph::New( ); + std::vector< std::string > n; + for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i ) + n.push_back( i->first ); + return( n ); } // ------------------------------------------------------------------------- cpPlugins::Interface::Workspace:: -~Workspace( ) +TProcess* cpPlugins::Interface::Workspace:: +GetFilter( const std::string& name ) +{ + auto i = this->m_Filters.find( name ); + if( i != this->m_Filters.end( ) ) + return( i->second.GetPointer( ) ); + else + return( NULL ); +} + +// ------------------------------------------------------------------------- +const cpPlugins::Interface::Workspace:: +TProcess* cpPlugins::Interface::Workspace:: +GetFilter( const std::string& name ) const +{ + auto i = this->m_Filters.find( name ); + if( i != this->m_Filters.end( ) ) + return( i->second.GetPointer( ) ); + else + return( NULL ); +} + +// ------------------------------------------------------------------------- +bool cpPlugins::Interface::Workspace:: +HasFilter( const std::string& name ) const { + return( this->m_Filters.find( name ) != this->m_Filters.end( ) ); } // ------------------------------------------------------------------------- cpPlugins::Interface::Workspace:: -TInterface* cpPlugins::Interface::Workspace:: -GetInterface( ) +TProcess* cpPlugins::Interface::Workspace:: +CreateFilter( + const std::string& category, + const std::string& filter, + const std::string& name + ) +{ + typedef cpPlugins::BaseObjects::Widget _TWidget; + + TInterface::Pointer interface = TInterface::New( ); + TProcess::Pointer o = this->GetFilter( name ); + if( o.IsNull( ) ) + { + 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 + + } // fi + return( o.GetPointer( ) ); +} + +// ------------------------------------------------------------------------- +bool cpPlugins::Interface::Workspace:: +RenameFilter( const std::string& old_name, const std::string& new_name ) +{ + auto o = this->m_Filters.find( old_name ); + auto n = this->m_Filters.find( new_name ); + if( o != this->m_Filters.end( ) && n == this->m_Filters.end( ) ) + { + // Rename filter + o->second->SetName( new_name ); + this->m_Filters[ new_name ] = o->second; + 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; + + return( true ); + } + else + return( false ); +} + +// ------------------------------------------------------------------------- +bool cpPlugins::Interface::Workspace:: +RemoveFilter( const std::string& name ) { - return( this->m_Interface ); + auto i = this->m_Filters.find( name ); + if( i != this->m_Filters.end( ) ) + { + i->second->Disconnect( ); + this->m_Filters.erase( i ); + return( true ); + } + else + return( false ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Workspace:: -SetInterface( TInterface* i ) +SetPrintExecution( bool b ) { - if( this->m_Interface != i ) - this->m_Interface = i; + this->m_PrintExecution = b; + for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i ) + i->second->SetPrintExecution( b ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Workspace:: -Clear( ) +PrintExecutionOn( ) { - if( this->m_Graph.IsNotNull( ) ) - this->m_Graph->Clear( ); + this->SetPrintExecution( true ); } // ------------------------------------------------------------------------- -cpPlugins::Interface::Workspace:: -TGraph* cpPlugins::Interface::Workspace:: -GetGraph( ) +void cpPlugins::Interface::Workspace:: +PrintExecutionOff( ) +{ + this->SetPrintExecution( true ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Interface::Workspace:: +AddInteractor( vtkRenderWindowInteractor* iren ) { - return( this->m_Graph ); + if( iren != NULL ) + this->m_Interactors.insert( iren ); } // ------------------------------------------------------------------------- const cpPlugins::Interface::Workspace:: -TGraph* cpPlugins::Interface::Workspace:: -GetGraph( ) const +TExposedPorts& cpPlugins::Interface::Workspace:: +GetExposedInputs( ) const { - return( this->m_Graph ); + return( this->m_ExposedInputs ); } // ------------------------------------------------------------------------- -bool cpPlugins::Interface::Workspace:: -CreateFilter( const std::string& filter, const std::string& name ) +const cpPlugins::Interface::Workspace:: +TExposedPorts& cpPlugins::Interface::Workspace:: +GetExposedOutputs( ) const { - if( this->m_Interface == NULL ) - return( false ); + return( this->m_ExposedOutputs ); +} - // Get or create new filter from name - if( !( this->m_Graph->HasVertexIndex( name ) ) ) +// ------------------------------------------------------------------------- +cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace:: +GetExposedOutput( const std::string& name ) +{ + auto i = this->m_ExposedOutputs.find( name ); + if( i != this->m_ExposedOutputs.end( ) ) { - TFilter::Pointer f = this->m_Interface->CreateObject( filter ); - if( f.IsNotNull( ) ) - { - f->SetName( name ); - TObject::Pointer o = f.GetPointer( ); - this->m_Graph->SetVertex( name, o ); - return( true ); - } + auto f = this->GetFilter( i->second.first ); + if( f != NULL ) + return( f->GetOutput( i->second.second ) ); else - return( false ); + return( NULL ); } else - return( true ); + return( NULL ); } // ------------------------------------------------------------------------- -bool cpPlugins::Interface::Workspace:: -Connect( - const std::string& orig_filter, const std::string& dest_filter, - const std::string& output_name, const std::string& input_name - ) +const cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace:: +GetExposedOutput( const std::string& name ) const { - // Get filters - TFilter* orig = this->GetFilter( orig_filter ); - TFilter* dest = this->GetFilter( dest_filter ); - if( orig == NULL || dest == NULL ) - return( false ); - - // Real connection - dest->SetInput( input_name, orig->GetOutput< TData >( output_name ) ); - this->m_Graph->AddConnection( - orig_filter, dest_filter, - TConnection( output_name, input_name ) - ); - return( false ); + auto i = this->m_ExposedOutputs.find( name ); + if( i != this->m_ExposedOutputs.end( ) ) + { + auto f = this->GetFilter( i->second.first ); + if( f != NULL ) + return( f->GetOutput( i->second.second ) ); + else + return( NULL ); + } + else + return( NULL ); } // ------------------------------------------------------------------------- bool cpPlugins::Interface::Workspace:: -Connect( TData* input_object, const std::string& input_name ) +ExposeInput( + const std::string& name, + const std::string& filter, const std::string& filter_input + ) { - auto port = this->m_InputPorts.find( input_name ); - if( port != this->m_InputPorts.end( ) ) + auto i = this->m_ExposedInputs.find( name ); + if( i == this->m_ExposedInputs.end( ) ) { - TFilter* filter = this->GetFilter( port->second.first ); - if( filter != NULL ) - { - filter->SetInput( port->second.second, input_object ); - return( true ); - } - else - return( false ); + this->m_ExposedInputs[ name ] = + std::pair< std::string, std::string >( filter, filter_input ); + return( true ); } else return( false ); } // ------------------------------------------------------------------------- -cpPlugins::Interface::Workspace:: -TParameters* cpPlugins::Interface::Workspace:: -GetParameters( const std::string& name ) +bool cpPlugins::Interface::Workspace:: +ExposeOutput( + const std::string& name, + const std::string& filter, const std::string& filter_output + ) { - TFilter* f = this->GetFilter( name ); - if( f != NULL ) - return( f->GetParameters( ) ); + auto i = this->m_ExposedOutputs.find( name ); + if( i == this->m_ExposedOutputs.end( ) ) + { + this->m_ExposedOutputs[ name ] = + std::pair< std::string, std::string >( filter, filter_output ); + return( true ); + } else - return( NULL ); + return( false ); } // ------------------------------------------------------------------------- -const cpPlugins::Interface::Workspace:: -TParameters* cpPlugins::Interface::Workspace:: -GetParameters( const std::string& name ) const +void cpPlugins::Interface::Workspace:: +HideInput( const std::string& name ) { - const TFilter* f = this->GetFilter( name ); - if( f != NULL ) - return( f->GetParameters( ) ); - else - return( NULL ); + auto i = this->m_ExposedInputs.find( name ); + if( i != this->m_ExposedInputs.end( ) ) + this->m_ExposedInputs.erase( i ); } // ------------------------------------------------------------------------- -cpPlugins::Interface::Workspace:: -TFilter* cpPlugins::Interface::Workspace:: -GetFilter( const std::string& name ) +void cpPlugins::Interface::Workspace:: +HideOutput( const std::string& name ) { - TFilter* f = - dynamic_cast< TFilter* >( - this->m_Graph->GetVertex( name ).GetPointer( ) - ); - return( f ); + auto i = this->m_ExposedOutputs.find( name ); + if( i != this->m_ExposedOutputs.end( ) ) + this->m_ExposedOutputs.erase( i ); } // ------------------------------------------------------------------------- -const cpPlugins::Interface::Workspace:: -TFilter* cpPlugins::Interface::Workspace:: -GetFilter( const std::string& name ) const +bool cpPlugins::Interface::Workspace:: +RenameExposedInput( + const std::string& old_name, const std::string& new_name + ) { - const TFilter* f = - dynamic_cast< const TFilter* >( - this->m_Graph->GetVertex( name ).GetPointer( ) - ); - return( f ); + auto o = this->m_ExposedInputs.find( old_name ); + auto n = this->m_ExposedInputs.find( new_name ); + if( o != this->m_ExposedInputs.end( ) && n == this->m_ExposedInputs.end( ) ) + { + this->m_ExposedInputs[ new_name ] = o->second; + this->m_ExposedInputs.erase( o ); + return( true ); + } + else + return( false ); } // ------------------------------------------------------------------------- bool cpPlugins::Interface::Workspace:: -HasFilter( const std::string& name ) const +RenameExposedOutput( + const std::string& old_name, const std::string& new_name + ) { - if( this->m_Graph->HasVertexIndex( name ) ) - return( this->GetFilter( name ) != NULL ); + auto o = this->m_ExposedOutputs.find( old_name ); + auto n = this->m_ExposedOutputs.find( new_name ); + if( + o != this->m_ExposedOutputs.end( ) && n == this->m_ExposedOutputs.end( ) + ) + { + this->m_ExposedOutputs[ new_name ] = o->second; + this->m_ExposedOutputs.erase( o ); + return( true ); + } else return( false ); } // ------------------------------------------------------------------------- -bool cpPlugins::Interface::Workspace:: -Reduce( const std::string& name ) +std::vector< std::pair< std::string, std::string > > +cpPlugins::Interface::Workspace:: +GetConnections( + const std::string& origin, const std::string& destination + ) const { - return( false ); + std::vector< std::pair< std::string, std::string > > conns; + auto orig = this->GetFilter( origin ); + auto dest = this->GetFilter( destination ); + if( orig != NULL && dest != NULL ) + { + auto outs = orig->GetOutputsNames( ); + auto ins = dest->GetInputsNames( ); + for( auto o = outs.begin( ); o != outs.end( ); ++o ) + { + 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 ) + ); + + } // rof + + } // rof + + } // fi + return( conns ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Workspace:: -AddInputPort( - const std::string& name, - const std::string& filter, const std::string& filter_input +Connect( + const std::string& orig_filter, const std::string& dest_filter, + const std::string& output_name, const std::string& input_name ) { - this->m_InputPorts[ name ] = TGlobalPort( filter, filter_input ); + auto o = this->GetFilter( orig_filter ); + auto d = this->GetFilter( dest_filter ); + if( o != NULL && d != NULL ) + { + try + { + d->AddInput( input_name, o->GetOutput( output_name ) ); + } + catch( std::exception& err ) + { + throw std::logic_error( + std::string( "Error connecting \"" ) + + output_name + std::string( "@" ) + orig_filter + + std::string( "\" with \"" ) + + input_name + std::string( "@" ) + dest_filter + + std::string( "\": " ) + + err.what( ) + ); + + } // yrt + + } // fi } // ------------------------------------------------------------------------- void cpPlugins::Interface::Workspace:: -AddOutputPort( - const std::string& name, - const std::string& filter, const std::string& filter_output +Connect( + cpPlugins::BaseObjects::DataObject* output, + const std::string& dest_filter, const std::string& input_name ) { - this->m_OutputPorts[ name ] = TGlobalPort( filter, filter_output ); + auto d = this->GetFilter( dest_filter ); + if( d != NULL ) + d->AddInput( input_name, output ); } // ------------------------------------------------------------------------- -cpPlugins::Interface::Workspace:: -TData* cpPlugins::Interface::Workspace:: -GetOutput( const std::string& name ) +void cpPlugins::Interface::Workspace:: +Connect( + cpPlugins::BaseObjects::DataObject* output, + const std::string& exposed_input_name + ) { - auto port = this->m_OutputPorts.find( name ); - if( port != this->m_OutputPorts.end( ) ) - { - TFilter* f = this->GetFilter( port->second.first ); - if( f != NULL ) - return( f->GetOutput< TData >( port->second.second ) ); - else - return( NULL ); - } - else - return( NULL ); + auto i = this->m_ExposedInputs.find( exposed_input_name ); + if( i != this->m_ExposedInputs.end( ) ) + this->Connect( output, i->second.first, i->second.second ); } // ------------------------------------------------------------------------- -const cpPlugins::Interface::Workspace:: -TData* cpPlugins::Interface::Workspace:: -GetOutput( const std::string& name ) const +void cpPlugins::Interface::Workspace:: +Disconnect( + const std::string& orig_filter, const std::string& dest_filter, + const std::string& output_name, const std::string& input_name + ) { - auto port = this->m_OutputPorts.find( name ); - if( port != this->m_OutputPorts.end( ) ) + auto orig = this->GetFilter( orig_filter ); + auto dest = this->GetFilter( dest_filter ); + if( orig != NULL && dest != NULL ) { - const TFilter* f = this->GetFilter( port->second.first ); - if( f != NULL ) - return( f->GetOutput< TData >( port->second.second ) ); - else - return( NULL ); - } - else - return( NULL ); + auto out = orig->GetOutput( output_name ); + auto in = dest->GetInput( input_name ); + if( out != NULL && out == in ) + dest->SetInput( + input_name, ( cpPlugins::BaseObjects::DataObject* )( NULL ) + ); + + } // fi } // ------------------------------------------------------------------------- void cpPlugins::Interface::Workspace:: -ClearInputPorts( ) +Disconnect( + const std::string& dest_filter, const std::string& input_name + ) { - this->m_InputPorts.clear( ); + throw std::logic_error( "Disconnect 1" ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Workspace:: -ClearOutputPorts( ) +Disconnect( const std::string& dest_filter ) { - this->m_OutputPorts.clear( ); + throw std::logic_error( "Disconnect 2" ); } // ------------------------------------------------------------------------- -std::string cpPlugins::Interface::Workspace:: +void cpPlugins::Interface::Workspace:: Execute( ) { - // Find sinks - std::set< std::string > sinks = this->m_Graph->GetSinks( ); - - // Update sinks - std::string err = ""; - for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt ) - { - std::string lerr = this->Execute( *sIt, NULL ); - if( lerr != "" ) - err += lerr + std::string( "\n" ); + for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f ) + f->second->Update( ); +} - } // rof - return( err ); +// ------------------------------------------------------------------------- +void cpPlugins::Interface::Workspace:: +Execute( const std::string& name ) +{ + auto filter = this->GetFilter( name ); + if( filter != NULL ) + filter->Update( ); } // ------------------------------------------------------------------------- -std::string cpPlugins::Interface::Workspace:: -Execute( const std::string& name, QWidget* p ) +cpPlugins::Interface::Workspace:: +Workspace( ) + : Superclass( ), + m_PrintExecution( false ) { - // Get filter - TFilter* f = this->GetFilter( name ); - if( f == NULL ) - return( - std::string( "cpPlugins::Interface::Workspace: Vertex \"" ) + - name + std::string( "\" is not a filter." ) - ); +} - // Execute and return - if( p != NULL ) - { - auto diag_res = f->ExecConfigurationDialog( p ); - if( diag_res == TFilter::DialogResult_NoModal ) - return( f->Update( ) ); - else - return( "" ); - } - else - return( f->Update( ) ); +// ------------------------------------------------------------------------- +cpPlugins::Interface::Workspace:: +~Workspace( ) +{ + this->m_ExposedOutputs.clear( ); + this->m_ExposedInputs.clear( ); + this->m_Filters.clear( ); } // eof - $RCSfile$