]> Creatis software - cpPlugins.git/blobdiff - lib/cpBaseQtApplication/MainWindow.cxx
Cast image filter added. ROI filter modified.
[cpPlugins.git] / lib / cpBaseQtApplication / MainWindow.cxx
index 67a3844aaa3641fdf40cbef029ace9cce8fd1a2d..1bdca767b08be6428f5c5336d663c35d78a176d9 100644 (file)
@@ -1,82 +1,39 @@
 #include <cpBaseQtApplication/MainWindow.h>
+#include <cpBaseQtApplication/Plugins/Navigator.h>
+#include <cpBaseQtApplication/Pipeline/Canvas.h>
+#include <cpExtensions/QT/ConfigurationChooser.h>
+#include <cpExtensions/QT/ActorsWidgetInterface.h>
 
-#include <QApplication>
 #include <QDir>
 #include <QFileDialog>
-#include <QFileInfo>
-#include <QMessageBox>
-
-#include <cpExtensions/QT/SimpleMPRWidget.h>
-#include <cpExtensions/QT/ConfigurationChooser.h>
-
-#include <vtkDataSet.h>
-
-#include <cpBaseQtApplication/Editor.h>
-#include <cpBaseQtApplication/PathsDialog.h>
-#include <cpBaseQtApplication/PluginsNavigator.h>
-#include <cpBaseQtApplication/ActorPropertiesQDialog.h>
-
-/* TODO
-   #include <cpPlugins/Interface/Plugins.h>
-   #include <cpPlugins/BaseObjects/Widget.h>
-   #include <vtkImageData.h>
-   #include <vtkPolyData.h>
-   #include <vtkRenderer.h>
-   #include <QColorDialog>
-*/
-
-// -------------------------------------------------------------------------
-bool cpBaseQtApplication::MainWindow::_TBlocker::
-eventFilter( QObject* obj, QEvent* event )
-{
-  return( true ); // -> Block all events
-  /* NOTE: this should be the correct implementation:
-     switch( event->type( ) )
-     {
-     //list event you want to prevent here ...
-     case QEvent::KeyPress:
-     case QEvent::KeyRelease:
-     case QEvent::MouseButtonRelease:
-     case QEvent::MouseButtonPress:
-     case QEvent::MouseButtonDblClick:
-     //...
-     return( true );
-     } // hctiws
-     return( this->QObject::eventFilter( obj, event ) );
-  */
-}
+#include <QInputDialog>
 
 // -------------------------------------------------------------------------
 cpBaseQtApplication::MainWindow::
-MainWindow( int argc, char* argv[], QApplication* app, QWidget* parent )
+MainWindow(
+  int argc, char* argv[],
+  QWidget* parent
+  )
   : Superclass( parent ),
-    m_Application( app ),
+    m_LastSaveFileName( "" ),
+    m_BaseWindowTitle( "cpBaseQtApplication" ),
+    m_Canvas( NULL ),
     m_Navigator( NULL ),
-    m_Editor( NULL ),
-    m_MPR( NULL )
+    m_Viewer( NULL )
 {
-  // Use some evident paths
-  QFileInfo e_path( argv[ 0 ] );
-  QDir r_path( "." );
-  std::set< std::string > paths;
-  paths.insert( e_path.canonicalPath( ).toStdString( ) );
-  paths.insert( r_path.canonicalPath( ).toStdString( ) );
-  std::stringstream env;
-  for( auto p = paths.begin( ); p != paths.end( ); ++p )
-    env << *p << cpPlugins_ENV_SEPARATOR;
-
-  // Some variables
-  this->m_RunPath = r_path.canonicalPath( ).toStdString( );
-
-  // Get the plugins interface
+  this->m_RunPath = QDir( "." ).canonicalPath( ).toStdString( );
   this->m_Plugins = TPlugins::New( );
-  try { this->m_Plugins->AddEnvironments( env.str( ) ); } catch( ... ) { }
-  try { this->m_Plugins->OpenEnvironments( env.str( ) ); } catch( ... ) { }
-  try { this->m_Plugins->SaveEnvironments( this->m_RunPath ); } catch( ... ) { }
-  this->updateEnvironment( );
+  try
+  {
+    this->m_Plugins->GuessEnvironment( this->m_RunPath );
+    this->m_Plugins->GuessPlugins( );
+  }
+  catch( std::exception& err )
+  {
+    QMessageBox::critical( this, "Error guessing plugins.", err.what( ) );
 
-  // Create local workspace
-  this->m_Workspace = TWorkspace::New( );
+  } // yrt
+  this->_clearWorkspace( );
 }
 
 // -------------------------------------------------------------------------
