]> Creatis software - cpPlugins.git/blobdiff - appli/ImageMPR/ImageMPR.cxx
...
[cpPlugins.git] / appli / ImageMPR / ImageMPR.cxx
index 26c8f349076c6e8c50a8047c2f7a4a200dc2d474..c7921b1924966bbe3fb775e17562b569d03da8e6 100644 (file)
 #include "ImageMPR.h"
 #include "ui_ImageMPR.h"
 
-#include <vtkRenderWindow.h>
-
-#include <QFileDialog>
+#include <QFileInfo>
 #include <QMessageBox>
 
 // -------------------------------------------------------------------------
-ImageMPR::ImageMPR( QWidget* parent )
+#define ImageMPR_ConnectAction( ACTION )                \
+  QObject::connect(                                     \
+    this->m_UI->a##ACTION, SIGNAL( triggered( ) ),      \
+    this, SLOT( _a##ACTION( ) )                         \
+    )
+
+// -------------------------------------------------------------------------
+ImageMPR::
+ImageMPR( QWidget* parent )
   : QMainWindow( parent ),
-    m_UI( new Ui::ImageMPR ),
-    m_InputImage( NULL )
+    m_UI( new Ui::ImageMPR )
 {
   this->m_UI->setupUi( this );
+  this->m_Plugins.SetWidget( this );
+  this->m_Plugins.SetApplication( this );
 
-  // Create and associate renderers
-  this->m_MPR = new TMPR(
-    this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
-    this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
-    this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
-    this->m_UI->m_3DVTK->GetRenderWindow( )
-    );
+  // Connect actions
+  ImageMPR_ConnectAction( OpenImage );
+  ImageMPR_ConnectAction( OpenDICOMSeries );
+  ImageMPR_ConnectAction( OpenSegmentation );
+  ImageMPR_ConnectAction( OpenPolyData );
+  ImageMPR_ConnectAction( SaveImage );
+  ImageMPR_ConnectAction( SavePolyData );
+  ImageMPR_ConnectAction( Undo );
+  ImageMPR_ConnectAction( Redo );
+  ImageMPR_ConnectAction( LoadPlugins );
+  ImageMPR_ConnectAction( ShowPlugins );
 
-  // signals <-> slots
-  QObject::connect(
-    this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
-    this, SLOT( _triggered_actionOpenPlugins( ) )
-    );
-  QObject::connect(
-    this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
-    this, SLOT( _triggered_actionOpenInputImage( ) )
+  // Associate model with view
+  for( unsigned int i = 0; i < 4; ++i )
+    this->m_Plugins.AddInteractor( this->m_UI->MPR->GetInteractor( i ) );
+
+  // Try to load default plugins
+  QStringList args = QApplication::arguments( );
+  QFileInfo info( args.at( 0 ) );
+  this->m_Plugins.LoadPluginsPath(
+    info.absolutePath( ).toStdString( ), true
     );
 
-  // Start: load all disponible plugins
-  this->_triggered_actionOpenPlugins( );
+  // Put loaded plugins into menu
+  this->_AssociatePluginsToMenu( );
 }
 
 // -------------------------------------------------------------------------
 ImageMPR::
 ~ImageMPR( )
 {
-  // Close all connections
-  this->m_Plugins.UnloadAll( );
-
-  // Delete objects
   delete this->m_UI;
-  delete this->m_MPR;
-  if( this->m_InputImage != NULL ) delete this->m_InputImage;
 }
 
 // -------------------------------------------------------------------------
 void ImageMPR::
-_triggered_actionOpenPlugins( )
+UpdateActualFilter( )
 {
-  // Show dialog and check if it was accepted
-  QFileDialog dialog( this );
-  dialog.setFileMode( QFileDialog::ExistingFile );
-  dialog.setDirectory( "." );
-  dialog.setNameFilter(
-    tr( "Plugins file (*.so);;All files (*)" )
-    );
-  dialog.setDefaultSuffix( tr( "so" ) );
-  if( !( dialog.exec( ) ) )
+  if( !( this->m_Plugins.HasActiveFilter( ) ) )
+    return;
+
+  // Update filter
+  TPlugins::TStringContainer outputs;
+  if(
+    !(
+      this->m_Plugins.UpdateActiveFilter(
+        outputs, this->m_ActiveFilterMainInput
+        )
+      )
+    )
     return;
-  
-  std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
-  this->m_Plugins.UnloadAll( );
-  if( !( this->m_Plugins.Load( fname ) ) )
+
+  // Show outputs
+  for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
+  {
+    std::string parent = this->m_Plugins.GetParent( *oIt );
+    TDataObject* dobj = this->m_Plugins.GetData< TDataObject >( *oIt );
+    if( this->m_UI->MPR->AddData( dobj, *oIt, parent ) )
+      this->m_UI->MPR->ShowData( *oIt );
+
+  } // rof
+}
+
+// -------------------------------------------------------------------------
+void ImageMPR::
+_AssociatePluginsToMenu( )
+{
+  this->m_UI->MenuFilters->clear( );
+
+  TPlugins::TStringContainer categories;
+  this->m_Plugins.GetLoadedCategories( categories );
+  for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
+  {
+    QMenu* category = this->m_UI->MenuFilters->addMenu( cIt->c_str( ) );
+    const TPlugins::TStringContainer& filters =
+      this->m_Plugins.GetLoadedFilters( *cIt );
+    for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
+    {
+      QAction* filter = category->addAction( fIt->c_str( ) );
+      this->connect(
+        filter, SIGNAL( triggered( ) ),
+        this, SLOT( _execPlugin( ) )
+        );
+
+    } // rof
+
+  } // rof
+}
+
+// -------------------------------------------------------------------------
+#define ImageMPR_ReadImage( F )                                         \
+  this->m_UI->MPR->DeleteAllData( );                                    \
+  this->m_Plugins.ClearDataObjects( );                                  \
+  try                                                                   \
+  {                                                                     \
+    std::string name = this->m_Plugins.Read##F( "" );                   \
+    if( name == "" )                                                    \
+      return;                                                           \
+    TImage* image = this->m_Plugins.GetData< TImage >( name );          \
+    this->m_UI->MPR->AddData( image, name, "" );                        \
+    this->m_UI->MPR->SetMainImage( name );                              \
+    this->m_UI->MPR->ShowData( name );                                  \
+  }                                                                     \
+  catch( std::exception& err )                                          \
+  {                                                                     \
+    QMessageBox::critical(                                              \
+      this,                                                             \
+      QMessageBox::tr( "Error reading image." ),                        \
+      QMessageBox::tr( err.what( ) )                                    \
+      );                                                                \
+  }
+
+void ImageMPR::_aOpenImage( )       { ImageMPR_ReadImage( Image ) }
+void ImageMPR::_aOpenDICOMSeries( ) { ImageMPR_ReadImage( DicomSeries ) }
+
+// -------------------------------------------------------------------------
+void ImageMPR::
+_aOpenSegmentation( )
+{
+  try
+  {
+    std::string parent = this->m_UI->MPR->GetMainImage( );
+    std::string name = this->m_Plugins.ReadImage( parent );
+    if( name == "" )
+      return;
+    TImage* image = this->m_Plugins.GetData< TImage >( name );
+    this->m_UI->MPR->AddData( image, name, parent );
+    this->m_UI->MPR->ShowData( name );
+  }
+  catch( std::exception& err )
   {
     QMessageBox::critical(
       this,
-      tr( "Ignoring plugin" ),
-      tr( fname.c_str( ) )
+      QMessageBox::tr( "Error reading image." ),
+      QMessageBox::tr( err.what( ) )
       );
-    this->m_Plugins.UnloadAll( );
-    return;
 
-  } // fi
+  } // yrt
+}
 
-  this->m_BaseClasses[ "ImageReader" ] =
-    "cpPlugins::Plugins::ImageReader";
-  this->m_BaseClasses[ "ImageSeriesReader" ] =
-    "cpPlugins::Plugins::ImageSeriesReader";
+// -------------------------------------------------------------------------
+void ImageMPR::
+_aOpenPolyData( )
+{
 }
 
 // -------------------------------------------------------------------------
 void ImageMPR::
-_triggered_actionOpenInputImage( )
+_aSaveImage( )
 {
-  // Show dialog and check if it was accepted
-  QFileDialog dialog( this );
-  dialog.setFileMode( QFileDialog::ExistingFiles );
-  dialog.setDirectory( tr( "." ) );
-  dialog.setNameFilter(
-    tr( "Medical image files (*.mhd *.bin *.dcm);;All files (*)" )
-    );
-  dialog.setDefaultSuffix( tr( "mhd" ) );
-  if( !( dialog.exec( ) ) )
+  this->m_Plugins.WriteDataObject( this->m_UI->MPR->GetSelectedData( ) );
+}
+
+// -------------------------------------------------------------------------
+void ImageMPR::
+_aSavePolyData( )
+{
+}
+
+// -------------------------------------------------------------------------
+void ImageMPR::
+_aUndo( )
+{
+}
+
+// -------------------------------------------------------------------------
+void ImageMPR::
+_aRedo( )
+{
+}
+
+// -------------------------------------------------------------------------
+void ImageMPR::
+_aLoadPlugins( )
+{
+  this->m_Plugins.DialogLoadPlugins( );
+  this->_AssociatePluginsToMenu( );
+}
+
+// -------------------------------------------------------------------------
+void ImageMPR::
+_aShowPlugins( )
+{
+}
+
+// -------------------------------------------------------------------------
+void ImageMPR::
+_execPlugin( )
+{
+  // Get filter's name
+  QAction* action = dynamic_cast< QAction* >( this->sender( ) );
+  if( action == NULL )
     return;
-  
-  if( this->m_InputImage != NULL )
-    delete this->m_InputImage;
-  this->m_InputImage = NULL;
-  unsigned int nFiles = dialog.selectedFiles( ).size( );
-  if( nFiles == 1 )
-  {
-    std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
+  std::string filter_name = action->text( ).toStdString( );
 
-    TPlugin* reader =
-      dynamic_cast< TPlugin* >(
-      this->m_Plugins.CreateObject( this->m_BaseClasses[ "ImageReader" ] )
-        );
+  // Activate filter
+  if( !( this->m_Plugins.ActivateFilter( filter_name ) ) )
+    return;
 
-    TParameters reader_params = reader->GetDefaultParameters( );
-    reader_params[ "FileName" ].second = fname;
-    reader_params[ "PixelType" ].second = "short";
-    reader_params[ "ImageDimension" ].second = "3";
-    reader_params[ "IsColorImage" ].second = "0";
-    reader->SetParameters( reader_params );
-    std::string err = reader->Update( );
+  // Get IO names
+  TPlugins::TStringContainer inputs;
+  this->m_Plugins.GetActiveFilterInputsNames( inputs );
 
-    if( err == "" )
-    {
-      this->m_InputImage =
-        dynamic_cast< TPluginImage* >( reader->GetOutput( 0 ) );
-      reader->DisconnectOutputs( );
-    }
-    else
-      QMessageBox::critical(
-        this,
-        tr( "Error reading single image" ),
-        tr( err.c_str( ) )
-        );
-    delete reader;
+  // Configure inputs
+  if( inputs.size( ) > 1 )
+  {
+    // TODO
   }
-  else if( nFiles > 1 )
+  else if( inputs.size( ) == 1 )
   {
-    /* TODO
-       if( this->m_ImageSeriesReaderClassName == "" )
-       {
-       QMessageBox::critical(
-       this,
-       tr( "No plugin to read an image series found!" ),
-       tr( "No plugin to read an image series found!" )
-       );
-       return( ret );
-
-       } // fi
-       std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
-       this->m_LastOpenedFile = fname;
-       return( ret );
-    */
+    this->m_ActiveFilterMainInput = this->m_UI->MPR->GetSelectedData( );
+    this->m_Plugins.ConnectInputInActiveFilter(
+      this->m_ActiveFilterMainInput, *( inputs.begin( ) )
+      );
 
   } // fi
 
-  if( this->m_InputImage != NULL )
-    this->m_MPR->SetImage( this->m_InputImage->GetVTKImageData( ) );
+  // Configure paramereters
+  auto dlg_res = this->m_Plugins.ConfigureActiveFilter( );
+  if( !dlg_res )
+  {
+    // Just deactivate filter, since it was canceled
+    this->m_Plugins.DeactivateFilter( );
+    return;
+  }
+  else
+  {
+    // Execute automatic filter and associate outputs
+    this->UpdateActualFilter( );
+    this->m_Plugins.DeactivateFilter( );
+
+  } // fi
 }
 
 // eof - $RCSfile$