]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/BaseMPRWindow.cxx
Parameters are now part of the pipeline update process
[cpPlugins.git] / lib / cpPlugins / Interface / BaseMPRWindow.cxx
index cc3f9b3a3c2f74c8ecf4631cf6e67b556156bef7..b43975547a21ccaca3bf3efcfd693dafceda39c8 100644 (file)
@@ -2,21 +2,42 @@
 
 #ifdef cpPlugins_Interface_QT4
 
-#include <cpPlugins/Interface/ui_BaseMPRWindow.h>
+#ifdef _WIN32
+#  define PLUGIN_PREFIX ""
+#  define PLUGIN_EXT "dll"
+#  define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
+#else // Linux
+#  define PLUGIN_PREFIX "lib"
+#  define PLUGIN_EXT "so"
+#  define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
+#endif // _WIN32
+
+#include <fstream>
+#include <sstream>
+
+#include <QFileDialog>
+#include <QMessageBox>
 
 // -------------------------------------------------------------------------
 cpPlugins::Interface::BaseMPRWindow::
 BaseMPRWindow( QWidget* parent )
-  : QMainWindow( parent ),
-    m_UI( new Ui::BaseMPRWindow )
+  : cpExtensions::QT::QuadSplitter( parent ),
+    m_LastLoadedPlugin( "." )
 {
-  // Create and associate renderers
+  // Configure splitter
+  this->m_XVTK = new QVTKWidget( this );
+  this->m_YVTK = new QVTKWidget( this );
+  this->m_ZVTK = new QVTKWidget( this );
+  this->m_WVTK = new QVTKWidget( this );
+  this->addWidgets( this->m_XVTK, this->m_YVTK, this->m_ZVTK, this->m_WVTK );
+
+  // Create and associate vtk renderers
   this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
   this->m_MPRObjects->SetRenderWindows(
-    this->m_UI->m_XVTK->GetRenderWindow( ),
-    this->m_UI->m_YVTK->GetRenderWindow( ),
-    this->m_UI->m_ZVTK->GetRenderWindow( ),
-    this->m_UI->m_WVTK->GetRenderWindow( )
+    this->m_XVTK->GetRenderWindow( ),
+    this->m_YVTK->GetRenderWindow( ),
+    this->m_ZVTK->GetRenderWindow( ),
+    this->m_WVTK->GetRenderWindow( )
     );
 }
 