@@ -86,228 +43,157 @@ cpBaseQtApplication::MainWindow::
 }
 
 // -------------------------------------------------------------------------
-void cpBaseQtApplication::MainWindow::
-updateEnvironment( )
+cpBaseQtApplication::MainWindow::
+TWorkspace* cpBaseQtApplication::MainWindow::
+workspace( )
 {
-  try
-  {
-    this->m_Plugins->LoadEnvironments( );
-    this->m_Plugins->GuessPlugins( );
-  }
-  catch( std::exception& err )
-  {
-    QMessageBox::critical(
-      this, "Error loading required libraries", err.what( )
-      );
-    return;
+  return( this->m_Workspace );
+}
 
-  } // yrt
+// -------------------------------------------------------------------------
+const cpBaseQtApplication::MainWindow::
+TWorkspace* cpBaseQtApplication::MainWindow::
+workspace( ) const
+{
+  return( this->m_Workspace );
 }
 
 // -------------------------------------------------------------------------
-void cpBaseQtApplication::MainWindow::
-updateFilter( const std::string& name )
+cpBaseQtApplication::Pipeline::Canvas*
+cpBaseQtApplication::MainWindow::
+canvas( )
 {
-  auto filter = this->m_Workspace->GetFilter( name );
-  if( filter != NULL )
-    cpBaseQtApplication_Execute( filter->Update( ) );
+  return( this->m_Canvas );
 }
 
 // -------------------------------------------------------------------------
-void cpBaseQtApplication::MainWindow::
-showData( const std::string& name, const std::string& port )
+const cpBaseQtApplication::Pipeline::Canvas*
+cpBaseQtApplication::MainWindow::
+canvas( ) const
 {
-  this->updateFilter( name );
-  auto filter = this->m_Workspace->GetFilter( name );
-  if( filter != NULL )
-    this->showData(
-      filter->GetOutput( port ), port + std::string( "@" ) + name
-      );
-  else
-    QMessageBox::critical(
-      this,
-      QMessageBox::tr( "Error showing data" ),
-      QMessageBox::tr( "Unknown filter." )
-      );
+  return( this->m_Canvas );
 }
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-showData( cpPlugins::BaseObjects::DataObject* data, const std::string& name )
+setCanvas( cpBaseQtApplication::Pipeline::Canvas* c )
 {
-  if( this->m_MPR == NULL || data == NULL )
-    return;
-
-  // Associate visual data
-  this->_block( );
-  bool success = this->m_MPR->Add( data->GetVTK< vtkDataSet >( ), name );
-  this->_unBlock( );
+  this->m_Canvas = c;
+  if( this->m_Canvas != NULL )
+    this->m_Canvas->setWorkspace( this->m_Workspace );
 
-  // Show data or show an error
-  if( success )
-    this->m_MPR->Render( );
-  else
-    QMessageBox::critical(
-      this,
-      QMessageBox::tr( "Error showing data" ),
-      QMessageBox::tr( "Unknown conversion to a \"vtkProp\" object." )
-      );
 }
 
 // -------------------------------------------------------------------------
-/* TODO
-   void cpBaseQtApplication::MainWindow::
-   hideData( const std::string& name, const std::string& port )
-   {
-   this->hideData( port + std::string( "@" ) + name );
-   }
-
-   // -------------------------------------------------------------------------
-   void cpBaseQtApplication::MainWindow::
-   hideData( const std::string& name )
-   {
-   }
-*/
+cpBaseQtApplication::Plugins::Navigator*
+cpBaseQtApplication::MainWindow::
+navigator( )
+{
+  return( this->m_Navigator );
+}
 
 // -------------------------------------------------------------------------
