]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/BaseMPRWidget.cxx
XML IO added. Workspace singleton added to simplify pipeline definition and execution.
[cpPlugins.git] / lib / cpPlugins / Interface / BaseMPRWidget.cxx
index 7caa4e0d11adc2d197259571f10fd2beb6c7ea99..052e374f51c879794a222e1cf336e59eecca1a66 100644 (file)
@@ -3,14 +3,31 @@
 #ifdef cpPlugins_Interface_QT4
 
 #include <cpPlugins/Interface/ui_BaseMPRWidget.h>
+#include <cpPlugins/Interface/DataObject.h>
+#include <cpPlugins/Interface/Image.h>
+#include <cpPlugins/Interface/Mesh.h>
 #include <QTreeWidgetItem>
 #include <vtkRendererCollection.h>
 
+double cpPlugins::Interface::BaseMPRWidget::
+cm_Colors[ 8 ][ 3 ] =
+{
+  { 1.0, 0.0, 0.0 },
+  { 0.0, 1.0, 0.0 },
+  { 0.0, 0.0, 1.0 },
+  { 0.0, 1.0, 1.0 },
+  { 1.0, 0.0, 1.0 },
+  { 1.0, 1.0, 0.0 },
+  { 1.0, 0.5, 0.0 },
+  { 1.0, 0.0, 0.5 }
+};
+
 // -------------------------------------------------------------------------
 cpPlugins::Interface::BaseMPRWidget::
 BaseMPRWidget( QWidget* parent )
   : QWidget( parent ),
