-#include <cpPlugins/Interface/Plugins.h>
-
-#include <stdexcept>
-
-#include <cpPlugins/Interface/Image.h>
-#include <cpPlugins/Interface/Mesh.h>
-
-#ifdef cpPlugins_Interface_QT4
-
-#include <QApplication>
-#include <QFileDialog>
-#include <QMessageBox>
-#include <QWidget>
-
-#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
-
-#endif // cpPlugins_Interface_QT4
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-Plugins( )
- : m_Widget( NULL ),
- m_Application( NULL ),
- m_LastLoadedPlugin( "" ),
- m_ActiveFilter( NULL )
-{
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-~Plugins( )
-{
-}
-
-// -------------------------------------------------------------------------
-QWidget* cpPlugins::Interface::Plugins::
-GetWidget( )
-{
- return( this->m_Widget );
-}
-
-// -------------------------------------------------------------------------
-const QWidget* cpPlugins::Interface::Plugins::
-GetWidget( ) const
-{
- return( this->m_Widget );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-SetWidget( QWidget* widget )
-{
- this->m_Widget = widget;
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-BlockWidget( )
-{
-#ifdef cpPlugins_Interface_QT4
- if( this->m_Widget != NULL )
- {
- QApplication::setOverrideCursor( Qt::WaitCursor );
- this->m_Widget->setEnabled( false );
-
- } // fi
-#endif // cpPlugins_Interface_QT4
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-UnblockWidget( )
-{
-#ifdef cpPlugins_Interface_QT4
- if( this->m_Widget != NULL )
- {
- QApplication::restoreOverrideCursor( );
- this->m_Widget->setEnabled( true );
-
- } // fi
-#endif // cpPlugins_Interface_QT4
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-DialogLoadPlugins( )
-{
-#ifdef cpPlugins_Interface_QT4
- if( this->m_Widget != NULL )
- {
- QFileDialog dialog( this->m_Widget );
- dialog.setFileMode( QFileDialog::ExistingFile );
- dialog.setDirectory( this->m_LastLoadedPlugin.c_str( ) );
- dialog.setNameFilter( QFileDialog::tr( PLUGIN_REGEX ) );
- dialog.setDefaultSuffix( QFileDialog::tr( PLUGIN_EXT ) );
- if( !( dialog.exec( ) ) )
- return;
-
- std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
- if( !( this->LoadPlugins( fname ) ) )
- QMessageBox::critical(
- this->m_Widget,
- QMessageBox::tr( "Ignoring plugin" ),
- QMessageBox::tr( fname.c_str( ) )
- );
-
- } // fi
-#endif // cpPlugins_Interface_QT4
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::
-BaseApplication* cpPlugins::Interface::Plugins::
-GetApplication( )
-{
- return( this->m_Application );
-}
-
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::
-BaseApplication* cpPlugins::Interface::Plugins::
-GetApplication( ) const
-{
- return( this->m_Application );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-SetApplication( BaseApplication* a )
-{
- this->m_Application = a;
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-LoadPluginsPath( const std::string& path, bool r )
-{
- /* TODO
- this->BlockWidget( );
-
- // Load all plugins from given folder
- std::list< std::string > files =
- this->m_Interface.LoadFromFolder( path, r );
-
- // Update a simple track
- bool ret = false;
- if( files.size( ) > 0 )
- {
- for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
- {
- this->m_LoadedPlugins.insert( *fIt );
- this->m_LastLoadedPlugin = *fIt;
- ret = true;
-
- } // rof
- this->_UpdateLoadedPluginsInformation( );
-
- } // fi
-
- this->UnblockWidget( );
- return( ret );
- */
- return( false );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-LoadPlugins( const std::string& fname )
-{
- this->BlockWidget( );
-
- // Is it already loaded?
- bool ret = true;
- if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
- {
- // Was it succesfully loaded?
- ret = this->m_Interface.Load( fname );
-
- // Update a simple track
- if( ret )
- {
- this->m_LoadedPlugins.insert( fname );
- this->m_LastLoadedPlugin = fname;
- this->_UpdateLoadedPluginsInformation( );
-
- } // fi
-
- } // fi
-
- this->UnblockWidget( );
- return( ret );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-LoadPluginsConfigurationFile( const std::string& fname )
-{
- // Load file into a char buffer
- std::ifstream in(
- fname.c_str( ), std::ios::in | std::ios::binary | std::ios::ate
- );
- if( !in.is_open( ) )
- return( false );
-
- 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 );
- char line[ 4096 ];
- while( in_stream )
- {
- in_stream.getline( line, 4096 );
- this->LoadPlugins( line );
-
- } // elihw
- delete buffer;
-
- return( true );
-}
-
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TStringContainer& cpPlugins::Interface::Plugins::
-GetLoadedPlugins( ) const
-{
- return( this->m_LoadedPlugins );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-GetLoadedCategories( TStringContainer& categories ) const
-{
- categories.clear( );
- auto fIt = this->m_LoadedFilters.begin( );
- for( ; fIt != this->m_LoadedFilters.end( ); ++fIt )
- categories.insert( fIt->first );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-GetLoadedFilters( TStringContainer& filters ) const
-{
- filters.clear( );
- auto pIt = this->m_LoadedFilters.begin( );
- for( ; pIt != this->m_LoadedFilters.end( ); ++pIt )
- for( auto fIt = pIt->second.begin( ); fIt != pIt->second.end( ); ++fIt )
- filters.insert( *fIt );
-}
-
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TStringContainer& cpPlugins::Interface::Plugins::
-GetLoadedFilters( const std::string& category ) const
-{
- static const TStringContainer EMPTY;
- auto pIt = this->m_LoadedFilters.find( category );
- if( pIt != this->m_LoadedFilters.end( ) )
- return( pIt->second );
- else
- return( EMPTY );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-AddInteractor( vtkRenderWindowInteractor* interactor )
-{
- this->m_Interactors.insert( interactor );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-RemoveInteractor( vtkRenderWindowInteractor* interactor )
-{
- this->m_Interactors.erase( interactor );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-ClearInteractors( )
-{
- this->m_Interactors.clear( );
-}
-
-// -------------------------------------------------------------------------
-#define cpPlugins_Plugins_HasMacro( F ) \
- bool cpPlugins::Interface::Plugins:: \
- Has##F( ) const \
- { \
- return( this->m_IOFilters.find( #F ) != this->m_IOFilters.end( ) ); \
- }
-
-cpPlugins_Plugins_HasMacro( ImageReader );
-cpPlugins_Plugins_HasMacro( DicomSeriesReader );
-cpPlugins_Plugins_HasMacro( MeshReader );
-cpPlugins_Plugins_HasMacro( ImageWriter );
-cpPlugins_Plugins_HasMacro( MeshWriter );
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::Interface::Plugins::
-ReadImage( const std::string& fname, const std::string& parent )
-{
- std::vector< std::string > fnames( 1, fname );
- return( this->ReadImage( fnames, parent ) );
-}
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::Interface::Plugins::
-ReadImage(
- const std::vector< std::string >& fnames, const std::string& parent
- )
-{
- // Load source
- this->_ActivateIOFilter( "ImageReader" );
-
- // Configure reader
- TParameters* params = this->GetActiveFilterParameters( );
- params->ClearStringList( "FileNames" );
- for( auto nIt = fnames.begin( ); nIt != fnames.end( ); ++nIt )
- params->AddToStringList( "FileNames", *nIt );
-
- // Execute filter
- return( this->_ReadData( parent ) );
-}
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::Interface::Plugins::
-ReadImage( const std::string& parent )
-{
- // Load source
- this->_ActivateIOFilter( "ImageReader" );
-
- // Try to configure source
- if( !( this->ConfigureActiveFilter( ) ) )
- {
- this->DeactivateFilter( );
- return( "" );
-
- } // fi
-
- // Execute filter
- return( this->_ReadData( parent ) );
-}
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::Interface::Plugins::
-ReadDicomSeries( const std::string& parent )
-{
- // Load source
- this->_ActivateIOFilter( "DicomSeriesReader" );
-
- // Try to configure source
- if( !( this->ConfigureActiveFilter( ) ) )
- {
- this->DeactivateFilter( );
- return( "" );
-
- } // fi
-
- // Execute filter
- return( this->_ReadData( parent ) );
-}
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::Interface::Plugins::
-ReadMesh( const std::string& fname, const std::string& parent )
-{
- // Load source
- this->_ActivateIOFilter( "MeshReader" );
-
- // Configure reader
- TParameters* params = this->GetActiveFilterParameters( );
- params->SetString( "FileName", fname );
-
- // Execute filter
- return( this->_ReadData( parent ) );
-}
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::Interface::Plugins::
-ReadMesh( const std::string& parent )
-{
- // Load source
- this->_ActivateIOFilter( "MeshReader" );
-
- // Try to configure source
- if( !( this->ConfigureActiveFilter( ) ) )
- {
- this->DeactivateFilter( );
- return( "" );
-
- } // fi
-
- // Execute filter
- return( this->_ReadData( parent ) );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-WriteDataObject( const std::string& fname, const std::string& name )
-{
- typedef cpPlugins::Interface::Image _TImage;
- typedef cpPlugins::Interface::Mesh _TMesh;
-
- // Activate sink
- TDataObject* obj = this->GetData< TDataObject >( name );
- if( dynamic_cast< cpPlugins::Interface::Image* >( obj ) != NULL )
- this->_ActivateIOFilter( "ImageWriter" );
- else if( dynamic_cast< cpPlugins::Interface::Mesh* >( obj ) != NULL )
- this->_ActivateIOFilter( "MeshWriter" );
- else
- {
- this->DeactivateFilter( );
- return( false );
-
- } // fi
-
- // Configure writer
- TParameters* params = this->GetActiveFilterParameters( );
- params->SetString( "FileName", fname );
-
- // Execute filter
- return( this->_WriteData( name ) );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-WriteDataObject( const std::string& name )
-{
- typedef cpPlugins::Interface::Image _TImage;
- typedef cpPlugins::Interface::Mesh _TMesh;
-
- // Activate sink
- TDataObject* obj = this->GetData< TDataObject >( name );
- if( dynamic_cast< cpPlugins::Interface::Image* >( obj ) != NULL )
- this->_ActivateIOFilter( "ImageWriter" );
- else if( dynamic_cast< cpPlugins::Interface::Mesh* >( obj ) != NULL )
- this->_ActivateIOFilter( "MeshWriter" );
- else
- {
- this->DeactivateFilter( );
- return( false );
-
- } // fi
-
- // Try to configure source
- if( !( this->ConfigureActiveFilter( ) ) )
- {
- this->DeactivateFilter( );
- return( false );
-
- } // fi
-
- // Execute filter
- return( this->_WriteData( name ) );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-ClearDataObjects( )
-{
- this->m_DataObjects.clear( );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-DeleteDataObject( const std::string& name )
-{
- // Find and delete object
- auto oIt = this->m_DataObjects.find( name );
- if( oIt == this->m_DataObjects.end( ) )
- return;
- this->m_DataObjects.erase( oIt );
-
- // Delete children
- TStringContainer children;
- this->GetChildren( children, name );
- auto cIt = children.begin( );
- for( ; cIt != children.end( ); ++cIt )
- this->DeleteDataObject( *cIt );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-GetDataObjects( TStringContainer& names )
-{
- names.clear( );
- auto oIt = this->m_DataObjects.begin( );
- for( ; oIt != this->m_DataObjects.end( ); ++oIt )
- names.insert( oIt->first );
-}
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::Interface::Plugins::
-GetParent( const std::string& name ) const
-{
- // Find and delete object
- auto oIt = this->m_DataObjects.find( name );
- if( oIt != this->m_DataObjects.end( ) )
- return( oIt->second.first );
- else
- return( "" );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-GetChildren( TStringContainer& names, const std::string& name ) const
-{
- names.clear( );
- auto oIt = this->m_DataObjects.begin( );
- for( ; oIt != this->m_DataObjects.end( ); ++oIt )
- if( oIt->second.first == name )
- names.insert( oIt->first );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-GetRoots( TStringContainer& names ) const
-{
- this->GetChildren( names, "" );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-ActivateFilter( const std::string& name )
-{
- this->m_ActiveFilter = this->m_Interface.CreateObject( name );
- if( this->m_ActiveFilter.IsNotNull( ) )
- {
- this->m_ActiveFilter->SetPlugins( this );
- auto iIt = this->m_Interactors.begin( );
- for( ; iIt != this->m_Interactors.end( ); ++iIt )
- this->m_ActiveFilter->AddInteractor( *iIt );
- return( true );
- }
- else
- return( false );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-DeactivateFilter( )
-{
- if( this->m_ActiveFilter.IsNotNull( ) )
- this->m_ActiveFilter->DisconnectOutputs( );
- this->m_ActiveFilter = NULL;
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-HasActiveFilter( ) const
-{
- return( this->m_ActiveFilter.IsNotNull( ) );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-IsActiveFilterInteractive( ) const
-{
- if( this->m_ActiveFilter.IsNotNull( ) )
- return( this->m_ActiveFilter->GetInteractive( ) );
- else
- return( false );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-GetActiveFilterInputsNames( TStringContainer& names ) const
-{
- if( this->m_ActiveFilter.IsNotNull( ) )
- this->m_ActiveFilter->GetInputsNames( names );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-GetActiveFilterOutputsNames( TStringContainer& names ) const
-{
- if( this->m_ActiveFilter.IsNotNull( ) )
- this->m_ActiveFilter->GetOutputsNames( names );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-ConnectInputInActiveFilter(
- const std::string& object_name, const std::string& input_name
- )
-{
- if( this->m_ActiveFilter.IsNotNull( ) )
- {
- TDataObject* dobj = this->GetData< TDataObject >( object_name );
- if( dobj != NULL )
- {
- this->m_ActiveFilter->SetInput( input_name, dobj );
- return( true );
-
- } // fi
-
- } // fi
- return( false );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-SetOutputNameInActiveFilter(
- const std::string& new_object_name, const std::string& output_name
- )
-{
- if( this->m_ActiveFilter.IsNotNull( ) )
- return(
- this->m_ActiveFilter->SetOutputObjectName(
- new_object_name, output_name
- )
- );
- else
- return( false );
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetActiveFilterParameters( )
-{
- if( this->m_ActiveFilter.IsNotNull( ) )
- return( this->m_ActiveFilter->GetParameters( ) );
- else
- return( NULL );
-}
-
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetActiveFilterParameters( ) const
-{
- if( this->m_ActiveFilter.IsNotNull( ) )
- return( this->m_ActiveFilter->GetParameters( ) );
- else
- return( NULL );
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-TProcessObject::DialogResult cpPlugins::Interface::Plugins::
-ConfigureActiveFilter( )
-{
- if( this->m_ActiveFilter.IsNotNull( ) )
- return( this->m_ActiveFilter->ExecConfigurationDialog( this->m_Widget ) );
- else
- return( false );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-UpdateActiveFilter( TStringContainer& outputs, const std::string& parent )
-{
- if( this->m_ActiveFilter.IsNull( ) )
- return( false );
-
- // Execute filter
- this->BlockWidget( );
- std::string err = this->m_ActiveFilter->Update( );
- this->UnblockWidget( );
-
- // Associate outputs
- outputs.clear( );
- if( err == "" )
- {
- std::set< std::string > names;
- this->m_ActiveFilter->GetOutputsNames( names );
- for( auto oIt = names.begin( ); oIt != names.end( ); ++oIt )
- {
- TDataObject* dobj =
- this->m_ActiveFilter->GetOutput< TDataObject >( *oIt );
-
- if( std::string( dobj->GetName( ) ) == std::string( "" ) )
- dobj->SetName(
- this->m_ActiveFilter->GetName( ) + std::string( "_" ) + *oIt
- );
- this->_InsertNewData( dobj, parent );
- outputs.insert( dobj->GetName( ) );
-
- } // rof
- // this->m_ActiveFilter->DisconnectOutputs( );
- return( true );
- }
- else
- {
-#ifdef cpPlugins_Interface_QT4
- if( this->m_Widget != NULL )
- QMessageBox::critical(
- this->m_Widget,
- QMessageBox::tr( "Error reading image." ),
- QMessageBox::tr( err.c_str( ) )
- );
- else
- throw std::runtime_error(
- std::string( "Error reading image: " ) + err
- );
-#else // cpPlugins_Interface_QT4
- throw std::runtime_error(
- std::string( "Error reading image: " ) + err
- );
-#endif // cpPlugins_Interface_QT4
- return( false );
-
- } // fi
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-_UpdateLoadedPluginsInformation( )
-{
- this->m_LoadedFilters.clear( );
- const TInterface::TClasses& cls = this->m_Interface.GetClasses( );
- for( auto i = cls.begin( ); i != cls.end( ); ++i )
- {
- TProcessObject::Pointer o = this->m_Interface.CreateObject( i->first );
- std::string name = o->GetClassName( );
- std::string category = o->GetClassCategory( );
-
- if(
- category == "ImageReader" ||
- category == "ImageWriter" ||
- category == "MeshReader" ||
- category == "MeshWriter" ||
- category == "DicomSeriesReader"
- )
- this->m_IOFilters[ category ] = o;
- else
- this->m_LoadedFilters[ category ].insert( name );
-
- } // rof
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-_ActivateIOFilter( const std::string& filter )
-{
- // Activate reader
- auto fIt = this->m_IOFilters.find( filter );
- if( fIt != this->m_IOFilters.end( ) )
- {
- this->m_ActiveFilter = fIt->second;
- return( true );
- }
- else
- return( false );
-}
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::Interface::Plugins::
-_ReadData( const std::string& parent )
-{
- if( !( this->HasActiveFilter( ) ) )
- return( "" );
-
- TStringContainer outputs;
- if( this->UpdateActiveFilter( outputs, parent ) )
- return( *( outputs.begin( ) ) );
- else
- return( "" );
-}
-
-// ------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-_WriteData( const std::string& name )
-{
- if( !( this->HasActiveFilter( ) ) )
- return( false );
-
- TStringContainer inputs;
- this->GetActiveFilterInputsNames( inputs );
- bool r = true;
- for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
- r &= this->ConnectInputInActiveFilter( name, *iIt );
-
- if( r )
- {
- TStringContainer outputs;
- return( this->UpdateActiveFilter( outputs, "" ) );
- }
- else
- return( false );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-_InsertNewData( TDataObject* dobj, const std::string& parent )
-{
- if( parent != "" )
- {
- auto oIt = this->m_DataObjects.find( parent );
- if( oIt == this->m_DataObjects.end( ) )
- return( false );
-
- } // fi
- this->m_DataObjects[ dobj->GetName( ) ] = _TTreeNode( parent, dobj );
- return( true );
-}
-
-// eof - $RCSfile$