-void cpBaseQtApplication::MainWindow::
-dataProperties( const std::string& name, const std::string& port )
+const cpBaseQtApplication::Plugins::Navigator*
+cpBaseQtApplication::MainWindow::
+navigator( ) const
 {
-  this->dataProperties( port + std::string( "@" ) + name );
+  return( this->m_Navigator );
 }
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-dataProperties( const std::string& name )
+setNavigator( cpBaseQtApplication::Plugins::Navigator* n )
 {
-  if( this->m_MPR == NULL )
-    return;
-
-  auto props = this->m_MPR->GetViewProps( name );
-  if( props.size( ) > 0 )
-  {
-    this->_block( );
-    auto dlg = new cpBaseQtApplication::ActorPropertiesQDialog( NULL );
-    dlg->setProps( props );
-    dlg->addRenderWindow( this->m_MPR->GetXRenderWindow( ) );
-    dlg->addRenderWindow( this->m_MPR->GetYRenderWindow( ) );
-    dlg->addRenderWindow( this->m_MPR->GetZRenderWindow( ) );
-    dlg->addRenderWindow( this->m_MPR->GetWRenderWindow( ) );
-    this->_unBlock( );
-    dlg->exec( );
-
-  } // fi
+  this->m_Navigator = n;
 }
 
 // -------------------------------------------------------------------------
-void cpBaseQtApplication::MainWindow::
-_block( )
+cpExtensions::QT::ActorsWidgetInterface*
+cpBaseQtApplication::MainWindow::
+viewer( )
 {
-  if( this->m_Application != NULL )
-  {
-    this->m_Application->setOverrideCursor( Qt::WaitCursor );
-    this->m_Application->installEventFilter( &( this->m_Blocker ) );
-
-  } // fi
+  return( this->m_Viewer );
 }
 
 // -------------------------------------------------------------------------