@@ -24,42 +45,268 @@ BaseMPRWindow( QWidget* parent )
 cpPlugins::Interface::BaseMPRWindow::
 ~BaseMPRWindow( )
 {
-  if( this->m_UI != NULL )
-    delete this->m_UI;
+  if( this->m_WVTK != NULL )
+    delete this->m_WVTK;
+  if( this->m_ZVTK != NULL )
+    delete this->m_ZVTK;
+  if( this->m_YVTK != NULL )
+    delete this->m_YVTK;
+  if( this->m_XVTK != NULL )
+    delete this->m_XVTK;
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWindow::
+DialogLoadPlugins( )
+{
+  // Show dialog and check if it was accepted
+  QFileDialog dialog( this );
+  dialog.setFileMode( QFileDialog::ExistingFile );
+  dialog.setDirectory( this->m_LastLoadedPlugin.c_str( ) );
+  dialog.setNameFilter( tr( PLUGIN_REGEX ) );
+  dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
+  if( !( dialog.exec( ) ) )
+    return;
+
+  std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
+  if( !( this->LoadPlugins( fname ) ) )
+    QMessageBox::critical(
+      this, tr( "Ignoring plugin" ), tr( fname.c_str( ) )
+      );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWindow::
+AssociatePluginsToMenu( QMenu* menu, QObject* obj, const char* slot )
+{
+  std::map< std::string, std::set< std::string > >::const_iterator i;
+  std::set< std::string >::const_iterator j;
+
+  menu->clear( );
+  for( i = this->m_Filters.begin( ); i != this->m_Filters.end( ); i++ )
+  {
+    QMenu* newMenu = menu->addMenu( i->first.c_str( ) );
+    for( j = i->second.begin( ); j != i->second.end( ); ++j )
+    {
+      QAction* a = newMenu->addAction( j->c_str( ) );
+      QObject::connect( a, SIGNAL( triggered( ) ), obj, slot );
+
+    } // rof
+
+  } // rof
 }
 
 // -------------------------------------------------------------------------
 bool cpPlugins::Interface::BaseMPRWindow::
-_LoadPlugins( const std::string& fname )
+LoadPlugins( const std::string& fname )
 {
-  return( false );
+  this->Block( );
+
+  // Is it already loaded?
+  if( this->m_LoadedPlugins.find( fname ) != this->m_LoadedPlugins.end( ) )
+  {
+    this->Unblock( );
+    return( true );
+
+  } // fi
+
+  // Was it succesfully loaded?
+  if( !( this->m_Interface.Load( fname ) ) )
+  {
+    this->Unblock( );
+    return( false );
+
+  } // fi
+
+  // Update a simple track
+  this->m_LoadedPlugins.insert( fname );
+  this->m_LastLoadedPlugin = fname;
+  this->_UpdatePlugins( );
+
+  this->Unblock( );
+  return( true );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWindow::
+LoadPlugins( )
+{
+  // Load file into a char buffer
+  std::ifstream in(
+    "Plugins.cfg", std::ios::in | std::ios::binary | std::ios::ate
+    );
+  if( !in.is_open( ) )
+    return;
+  std::streampos size = in.tellg( );
+  char* buffer = new char[ size ];
+  in.seekg( 0, std::ios::beg );
+  in.read( buffer, size );
+  in.close( );
+
+  // Read stream
+  std::stringstream in_stream( buffer );
+  while( in_stream )
+  {
+    char line[ 2048 ];
+    in_stream.getline( line, 2048 );
+    this->LoadPlugins( line );
+
+  } // elihw
+
+  // Finish
+  delete buffer;
 }
 
 // -------------------------------------------------------------------------
 bool cpPlugins::Interface::BaseMPRWindow::
-_LoadImage( const std::string& fname )
+LoadImage( )
 {
-  return( false );
+  std::string msg = "";
+  bool ret = true;
+  if( this->m_ImageReader.IsNull( ) )
+  {
+    msg = "No valid image reader. Please load a valid plugin file.";
+    ret = false;
+
+  } // fi
+
+  if( this->m_ImageReader->ExecConfigurationDialog( this ) )
+  {
+    this->Block( );
+    msg = this->m_ImageReader->Update( );
+    if( msg == "" )
+    {
+      this->m_Images.push_back(
+        this->m_ImageReader->GetOutput< TImage >( 0 )
+        );
+      this->m_ImageReader->DisconnectOutputs( );
+      this->m_ImageReader->GetParameters( )->ClearStringList( "FileNames" );
+      vtkImageData* vtk_id =
+        this->m_Images.rbegin( )->GetPointer( )->GetVTK< vtkImageData >( );
+      if( vtk_id != NULL )
+      {
+        this->m_MPRObjects->AddImage( vtk_id );
+        /*
+          MementoState(m_state, this->m_Image);         
+          this->m_state++;
+        */
+      }
+      else
+        msg = "Read image does not have a valid VTK converter.";
+    }
+    else
+      ret = false;
+
+  } // fi
+
+  // Show errors and return
+  this->Unblock( );
+  if( msg != "" )
+    QMessageBox::critical(
+      this, tr( "Error reading image." ), tr( msg.c_str( ) )
+      );
+  return( ret );
 }
 
 // -------------------------------------------------------------------------
 bool cpPlugins::Interface::BaseMPRWindow::
-_LoadMesh( const std::string& fname )
+LoadMesh( )
 {
   return( false );
 }
 
-/*
-  TInterface m_Interface;
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWindow::
+ExecuteFilter( const std::string& name, int input_id, int output_id )
+{
+  TProcessObject::Pointer filter =
+    this->m_Interface.CreateProcessObject( name );
+  std::string category = filter->GetClassCategory( );
+  if( category == "ImageToBinaryImageFilter" )
+  {
+    if( input_id < this->m_Images.size( ) )
+    {
+      filter->SetInput( 0, this->m_Images[ input_id ] );
+      bool dlg_ok = filter->ExecConfigurationDialog( NULL );
+      if( !dlg_ok )
+        return;
 
-  TProcessObject::Pointer m_ImageReader;
-  TProcessObject::Pointer m_ImageWriter;
-  TProcessObject::Pointer m_MeshReader;
-  TProcessObject::Pointer m_MeshWriter;
+    } // fi
 
-  TImages m_Images;
-  TMeshes m_Meshes;
-*/
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWindow::
+ClearAll( )
+{
+  this->m_MPRObjects->ClearAll( );
+  this->m_Images.clear( );
+  this->m_Meshes.clear( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::BaseMPRWindow::
+_UpdatePlugins( )
+{
+  typedef TInterface::TClasses _C;
+
+  this->m_Filters.clear( );
+  _C& classes = this->m_Interface.GetClasses( );
+  for( _C::const_iterator i = classes.begin( ); i != classes.end( ); ++i )
+  {
+    TProcessObject::Pointer o =
+      this->m_Interface.CreateProcessObject( i->first );
+    std::string name = o->GetClassName( );
+    std::string category = o->GetClassCategory( );
+    if( category == "ImageReader" )
+      this->m_ImageReader = o;
+    else if( category == "ImageWriter" )
+      this->m_ImageWriter = o;
+    else if( category == "MeshReader" )
+      this->m_MeshReader = o;
+    else if( category == "MeshWriter" )
+      this->m_MeshWriter = o;
+    else
+      this->m_Filters[ category ].insert( name );
+
+    /*
+      if( category == "ImageReader" )
+      this->m_ImageReaderClass = name;
+      else if( category == "ImageWriter" )
+      this->m_ImageWriterClass = name;
+      else if( category == "MeshReader" )
+      this->m_MeshReaderClass = name;
+      else if( category == "MeshWriter" )
+      this->m_MeshWriterClass = name;
+      else if( category == "MeshToMeshFilter" )
+      {
+      if( name.find_last_of( "Cutter" ) != std::string::npos )
+      this->m_MeshCutterClass = name;
+      }
+      else if( category == "ImageToImageFilter" )
+      {
+      QAction* action =
+      this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
+      QObject::connect(
+      action, SIGNAL( triggered( ) ),
+      this, SLOT( _triggered_actionImageToImage( ) )
+      );
+      }
+      else if( category == "ImageToMeshFilter" )
+      {
+      QAction* action =
+      this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
+      QObject::connect(
+      action, SIGNAL( triggered( ) ),
+      this, SLOT( _triggered_actionImageToMesh( ) )
+      );
+
+      } // fi
+    */
+
+  } // rof
+}
 
 #endif // cpPlugins_Interface_QT4