]> Creatis software - cpPlugins.git/blobdiff - appli/PipelineEditor/PipelineEditor.cxx
..
[cpPlugins.git] / appli / PipelineEditor / PipelineEditor.cxx
index 2f7d7eabee3d7f4adf90e6a7fc8241dc7cf40901..f49c2a43ad05d1f542d7da7e357c017944687740 100644 (file)
-#include "PipelineEditor.h"
-#include "ui_PipelineEditor.h"
-
-#include <cpPipelineEditor/Editor.h>
-
-#include <QFileDialog>
-#include <QMessageBox>
+#include <PipelineEditor.h>
+#include <ui_PipelineEditor.h>
 
+#include <cpPlugins/Utility.h>
+#include <cpExtensions/QT/SimpleMPRWidget.h>
+#include <cpExtensions/QT/ImageWidget.h>
+#include <cpExtensions/QT/ActorsWidgetInterface.h>
 #include <vtkImageData.h>
 #include <vtkPolyData.h>
 
-#include <cpPlugins/DataObject.h>
-
-// -------------------------------------------------------------------------
-#define PipelineEditor_ConnectAction( ACTION )          \
-  this->connect(                                        \
-    this->m_UI->Action##ACTION, SIGNAL( triggered( ) ), \
-    this, SLOT( _Action##ACTION( ) )                    \
-    )
-
-// -------------------------------------------------------------------------
-#define PipelineEditor_ConnectButton( BUTTON )          \
-  this->connect(                                        \
-    this->m_UI->Button##BUTTON, SIGNAL( clicked( ) ),   \
-    this, SLOT( _Button##BUTTON( ) )                    \
-    )
-
-// -------------------------------------------------------------------------
-bool PipelineEditor_Blocker::
-eventFilter( QObject* obj, QEvent* event )
-{
-  return( true ); // -> Block all events
-  /* NOTE: 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 ) );
-  */
-}
-
 // -------------------------------------------------------------------------
 PipelineEditor::
-PipelineEditor( int argc, char* argv[], QApplication* app, QWidget* parent )
-  : QMainWindow( parent ),
-    m_UI( new Ui::PipelineEditor ),
-    m_Application( app ),
-    m_Workspace( NULL ),
-    m_PluginsPath( "." )
+PipelineEditor( int argc, char* argv[], QWidget* parent )
+  : Superclass( argc, argv, parent ),
+    m_UI( new Ui::PipelineEditor )
 {
+  // Basic configuration
+  this->m_BaseWindowTitle = "PipelineEditor - ";
   this->m_UI->setupUi( this );
-
-  // Prepare plugins interface
-  QFileInfo info( argv[ 0 ] );
-  if( info.exists( ) )
-  {
-    if( !( this->m_Interface.LoadConfiguration( cpPlugins_CONFIG_FILE ) ) )
-    {
-      this->m_PluginsPath = info.canonicalPath( ).toStdString( );
-      this->_LoadPluginsFromPath( this->m_PluginsPath );
-    }
-    /*
-      else
-      this->_UpdateLoadedPlugins( );
-    */
-
-  } // fi
-  QDir exec_dir( "." );
-  if( exec_dir.exists( ) )
-    this->_LoadPluginsFromPath( exec_dir.canonicalPath( ).toStdString( ) );
-
-  /* TODO
-     this->m_Interface = new cpPlugins::Interface( );
-     this->m_PluginsPath = info.canonicalPath( ).toStdString( );
-     if( !( this->m_Interface->LoadDefaultConfiguration( this->m_PluginsPath ) ) )
-     if( this->m_Interface->LoadFromFolder( this->m_PluginsPath, false ) != "" )
-     if( !( this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath ) ) )
-     QMessageBox::critical(
-     this,
-     "Error creating default plugins configuration",
-     "Could not save default plugins configuration"
-     );
-     this->_UpdateLoadedPlugins( );
-
-     } // fi
-  */
-
-  // Create an empty workspace
-  this->m_Workspace = new cpPlugins::Workspace( );
-  this->m_Workspace->SetInterface( &( this->m_Interface ) );
-  this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
-  this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
-
-  // Connect actions to slots
-  PipelineEditor_ConnectButton( LoadPluginsFile );
-  PipelineEditor_ConnectButton( LoadPluginsPath );
-  PipelineEditor_ConnectAction( OpenWorkspace );
-  PipelineEditor_ConnectAction( SaveWorkspace );
-  this->connect(
-    this->m_UI->Canvas->editor( ),
-    SIGNAL( execFilter( const std::string& ) ),
-    this,
-    SLOT( _ExecFilter( const std::string& ) )
-    );
-  this->connect(
-    this->m_UI->Canvas->editor( ),
-    SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
-    this,
-    SLOT( _ShowFilterOutput( const std::string&, const std::string& ) )
+  this->setCanvas( this->m_UI->Canvas );
+  this->setNavigator( this->m_UI->Navigator );
+  this->m_UI->Navigator->Update( );
+
+  // Connect slots <-> signals
+  cpBaseQtApplication_ConnectAction( actionLoadDirectory, _loadPluginsFromPath );
+  cpBaseQtApplication_ConnectAction( actionLoadLibrary, _loadPlugins );
+  cpBaseQtApplication_ConnectAction( actionOpenWorkspace, _loadWorkspace );
+  cpBaseQtApplication_ConnectAction( actionSaveWorkspace, _saveWorkspace );
+  cpBaseQtApplication_ConnectAction( actionSaveWorkspaceAs, _saveWorkspace );
+  cpBaseQtApplication_ConnectAction( actionActorsProperties, _actorsProperties );
+  this->m_UI->Canvas->connectOutputPortSlot(
+    this, SLOT( _slotView( const std::string&, bool ) )
     );
+
+  // Load command-line given workspace (if any)
+  if( argc > 1 )
+    this->_loadWorkspace( argv[ 1 ] );
 }
 
 // -------------------------------------------------------------------------
 PipelineEditor::
 ~PipelineEditor( )
 {
-  if( this->m_Workspace != NULL )
-    delete this->m_Workspace;
   delete this->m_UI;
 }
 
 // -------------------------------------------------------------------------
 void PipelineEditor::
-_LoadPluginsFromPath( const std::string& path )
+_slotView( const std::string& name, bool show )
 {
-  QDir dir( path.c_str( ) );
-  std::stringstream filters_str;
-  filters_str << "*." << cpPlugins_PLUGIN_EXT;
-  QStringList filters;
-  filters << filters_str.str( ).c_str( );
-  auto files = dir.entryList( filters );
-  for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
-  {
-    try
-    {
-      this->m_Interface.LoadPluginFile(
-        ( dir.absolutePath( ) + QDir::separator( ) + *fIt ).toStdString( )
-        );
-    }
-    catch( std::exception& err )
-    {
-      // Just ignore un-loadable libraries
-    } // yrt
-
-  } // rof
-  this->_UpdateLoadedPlugins( );
-}
-
-// -------------------------------------------------------------------------
-void PipelineEditor::
-_UpdateLoadedPlugins( )
-{
-  this->_Block( );
-  auto filters = this->m_Interface.GetFilters( );
-  if( filters.size( ) == 0 )
-  {
-    QMessageBox::critical(
-      this,
-      "Error loading default plugins",
-      "No plugins loaded: remember to load some!!!"
-      );
-    this->_UnBlock( );
-    return;
-
-  } // fi
-
-  for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
-  {
-    // Create or get category
-    QList< QTreeWidgetItem* > cat_items =
-      this->m_UI->LoadedPlugins->findItems(
-        cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
-        );
-    QTreeWidgetItem* cat = NULL;
-    if( cat_items.size( ) == 0 )
-    {
-      cat = new QTreeWidgetItem(
-        ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
-        );
-      this->m_UI->LoadedPlugins->addTopLevelItem( cat );
-    }
-    else
-      cat = cat_items[ 0 ];
-
-    // Create filters
-    auto fIt = cIt->second.begin( );
-    for( ; fIt != cIt->second.end( ); ++fIt )
-    {
-      QList< QTreeWidgetItem* > filter_items =
-        this->m_UI->LoadedPlugins->findItems(
-          fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
-          );
-      auto fiIt = filter_items.begin( );
-      auto found_fiIt = filter_items.end( );
-      for( ; fiIt != filter_items.end( ); ++fiIt )
-        if( ( *fiIt )->parent( ) == cat )
-          found_fiIt = fiIt;
-
-      // Add filter
-      if( found_fiIt == filter_items.end( ) )
-        QTreeWidgetItem* filter = new QTreeWidgetItem(
-          cat, QStringList( fIt->c_str( ) )
-          );
-
-    } // rof
-
-  } // rof
-  this->_UnBlock( );
-  this->m_Interface.SaveConfiguration( cpPlugins_CONFIG_FILE );
-}
-
-// -------------------------------------------------------------------------
-void PipelineEditor::
-_Block( )
-{
-  this->m_Application->setOverrideCursor( Qt::WaitCursor );
-  this->m_Application->installEventFilter( &( this->m_Blocker ) );
-}
-
-// -------------------------------------------------------------------------
-void PipelineEditor::
-_UnBlock( )
-{
-  while( this->m_Application->overrideCursor( ) )
-    this->m_Application->restoreOverrideCursor( );
-  this->m_Application->removeEventFilter( &( this->m_Blocker ) );
-}
-
-// -------------------------------------------------------------------------
-void PipelineEditor::
-_ButtonLoadPluginsFile( )
-{
-  QFileDialog dlg( this );
-  dlg.setFileMode( QFileDialog::ExistingFiles );
-  dlg.setDirectory( "." );
-
-  std::stringstream name_filter;
-  std::string suffix = std::string( cpPlugins_PLUGIN_EXT );
-  name_filter
-    << "Plugins file (*." << cpPlugins_PLUGIN_EXT << ");;All files (*)";
-  dlg.setNameFilter( name_filter.str( ).c_str( ) );
-  dlg.setDefaultSuffix( suffix.c_str( ) );
-
-  if( !( dlg.exec( ) ) )
-    return;
-
-  // Read
-  QStringList names = dlg.selectedFiles( );
-  std::stringstream err_str;
-  for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
-  {
-    try
-    {
-      this->m_Interface.LoadPluginFile( qIt->toStdString( ) );
-    }
-    catch( std::exception& err )
-    {
-      err_str << err.what( ) << std::endl;
-
-    } // yrt
-
-  } // rof
+  /* TODO
+     typedef cpExtensions::QT::ActorsWidgetInterface _TViewer;
+
+     // Get filter parameters
+     std::vector< std::string > tokens;
+     cpPlugins::Tokenize( tokens, name, "@" );
+     if( tokens.size( ) != 2 )
+     return;
+     auto filter_name = tokens[ 1 ];
+     auto output_name = tokens[ 0 ];
+
+     // Process data
+     if( show )
+     {
+     try
+     {
+     auto ws = this->workspace( this->m_ActiveWS );
+     auto filter = ws->GetFilter( filter_name );
+     cpBaseQtApplication_Execute( filter->Update( ) );
+     auto image = filter->GetOutputData< vtkImageData >( output_name );
+     auto mesh = filter->GetOutputData< vtkPolyData >( output_name );
+     _TViewer* viewer = NULL;
+     if( image != NULL )
+     {
+     int dim = image->GetDataDimension( );
+     if( dim == 2 )
+     viewer =
+     this->_configureViewer< cpExtensions::QT::ImageWidget >(
+     this->m_UI->Viewer
+     );
+     else if( dim == 3 )
+     viewer =
+     this->_configureViewer< cpExtensions::QT::SimpleMPRWidget >(
+     this->m_UI->Viewer
+     );
+     }
+     else if( mesh != NULL )
+     {
+     viewer =
+     this->_configureViewer< cpExtensions::QT::SimpleMPRWidget >(
+     this->m_UI->Viewer
+     );
 
-  // Show an error message
-  std::string err = err_str.str( );
-  if( err.size( ) > 0 )
-    QMessageBox::critical(
-      this,
-      "Error loading plugins",
-      err.c_str( )
-      );
+     } // fi
+     if(
+     dynamic_cast< QWidget* >( viewer ) !=
+     dynamic_cast< QWidget* >( this->m_UI->Viewer )
+     )
+     {
+     delete this->m_UI->Viewer;
+     this->m_UI->Viewer = dynamic_cast< QWidget* >( viewer );
+     this->m_UI->MainSplitter->insertWidget( 0, this->m_UI->Viewer );
+     this->setViewer( viewer );
 
-  // Update view
-  // TODO: this->m_Interface.SaveDefaultConfiguration( this->m_PluginsPath );
-  this->_UpdateLoadedPlugins( );
-}
+     } // fi
+     if( image != NULL )
+     {
+     this->m_Blocker.block( );
+     auto mpr = dynamic_cast< cpExtensions::QT::SimpleMPRWidget* >( viewer );
+     auto imv = dynamic_cast< cpExtensions::QT::ImageWidget* >( viewer );
+     if( mpr != NULL )
+     mpr->SetImage( image, name );
+     else if( imv != NULL )
+     imv->SetImage( image, name );
+     this->m_Blocker.unblock( );
+     }
+     else if( mesh != NULL )
+     {
+     this->m_Blocker.block( );
+     auto mpr = dynamic_cast< cpExtensions::QT::SimpleMPRWidget* >( viewer );
+     if( mpr != NULL )
+     mpr->Add( mesh, name );
+     this->m_Blocker.unblock( );
 
-// -------------------------------------------------------------------------
-void PipelineEditor::
-_ButtonLoadPluginsPath( )
-{
-  /*
-    QFileDialog dlg( this );
-    dlg.setFileMode( QFileDialog::DirectoryOnly );
-    dlg.setDirectory( "." );
-    if( !( dlg.exec( ) ) )
-    return;
+     } // fi
+     }
+     catch( std::exception& err )
+     {
+     QMessageBox::critical(
+     NULL,
+     QMessageBox::tr( "Error showing data" ),
+     QMessageBox::tr( err.what( ) )
+     );
 
-    // Read
-    std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
-    std::string err = this->m_Interface->LoadFromFolder( dir, false );
-    if( err != "" )
-    QMessageBox::critical(
-    this,
-    "Error loading plugins directory",
-    err.c_str( )
-    );
+     } // yrt
 
-    // Update view
-    this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath );
-    this->_UpdateLoadedPlugins( );
+     } // fi
   */
 }
 
 // -------------------------------------------------------------------------
-void PipelineEditor::
-_ActionOpenWorkspace( )
-{
-  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;
-
-  std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
-  if( this->m_Workspace != NULL )
-    delete this->m_Workspace;
-  this->m_Workspace = new cpPlugins::Workspace( );
-  this->m_Workspace->SetInterface( &( this->m_Interface ) );
-  this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
-  std::string err = this->m_Workspace->LoadWorkspace( fname );
-  if( err != "" )
-  {
-    delete this->m_Workspace;
-    this->m_Workspace = NULL;
-    QMessageBox::critical(
-      this,
-      QMessageBox::tr( "Error loading workspace" ),
-      QMessageBox::tr( err.c_str( ) )
-      );
-  }
-  else
-    this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
-}
-
-// -------------------------------------------------------------------------
-void PipelineEditor::
-_ActionSaveWorkspace( )
-{
-  if( this->m_Workspace == NULL )
-    return;
-
-  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( ) ) )
-    return;
-  std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
-
-  std::string err = this->m_Workspace->SaveWorkspace( fname );
-  if( err != "" )
-    QMessageBox::critical(
-      this,
-      QMessageBox::tr( "Error saving workspace" ),
-      QMessageBox::tr( err.c_str( ) )
-      );
-}
-
-
-// -------------------------------------------------------------------------
-void PipelineEditor::
-_ExecFilter( const std::string& filter_name )
-{
-  if( this->m_Workspace != NULL )
-  {
-    // Update filter, if needed
-    this->_Block( );
-    std::string err = this->m_Workspace->Execute( filter_name );
-    this->_UnBlock( );
-    if( err != "" )
-      QMessageBox::critical(
-        this,
-        QMessageBox::tr( "Error executing filter" ),
-        QMessageBox::tr( err.c_str( ) )
-        );
-
-  } // fi
-}
-
-// -------------------------------------------------------------------------
-void PipelineEditor::
-_ShowFilterOutput(
-  const std::string& filter_name, const std::string& output_name
-  )
-{
-  typedef cpPlugins::DataObject _TDataObject;
-
-  // Update filter, if needed
-  this->_ExecFilter( filter_name );
-
-  // Get output
-  auto filter = this->m_Workspace->GetFilter( filter_name );
-  if( filter != NULL )
-  {
-    auto output = filter->GetOutputData( output_name );
-    if( output != NULL )
-    {
-      std::string data_name = output_name + "@" + filter_name;
-      auto idata = output->GetVTK< vtkImageData >( );
-      auto mdata = output->GetVTK< vtkPolyData >( );
-      if( idata != NULL )
-      {
-        if( this->m_UI->Viewer->AddData( idata, data_name, "" ) )
-        {
-          if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
-            this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
-          else
-            this->m_UI->Viewer->SetMainImage( data_name );
-          this->_Block( );
-          this->m_UI->Viewer->ShowData( data_name );
-          this->_UnBlock( );
-
-        } // fi
-      }
-      else if( mdata != NULL )
-      {
-        if( this->m_UI->Viewer->AddData( mdata, data_name ) )
-        {
-          this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
-          this->_Block( );
-          this->m_UI->Viewer->ShowData( data_name );
-          this->_UnBlock( );
-
-        } // fi
-      }
-      else
-        QMessageBox::critical(
-          this,
-          QMessageBox::tr( "Error showing data" ),
-          QMessageBox::tr( "No known VTK conversion!" )
-          );
-
-      /* TODO
-         if( this->m_UI->Viewer->AddData( output, data_name, "" ) )
-         {
-         if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
-         this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
-         else
-         this->m_UI->Viewer->SetMainImage( data_name );
-         this->m_UI->Viewer->ShowData( data_name );
-         }
-         else
-      */
-
-    } // fi
-
-  } // fi
-}
+#include <cpBaseQtApplication/MainHelper.h>
+cpBaseQtApplication_Main( PipelineEditor );
+cpBaseQtApplication_MainComplement;
 
 // eof - $RCSfile$