-    m_UI( new Ui::BaseMPRWidget )
+    m_UI( new Ui::BaseMPRWidget ),
+    m_MainImage( "" )
 {
   this->m_UI->setupUi( this );
 
@@ -44,27 +61,206 @@ cpPlugins::Interface::BaseMPRWidget::
 ~BaseMPRWidget( )
 {
   delete this->m_UI;
+}
 
-  // Delete polydata actors
-  std::map< std::string, PolyDataActor* >::iterator mIt =
-    this->m_Meshes.begin( );
-  for( ; mIt != this->m_Meshes.end( ); ++mIt )
-    delete mIt->second;
-  this->m_Meshes.clear( );
+// -------------------------------------------------------------------------
+bool cpPlugins::Interface::BaseMPRWidget::
+AddData(
+  cpPlugins::Interface::DataObject* data, const std::string& name,
+  const std::string& parent
+  )
+{
+  if( name == "" )
+    return( false );
+
+  auto iIt = this->m_Data.find( name );
+  if( iIt == this->m_Data.end( ) )
+  {
+    if( parent != "" )
+    {
+      auto pIt = this->m_Data.find( parent );
+      if( pIt == this->m_Data.end( ) )
+        return( false );
+
+    } // fi
+
+    // Add new data
+    this->m_Data[ name ].SetSourceDataObject( data );
+
+    // Add to tree view
+    this->_UpdateTreeItem( name, parent );
+    return( true );
+  }
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+const std::string& cpPlugins::Interface::BaseMPRWidget::
+GetMainImage( ) const
+{
+  return( this->m_MainImage );
 }
 
 // -------------------------------------------------------------------------
 bool cpPlugins::Interface::BaseMPRWidget::
-ShowImage(
+SetMainImage( const std::string& name )
+{
+  auto iIt = this->m_Data.find( name );
+  if( iIt != this->m_Data.end( ) )
+  {
+    if( iIt->second.Tag == Data::IMAGE )
+    {
+      this->m_MainImage = name;
+      return( true );
+    }
+    else
+      return( false );
+  }
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWidget::
+DeleteData( const std::string& name )
+{
+  auto iIt = this->m_Data.find( name );
+  if( iIt != this->m_Data.end( ) )
+  {
+    this->m_Data.erase( iIt );
+
+    // Get children
+    std::vector< std::string > to_erase;
+    auto tIt = this->m_Tree.begin( );
+    for( ; tIt != this->m_Tree.end( ); ++tIt )
+      if( tIt->second == name )
+        to_erase.push_back( tIt->first );
+
+    // Delete from tree
+    tIt = this->m_Tree.find( name );
+    if( tIt != this->m_Tree.end( ) )
+      this->m_Tree.erase( tIt );
+
+    // Recursive erase
+    auto dIt = to_erase.begin( );
+    for( ; dIt != to_erase.end( ); ++dIt )
+      this->DeleteData( *dIt );
+
+    // Delete from tree widget
+    QTreeWidgetItem* item = this->_FindItemInTree( name );
+    if( item != NULL )
+      this->m_UI->LoadedData->removeItemWidget( item, 0 );
+
+    // Reset main image, just in case
+    if( this->m_Data.size( ) == 0 )
+      this->m_MainImage = "";
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWidget::
+DeleteAllData( )
+{
+  this->m_MPRObjects->Clear( );
+  this->m_Data.clear( );
+  this->m_Tree.clear( );
+  this->m_UI->LoadedData->clear( );
+  this->m_MainImage = "";
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWidget::
+SetDataColor(
+  const std::string& name, const double& r, const double& g, const double& b
+  )
+{
+  /*
+    auto iIt = this->m_Data.find( name );
+    if( iIt == this->m_Data.end( ) )
+    return;
+
+    if( iIt->second.Tag == Data::IMAGE )
+    {
+    }
+    else if( iIt->second.Tag == Data::MESH )
+    {
+    } // fi
+  */
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWidget::
+ShowData( const std::string& name )
+{
+  auto iIt = this->m_Data.find( name );
+  if( iIt == this->m_Data.end( ) )
+    return;
+
+  if( iIt->second.Tag == Data::IMAGE )
+  {
+    if( name == this->m_MainImage )
+      this->m_MPRObjects->SetInputImage( iIt->second.Image );
+    else
+    {
+      unsigned int i = ( this->m_MPRObjects->GetNumberOfImages( ) - 1 ) % 8;
+      this->m_MPRObjects->AddBinaryImage(
+        iIt->second.Image,
+        Self::cm_Colors[ i ][ 0 ],
+        Self::cm_Colors[ i ][ 1 ],
+        Self::cm_Colors[ i ][ 2 ]
+        );
+    }
+    this->m_MPRObjects->Show( );
+  }
+  else if( iIt->second.Tag == Data::MESH )
+  {
+    /* TODO
+       vtkRenderer* ren =
+       this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
+       if( ren == NULL )
+       return;
+       ren->AddActor( iIt->second.GetMeshActor( ) );
+       this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
+    */
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWidget::
+HideData( const std::string& name )
+{
+}
+
+// -------------------------------------------------------------------------
+vtkRenderWindowInteractor* cpPlugins::Interface::BaseMPRWidget::
+GetInteractor( unsigned int i )
+{
+  if( i < 4 )
+  {
+    if( this->m_VTK[ i ] != NULL )
+      return( this->m_VTK[ i ]->GetInteractor( ) );
+    else
+      return( NULL );
+  }
+  else
+    return( NULL );
+}
+
+// -------------------------------------------------------------------------
+/*
+  bool cpPlugins::Interface::BaseMPRWidget::
+  ShowImage(
   vtkImageData* image,
   const std::string& name,
   const std::string& parent
   )
-{
+  {
   // Update tree view
-  QTreeWidgetItem* new_item = this->_UpdateItem( name, parent );
+  QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
   if( new_item == NULL )
-    return( false );
+  return( false );
 
   // Associate new data
   this->m_Images[ name ] = image;
@@ -73,38 +269,43 @@ ShowImage(
   // Show image and return
   this->m_MPRObjects->AddImage( image );
   return( true );
-}
+  }
 
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::BaseMPRWidget::
-ShowImage(
+  // -------------------------------------------------------------------------
+  bool cpPlugins::Interface::BaseMPRWidget::
+  ShowImage(
   vtkImageData* image,
   const std::string& name,
   const std::string& parent,
   const double& r, const double& g, const double& b
   )
-{
-  std::cout
-    << "BaseMPRWidget: "
-    << image << " "
-    << name << " "
-    << parent << " "
-    << r << " " << g << " " << b << std::endl;
+  {
+  // Update tree view
+  QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
+  if( new_item == NULL )
   return( false );
-}
 
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::BaseMPRWidget::
-ShowMesh(
+  // Associate new data
+  this->m_Images[ name ] = image;
+  this->m_Tree[ name ] = parent;
+
+  // Show image and return
+  this->m_MPRObjects->AddImage( image );
+  return( true );
+  }
+
+  // -------------------------------------------------------------------------
+  bool cpPlugins::Interface::BaseMPRWidget::
+  ShowMesh(
   vtkPolyData* mesh,
   const std::string& name,
   const std::string& parent
   )
-{
+  {
   // Update tree view
-  QTreeWidgetItem* new_item = this->_UpdateItem( name, parent );
+  QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
   if( new_item == NULL )
-    return( false );
+  return( false );
 
   // Associate new data
   PolyDataActor* actor = new PolyDataActor( mesh );
@@ -114,30 +315,29 @@ ShowMesh(
   // Show mesh
   this->_Add3DActor( actor->Actor );
   return( true );
-}
+  }
 
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::BaseMPRWidget::
-ShowMesh(
+  // -------------------------------------------------------------------------
+  bool cpPlugins::Interface::BaseMPRWidget::
+  ShowMesh(
   vtkPolyData* mesh,
   const std::string& name,
   const std::string& parent,
   const double& r, const double& g, const double& b
   )
-{
+  {
   return false;
-}
+  }
 
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::BaseMPRWidget::
-ClearAll( )
-{
-  /*
-    this->m_MPRObjects->ClearAll( );
-    this->m_Images.clear( );
-    this->m_Meshes.clear( );
-  */
-}
+  // -------------------------------------------------------------------------
+  void cpPlugins::Interface::BaseMPRWidget::
+  ClearAll( )
+  {
+  this->m_MPRObjects->ClearAll( );
+  this->m_Images.clear( );
+  this->m_Meshes.clear( );
+  }
+*/
 
 // -------------------------------------------------------------------------
 std::string cpPlugins::Interface::BaseMPRWidget::
@@ -152,10 +352,12 @@ GetSelectedData( ) const
 
 // -------------------------------------------------------------------------
 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
-_FindItem( const std::string& name ) const
+_FindItemInTree( const std::string& name ) const
 {
   QList< QTreeWidgetItem* > items =
-    this->m_UI->LoadedData->findItems( name.c_str( ), Qt::MatchExactly );
+    this->m_UI->LoadedData->findItems(
+      name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
+      );
   if( items.size( ) > 0 )
     return( items[ 0 ] );
   else
@@ -164,16 +366,16 @@ _FindItem( const std::string& name ) const
 
 // -------------------------------------------------------------------------
 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
-_UpdateItem( const std::string& name, const std::string& parent )
+_UpdateTreeItem( const std::string& name, const std::string& parent )
 {
   // Update tree view
   QTreeWidgetItem* new_item = NULL;
   if( parent != "" )
   {
-    QTreeWidgetItem* parent_item = this->_FindItem( parent );
+    QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
     if( parent_item != NULL )
     {
-      QTreeWidgetItem* old_item = this->_FindItem( name );
+      QTreeWidgetItem* old_item = this->_FindItemInTree( name );
       if( old_item == NULL )
       {
         new_item =
@@ -196,16 +398,18 @@ _UpdateItem( const std::string& name, const std::string& parent )
 }
 
 // -------------------------------------------------------------------------
-void cpPlugins::Interface::BaseMPRWidget::
-_Add3DActor( vtkProp3D* prop )
-{
+/*
+  void cpPlugins::Interface::BaseMPRWidget::
+  _Add3DActor( vtkProp3D* prop )
+  {
   vtkRenderer* ren =
-    this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
+  this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
   if( ren == NULL )
-    return;
+  return;
   ren->AddActor( prop );
   this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
-}
+  }
+*/
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::BaseMPRWidget::
@@ -223,7 +427,28 @@ _SyncTop( int a, int b )
 
 // -------------------------------------------------------------------------
 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
-PolyDataActor( vtkPolyData* pd )
+PolyDataActor( )
+  : Mesh( NULL ),
+    Normals( NULL ),
+    Stripper( NULL ),
+    Mapper( NULL ),
+    Actor( NULL )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
+~PolyDataActor( )
+{
+  if( this->Actor != NULL )    this->Actor->Delete( );
+  if( this->Mapper != NULL )   this->Mapper->Delete( );
+  if( this->Stripper != NULL ) this->Stripper->Delete( );
+  if( this->Normals != NULL )  this->Normals->Delete( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
+Configure( vtkPolyData* pd )
 {
   if( pd ==  NULL )
     return;
@@ -231,11 +456,12 @@ PolyDataActor( vtkPolyData* pd )
   double range[ 2 ];
   pd->GetScalarRange( range );
 
-  this->Normals = vtkSmartPointer< vtkPolyDataNormals >::New( );
-  this->Stripper = vtkSmartPointer< vtkStripper >::New( );
-  this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
-  this->Actor = vtkSmartPointer< vtkQuadricLODActor >::New( );
+  this->Normals = vtkPolyDataNormals::New( );
+  this->Stripper = vtkStripper::New( );
+  this->Mapper = vtkPolyDataMapper::New( );
+  this->Actor = vtkQuadricLODActor::New( );
 
+  this->Mesh = pd;
   this->Normals->SetInputData( pd );
   this->Normals->SetFeatureAngle( 60.0 );
   this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
@@ -248,6 +474,107 @@ PolyDataActor( vtkPolyData* pd )
   this->Actor->DeferLODConstructionOff( );
 }
 
+// -------------------------------------------------------------------------
+cpPlugins::Interface::BaseMPRWidget::Data::
+Data( )
+{
+  this->Tag = Data::IMAGE;
+  this->Source = NULL;
+  this->Image = NULL;
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Interface::BaseMPRWidget::Data::
+~Data( )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Interface::BaseMPRWidget::
+Data& cpPlugins::Interface::BaseMPRWidget::Data::
+operator=( const Data& data )
+{
+  this->Tag = data.Tag;
+  this->Source = data.Source;
+  if( this->Tag == Data::IMAGE )
+    this->Image = data.Image;
+  else if( this->Tag == Data::MESH )
+    this->Mesh = data.Mesh;
+  return( *this );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Interface::
+DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
+GetSourceDataObject( )
+{
+  return( this->Source );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Interface::
+DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
+GetSourceDataObject( ) const
+{
+  return( this->Source );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWidget::Data::
+SetSourceDataObject( cpPlugins::Interface::DataObject* dobj )
+{
+  typedef cpPlugins::Interface::Image _TImage;
+  typedef cpPlugins::Interface::Mesh  _TMesh;
+
+  this->Source = dobj;
+  _TImage* image = dynamic_cast< _TImage* >( dobj );
+  if( image != NULL )
+  {
+    this->Tag = Data::IMAGE;
+    this->Image = image->GetVTK< vtkImageData >( );
+  }
+  else
+  {
+    _TMesh* mesh = dynamic_cast< _TMesh* >( dobj );
+    if( mesh == NULL )
+      return;
+
+    this->Tag = Data::MESH;
+    this->Mesh.Configure( mesh->GetVTK< vtkPolyData >( ) );
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+vtkImageData* cpPlugins::Interface::BaseMPRWidget::Data::
+GetImage( )
+{
+  if( this->Tag == Data::IMAGE )
+    return( this->Image );
+  else
+    return( NULL );
+}
+
+// -------------------------------------------------------------------------
+vtkPolyData* cpPlugins::Interface::BaseMPRWidget::Data::
+GetMesh( )
+{
+  if( this->Tag == Data::MESH )
+    return( this->Mesh.Mesh );
+  else
+    return( NULL );
+}
+
+// -------------------------------------------------------------------------
+vtkProp* cpPlugins::Interface::BaseMPRWidget::Data::
+GetMeshActor( )
+{
+  if( this->Tag == Data::MESH )
+    return( this->Mesh.Actor );
+  else
+    return( NULL );
+}
+
 #endif // cpPlugins_Interface_QT4
 
 // eof - $RCSfile$