From b099c70da465fb2066928a0977899a40bdfff8e3 Mon Sep 17 00:00:00 2001 From: Leonardo Florez-Valencia Date: Wed, 4 Nov 2015 19:23:52 -0500 Subject: [PATCH] MVC pattern finished --- appli/ImageMPR/ImageMPR.cxx | 293 +++--- appli/ImageMPR/ImageMPR.h | 42 +- lib/cpPlugins/Interface/BaseMPRWidget.cxx | 36 +- lib/cpPlugins/Interface/BaseMPRWidget.h | 18 +- .../Interface/BasePluginsApplication.h | 27 + lib/cpPlugins/Interface/Parameters.cxx | 8 + lib/cpPlugins/Interface/Parameters.h | 3 +- .../Interface/ParametersQtDialog.cxx | 60 +- lib/cpPlugins/Interface/Plugins.cxx | 858 ++++++++++++++---- lib/cpPlugins/Interface/Plugins.h | 111 ++- lib/cpPlugins/Interface/ProcessObject.cxx | 47 +- lib/cpPlugins/Interface/ProcessObject.h | 7 + .../BasicFilters/OtsuThresholdImageFilter.cxx | 4 +- 13 files changed, 1118 insertions(+), 396 deletions(-) create mode 100644 lib/cpPlugins/Interface/BasePluginsApplication.h diff --git a/appli/ImageMPR/ImageMPR.cxx b/appli/ImageMPR/ImageMPR.cxx index 89af18d..6c7e119 100644 --- a/appli/ImageMPR/ImageMPR.cxx +++ b/appli/ImageMPR/ImageMPR.cxx @@ -19,6 +19,7 @@ ImageMPR( QWidget* parent ) { this->m_UI->setupUi( this ); this->m_Plugins->SetWidget( this ); + this->m_Plugins->SetApplication( this ); // Connect actions ImageMPR_ConnectAction( OpenImage ); @@ -33,12 +34,16 @@ ImageMPR( QWidget* parent ) ImageMPR_ConnectAction( LoadPlugins ); ImageMPR_ConnectAction( ShowPlugins ); + // 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 #ifdef WIN32 - this->m_Plugins->LoadPlugins("cpPluginsIO.dll"); - this->m_Plugins->LoadPlugins("cpPluginsBasicFilters.dll"); + this->m_Plugins->LoadPlugins( "cpPluginsIO.dll" ); + this->m_Plugins->LoadPlugins( "cpPluginsBasicFilters.dll" ); #else - this->m_Plugins->LoadPluginsConfigurationFile("Plugins.cfg"); + this->m_Plugins->LoadPluginsConfigurationFile( "Plugins.cfg" ); #endif this->m_Plugins->AssociatePluginsToMenu( this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) ) @@ -53,17 +58,82 @@ ImageMPR:: delete this->m_Plugins; } +// ------------------------------------------------------------------------- +void ImageMPR:: +UpdateActualFilter( ) +{ + if( !( this->m_Plugins->HasActiveFilter( ) ) ) + return; + + std::vector< std::string > outputs; + std::string err = this->m_Plugins->UpdateActiveFilter( outputs ); + if( err == "" ) + { + for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt ) + { + TPlugins::TImage* image = this->m_Plugins->GetImage( *oIt ); + if( image != NULL ) + { + vtkImageData* vimage = image->GetVTK< vtkImageData >( ); + if( vimage != NULL ) + { + this->m_UI->MPR->AddImage( + vimage, *oIt, this->m_Plugins->GetParent( *oIt ) + ); + this->m_UI->MPR->ShowData( *oIt ); + + } // fi + continue; + + } // fi + + TPlugins::TMesh* mesh = this->m_Plugins->GetMesh( *oIt ); + if( mesh != NULL ) + { + this->m_Plugins->BlockWidget( ); + this->m_UI->MPR->AddMesh( + mesh->GetVTK< vtkPolyData >( ), + *oIt, + this->m_Plugins->GetParent( *oIt ) + ); + this->m_UI->MPR->ShowData( *oIt ); + this->m_Plugins->UnblockWidget( ); + + } // fi + + } // rof + } + else + { + QMessageBox::critical( + this, + tr( "Error executing filter" ), + tr( ( std::string( "Error caught: " ) + err ).c_str( ) ) + ); + return; + + } // fi +} + // ------------------------------------------------------------------------- void ImageMPR:: _aOpenImage( ) { - // Read and show image, if possible - TPlugins::TImage::Pointer image; - if( this->m_Plugins->ReadImage( image, true ) ) + std::string name = this->m_Plugins->ReadImage( "" ); + if( name != "" ) { + // Since we are opening an image, clear view + this->m_UI->MPR->DeleteAllData( ); + + // Prepare visualization + TPlugins::TImage* image = this->m_Plugins->GetImage( name ); vtkImageData* vimage = image->GetVTK< vtkImageData >( ); - if( vimage == NULL ) + if( vimage != NULL ) { + this->m_UI->MPR->AddImage( vimage, name ); + this->m_UI->MPR->ShowData( name ); + } + else QMessageBox::critical( this, QMessageBox::tr( "Error showing image." ), @@ -71,21 +141,6 @@ _aOpenImage( ) "Image was read, but no valid VTK conversion was found." ) ); - return; - } - else - { - // Since we are opening an image, clear all - this->m_UI->MPR->DeleteAllData( ); - this->m_Objects.clear( ); - - // Update references - std::string name = image->GetName( ); - this->m_Objects[ name ] = TTreeNode( "", image.GetPointer( ) ); - this->m_UI->MPR->AddImage( vimage, name ); - this->m_UI->MPR->ShowData( name ); - - } // fi } // fi } @@ -94,13 +149,21 @@ _aOpenImage( ) void ImageMPR:: _aOpenDICOMSeries( ) { - // Read and show image, if possible - TPlugins::TImage::Pointer image; - if( this->m_Plugins->ReadDicomSeries( image ) ) + std::string name = this->m_Plugins->ReadDicomSeries( "" ); + if( name != "" ) { + // Since we are opening an image, clear view + this->m_UI->MPR->DeleteAllData( ); + + // Prepare visualization + TPlugins::TImage* image = this->m_Plugins->GetImage( name ); vtkImageData* vimage = image->GetVTK< vtkImageData >( ); - if( vimage == NULL ) + if( vimage != NULL ) { + this->m_UI->MPR->AddImage( vimage, name ); + this->m_UI->MPR->ShowData( name ); + } + else QMessageBox::critical( this, QMessageBox::tr( "Error showing image." ), @@ -108,21 +171,6 @@ _aOpenDICOMSeries( ) "Image was read, but no valid VTK conversion was found." ) ); - return; - } - else - { - // Since we are opening an image, clear all - this->m_UI->MPR->DeleteAllData( ); - this->m_Objects.clear( ); - - // Update references - std::string name = image->GetName( ); - this->m_Objects[ name ] = TTreeNode( "", image.GetPointer( ) ); - this->m_UI->MPR->AddImage( vimage, name ); - this->m_UI->MPR->ShowData( name ); - - } // fi } // fi } @@ -148,12 +196,7 @@ void ImageMPR:: _aSaveImage( ) { std::string data_name = this->m_UI->MPR->GetSelectedData( ); - TPlugins::TImage* image = dynamic_cast< TPlugins::TImage* >( - this->m_Objects[ data_name ].second.GetPointer( ) - ); - if( image == NULL ) - return; - this->m_Plugins->WriteImage( image, true ); + this->m_Plugins->WriteImage( data_name ); } // ------------------------------------------------------------------------- @@ -166,13 +209,6 @@ _aSaveSegmentation( ) void ImageMPR:: _aSavePolyData( ) { - std::string data_name = this->m_UI->MPR->GetSelectedData( ); - TPlugins::TMesh* mesh = dynamic_cast< TPlugins::TMesh* >( - this->m_Objects[ data_name ].second.GetPointer( ) - ); - if( mesh == NULL ) - return; - this->m_Plugins->WriteMesh( mesh, true ); } // ------------------------------------------------------------------------- @@ -217,59 +253,19 @@ _execPlugin( ) std::string filter_cate = menu->title( ).toStdString( ); std::string filter_name = action->text( ).toStdString( ); - // Create filter - if( !( this->m_Plugins->CreateFilter( this->m_ActiveFilter, filter_name ) ) ) - { - QMessageBox::critical( - this, - tr( "Error creating filter" ), - tr( ( - std::string( "No valid filter \"" ) + - filter_name + - std::string( "\"defined." ) - ).c_str( ) ) - ); - this->m_ActiveFilter = NULL; - return; - - } // fi - - // Configure filter - this->m_ActiveFilter->AddInteractor( this->m_UI->MPR->GetInteractor( 0 ) ); - this->m_ActiveFilter->AddInteractor( this->m_UI->MPR->GetInteractor( 1 ) ); - this->m_ActiveFilter->AddInteractor( this->m_UI->MPR->GetInteractor( 2 ) ); - this->m_ActiveFilter->AddInteractor( this->m_UI->MPR->GetInteractor( 3 ) ); - - TPlugins::TProcessObject::DialogResult res = - this->m_ActiveFilter->ExecConfigurationDialog( this ); - if( res == TPlugins::TProcessObject::DialogResult_Cancel ) - { - this->m_ActiveFilter = NULL; + // Activate filter + if( !( this->m_Plugins->ActivateFilter( filter_name ) ) ) return; - } // fi - - // Assign inputs - std::string data_name = this->m_UI->MPR->GetSelectedData( ); - std::vector< std::string > inputs_names = - this->m_ActiveFilter->GetInputsNames( ); - if( inputs_names.size( ) == 1 ) + // Associate inputs + std::vector< std::string > inputs = + this->m_Plugins->GetActiveFilterInputsNames( ); + if( inputs.size( ) == 1 ) { - TTree::iterator iIt = this->m_Objects.find( data_name ); - if( iIt == this->m_Objects.end( ) ) - { - QMessageBox::critical( - this, - tr( "Error configuring filter" ), - tr( "No valid input found. Please select a valid input." ) - ); - this->m_ActiveFilter = NULL; - return; - - } //fi - this->m_ActiveFilter->SetInput( inputs_names[ 0 ], iIt->second.second ); + std::string data_name = this->m_UI->MPR->GetSelectedData( ); + this->m_Plugins->ConnectInputInActiveFilter( data_name, inputs[ 0 ] ); } - else if( inputs_names.size( ) > 1 ) + else if( inputs.size( ) > 1 ) { QMessageBox::critical( this, @@ -280,82 +276,31 @@ _execPlugin( ) } // fi - // Execute filter - /* - if( res == TPlugins::TProcessObject::DialogResult_NoModal ) - { - this->_Block( ); - std::string filter_err = this->m_ActiveFilter->Update( ); - this->_Unblock( ); - if( filter_err != "" ) - { - QMessageBox::critical( - this, - tr( "Error executing" ), - tr( filter_err.c_str( ) ) - ); - this->m_ActiveFilter = NULL; - return; - - } // fi - - // Get outputs - std::vector< std::string > outputs_names = filter->GetOutputsNames( ); - for( - auto oIt = outputs_names.begin( ); - oIt != outputs_names.end( ); - ++oIt - ) - { - std::string out_name = filter_name + "_" + *oIt; - - TPlugins::TImage* image = - this->m_ActiveFilter->GetOutput< TPlugins::TImage >( *oIt ); - if( image != NULL ) - { - if( filter_cate == "ImageToBinaryImageFilter" ) - { - this->m_UI->MPR->ShowImage( - image->GetVTK< vtkImageData >( ), - out_name, - data_name, 1, 0, 0 - ); - } - else if( filter_cate == "ImageToImageFilter" ) - { - } // fi - - // Keep a track on a local data tree and go to next output - this->m_Objects[ out_name ] = TTreeNode( data_name, image ); - continue; - - } // fi - - TPlugins::TMesh* mesh = filter->GetOutput< TPlugins::TMesh >( *oIt ); - if( mesh != NULL ) - { - // Show mesh - this->_Block( ); - this->m_UI->MPR->ShowMesh( - mesh->GetVTK< vtkPolyData >( ), - out_name, - data_name - ); - this->_Unblock( ); - - // Keep a track on a local data tree and go to next output - this->m_Objects[ out_name ] = TTreeNode( data_name, mesh ); - continue; + // Associate outputs + std::vector< std::string > outputs = + this->m_Plugins->GetActiveFilterOutputsNames( ); + for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt ) + this->m_Plugins->SetOutputNameInActiveFilter( + filter_name + "_" + *oIt, *oIt + ); - } // fi + // Configure filter + TPlugins::TProcessObject::DialogResult dlg_res = + this->m_Plugins->ConfigureActiveFilter( ); + if( dlg_res == TPlugins::TProcessObject::DialogResult_Cancel ) + { + this->m_Plugins->DeactivateFilter( ); + return; - } // rof + } // fi - // No-modal filters just exists for one usage - this->m_ActiveFilter = NULL; + // Execute filter and associate outputs + if( dlg_res == TPlugins::TProcessObject::DialogResult_NoModal ) + { + this->UpdateActualFilter( ); + this->m_Plugins->DeactivateFilter( ); } // fi - */ } // ------------------------------------------------------------------------- diff --git a/appli/ImageMPR/ImageMPR.h b/appli/ImageMPR/ImageMPR.h index f5cb03d..9a866e0 100644 --- a/appli/ImageMPR/ImageMPR.h +++ b/appli/ImageMPR/ImageMPR.h @@ -14,6 +14,7 @@ // Plugins interface #include +#include // ------------------------------------------------------------------------- namespace Ui @@ -109,7 +110,8 @@ namespace Ui class ImageMPR - : public QMainWindow + : public QMainWindow, + public cpPlugins::Interface::BasePluginsApplication { Q_OBJECT; @@ -137,17 +139,21 @@ public: explicit ImageMPR( QWidget* parent = 0 ); virtual ~ImageMPR( ); -protected: - inline void _Block( ) - { - QApplication::setOverrideCursor( Qt::WaitCursor ); - this->setEnabled( false ); - } - inline void _Unblock( ) - { - QApplication::restoreOverrideCursor( ); - this->setEnabled( true ); - } + virtual void UpdateActualFilter( ); + + /* TODO + protected: + inline void _Block( ) + { + QApplication::setOverrideCursor( Qt::WaitCursor ); + this->setEnabled( false ); + } + inline void _Unblock( ) + { + QApplication::restoreOverrideCursor( ); + this->setEnabled( true ); + } + */ /* protected: @@ -176,12 +182,14 @@ private: TPlugins* m_Plugins; // Objects - typedef std::pair< std::string, TPlugins::TDataObject::Pointer > TTreeNode; - typedef std::map< std::string, TTreeNode > TTree; - TTree m_Objects; + /* + typedef std::pair< std::string, TPlugins::TDataObject::Pointer > TTreeNode; + typedef std::map< std::string, TTreeNode > TTree; + TTree m_Objects; - // Active filter (for modal configuration) - TPlugins::TProcessObject::Pointer m_ActiveFilter; + // Active filter (for modal configuration) + TPlugins::TProcessObject::Pointer m_ActiveFilter; + */ // Plugins objects /* diff --git a/lib/cpPlugins/Interface/BaseMPRWidget.cxx b/lib/cpPlugins/Interface/BaseMPRWidget.cxx index ae56e07..e7f270c 100644 --- a/lib/cpPlugins/Interface/BaseMPRWidget.cxx +++ b/lib/cpPlugins/Interface/BaseMPRWidget.cxx @@ -202,6 +202,13 @@ ShowData( const std::string& name ) } else if( iIt->second.Tag == Data::MESH ) { + vtkRenderer* ren = + this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( ); + if( ren == NULL ) + return; + ren->AddActor( iIt->second.GetMeshActor( ) ); + this->m_VTK[ 3 ]->GetRenderWindow( )->Render( ); + } // fi } @@ -395,6 +402,27 @@ _SyncTop( int a, int b ) this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) ); } +// ------------------------------------------------------------------------- +cpPlugins::Interface::BaseMPRWidget::PolyDataActor:: +PolyDataActor( ) + : Mesh( NULL ), + Normals( NULL ), + Stripper( NULL ), + Mapper( NULL ), + Actor( NULL ) +{ +} + +// ------------------------------------------------------------------------- +cpPlugins::Interface::BaseMPRWidget::PolyDataActor:: +~PolyDataActor( ) +{ + if( this->Actor != NULL ) this->Actor->Delete( ); + if( this->Mapper != NULL ) this->Mapper->Delete( ); + if( this->Stripper != NULL ) this->Stripper->Delete( ); + if( this->Normals != NULL ) this->Normals->Delete( ); +} + // ------------------------------------------------------------------------- void cpPlugins::Interface::BaseMPRWidget::PolyDataActor:: Configure( vtkPolyData* pd ) @@ -405,10 +433,10 @@ Configure( vtkPolyData* pd ) double range[ 2 ]; pd->GetScalarRange( range ); - this->Normals = vtkSmartPointer< vtkPolyDataNormals >::New( ); - this->Stripper = vtkSmartPointer< vtkStripper >::New( ); - this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( ); - this->Actor = vtkSmartPointer< vtkQuadricLODActor >::New( ); + this->Normals = vtkPolyDataNormals::New( ); + this->Stripper = vtkStripper::New( ); + this->Mapper = vtkPolyDataMapper::New( ); + this->Actor = vtkQuadricLODActor::New( ); this->Mesh = pd; this->Normals->SetInputData( pd ); diff --git a/lib/cpPlugins/Interface/BaseMPRWidget.h b/lib/cpPlugins/Interface/BaseMPRWidget.h index 0807a87..6cb89f3 100644 --- a/lib/cpPlugins/Interface/BaseMPRWidget.h +++ b/lib/cpPlugins/Interface/BaseMPRWidget.h @@ -137,12 +137,14 @@ namespace cpPlugins struct PolyDataActor { - vtkSmartPointer< vtkPolyData > Mesh; - vtkSmartPointer< vtkPolyDataNormals > Normals; - vtkSmartPointer< vtkStripper > Stripper; - vtkSmartPointer< vtkPolyDataMapper > Mapper; - vtkSmartPointer< vtkQuadricLODActor > Actor; - + vtkPolyData* Mesh; + vtkPolyDataNormals* Normals; + vtkStripper* Stripper; + vtkPolyDataMapper* Mapper; + vtkQuadricLODActor* Actor; + + PolyDataActor( ); + virtual ~PolyDataActor( ); void Configure( vtkPolyData* pd ); }; @@ -178,14 +180,14 @@ namespace cpPlugins inline vtkPolyData* GetMesh( ) { if( this->Tag == Data::MESH ) - return( this->Mesh.Mesh.GetPointer( ) ); + return( this->Mesh.Mesh ); else return( NULL ); } inline vtkProp* GetMeshActor( ) { if( this->Tag == Data::MESH ) - return( this->Mesh.Actor.GetPointer( ) ); + return( this->Mesh.Actor ); else return( NULL ); } diff --git a/lib/cpPlugins/Interface/BasePluginsApplication.h b/lib/cpPlugins/Interface/BasePluginsApplication.h new file mode 100644 index 0000000..26ba271 --- /dev/null +++ b/lib/cpPlugins/Interface/BasePluginsApplication.h @@ -0,0 +1,27 @@ +#ifndef __CPPLUGINS__INTERFACE__BASEPLUGINSAPPLICATION__H__ +#define __CPPLUGINS__INTERFACE__BASEPLUGINSAPPLICATION__H__ + +#include + +namespace cpPlugins +{ + namespace Interface + { + /** + */ + class cpPlugins_Interface_EXPORT BasePluginsApplication + { + public: + BasePluginsApplication( ) { } + virtual ~BasePluginsApplication( ) { } + + virtual void UpdateActualFilter( ) = 0; + }; + + } // ecapseman + +} // ecapseman + +#endif // __CPPLUGINS__INTERFACE__BASEPLUGINSAPPLICATION__H__ + +// eof - $RCSfile$ diff --git a/lib/cpPlugins/Interface/Parameters.cxx b/lib/cpPlugins/Interface/Parameters.cxx index af641e0..f63fbbc 100644 --- a/lib/cpPlugins/Interface/Parameters.cxx +++ b/lib/cpPlugins/Interface/Parameters.cxx @@ -3,6 +3,14 @@ #include +// ------------------------------------------------------------------------- +cpPlugins::Interface:: +ProcessObject* cpPlugins::Interface::Parameters:: +GetProcessObject( ) +{ + return( this->m_Process ); +} + // ------------------------------------------------------------------------- const cpPlugins::Interface:: ProcessObject* cpPlugins::Interface::Parameters:: diff --git a/lib/cpPlugins/Interface/Parameters.h b/lib/cpPlugins/Interface/Parameters.h index 858a90b..ef8e3b3 100644 --- a/lib/cpPlugins/Interface/Parameters.h +++ b/lib/cpPlugins/Interface/Parameters.h @@ -56,6 +56,7 @@ namespace cpPlugins public: // To impact pipeline + virtual ProcessObject* GetProcessObject( ); virtual const ProcessObject* GetProcessObject( ) const; virtual void SetProcessObject( ProcessObject* v ); virtual void Modified( ) const; @@ -203,7 +204,7 @@ namespace cpPlugins protected: TParameters m_Parameters; - const ProcessObject* m_Process; + ProcessObject* m_Process; }; } // ecapseman diff --git a/lib/cpPlugins/Interface/ParametersQtDialog.cxx b/lib/cpPlugins/Interface/ParametersQtDialog.cxx index 12cca5d..3f24a44 100644 --- a/lib/cpPlugins/Interface/ParametersQtDialog.cxx +++ b/lib/cpPlugins/Interface/ParametersQtDialog.cxx @@ -17,6 +17,10 @@ #include // ------------------------------------------------------------------------- +#include +#include +#include + class SingleSeedCommand : public vtkCommand { @@ -25,10 +29,60 @@ public: { return( new SingleSeedCommand ); } virtual void Execute( vtkObject* caller, unsigned long eid, void* data ) { - if( eid != vtkCommand::PlacePointEvent ) + // Get seed, avoiding segfaults!!! + if( eid != vtkCommand::PlacePointEvent || this->Dialog == NULL ) + return; + vtkSeedWidget* widget = dynamic_cast< vtkSeedWidget* >( caller ); + if( widget == NULL ) + return; + vtkSeedRepresentation* rep = widget->GetSeedRepresentation( ); + if( rep == NULL ) return; -#error ACA VOY + if( rep->GetNumberOfSeeds( ) == 0 ) + return; + double seed[ 3 ]; + rep->GetSeedWorldPosition( 0, seed ); + + // Delete all seeds (remember that this command is just for one seed) + while( rep->GetNumberOfSeeds( ) > 0 ) + widget->DeleteSeed( 0 ); + + if( this->Dialog->getParameters( )->HasIndex( this->Name ) ) + { + } + else if( this->Dialog->getParameters( )->HasPoint( this->Name ) ) + { + this->Dialog->getParameters( )->SetPoint( this->Name, 3, seed ); + this->Dialog->syncParameters( ); + auto filter = this->Dialog->getParameters( )->GetProcessObject( ); + if( filter != NULL ) + { + auto plugins = filter->GetPlugins( ); + if( plugins != NULL ) + { + auto app = plugins->GetApplication( ); + if( app != NULL ) + app->UpdateActualFilter( ); + + } // fi + + } // fi + + } // fi + } +protected: + SingleSeedCommand( ) + : vtkCommand( ), + Dialog( NULL ) + { + } + virtual ~SingleSeedCommand( ) + { } + +public: + cpPlugins::Interface::ParametersQtDialog* Dialog; + std::string Name; }; // ------------------------------------------------------------------------- @@ -149,6 +203,8 @@ setParameters( Parameters* parameters ) { vtkSmartPointer< SingleSeedCommand > command = vtkSmartPointer< SingleSeedCommand >::New( ); + command->Dialog = this; + command->Name = *nIt; auto iIt = this->m_Interactors.begin( ); for( ; iIt != this->m_Interactors.end( ); ++iIt ) diff --git a/lib/cpPlugins/Interface/Plugins.cxx b/lib/cpPlugins/Interface/Plugins.cxx index 0e69212..99aaebb 100644 --- a/lib/cpPlugins/Interface/Plugins.cxx +++ b/lib/cpPlugins/Interface/Plugins.cxx @@ -28,6 +28,7 @@ cpPlugins::Interface::Plugins:: Plugins( QWidget* widget ) : m_Widget( widget ), + m_Application( NULL ), m_LastLoadedPlugin( "." ) { } @@ -138,6 +139,29 @@ AssociatePluginsToMenu( QMenu* menu, QObject* obj, const char* slot ) #endif // cpPlugins_Interface_QT4 } +// ------------------------------------------------------------------------- +cpPlugins::Interface:: +BasePluginsApplication* cpPlugins::Interface::Plugins:: +GetApplication( ) +{ + return( this->m_Application ); +} + +// ------------------------------------------------------------------------- +const cpPlugins::Interface:: +BasePluginsApplication* cpPlugins::Interface::Plugins:: +GetApplication( ) const +{ + return( this->m_Application ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Interface::Plugins:: +SetApplication( cpPlugins::Interface::BasePluginsApplication* a ) +{ + this->m_Application = a; +} + // ------------------------------------------------------------------------- bool cpPlugins::Interface::Plugins:: LoadPlugins( const std::string& fname ) @@ -156,7 +180,7 @@ LoadPlugins( const std::string& fname ) { this->m_LoadedPlugins.insert( fname ); this->m_LastLoadedPlugin = fname; - this->_Update( ); + this->_UpdateLoadedPluginsInformation( ); } // fi @@ -198,205 +222,344 @@ LoadPluginsConfigurationFile( const std::string& fname ) } // ------------------------------------------------------------------------- -cpPlugins::Interface::Plugins:: -TParameters* cpPlugins::Interface::Plugins:: -GetImageReaderParameters( ) +void cpPlugins::Interface::Plugins:: +AddInteractor( vtkRenderWindowInteractor* interactor ) { - return( this->m_ImageReader->GetParameters( ) ); + this->m_Interactors.insert( interactor ); } // ------------------------------------------------------------------------- -cpPlugins::Interface::Plugins:: -TParameters* cpPlugins::Interface::Plugins:: -GetMeshReaderParameters( ) +void cpPlugins::Interface::Plugins:: +RemoveInteractor( vtkRenderWindowInteractor* interactor ) { - return( this->m_MeshReader->GetParameters( ) ); + this->m_Interactors.erase( interactor ); } // ------------------------------------------------------------------------- -cpPlugins::Interface::Plugins:: -TParameters* cpPlugins::Interface::Plugins:: -GetImageWriterParameters( ) +void cpPlugins::Interface::Plugins:: +ClearInteractors( ) { - return( this->m_ImageWriter->GetParameters( ) ); + this->m_Interactors.clear( ); } // ------------------------------------------------------------------------- -cpPlugins::Interface::Plugins:: -TParameters* cpPlugins::Interface::Plugins:: -GetMeshWriterParameters( ) +bool cpPlugins::Interface::Plugins:: +HasImageReader( ) const { - return( this->m_MeshWriter->GetParameters( ) ); + return( this->m_ImageReader.IsNotNull( ) ); } // ------------------------------------------------------------------------- -const cpPlugins::Interface::Plugins:: -TParameters* cpPlugins::Interface::Plugins:: -GetImageReaderParameters( ) const +bool cpPlugins::Interface::Plugins:: +HasDicomSeriesReader( ) const { - return( this->m_ImageReader->GetParameters( ) ); + return( this->m_DicomSeriesReader.IsNotNull( ) ); } // ------------------------------------------------------------------------- -const cpPlugins::Interface::Plugins:: -TParameters* cpPlugins::Interface::Plugins:: -GetMeshReaderParameters( ) const +bool cpPlugins::Interface::Plugins:: +HasMeshReader( ) const { - return( this->m_MeshReader->GetParameters( ) ); + return( this->m_MeshReader.IsNotNull( ) ); } // ------------------------------------------------------------------------- -const cpPlugins::Interface::Plugins:: -TParameters* cpPlugins::Interface::Plugins:: -GetImageWriterParameters( ) const +bool cpPlugins::Interface::Plugins:: +HasImageWriter( ) const { - return( this->m_ImageWriter->GetParameters( ) ); + return( this->m_ImageWriter.IsNotNull( ) ); } // ------------------------------------------------------------------------- -const cpPlugins::Interface::Plugins:: -TParameters* cpPlugins::Interface::Plugins:: -GetMeshWriterParameters( ) const +bool cpPlugins::Interface::Plugins:: +HasMeshWriter( ) const { - return( this->m_MeshWriter->GetParameters( ) ); + return( this->m_MeshWriter.IsNotNull( ) ); } // ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -ReadImage( TImage::Pointer& image, bool exec_qt ) +std::string cpPlugins::Interface::Plugins:: +ReadImage( const std::string& fname, const std::string& parent ) { - std::string ret = ""; + std::vector< std::string > fnames; + fnames.push_back( fname ); + return( this->ReadImage( fnames, parent ) ); +} + +// ------------------------------------------------------------------------- +std::string cpPlugins::Interface::Plugins:: +ReadImage( + const std::vector< std::string >& fnames, const std::string& parent + ) +{ + // Check if object exists if( this->m_ImageReader.IsNull( ) ) - ret = "Plugins: No valid image reader. Please load a valid plugin."; + return( "" ); - if( ret == "" ) - { - TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal; - if( exec_qt ) - dret = this->m_ImageReader->ExecConfigurationDialog( this->m_Widget ); - if( dret != TProcessObject::DialogResult_Cancel ) - { - this->BlockWidget( ); - ret = this->m_ImageReader->Update( ); - this->UnblockWidget( ); - if( ret == "" ) - { - image = this->m_ImageReader->GetOutput< TImage >( "Output" ); - this->m_ImageReader->DisconnectOutputs( ); - this->m_ImageReader->GetParameters( )->ClearStringList( "FileNames" ); - } - else - image = NULL; + // Configure object + TParameters* params = this->m_ImageReader->GetParameters( ); + params->ClearStringList( "FileNames" ); + auto i = fnames.begin( ); + for( ; i != fnames.end( ); ++i ) + params->AddToStringList( "FileNames", *i ); - } // fi + // Execute filter + this->BlockWidget( ); + std::string err = this->m_ImageReader->Update( ); + this->UnblockWidget( ); - } // fi + // Get result, if any + if( err == "" ) + { + TImage* image = this->m_ImageReader->GetOutput< TImage >( "Output" ); + this->m_ImageReader->DisconnectOutputs( ); - // Show an error message and return - if( ret != "" ) + // Add newly added data + if( this->_InsertNewData( image, parent ) ) + return( image->GetName( ) ); + else + return( "" ); + } + else { #ifdef cpPlugins_Interface_QT4 if( this->m_Widget != NULL ) QMessageBox::critical( this->m_Widget, QMessageBox::tr( "Error reading image." ), - QMessageBox::tr( ret.c_str( ) ) + QMessageBox::tr( err.c_str( ) ) ); +#else // cpPlugins_Interface_QT4 + std::cerr << "Error reading image: " << err << std::endl; #endif // cpPlugins_Interface_QT4 - return( false ); + return( "" ); + + } // fi +} + +// ------------------------------------------------------------------------- +std::string cpPlugins::Interface::Plugins:: +ReadImage( const std::string& parent ) +{ + // Check if object exists + if( this->m_ImageReader.IsNull( ) ) + return( "" ); + + // Configure object + TProcessObject::DialogResult dret = + this->m_ImageReader->ExecConfigurationDialog( this->m_Widget ); + if( dret == TProcessObject::DialogResult_Cancel ) + return( "" ); + + // Execute filter + this->BlockWidget( ); + std::string err = this->m_ImageReader->Update( ); + this->UnblockWidget( ); + + // Get result, if any + if( err == "" ) + { + TImage* image = this->m_ImageReader->GetOutput< TImage >( "Output" ); + this->m_ImageReader->DisconnectOutputs( ); + + // Add newly added data + if( this->_InsertNewData( image, parent ) ) + return( image->GetName( ) ); + else + return( "" ); } else - return( true ); + { +#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 // cpPlugins_Interface_QT4 + std::cerr << "Error reading image: " << err << std::endl; +#endif // cpPlugins_Interface_QT4 + return( "" ); + + } // fi } // ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -ReadDicomSeries( TImage::Pointer& image ) +std::string cpPlugins::Interface::Plugins:: +ReadDicomSeries( const std::string& parent ) { - std::string ret = ""; + // Check if object exists if( this->m_DicomSeriesReader.IsNull( ) ) - ret = "Plugins: No valid dicom series reader. Please load a valid plugin."; + return( "" ); - if( ret == "" ) - { - TProcessObject::DialogResult dret = - this->m_DicomSeriesReader->ExecConfigurationDialog( this->m_Widget ); - if( dret != TProcessObject::DialogResult_Cancel ) - { - this->BlockWidget( ); - ret = this->m_DicomSeriesReader->Update( ); - this->UnblockWidget( ); - if( ret == "" ) - { - image = this->m_DicomSeriesReader->GetOutput< TImage >( "Output" ); - this->m_DicomSeriesReader->DisconnectOutputs( ); - this->m_DicomSeriesReader->GetParameters( )-> - ClearStringList( "FileNames" ); - } - else - image = NULL; + // Configure object + TProcessObject::DialogResult dret = + this->m_DicomSeriesReader->ExecConfigurationDialog( this->m_Widget ); + if( dret == TProcessObject::DialogResult_Cancel ) + return( "" ); - } // fi + // Execute filter + this->BlockWidget( ); + std::string err = this->m_DicomSeriesReader->Update( ); + this->UnblockWidget( ); - } // fi + // Get result, if any + if( err == "" ) + { + TImage* image = + this->m_DicomSeriesReader->GetOutput< TImage >( "Output" ); + this->m_DicomSeriesReader->DisconnectOutputs( ); - // Show an error message and return - if( ret != "" ) + // Add newly added data + if( this->_InsertNewData( image, parent ) ) + return( image->GetName( ) ); + else + return( "" ); + } + else { #ifdef cpPlugins_Interface_QT4 if( this->m_Widget != NULL ) QMessageBox::critical( this->m_Widget, - QMessageBox::tr( "Error reading dicom series." ), - QMessageBox::tr( ret.c_str( ) ) + QMessageBox::tr( "Error reading image." ), + QMessageBox::tr( err.c_str( ) ) ); +#else // cpPlugins_Interface_QT4 + std::cerr << "Error reading image: " << err << std::endl; #endif // cpPlugins_Interface_QT4 - return( false ); + return( "" ); + + } // fi +} + +// ------------------------------------------------------------------------- +std::string cpPlugins::Interface::Plugins:: +ReadMesh( const std::string& fname, const std::string& parent ) +{ + // Check if object exists + if( this->m_MeshReader.IsNull( ) ) + return( "" ); + + // Configure object + TParameters* params = this->m_MeshReader->GetParameters( ); + params->SetString( "FileName", fname ); + + // Execute filter + this->BlockWidget( ); + std::string err = this->m_MeshReader->Update( ); + this->UnblockWidget( ); + + // Get result, if any + if( err == "" ) + { + TMesh* mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" ); + this->m_MeshReader->DisconnectOutputs( ); + + // Add newly added data + if( this->_InsertNewData( mesh, parent ) ) + return( mesh->GetName( ) ); + else + return( "" ); } else - return( true ); + { +#ifdef cpPlugins_Interface_QT4 + if( this->m_Widget != NULL ) + QMessageBox::critical( + this->m_Widget, + QMessageBox::tr( "Error reading mesh." ), + QMessageBox::tr( err.c_str( ) ) + ); +#else // cpPlugins_Interface_QT4 + std::cerr << "Error reading mesh: " << err << std::endl; +#endif // cpPlugins_Interface_QT4 + return( "" ); + + } // fi } // ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -ReadMesh( TMesh::Pointer& mesh, bool exec_qt ) +std::string cpPlugins::Interface::Plugins:: +ReadMesh( const std::string& parent ) { - std::string ret = ""; + // Check if object exists if( this->m_MeshReader.IsNull( ) ) - ret = "Plugins: No valid mesh reader. Please load a valid plugin."; + return( "" ); - if( ret == "" ) + // Configure object + TProcessObject::DialogResult dret = + this->m_MeshReader->ExecConfigurationDialog( this->m_Widget ); + if( dret == TProcessObject::DialogResult_Cancel ) + return( "" ); + + // Execute filter + this->BlockWidget( ); + std::string err = this->m_MeshReader->Update( ); + this->UnblockWidget( ); + + // Get result, if any + if( err == "" ) { - TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal; - if( exec_qt ) - dret = this->m_MeshReader->ExecConfigurationDialog( this->m_Widget ); - if( dret != TProcessObject::DialogResult_Cancel ) - { - this->BlockWidget( ); - ret = this->m_MeshReader->Update( ); - this->UnblockWidget( ); - if( ret == "" ) - { - mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" ); - this->m_MeshReader->DisconnectOutputs( ); - } - else - mesh = NULL; + TMesh* mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" ); + this->m_MeshReader->DisconnectOutputs( ); - } // fi + // Add newly added data + if( this->_InsertNewData( mesh, parent ) ) + return( mesh->GetName( ) ); + else + return( "" ); + } + else + { +#ifdef cpPlugins_Interface_QT4 + if( this->m_Widget != NULL ) + QMessageBox::critical( + this->m_Widget, + QMessageBox::tr( "Error reading mesh." ), + QMessageBox::tr( err.c_str( ) ) + ); +#else // cpPlugins_Interface_QT4 + std::cerr << "Error reading mesh: " << err << std::endl; +#endif // cpPlugins_Interface_QT4 + return( "" ); } // fi +} + +// ------------------------------------------------------------------------- +bool cpPlugins::Interface::Plugins:: +WriteImage( const std::string& fname, const std::string& name ) +{ + // Check if objects exist + if( this->m_ImageWriter.IsNull( ) ) + return( false ); + TImage* image = this->GetImage( name ); + if( image == NULL ) + return( false ); - // Show an error message and return - if( ret != "" ) + // Configure writer + this->m_ImageWriter->GetParameters( )->SetString( "FileName", fname ); + this->m_ImageWriter->SetInput( "Input", image ); + + // Execute filter + this->BlockWidget( ); + std::string err = this->m_ImageWriter->Update( ); + this->UnblockWidget( ); + + // Get result, if any + if( err != "" ) { #ifdef cpPlugins_Interface_QT4 if( this->m_Widget != NULL ) QMessageBox::critical( this->m_Widget, - QMessageBox::tr( "Error reading image." ), - QMessageBox::tr( ret.c_str( ) ) + QMessageBox::tr( "Error reading mesh." ), + QMessageBox::tr( err.c_str( ) ) ); +#else // cpPlugins_Interface_QT4 + std::cerr << "Error reading mesh: " << err << std::endl; #endif // cpPlugins_Interface_QT4 return( false ); } @@ -406,38 +569,39 @@ ReadMesh( TMesh::Pointer& mesh, bool exec_qt ) // ------------------------------------------------------------------------- bool cpPlugins::Interface::Plugins:: -WriteImage( TImage* image, bool exec_qt ) +WriteImage( const std::string& name ) { - std::string ret = ""; + // Check if objects exist if( this->m_ImageWriter.IsNull( ) ) - ret = "Plugins: No valid image writer. Please load a valid plugin."; - - if( ret == "" ) - { - TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal; - if( exec_qt ) - dret = this->m_ImageWriter->ExecConfigurationDialog( this->m_Widget ); - if( dret != TProcessObject::DialogResult_Cancel ) - { - this->m_ImageWriter->SetInput( "Input", image ); - this->BlockWidget( ); - ret = this->m_ImageWriter->Update( ); - this->UnblockWidget( ); + return( false ); + TImage* image = this->GetImage( name ); + if( image == NULL ) + return( false ); - } // fi + // Configure writer + TProcessObject::DialogResult dret = + this->m_ImageWriter->ExecConfigurationDialog( this->m_Widget ); + if( dret == TProcessObject::DialogResult_Cancel ) + return( "" ); + this->m_ImageWriter->SetInput( "Input", image ); - } // fi + // Execute filter + this->BlockWidget( ); + std::string err = this->m_ImageWriter->Update( ); + this->UnblockWidget( ); - // Show an error message and return - if( ret != "" ) + // Get result, if any + if( err != "" ) { #ifdef cpPlugins_Interface_QT4 if( this->m_Widget != NULL ) QMessageBox::critical( this->m_Widget, - QMessageBox::tr( "Error reading image." ), - QMessageBox::tr( ret.c_str( ) ) + QMessageBox::tr( "Error reading mesh." ), + QMessageBox::tr( err.c_str( ) ) ); +#else // cpPlugins_Interface_QT4 + std::cerr << "Error reading mesh: " << err << std::endl; #endif // cpPlugins_Interface_QT4 return( false ); } @@ -447,38 +611,78 @@ WriteImage( TImage* image, bool exec_qt ) // ------------------------------------------------------------------------- bool cpPlugins::Interface::Plugins:: -WriteMesh( TMesh* mesh, bool exec_qt ) +WriteMesh( const std::string& fname, const std::string& name ) { - std::string ret = ""; + // Check if objects exist if( this->m_MeshWriter.IsNull( ) ) - ret = "Plugins: No valid mesh writer. Please load a valid plugin."; + return( false ); + TMesh* mesh = this->GetMesh( name ); + if( mesh == NULL ) + return( false ); - if( ret == "" ) + // Configure writer + this->m_MeshWriter->GetParameters( )->SetString( "FileName", fname ); + this->m_MeshWriter->SetInput( "Input", mesh ); + + // Execute filter + this->BlockWidget( ); + std::string err = this->m_MeshWriter->Update( ); + this->UnblockWidget( ); + + // Get result, if any + if( err != "" ) { - TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal; - if( exec_qt ) - dret = this->m_MeshWriter->ExecConfigurationDialog( this->m_Widget ); - if( dret != TProcessObject::DialogResult_Cancel ) - { - this->m_MeshWriter->SetInput( "Input", mesh ); - this->BlockWidget( ); - ret = this->m_MeshWriter->Update( ); - this->UnblockWidget( ); +#ifdef cpPlugins_Interface_QT4 + if( this->m_Widget != NULL ) + QMessageBox::critical( + this->m_Widget, + QMessageBox::tr( "Error reading mesh." ), + QMessageBox::tr( err.c_str( ) ) + ); +#else // cpPlugins_Interface_QT4 + std::cerr << "Error reading mesh: " << err << std::endl; +#endif // cpPlugins_Interface_QT4 + return( false ); + } + else + return( true ); +} - } // fi +// ------------------------------------------------------------------------- +bool cpPlugins::Interface::Plugins:: +WriteMesh( const std::string& name ) +{ + // Check if objects exist + if( this->m_MeshWriter.IsNull( ) ) + return( false ); + TMesh* mesh = this->GetMesh( name ); + if( mesh == NULL ) + return( false ); - } // fi + // Configure writer + TProcessObject::DialogResult dret = + this->m_MeshWriter->ExecConfigurationDialog( this->m_Widget ); + if( dret == TProcessObject::DialogResult_Cancel ) + return( "" ); + this->m_MeshWriter->SetInput( "Input", mesh ); + + // Execute filter + this->BlockWidget( ); + std::string err = this->m_MeshWriter->Update( ); + this->UnblockWidget( ); - // Show an error message and return - if( ret != "" ) + // Get result, if any + if( err != "" ) { #ifdef cpPlugins_Interface_QT4 if( this->m_Widget != NULL ) QMessageBox::critical( this->m_Widget, - QMessageBox::tr( "Error reading image." ), - QMessageBox::tr( ret.c_str( ) ) + QMessageBox::tr( "Error reading mesh." ), + QMessageBox::tr( err.c_str( ) ) ); +#else // cpPlugins_Interface_QT4 + std::cerr << "Error reading mesh: " << err << std::endl; #endif // cpPlugins_Interface_QT4 return( false ); } @@ -486,17 +690,308 @@ WriteMesh( TMesh* mesh, bool exec_qt ) return( true ); } +// ------------------------------------------------------------------------- +void cpPlugins::Interface::Plugins:: +ClearDataObjects( ) +{ + this->m_Objects.clear( ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Interface::Plugins:: +DeleteDataObject( const std::string& name ) +{ + auto i = this->m_Objects.find( name ); + if( i != this->m_Objects.end( ) ) + { + this->m_Objects.erase( i ); + + // Get children + std::vector< std::string > children; + for( i = this->m_Objects.begin( ); i != this->m_Objects.end( ); ++i ) + if( i->second.first == name ) + children.push_back( i->first ); + + // Erase children + auto c = children.begin( ); + for( ; c != children.end( ); ++c ) + this->DeleteDataObject( *c ); + + } // fi +} + +// ------------------------------------------------------------------------- +std::string cpPlugins::Interface::Plugins:: +GetParent( const std::string& name ) const +{ + auto i = this->m_Objects.find( name ); + if( i != this->m_Objects.end( ) ) + return( i->second.first ); + else + return( "" ); +} + +// ------------------------------------------------------------------------- +const cpPlugins::Interface::Plugins:: +TTree& cpPlugins::Interface::Plugins:: +GetDataObjects( ) const +{ + return( this->m_Objects ); +} + +// ------------------------------------------------------------------------- +cpPlugins::Interface::Plugins:: +TDataObject* cpPlugins::Interface::Plugins:: +GetDataObject( const std::string& name ) +{ + auto i = this->m_Objects.find( name ); + if( i != this->m_Objects.end( ) ) + return( dynamic_cast< TDataObject* >( i->second.second.GetPointer( ) ) ); + else + return( NULL ); +} + +// ------------------------------------------------------------------------- +const cpPlugins::Interface::Plugins:: +TDataObject* cpPlugins::Interface::Plugins:: +GetDataObject( const std::string& name ) const +{ + auto i = this->m_Objects.find( name ); + if( i != this->m_Objects.end( ) ) + return( + dynamic_cast< const TDataObject* >( i->second.second.GetPointer( ) ) + ); + else + return( NULL ); +} + +// ------------------------------------------------------------------------- +cpPlugins::Interface::Plugins:: +TImage* cpPlugins::Interface::Plugins:: +GetImage( const std::string& name ) +{ + auto i = this->m_Objects.find( name ); + if( i != this->m_Objects.end( ) ) + return( dynamic_cast< TImage* >( i->second.second.GetPointer( ) ) ); + else + return( NULL ); +} + +// ------------------------------------------------------------------------- +const cpPlugins::Interface::Plugins:: +TImage* cpPlugins::Interface::Plugins:: +GetImage( const std::string& name ) const +{ + auto i = this->m_Objects.find( name ); + if( i != this->m_Objects.end( ) ) + return( dynamic_cast< const TImage* >( i->second.second.GetPointer( ) ) ); + else + return( NULL ); +} + +// ------------------------------------------------------------------------- +cpPlugins::Interface::Plugins:: +TMesh* cpPlugins::Interface::Plugins:: +GetMesh( const std::string& name ) +{ + auto i = this->m_Objects.find( name ); + if( i != this->m_Objects.end( ) ) + return( dynamic_cast< TMesh* >( i->second.second.GetPointer( ) ) ); + else + return( NULL ); +} + +// ------------------------------------------------------------------------- +const cpPlugins::Interface::Plugins:: +TMesh* cpPlugins::Interface::Plugins:: +GetMesh( const std::string& name ) const +{ + auto i = this->m_Objects.find( name ); + if( i != this->m_Objects.end( ) ) + return( dynamic_cast< const TMesh* >( i->second.second.GetPointer( ) ) ); + else + return( NULL ); +} + // ------------------------------------------------------------------------- bool cpPlugins::Interface::Plugins:: -CreateFilter( TProcessObject::Pointer& filter, const std::string& name ) +ActivateFilter( const std::string& name ) { - filter = this->m_Interface.CreateProcessObject( name ); - return( filter.IsNotNull( ) ); + this->m_ActiveFilter = this->m_Interface.CreateProcessObject( name ); + if( this->m_ActiveFilter.IsNotNull( ) ) + { + this->m_ActiveFilter->SetPlugins( this ); + this->m_ActiveFilterOutputs.clear( ); + auto i = this->m_Interactors.begin( ); + for( ; i != this->m_Interactors.end( ); ++i ) + this->m_ActiveFilter->AddInteractor( *i ); + return( true ); + } + else + return( false ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: -_Update( ) +DeactivateFilter( ) +{ + 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->IsInteractive( ) ); + else + return( false ); +} + +// ------------------------------------------------------------------------- +unsigned int cpPlugins::Interface::Plugins:: +GetNumberOfInputsInActiveFilter( ) const +{ + if( this->m_ActiveFilter.IsNotNull( ) ) + return( this->m_ActiveFilter->GetNumberOfInputs( ) ); + else + return( 0 ); +} + +// ------------------------------------------------------------------------- +unsigned int cpPlugins::Interface::Plugins:: +GetNumberOfOutputsInActiveFilter( ) const +{ + if( this->m_ActiveFilter.IsNotNull( ) ) + return( this->m_ActiveFilter->GetNumberOfOutputs( ) ); + else + return( 0 ); +} + +// ------------------------------------------------------------------------- +std::vector< std::string > cpPlugins::Interface::Plugins:: +GetActiveFilterInputsNames( ) const +{ + if( this->m_ActiveFilter.IsNotNull( ) ) + return( this->m_ActiveFilter->GetInputsNames( ) ); + else + return( std::vector< std::string >( ) ); +} + +// ------------------------------------------------------------------------- +std::vector< std::string > cpPlugins::Interface::Plugins:: +GetActiveFilterOutputsNames( ) const +{ + if( this->m_ActiveFilter.IsNotNull( ) ) + return( this->m_ActiveFilter->GetOutputsNames( ) ); + else + return( std::vector< std::string >( ) ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Interface::Plugins:: +ConnectInputInActiveFilter( + const std::string& object_name, const std::string& input + ) +{ + if( this->m_ActiveFilter.IsNotNull( ) ) + { + TDataObject* dobj = this->GetDataObject( object_name ); + if( dobj != NULL ) + this->m_ActiveFilter->SetInput( input, dobj ); + + } // fi +} + +// ------------------------------------------------------------------------- +void cpPlugins::Interface::Plugins:: +SetOutputNameInActiveFilter( + const std::string& new_name, const std::string& output + ) +{ + this->m_ActiveFilterOutputs[ output ] = new_name; +} + +// ------------------------------------------------------------------------- +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( TProcessObject::DialogResult_Cancel ); +} + +// ------------------------------------------------------------------------- +std::string cpPlugins::Interface::Plugins:: +UpdateActiveFilter( std::vector< std::string >& outputs ) +{ + // Execute filter + this->BlockWidget( ); + std::string err = this->m_ActiveFilter->Update( ); + this->UnblockWidget( ); + + // Associate outputs + outputs.clear( ); + if( err == "" ) + { + std::string parent = ""; + if( this->GetNumberOfInputsInActiveFilter( ) > 0 ) + { + std::string input = this->m_ActiveFilter->GetInputsNames( )[ 0 ]; + parent = + this->m_ActiveFilter->GetInput< TDataObject >( input )->GetName( ); + + } // fi + + auto i = this->m_ActiveFilterOutputs.begin( ); + for( ; i != this->m_ActiveFilterOutputs.end( ); ++i ) + { + TDataObject* out = + this->m_ActiveFilter->GetOutput< TDataObject >( i->first ); + out->SetName( i->second ); + outputs.push_back( out->GetName( ) ); + this->_InsertNewData( out, parent ); + + } // rof + + } // fi + return( err ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Interface::Plugins:: +_UpdateLoadedPluginsInformation( ) { typedef TInterface::TClasses _C; @@ -524,4 +1019,45 @@ _Update( ) } // rof } +// ------------------------------------------------------------------------- +bool cpPlugins::Interface::Plugins:: +_InsertNewData( TDataObject* dobj, const std::string& parent ) +{ + std::string name = dobj->GetName( ); + auto i = this->m_Objects.find( name ); + bool ret = true; + if( i == this->m_Objects.end( ) ) + { + if( parent != "" ) + { + auto j = this->m_Objects.find( parent ); + if( j != this->m_Objects.end( ) ) + this->m_Objects[ name ] = TTreeNode( parent, dobj ); + else + ret = false; + } + else + this->m_Objects[ name ] = TTreeNode( "", dobj ); + } + else + i->second.second = dobj; + + if( !ret ) + { +#ifdef cpPlugins_Interface_QT4 + if( this->m_Widget != NULL ) + QMessageBox::critical( + this->m_Widget, + QMessageBox::tr( "Error inserting data." ), + QMessageBox::tr( "Given parent does not exists." ) + ); +#else // cpPlugins_Interface_QT4 + std::cerr + << "Error inserting data: Given parent does not exists." + << std::endl; +#endif // cpPlugins_Interface_QT4 + } // fi + return( ret ); +} + // eof - $RCSfile$ diff --git a/lib/cpPlugins/Interface/Plugins.h b/lib/cpPlugins/Interface/Plugins.h index 847b7e8..8eb4d3c 100644 --- a/lib/cpPlugins/Interface/Plugins.h +++ b/lib/cpPlugins/Interface/Plugins.h @@ -15,11 +15,14 @@ class QObject; class QMenu; class QWidget; +class vtkRenderWindowInteractor; namespace cpPlugins { namespace Interface { + class BasePluginsApplication; + /** */ class cpPlugins_Interface_EXPORT Plugins @@ -34,6 +37,8 @@ namespace cpPlugins typedef std::set< std::string > TOrderedStringContainer; typedef std::map< std::string, std::set< std::string > > TFilters; + typedef std::pair< std::string, TDataObject::Pointer > TTreeNode; + typedef std::map< std::string, TTreeNode > TTree; public: Plugins( QWidget* widget = NULL ); @@ -49,48 +54,102 @@ namespace cpPlugins QMenu* menu, QObject* obj, const char* slot ); + BasePluginsApplication* GetApplication( ); + const BasePluginsApplication* GetApplication( ) const; + void SetApplication( BasePluginsApplication* a ); + + bool LoadPlugins( ); bool LoadPlugins( const std::string& fname ); bool LoadPluginsConfigurationFile( const std::string& fname ); - TParameters* GetImageReaderParameters( ); - TParameters* GetMeshReaderParameters( ); - TParameters* GetImageWriterParameters( ); - TParameters* GetMeshWriterParameters( ); - - const TParameters* GetImageReaderParameters( ) const; - const TParameters* GetMeshReaderParameters( ) const; - const TParameters* GetImageWriterParameters( ) const; - const TParameters* GetMeshWriterParameters( ) const; + // Interactors + void AddInteractor( vtkRenderWindowInteractor* interactor ); + void RemoveInteractor( vtkRenderWindowInteractor* interactor ); + void ClearInteractors( ); // Data IO - bool ReadImage( TImage::Pointer& image, bool exec_qt = false ); - bool ReadDicomSeries( TImage::Pointer& image ); - bool ReadMesh( TMesh::Pointer& mesh, bool exec_qt = false ); - - bool WriteImage( TImage* image, bool exec_qt = false ); - bool WriteMesh( TMesh* mesh, bool exec_qt = false ); + bool HasImageReader( ) const; + bool HasDicomSeriesReader( ) const; + bool HasMeshReader( ) const; + bool HasImageWriter( ) const; + bool HasMeshWriter( ) const; + + std::string ReadImage( + const std::string& fname, const std::string& parent + ); + std::string ReadImage( + const std::vector< std::string >& fnames, const std::string& parent + ); + std::string ReadImage( const std::string& parent ); + std::string ReadDicomSeries( const std::string& parent ); + std::string ReadMesh( + const std::string& fname, const std::string& parent + ); + std::string ReadMesh( const std::string& parent ); + + bool WriteImage( const std::string& fname, const std::string& name ); + bool WriteImage( const std::string& name ); + bool WriteMesh( const std::string& fname, const std::string& name ); + bool WriteMesh( const std::string& name ); + + // Data objects + void ClearDataObjects( ); + void DeleteDataObject( const std::string& name ); + std::string GetParent( const std::string& name ) const; + const TTree& GetDataObjects( ) const; + TDataObject* GetDataObject( const std::string& name ); + const TDataObject* GetDataObject( const std::string& name ) const; + TImage* GetImage( const std::string& name ); + const TImage* GetImage( const std::string& name ) const; + TMesh* GetMesh( const std::string& name ); + const TMesh* GetMesh( const std::string& name ) const; // Filter acces - bool CreateFilter( - TProcessObject::Pointer& filter, const std::string& name + bool ActivateFilter( const std::string& name ); + void DeactivateFilter( ); + bool HasActiveFilter( ) const; + bool IsActiveFilterInteractive( ) const; + unsigned int GetNumberOfInputsInActiveFilter( ) const; + unsigned int GetNumberOfOutputsInActiveFilter( ) const; + std::vector< std::string > GetActiveFilterInputsNames( ) const; + std::vector< std::string > GetActiveFilterOutputsNames( ) const; + void ConnectInputInActiveFilter( + const std::string& object_name, const std::string& input + ); + void SetOutputNameInActiveFilter( + const std::string& new_name, const std::string& output ); + TParameters* GetActiveFilterParameters( ); + const TParameters* GetActiveFilterParameters( ) const; + TProcessObject::DialogResult ConfigureActiveFilter( ); + std::string UpdateActiveFilter( std::vector< std::string >& outputs ); protected: - void _Update( ); + void _UpdateLoadedPluginsInformation( ); + bool _InsertNewData( TDataObject* dobj, const std::string& parent ); protected: QWidget* m_Widget; + BasePluginsApplication* m_Application; - TInterface m_Interface; + TInterface m_Interface; TOrderedStringContainer m_LoadedPlugins; - std::string m_LastLoadedPlugin; - - TProcessObject::Pointer m_ImageReader; - TProcessObject::Pointer m_ImageWriter; - TProcessObject::Pointer m_MeshReader; - TProcessObject::Pointer m_MeshWriter; - TProcessObject::Pointer m_DicomSeriesReader; + std::string m_LastLoadedPlugin; + + TProcessObject::Pointer m_ImageReader; + TProcessObject::Pointer m_ImageWriter; + TProcessObject::Pointer m_MeshReader; + TProcessObject::Pointer m_MeshWriter; + TProcessObject::Pointer m_DicomSeriesReader; + TProcessObject::Pointer m_ActiveFilter; + std::map< std::string, std::string > m_ActiveFilterOutputs; TFilters m_Filters; + + // Loaded objects + TTree m_Objects; + + // Associated interactors + std::set< vtkRenderWindowInteractor* > m_Interactors; }; } // ecapseman diff --git a/lib/cpPlugins/Interface/ProcessObject.cxx b/lib/cpPlugins/Interface/ProcessObject.cxx index 9c3148d..6f3bd34 100644 --- a/lib/cpPlugins/Interface/ProcessObject.cxx +++ b/lib/cpPlugins/Interface/ProcessObject.cxx @@ -17,6 +17,26 @@ Modified( ) const this->Superclass::Modified( ); } +// ------------------------------------------------------------------------- +bool cpPlugins::Interface::ProcessObject:: +IsInteractive( ) const +{ + std::vector< std::string > names; + this->m_Parameters->GetNames( names ); + bool res = false; + auto i = names.begin( ); + for( ; i != names.end( ); ++i ) + { + TParameters::Type t = this->m_Parameters->GetType( *i ); + res |= ( t == TParameters::Point ); + res |= ( t == TParameters::Index ); + res |= ( t == TParameters::PointList ); + res |= ( t == TParameters::IndexList ); + + } // rof + return( res ); +} + // ------------------------------------------------------------------------- cpPlugins::Interface::ProcessObject:: TParameters* cpPlugins::Interface::ProcessObject:: @@ -33,6 +53,29 @@ GetParameters( ) const return( this->m_Parameters.GetPointer( ) ); } +// ------------------------------------------------------------------------- +cpPlugins::Interface:: +Plugins* cpPlugins::Interface::ProcessObject:: +GetPlugins( ) +{ + return( this->m_Plugins ); +} + +// ------------------------------------------------------------------------- +const cpPlugins::Interface:: +Plugins* cpPlugins::Interface::ProcessObject:: +GetPlugins( ) const +{ + return( this->m_Plugins ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Interface::ProcessObject:: +SetPlugins( Plugins* p ) +{ + this->m_Plugins = p; +} + // ------------------------------------------------------------------------- unsigned int cpPlugins::Interface::ProcessObject:: GetNumberOfInputs( ) const @@ -161,9 +204,11 @@ cpPlugins::Interface::ProcessObject:: ProcessObject( ) : Superclass( ), m_ITKObject( NULL ), - m_VTKObject( NULL ) + m_VTKObject( NULL ), + m_Plugins( NULL ) { this->m_Parameters = TParameters::New( ); + this->m_Parameters->SetProcessObject( this ); this->m_ParametersDialog = new ParametersQtDialog( ); this->m_ParametersDialog->setTitle( diff --git a/lib/cpPlugins/Interface/ProcessObject.h b/lib/cpPlugins/Interface/ProcessObject.h index 6f38814..49f0907 100644 --- a/lib/cpPlugins/Interface/ProcessObject.h +++ b/lib/cpPlugins/Interface/ProcessObject.h @@ -28,6 +28,7 @@ namespace cpPlugins { namespace Interface { + class Plugins; #ifdef cpPlugins_Interface_QT4 class ParametersQtDialog; #else @@ -64,9 +65,14 @@ namespace cpPlugins // To impact pipeline virtual void Modified( ) const; + virtual bool IsInteractive( ) const; virtual TParameters* GetParameters( ); virtual const TParameters* GetParameters( ) const; + Plugins* GetPlugins( ); + const Plugins* GetPlugins( ) const; + void SetPlugins( Plugins* p ); + virtual unsigned int GetNumberOfInputs( ) const; virtual unsigned int GetNumberOfOutputs( ) const; @@ -133,6 +139,7 @@ namespace cpPlugins Parameters::Pointer m_Parameters; ParametersQtDialog* m_ParametersDialog; + Plugins* m_Plugins; typedef std::map< std::string, DataObject::Pointer > _TDataContainer; _TDataContainer m_Inputs; diff --git a/lib/cpPlugins/Plugins/BasicFilters/OtsuThresholdImageFilter.cxx b/lib/cpPlugins/Plugins/BasicFilters/OtsuThresholdImageFilter.cxx index 8e2f6fb..22df71a 100644 --- a/lib/cpPlugins/Plugins/BasicFilters/OtsuThresholdImageFilter.cxx +++ b/lib/cpPlugins/Plugins/BasicFilters/OtsuThresholdImageFilter.cxx @@ -69,8 +69,8 @@ _RealGD( itk::DataObject* image ) _F* filter = this->_CreateITK< _F >( ); filter->SetInput( dynamic_cast< I* >( image ) ); filter->SetNumberOfHistogramBins( bins ); - filter->SetInsideValue( in_val ); - filter->SetOutsideValue( out_val ); + filter->SetInsideValue( out_val ); // WARNING: these are inverted + filter->SetOutsideValue( in_val ); filter->Update( ); // Connect output -- 2.47.1