]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Workspace.cxx
Code cleaning
[cpPlugins.git] / lib / cpPlugins / Workspace.cxx
diff --git a/lib/cpPlugins/Workspace.cxx b/lib/cpPlugins/Workspace.cxx
new file mode 100644 (file)
index 0000000..dd4ade8
--- /dev/null
@@ -0,0 +1,480 @@
+#include <cpPlugins/Workspace.h>
+#include <cpPlugins/BaseWidget.h>
+#include <cpExtensions/QT/SimpleMPRWidget.h>
+#include <vtkRenderWindowInteractor.h>
+
+// -------------------------------------------------------------------------
+cpPlugins::Workspace::
+Workspace( )
+  : m_Interface( NULL ),
+    m_PrintExecution( false ),
+    m_MPRViewer( NULL )
+{
+  this->m_Graph = TGraph::New( );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Workspace::
+~Workspace( )
+{
+  this->m_Graph->Clear( );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Interface* cpPlugins::Workspace::
+GetInterface( )
+{
+  return( this->m_Interface );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Interface* cpPlugins::Workspace::
+GetInterface( ) const
+{
+  return( this->m_Interface );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+SetInterface( cpPlugins::Interface* i )
+{
+  if( this->m_Interface != i )
+    this->m_Interface = i;
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Workspace::
+TGraph* cpPlugins::Workspace::
+GetGraph( )
+{
+  return( this->m_Graph );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Workspace::
+TGraph* cpPlugins::Workspace::
+GetGraph( ) const
+{
+  return( this->m_Graph );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+Clear( )
+{
+  if( this->m_Graph.IsNotNull( ) )
+    this->m_Graph->Clear( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+ClearConnections( )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::ProcessObject* cpPlugins::Workspace::
+GetFilter( const std::string& name )
+{
+  ProcessObject* f =
+    dynamic_cast< ProcessObject* >(
+      this->m_Graph->GetVertex( name ).GetPointer( )
+      );
+  return( f );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::ProcessObject* cpPlugins::Workspace::
+GetFilter( const std::string& name ) const
+{
+  const ProcessObject* f =
+    dynamic_cast< const ProcessObject* >(
+      this->m_Graph->GetVertex( name ).GetPointer( )
+      );
+  return( f );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::DataObject* cpPlugins::Workspace::
+GetOutput( const std::string& filter, const std::string& output )
+{
+  auto f = this->GetFilter( filter );
+  if( f != NULL )
+    return( f->GetOutput( output ) );
+  else
+    return( NULL );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::DataObject* cpPlugins::Workspace::
+GetOutput( const std::string& filter, const std::string& output ) const
+{
+  auto f = this->GetFilter( filter );
+  if( f != NULL )
+    return( f->GetOutput( output ) );
+  else
+    return( NULL );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Workspace::
+HasFilter( const std::string& name ) const
+{
+  if( this->m_Graph->HasVertexIndex( name ) )
+    return( this->GetFilter( name ) != NULL );
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::ProcessObject* cpPlugins::Workspace::
+CreateFilter(
+  const std::string& category,
+  const std::string& filter,
+  const std::string& name
+  )
+{
+  if( this->m_Interface == NULL )
+    return( NULL );
+
+  // Get or create new filter from name
+  if( !( this->m_Graph->HasVertexIndex( name ) ) )
+  {
+    ProcessObject::Pointer f =
+      this->m_Interface->CreateProcessObject( category, filter );
+    if( f.IsNotNull( ) )
+    {
+      if( f->IsInteractive( ) )
+      {
+        std::vector< void* > interactive_objects;
+        interactive_objects.push_back( this->m_SingleInteractor );
+        interactive_objects.push_back( this->m_MPRViewer );
+        f->SetInteractionObjects( interactive_objects );
+
+      } // fi
+      f->SetPrintExecution( this->m_PrintExecution );
+      Object::Pointer o = f.GetPointer( );
+      this->m_Graph->SetVertex( name, o );
+      f->SetName( name );
+
+    } // fi
+    return( f.GetPointer( ) );
+  }
+  else
+    return( this->GetFilter( name ) );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Workspace::
+RenameFilter( const std::string& old_name, const std::string& new_name )
+{
+  return( this->m_Graph->RenameVertex( old_name, new_name ) );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+RemoveFilter( const std::string& name )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+SetParameter( const std::string& name, const std::string& value )
+{
+  std::vector< std::string > tokens;
+  cpPlugins::TokenizeString( tokens, name, "@" );
+
+  if( this->HasFilter( tokens[ 1 ] ) )
+  {
+    auto filter = this->GetFilter( tokens[ 1 ] );
+    filter->GetParameters( )->SetString( tokens[ 0 ], value );
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+SetPrintExecution( bool b )
+{
+  this->m_PrintExecution = b;
+  auto vIt = this->m_Graph->BeginVertices( );
+  for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
+  {
+    auto f = dynamic_cast< ProcessObject* >( vIt->second.GetPointer( ) );
+    if( f != NULL )
+      f->SetPrintExecution( b );
+
+  } // rof
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+PrintExecutionOn( )
+{
+  this->SetPrintExecution( true );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+PrintExecutionOff( )
+{
+  this->SetPrintExecution( false );
+}
+
+// -------------------------------------------------------------------------
+vtkRenderWindowInteractor* cpPlugins::Workspace::
+GetSingleInteractor( )
+{
+  return( this->m_SingleInteractor );
+}
+
+// -------------------------------------------------------------------------
+const vtkRenderWindowInteractor* cpPlugins::Workspace::
+GetSingleInteractor( ) const
+{
+  return( this->m_SingleInteractor );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+SetSingleInteractor( vtkRenderWindowInteractor* interactor )
+{
+  this->m_SingleInteractor = interactor;
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
+GetMPRViewer( )
+{
+  return( this->m_MPRViewer );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
+GetMPRViewer( ) const
+{
+  return( this->m_MPRViewer );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+SetMPRViewer( cpPlugins::Workspace::TMPRWidget* wdg )
+{
+  this->m_MPRViewer = wdg;
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Workspace::
+Connect(
+  const std::string& orig_filter, const std::string& dest_filter,
+  const std::string& output_name, const std::string& input_name
+  )
+{
+  // Get filters
+  ProcessObject* orig = this->GetFilter( orig_filter );
+  ProcessObject* dest = this->GetFilter( dest_filter );
+  if( orig == NULL || dest == NULL )
+    return( false );
+
+  // Real connection
+  if( dest->SetInputPort( input_name, orig->GetOutputPort( output_name ) ) )
+  {
+    this->m_Graph->AddEdge(
+      orig_filter, dest_filter,
+      TConnection( output_name, input_name )
+      );
+    return( true );
+  }
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Workspace::
+Connect( const OutputPort& port, const std::string& exposed_port )
+{
+  auto i = this->m_ExposedInputPorts.find( exposed_port );
+  if( i != this->m_ExposedInputPorts.end( ) )
+  {
+    ProcessObject* filter = this->GetFilter( i->second.first );
+    if( filter != NULL )
+      return( filter->SetInputPort( i->second.second, port ) );
+    else
+      return( false );
+  }
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Workspace::
+Reduce( const std::string& name )
+{
+  return( false );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+ExposeInputPort(
+  const std::string& name,
+  const std::string& filter, const std::string& filter_input
+  )
+{
+  this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+ExposeOutputPort(
+  const std::string& name,
+  const std::string& filter, const std::string& filter_output
+  )
+{
+  this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+HideInputPort( const std::string& name )
+{
+  auto i = this->m_ExposedInputPorts.find( name );
+  if( i != this->m_ExposedInputPorts.end( ) )
+    this->m_ExposedInputPorts.erase( i );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+HideOutputPort( const std::string& name )
+{
+  auto i = this->m_ExposedOutputPorts.find( name );
+  if( i != this->m_ExposedOutputPorts.end( ) )
+    this->m_ExposedOutputPorts.erase( i );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Workspace::
+RenameExposedInputPort(
+  const std::string& old_name,
+  const std::string& new_name
+  )
+{
+  auto o = this->m_ExposedInputPorts.find( old_name );
+  auto n = this->m_ExposedInputPorts.find( new_name );
+  if(
+    o != this->m_ExposedInputPorts.end( ) &&
+    n == this->m_ExposedInputPorts.end( )
+    )
+  {
+    this->m_ExposedInputPorts[ new_name ] = o->second;
+    this->m_ExposedInputPorts.erase( o );
+    return( true );
+  }
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Workspace::
+RenameExposedOutputPort(
+  const std::string& old_name,
+  const std::string& new_name
+  )
+{
+  auto o = this->m_ExposedOutputPorts.find( old_name );
+  auto n = this->m_ExposedOutputPorts.find( new_name );
+  if(
+    o != this->m_ExposedOutputPorts.end( ) &&
+    n == this->m_ExposedOutputPorts.end( )
+    )
+  {
+    this->m_ExposedOutputPorts[ new_name ] = o->second;
+    this->m_ExposedOutputPorts.erase( o );
+    return( true );
+  }
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Workspace::
+TExposedPorts& cpPlugins::Workspace::
+GetExposedInputPorts( ) const
+{
+  return( this->m_ExposedInputPorts );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Workspace::
+TExposedPorts& cpPlugins::Workspace::
+GetExposedOutputPorts( ) const
+{
+  return( this->m_ExposedOutputPorts );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::
+OutputPort& cpPlugins::Workspace::
+GetExposedOutput( const std::string& name )
+{
+  static OutputPort null_port;
+
+  auto i = this->m_ExposedOutputPorts.find( name );
+  if( i != this->m_ExposedOutputPorts.end( ) )
+  {
+    ProcessObject* filter = this->GetFilter( i->second.first );
+    if( filter != NULL )
+      return( filter->GetOutputPort( i->second.second ) );
+
+  } // fi
+  return( null_port );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::
+OutputPort& cpPlugins::Workspace::
+GetExposedOutput( const std::string& name ) const
+{
+  static const OutputPort null_port;
+
+  auto i = this->m_ExposedOutputPorts.find( name );
+  if( i != this->m_ExposedOutputPorts.end( ) )
+  {
+    const ProcessObject* filter = this->GetFilter( i->second.first );
+    if( filter != NULL )
+      return( filter->GetOutputPort( i->second.second ) );
+
+  } // fi
+  return( null_port );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+Execute( )
+{
+  // Find sinks
+  std::set< std::string > sinks = this->m_Graph->GetSinks( );
+
+  // Update sinks
+  for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
+    this->Execute( *sIt );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Workspace::
+Execute( const std::string& name )
+{
+  // Get filter
+  ProcessObject* f = this->GetFilter( name );
+  if( f == NULL )
+  {
+    itkGenericExceptionMacro( 
+      "cpPlugins::Workspace: Vertex \"" << name << "\" is not a filter."
+      );
+
+  } // fi
+
+  // Execute
+  f->Update( );
+}
+
+// eof - $RCSfile$