-void cpBaseQtApplication::MainWindow::
-_unBlock( )
+const cpExtensions::QT::ActorsWidgetInterface*
+cpBaseQtApplication::MainWindow::
+viewer( ) const
 {
-  if( this->m_Application != NULL )
-  {
-    while( this->m_Application->overrideCursor( ) )
-      this->m_Application->restoreOverrideCursor( );
-    this->m_Application->removeEventFilter( &( this->m_Blocker ) );
-
-  } // fi
+  return( this->m_Viewer );
 }
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-_configure( PluginsNavigator* nav, TMPR* mpr, Editor* edt )
+setViewer( cpExtensions::QT::ActorsWidgetInterface* v )
 {
-  this->m_Navigator = nav;
-  this->m_MPR = mpr;
-  this->m_Editor = edt;
-  this->_updatePlugins( );
-  if( this->m_Editor != NULL )
-    this->m_Editor->setWorkspace( this->m_Workspace );
-
-  // Associate interactors
-  if( this->m_MPR != NULL )
+  this->m_Viewer = v;
+  if( this->m_Viewer != NULL )
   {
-    this->m_Workspace->AddInteractor( this->m_MPR->GetXInteractor( ) );
-    this->m_Workspace->AddInteractor( this->m_MPR->GetYInteractor( ) );
-    this->m_Workspace->AddInteractor( this->m_MPR->GetZInteractor( ) );
-    this->m_Workspace->AddInteractor( this->m_MPR->GetWInteractor( ) );
+    auto interactors = this->m_Viewer->GetInteractors( );
+    for( auto i : interactors )
+      this->m_Workspace->AddInteractor( i );
 
   } // fi
 }
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-_updatePlugins( )
-{
-  if( this->m_Navigator == NULL )
-    return;
-  this->_block( );
-  this->m_Navigator->Update( );
-  this->_unBlock( );
-}
-
-// -------------------------------------------------------------------------
-void cpBaseQtApplication::MainWindow::
-_loadWorkspace( const std::string& filename )
+_loadPlugins( const std::string& filename )
 {
   try
   {
-    this->m_Workspace->Load( filename );
+    this->m_Plugins->LoadPluginsFile( filename );
+    if( this->m_Navigator != NULL )
+      this->m_Navigator->Update( );
   }
   catch( std::exception& err )
   {
     QMessageBox::critical(
       this,
-      QMessageBox::tr( "Error loading workspace" ),
-      QMessageBox::tr( err.what( ) )
+      "Error loading plugins path",
+      err.what( )
       );
 
   } // yrt
-  if( this->m_Editor != NULL )
-    this->m_Editor->redrawWorkspace( );
 }
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-_loadWorkspace( )
+_loadPlugins( )
 {
   QFileDialog dlg( this );
-  dlg.setFileMode( QFileDialog::ExistingFile );
-  dlg.setDirectory( "." );
-  dlg.setNameFilter(
-    QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
-    );
-  dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
+  dlg.setFileMode( QFileDialog::ExistingFiles );
+
+  std::stringstream filter;
+  std::string suffix = std::string( cpPlugins_LIB_EXT );
+  filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
+  dlg.setNameFilter( filter.str( ).c_str( ) );
+  dlg.setDefaultSuffix( suffix.c_str( ) );
   if( !( dlg.exec( ) ) )
     return;
-  this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
+  QStringList names = dlg.selectedFiles( );
+  for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
+    this->_loadPlugins( qIt->toStdString( ) );
 }
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-_saveWorkspace( const std::string& filename )
+_loadPluginsFromPath( const std::string& path )
 {
   try
   {
-    this->m_Workspace->Save( filename );
+    this->m_Plugins->LoadPluginsDirectory( path );
+    this->m_Plugins->SaveEnvironment( this->m_RunPath );
+    if( this->m_Navigator != NULL )
+      this->m_Navigator->Update( );
   }
   catch( std::exception& err )
   {
     QMessageBox::critical(
       this,
-      QMessageBox::tr( "Error saving workspace" ),
-      QMessageBox::tr( err.what( ) )
+      "Error loading plugins path",
+      err.what( )
       );
 
   } // yrt
@@ -315,115 +201,85 @@ _saveWorkspace( const std::string& filename )
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-_saveWorkspace( )
+_loadPluginsFromPath( )
 {
-  QFileDialog dlg( this );
-  dlg.setFileMode( QFileDialog::AnyFile );
-  dlg.setDirectory( "." );
-  dlg.setAcceptMode( QFileDialog::AcceptSave );
-  dlg.setNameFilter(
-    QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
-    );
-  dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
-  if( !( dlg.exec( ) ) )
+  QFileDialog d( this );
+  d.setFileMode( QFileDialog::DirectoryOnly );
+  if( !( d.exec( ) ) )
     return;
-  this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
-}
-
-// -------------------------------------------------------------------------
-void cpBaseQtApplication::MainWindow::
-_showPlugins( )
-{
-  std::stringstream info;
-  this->m_Plugins->Print( info );
-  QMessageBox::information(
-    this,
-    "Loaded libraries and plugins information",
-    info.str( ).c_str( )
-    );
+  this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
 }
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-_addEnvironmentPaths( const std::string& envs )
+_clearWorkspace( )
 {
-  try { this->m_Plugins->AddEnvironments( envs ); } catch( ... ) { }
-  try { this->m_Plugins->SaveEnvironments( this->m_RunPath ); } catch( ... ) { }
-  this->updateEnvironment( );
-  this->_updatePlugins( );
-}
+  this->setWindowTitle( this->m_BaseWindowTitle.c_str( ) );
+  this->m_Workspace = TWorkspace::New( );
+  if( this->m_Canvas != NULL )
+  {
+    this->m_Canvas->clear( );
+    this->m_Canvas->setWorkspace( this->m_Workspace );
 
-// -------------------------------------------------------------------------
-void cpBaseQtApplication::MainWindow::
-_addEnvironmentPaths( )
-{
-  PathsDialog dlg( this );
-  dlg.addPaths( this->m_Plugins->GetPaths( ) );
-  if( dlg.exec( ) )
+  } // fi
+  if( this->m_Viewer != NULL )
   {
-    auto paths = dlg.getPaths( );
-    std::stringstream envs;
-    for( auto p = paths.begin( ); p != paths.end( ); ++p )
-      envs << *p << cpPlugins_ENV_SEPARATOR;
-    this->_addEnvironmentPaths( envs.str( ) );
+    // TODO: this->m_Viewer->clear( );
+    auto interactors = this->m_Viewer->GetInteractors( );
+    for( auto i : interactors )
+      this->m_Workspace->AddInteractor( i );
 
   } // fi
 }
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-_loadPlugins( const std::string& filename )
+_saveWorkspace( const std::string& fname )
 {
-  try
-  {
-    this->m_Plugins->LoadFile( filename );
-    this->_updatePlugins( );
-  }
-  catch( std::exception& err )
-  {
-    QMessageBox::critical(
-      this,
-      "Error loading plugins path",
-      err.what( )
-      );
-
-  } // yrt
+  this->m_LastSaveFileName = fname;
+  this->m_Workspace->Save( this->m_LastSaveFileName );
 }
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-_loadPlugins( )
+_saveWorkspace( )
 {
-  QFileDialog dlg( this );
-  dlg.setFileMode( QFileDialog::ExistingFiles );
-
-  std::stringstream filter;
-  std::string suffix = std::string( cpPlugins_LIB_EXT );
-  filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
-  dlg.setNameFilter( filter.str( ).c_str( ) );
-  dlg.setDefaultSuffix( suffix.c_str( ) );
-  if( !( dlg.exec( ) ) )
-    return;
-  QStringList names = dlg.selectedFiles( );
-  for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
-    this->_loadPlugins( qIt->toStdString( ) );
+  if( this->m_LastSaveFileName == "" )
+  {
+    QFileDialog dlg( this );
+    dlg.setFileMode( QFileDialog::AnyFile );
+    dlg.setDirectory( "." );
+    dlg.setAcceptMode( QFileDialog::AcceptSave );
+    dlg.setNameFilter(
+      QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
+      );
+    dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
+    dlg.setWindowTitle( "Saving workspace" );
+    if( dlg.exec( ) )
+      this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
+  }
+  else
+    this->_saveWorkspace( this->m_LastSaveFileName );
 }
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-_loadPluginsFromPath( const std::string& path )
+_loadWorkspace( const std::string& fname )
 {
   try
   {
-    this->m_Plugins->LoadDirectory( path );
-    this->_updatePlugins( );
+    this->_clearWorkspace( );
+    this->m_Workspace->Load( fname );
+    this->m_LastSaveFileName = "";
+    if( this->m_Canvas != NULL )
+      this->m_Canvas->setWorkspace( this->m_Workspace );
   }
   catch( std::exception& err )
   {
     QMessageBox::critical(
       this,
-      "Error loading plugins path",
-      err.what( )
+      QMessageBox::tr( "Error loading workspace" ),
+      QMessageBox::tr( err.what( ) )
       );
 
   } // yrt
@@ -431,37 +287,35 @@ _loadPluginsFromPath( const std::string& path )
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
-_loadPluginsFromPath( )
+_loadWorkspace( )
 {
-  QFileDialog d( this );
-  d.setFileMode( QFileDialog::DirectoryOnly );
-  if( !( d.exec( ) ) )
+  QFileDialog dlg( this );
+  dlg.setFileMode( QFileDialog::ExistingFile );
+  dlg.setDirectory( "." );
+  dlg.setNameFilter(
+    QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
+    );
+  dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
+  if( !( dlg.exec( ) ) )
     return;
-  this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
+  this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
 }
 
 // -------------------------------------------------------------------------
 void cpBaseQtApplication::MainWindow::
 _actorsProperties( )
 {
-  if( this->m_MPR != NULL )
+  auto data =
+    dynamic_cast< cpExtensions::QT::ActorsWidgetInterface* >(
+      this->m_Viewer
+      );
+  if( data != NULL )
   {
     auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
-    dlg->setData( this->m_MPR );
+    dlg->setData( data );
     dlg->exec( );
 
   } // fi
 }
 
-// -------------------------------------------------------------------------
-/* TODO
-   void cpBaseQtApplication::MainWindow::
-   _ClearWorkspace( )
-   {
-   if( this->m_Editor != NULL )
-   this->m_Editor->clear( );
-   this->m_Workspace->Clear( );
-   }
-*/
-
 // eof - $RCSfile$