]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Workspace.cxx
Cast image filter added. ROI filter modified.
[cpPlugins.git] / lib / cpPlugins / Interface / Workspace.cxx
index faef957a1aa042ff1285d46b2b099c14e57617de..5b80a36bb9a19f392bf941be9a7c9fd4f7cd3d90 100644 (file)
@@ -6,8 +6,6 @@ void cpPlugins::Interface::Workspace::
 Clear( )
 {
   this->m_Filters.clear( );
-  this->m_ExposedInputs.clear( );
-  this->m_ExposedOutputs.clear( );
 }
 
 // -------------------------------------------------------------------------
@@ -15,14 +13,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 +32,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 );
@@ -49,7 +47,7 @@ cpPlugins::Interface::Workspace::
 TWidget* cpPlugins::Interface::Workspace::
 GetWidget( const std::string& name )
 {
-  TProcess* process = this->GetFilter( name );
+  TFilter* process = this->GetFilter( name );
   return( dynamic_cast< TWidget* >( process ) );
 }
 
@@ -58,7 +56,7 @@ const cpPlugins::Interface::Workspace::
 TWidget* cpPlugins::Interface::Workspace::
 GetWidget( const std::string& name ) const
 {
-  const TProcess* process = this->GetFilter( name );
+  const TFilter* process = this->GetFilter( name );
   return( dynamic_cast< const TWidget* >( process ) );
 }
 
@@ -79,37 +77,31 @@ HasWidget( const std::string& name ) const
 
 // -------------------------------------------------------------------------
 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( ) );
@@ -129,14 +121,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 );
   }
@@ -187,10 +181,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
@@ -461,10 +586,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( );
@@ -472,7 +598,7 @@ Execute( )
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Workspace::
-Execute( const std::string& name )
+Update( const std::string& name )
 {
   auto filter = this->GetFilter( name );
   if( filter != NULL )
@@ -485,14 +611,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( );
 }