1 #include <cpPlugins/Interface/Plugins.h>
5 #include <cpPlugins/Interface/Image.h>
6 #include <cpPlugins/Interface/Mesh.h>
8 #ifdef cpPlugins_Interface_QT4
10 #include <QApplication>
11 #include <QFileDialog>
12 #include <QMessageBox>
16 # define PLUGIN_PREFIX ""
17 # define PLUGIN_EXT "dll"
18 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
20 # define PLUGIN_PREFIX "lib"
21 # define PLUGIN_EXT "so"
22 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
25 #endif // cpPlugins_Interface_QT4
27 // -------------------------------------------------------------------------
28 cpPlugins::Interface::Plugins::
31 m_Application( NULL ),
32 m_LastLoadedPlugin( "" ),
33 m_ActiveFilter( NULL )
37 // -------------------------------------------------------------------------
38 cpPlugins::Interface::Plugins::
43 // -------------------------------------------------------------------------
44 QWidget* cpPlugins::Interface::Plugins::
47 return( this->m_Widget );
50 // -------------------------------------------------------------------------
51 const QWidget* cpPlugins::Interface::Plugins::
54 return( this->m_Widget );
57 // -------------------------------------------------------------------------
58 void cpPlugins::Interface::Plugins::
59 SetWidget( QWidget* widget )
61 this->m_Widget = widget;
64 // -------------------------------------------------------------------------
65 void cpPlugins::Interface::Plugins::
68 #ifdef cpPlugins_Interface_QT4
69 if( this->m_Widget != NULL )
71 QApplication::setOverrideCursor( Qt::WaitCursor );
72 this->m_Widget->setEnabled( false );
75 #endif // cpPlugins_Interface_QT4
78 // -------------------------------------------------------------------------
79 void cpPlugins::Interface::Plugins::
82 #ifdef cpPlugins_Interface_QT4
83 if( this->m_Widget != NULL )
85 QApplication::restoreOverrideCursor( );
86 this->m_Widget->setEnabled( true );
89 #endif // cpPlugins_Interface_QT4
92 // -------------------------------------------------------------------------
93 void cpPlugins::Interface::Plugins::
96 #ifdef cpPlugins_Interface_QT4
97 if( this->m_Widget != NULL )
99 QFileDialog dialog( this->m_Widget );
100 dialog.setFileMode( QFileDialog::ExistingFile );
101 dialog.setDirectory( this->m_LastLoadedPlugin.c_str( ) );
102 dialog.setNameFilter( QFileDialog::tr( PLUGIN_REGEX ) );
103 dialog.setDefaultSuffix( QFileDialog::tr( PLUGIN_EXT ) );
104 if( !( dialog.exec( ) ) )
107 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
108 if( !( this->LoadPlugins( fname ) ) )
109 QMessageBox::critical(
111 QMessageBox::tr( "Ignoring plugin" ),
112 QMessageBox::tr( fname.c_str( ) )
116 #endif // cpPlugins_Interface_QT4
119 // -------------------------------------------------------------------------
120 cpPlugins::Interface::
121 BaseApplication* cpPlugins::Interface::Plugins::
124 return( this->m_Application );
127 // -------------------------------------------------------------------------
128 const cpPlugins::Interface::
129 BaseApplication* cpPlugins::Interface::Plugins::
130 GetApplication( ) const
132 return( this->m_Application );
135 // -------------------------------------------------------------------------
136 void cpPlugins::Interface::Plugins::
137 SetApplication( BaseApplication* a )
139 this->m_Application = a;
142 // -------------------------------------------------------------------------
143 bool cpPlugins::Interface::Plugins::
144 LoadPluginsPath( const std::string& path, bool r )
147 this->BlockWidget( );
149 // Load all plugins from given folder
150 std::list< std::string > files =
151 this->m_Interface.LoadFromFolder( path, r );
153 // Update a simple track
155 if( files.size( ) > 0 )
157 for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
159 this->m_LoadedPlugins.insert( *fIt );
160 this->m_LastLoadedPlugin = *fIt;
164 this->_UpdateLoadedPluginsInformation( );
168 this->UnblockWidget( );
174 // -------------------------------------------------------------------------
175 bool cpPlugins::Interface::Plugins::
176 LoadPlugins( const std::string& fname )
178 this->BlockWidget( );
180 // Is it already loaded?
182 if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
184 // Was it succesfully loaded?
185 ret = this->m_Interface.Load( fname );
187 // Update a simple track
190 this->m_LoadedPlugins.insert( fname );
191 this->m_LastLoadedPlugin = fname;
192 this->_UpdateLoadedPluginsInformation( );
198 this->UnblockWidget( );
202 // -------------------------------------------------------------------------
203 bool cpPlugins::Interface::Plugins::
204 LoadPluginsConfigurationFile( const std::string& fname )
206 // Load file into a char buffer
208 fname.c_str( ), std::ios::in | std::ios::binary | std::ios::ate
213 std::streampos size = in.tellg( );
214 char* buffer = new char[ size ];
215 in.seekg( 0, std::ios::beg );
216 in.read( buffer, size );
220 std::stringstream in_stream( buffer );
224 in_stream.getline( line, 4096 );
225 this->LoadPlugins( line );
233 // -------------------------------------------------------------------------
234 const cpPlugins::Interface::Plugins::
235 TStringContainer& cpPlugins::Interface::Plugins::
236 GetLoadedPlugins( ) const
238 return( this->m_LoadedPlugins );
241 // -------------------------------------------------------------------------
242 void cpPlugins::Interface::Plugins::
243 GetLoadedCategories( TStringContainer& categories ) const
246 auto fIt = this->m_LoadedFilters.begin( );
247 for( ; fIt != this->m_LoadedFilters.end( ); ++fIt )
248 categories.insert( fIt->first );
251 // -------------------------------------------------------------------------
252 void cpPlugins::Interface::Plugins::
253 GetLoadedFilters( TStringContainer& filters ) const
256 auto pIt = this->m_LoadedFilters.begin( );
257 for( ; pIt != this->m_LoadedFilters.end( ); ++pIt )
258 for( auto fIt = pIt->second.begin( ); fIt != pIt->second.end( ); ++fIt )
259 filters.insert( *fIt );
262 // -------------------------------------------------------------------------
263 const cpPlugins::Interface::Plugins::
264 TStringContainer& cpPlugins::Interface::Plugins::
265 GetLoadedFilters( const std::string& category ) const
267 static const TStringContainer EMPTY;
268 auto pIt = this->m_LoadedFilters.find( category );
269 if( pIt != this->m_LoadedFilters.end( ) )
270 return( pIt->second );
275 // -------------------------------------------------------------------------
276 void cpPlugins::Interface::Plugins::
277 AddInteractor( vtkRenderWindowInteractor* interactor )
279 this->m_Interactors.insert( interactor );
282 // -------------------------------------------------------------------------
283 void cpPlugins::Interface::Plugins::
284 RemoveInteractor( vtkRenderWindowInteractor* interactor )
286 this->m_Interactors.erase( interactor );
289 // -------------------------------------------------------------------------
290 void cpPlugins::Interface::Plugins::
293 this->m_Interactors.clear( );
296 // -------------------------------------------------------------------------
297 #define cpPlugins_Plugins_HasMacro( F ) \
298 bool cpPlugins::Interface::Plugins:: \
301 return( this->m_IOFilters.find( #F ) != this->m_IOFilters.end( ) ); \
304 cpPlugins_Plugins_HasMacro( ImageReader );
305 cpPlugins_Plugins_HasMacro( DicomSeriesReader );
306 cpPlugins_Plugins_HasMacro( MeshReader );
307 cpPlugins_Plugins_HasMacro( ImageWriter );
308 cpPlugins_Plugins_HasMacro( MeshWriter );
310 // -------------------------------------------------------------------------
311 std::string cpPlugins::Interface::Plugins::
312 ReadImage( const std::string& fname, const std::string& parent )
314 std::vector< std::string > fnames( 1, fname );
315 return( this->ReadImage( fnames, parent ) );
318 // -------------------------------------------------------------------------
319 std::string cpPlugins::Interface::Plugins::
321 const std::vector< std::string >& fnames, const std::string& parent
325 this->_ActivateIOFilter( "ImageReader" );
328 TParameters* params = this->GetActiveFilterParameters( );
329 params->ClearStringList( "FileNames" );
330 for( auto nIt = fnames.begin( ); nIt != fnames.end( ); ++nIt )
331 params->AddToStringList( "FileNames", *nIt );
334 return( this->_ReadData( parent ) );
337 // -------------------------------------------------------------------------
338 std::string cpPlugins::Interface::Plugins::
339 ReadImage( const std::string& parent )
342 this->_ActivateIOFilter( "ImageReader" );
344 // Try to configure source
345 if( !( this->ConfigureActiveFilter( ) ) )
347 this->DeactivateFilter( );
353 return( this->_ReadData( parent ) );
356 // -------------------------------------------------------------------------
357 std::string cpPlugins::Interface::Plugins::
358 ReadDicomSeries( const std::string& parent )
361 this->_ActivateIOFilter( "DicomSeriesReader" );
363 // Try to configure source
364 if( !( this->ConfigureActiveFilter( ) ) )
366 this->DeactivateFilter( );
372 return( this->_ReadData( parent ) );
375 // -------------------------------------------------------------------------
376 std::string cpPlugins::Interface::Plugins::
377 ReadMesh( const std::string& fname, const std::string& parent )
380 this->_ActivateIOFilter( "MeshReader" );
383 TParameters* params = this->GetActiveFilterParameters( );
384 params->SetString( "FileName", fname );
387 return( this->_ReadData( parent ) );
390 // -------------------------------------------------------------------------
391 std::string cpPlugins::Interface::Plugins::
392 ReadMesh( const std::string& parent )
395 this->_ActivateIOFilter( "MeshReader" );
397 // Try to configure source
398 if( !( this->ConfigureActiveFilter( ) ) )
400 this->DeactivateFilter( );
406 return( this->_ReadData( parent ) );
409 // -------------------------------------------------------------------------
410 bool cpPlugins::Interface::Plugins::
411 WriteDataObject( const std::string& fname, const std::string& name )
413 typedef cpPlugins::Interface::Image _TImage;
414 typedef cpPlugins::Interface::Mesh _TMesh;
417 TDataObject* obj = this->GetData< TDataObject >( name );
418 if( dynamic_cast< cpPlugins::Interface::Image* >( obj ) != NULL )
419 this->_ActivateIOFilter( "ImageWriter" );
420 else if( dynamic_cast< cpPlugins::Interface::Mesh* >( obj ) != NULL )
421 this->_ActivateIOFilter( "MeshWriter" );
424 this->DeactivateFilter( );
430 TParameters* params = this->GetActiveFilterParameters( );
431 params->SetString( "FileName", fname );
434 return( this->_WriteData( name ) );
437 // -------------------------------------------------------------------------
438 bool cpPlugins::Interface::Plugins::
439 WriteDataObject( const std::string& name )
441 typedef cpPlugins::Interface::Image _TImage;
442 typedef cpPlugins::Interface::Mesh _TMesh;
445 TDataObject* obj = this->GetData< TDataObject >( name );
446 if( dynamic_cast< cpPlugins::Interface::Image* >( obj ) != NULL )
447 this->_ActivateIOFilter( "ImageWriter" );
448 else if( dynamic_cast< cpPlugins::Interface::Mesh* >( obj ) != NULL )
449 this->_ActivateIOFilter( "MeshWriter" );
452 this->DeactivateFilter( );
457 // Try to configure source
458 if( !( this->ConfigureActiveFilter( ) ) )
460 this->DeactivateFilter( );
466 return( this->_WriteData( name ) );
469 // -------------------------------------------------------------------------
470 void cpPlugins::Interface::Plugins::
473 this->m_DataObjects.clear( );
476 // -------------------------------------------------------------------------
477 void cpPlugins::Interface::Plugins::
478 DeleteDataObject( const std::string& name )
480 // Find and delete object
481 auto oIt = this->m_DataObjects.find( name );
482 if( oIt == this->m_DataObjects.end( ) )
484 this->m_DataObjects.erase( oIt );
487 TStringContainer children;
488 this->GetChildren( children, name );
489 auto cIt = children.begin( );
490 for( ; cIt != children.end( ); ++cIt )
491 this->DeleteDataObject( *cIt );
494 // -------------------------------------------------------------------------
495 void cpPlugins::Interface::Plugins::
496 GetDataObjects( TStringContainer& names )
499 auto oIt = this->m_DataObjects.begin( );
500 for( ; oIt != this->m_DataObjects.end( ); ++oIt )
501 names.insert( oIt->first );
504 // -------------------------------------------------------------------------
505 std::string cpPlugins::Interface::Plugins::
506 GetParent( const std::string& name ) const
508 // Find and delete object
509 auto oIt = this->m_DataObjects.find( name );
510 if( oIt != this->m_DataObjects.end( ) )
511 return( oIt->second.first );
516 // -------------------------------------------------------------------------
517 void cpPlugins::Interface::Plugins::
518 GetChildren( TStringContainer& names, const std::string& name ) const
521 auto oIt = this->m_DataObjects.begin( );
522 for( ; oIt != this->m_DataObjects.end( ); ++oIt )
523 if( oIt->second.first == name )
524 names.insert( oIt->first );
527 // -------------------------------------------------------------------------
528 void cpPlugins::Interface::Plugins::
529 GetRoots( TStringContainer& names ) const
531 this->GetChildren( names, "" );
534 // -------------------------------------------------------------------------
535 bool cpPlugins::Interface::Plugins::
536 ActivateFilter( const std::string& name )
538 this->m_ActiveFilter = this->m_Interface.CreateObject( name );
539 if( this->m_ActiveFilter.IsNotNull( ) )
541 this->m_ActiveFilter->SetPlugins( this );
542 auto iIt = this->m_Interactors.begin( );
543 for( ; iIt != this->m_Interactors.end( ); ++iIt )
544 this->m_ActiveFilter->AddInteractor( *iIt );
551 // -------------------------------------------------------------------------
552 void cpPlugins::Interface::Plugins::
555 if( this->m_ActiveFilter.IsNotNull( ) )
556 this->m_ActiveFilter->DisconnectOutputs( );
557 this->m_ActiveFilter = NULL;
560 // -------------------------------------------------------------------------
561 bool cpPlugins::Interface::Plugins::
562 HasActiveFilter( ) const
564 return( this->m_ActiveFilter.IsNotNull( ) );
567 // -------------------------------------------------------------------------
568 bool cpPlugins::Interface::Plugins::
569 IsActiveFilterInteractive( ) const
571 if( this->m_ActiveFilter.IsNotNull( ) )
572 return( this->m_ActiveFilter->GetInteractive( ) );
577 // -------------------------------------------------------------------------
578 void cpPlugins::Interface::Plugins::
579 GetActiveFilterInputsNames( TStringContainer& names ) const
581 if( this->m_ActiveFilter.IsNotNull( ) )
582 this->m_ActiveFilter->GetInputsNames( names );
585 // -------------------------------------------------------------------------
586 void cpPlugins::Interface::Plugins::
587 GetActiveFilterOutputsNames( TStringContainer& names ) const
589 if( this->m_ActiveFilter.IsNotNull( ) )
590 this->m_ActiveFilter->GetOutputsNames( names );
593 // -------------------------------------------------------------------------
594 bool cpPlugins::Interface::Plugins::
595 ConnectInputInActiveFilter(
596 const std::string& object_name, const std::string& input_name
599 if( this->m_ActiveFilter.IsNotNull( ) )
601 TDataObject* dobj = this->GetData< TDataObject >( object_name );
604 this->m_ActiveFilter->SetInput( input_name, dobj );
613 // -------------------------------------------------------------------------
614 bool cpPlugins::Interface::Plugins::
615 SetOutputNameInActiveFilter(
616 const std::string& new_object_name, const std::string& output_name
619 if( this->m_ActiveFilter.IsNotNull( ) )
621 this->m_ActiveFilter->SetOutputObjectName(
622 new_object_name, output_name
629 // -------------------------------------------------------------------------
630 cpPlugins::Interface::Plugins::
631 TParameters* cpPlugins::Interface::Plugins::
632 GetActiveFilterParameters( )
634 if( this->m_ActiveFilter.IsNotNull( ) )
635 return( this->m_ActiveFilter->GetParameters( ) );
640 // -------------------------------------------------------------------------
641 const cpPlugins::Interface::Plugins::
642 TParameters* cpPlugins::Interface::Plugins::
643 GetActiveFilterParameters( ) const
645 if( this->m_ActiveFilter.IsNotNull( ) )
646 return( this->m_ActiveFilter->GetParameters( ) );
651 // -------------------------------------------------------------------------
652 cpPlugins::Interface::Plugins::
653 TProcessObject::DialogResult cpPlugins::Interface::Plugins::
654 ConfigureActiveFilter( )
656 if( this->m_ActiveFilter.IsNotNull( ) )
657 return( this->m_ActiveFilter->ExecConfigurationDialog( this->m_Widget ) );
662 // -------------------------------------------------------------------------
663 bool cpPlugins::Interface::Plugins::
664 UpdateActiveFilter( TStringContainer& outputs, const std::string& parent )
666 if( this->m_ActiveFilter.IsNull( ) )
670 this->BlockWidget( );
671 std::string err = this->m_ActiveFilter->Update( );
672 this->UnblockWidget( );
678 std::set< std::string > names;
679 this->m_ActiveFilter->GetOutputsNames( names );
680 for( auto oIt = names.begin( ); oIt != names.end( ); ++oIt )
683 this->m_ActiveFilter->GetOutput< TDataObject >( *oIt );
685 if( std::string( dobj->GetName( ) ) == std::string( "" ) )
687 this->m_ActiveFilter->GetName( ) + std::string( "_" ) + *oIt
689 this->_InsertNewData( dobj, parent );
690 outputs.insert( dobj->GetName( ) );
693 // this->m_ActiveFilter->DisconnectOutputs( );
698 #ifdef cpPlugins_Interface_QT4
699 if( this->m_Widget != NULL )
700 QMessageBox::critical(
702 QMessageBox::tr( "Error reading image." ),
703 QMessageBox::tr( err.c_str( ) )
706 throw std::runtime_error(
707 std::string( "Error reading image: " ) + err
709 #else // cpPlugins_Interface_QT4
710 throw std::runtime_error(
711 std::string( "Error reading image: " ) + err
713 #endif // cpPlugins_Interface_QT4
719 // -------------------------------------------------------------------------
720 void cpPlugins::Interface::Plugins::
721 _UpdateLoadedPluginsInformation( )
723 this->m_LoadedFilters.clear( );
724 const TInterface::TClasses& cls = this->m_Interface.GetClasses( );
725 for( auto i = cls.begin( ); i != cls.end( ); ++i )
727 TProcessObject::Pointer o = this->m_Interface.CreateObject( i->first );
728 std::string name = o->GetClassName( );
729 std::string category = o->GetClassCategory( );
732 category == "ImageReader" ||
733 category == "ImageWriter" ||
734 category == "MeshReader" ||
735 category == "MeshWriter" ||
736 category == "DicomSeriesReader"
738 this->m_IOFilters[ category ] = o;
740 this->m_LoadedFilters[ category ].insert( name );
745 // -------------------------------------------------------------------------
746 bool cpPlugins::Interface::Plugins::
747 _ActivateIOFilter( const std::string& filter )
750 auto fIt = this->m_IOFilters.find( filter );
751 if( fIt != this->m_IOFilters.end( ) )
753 this->m_ActiveFilter = fIt->second;
760 // -------------------------------------------------------------------------
761 std::string cpPlugins::Interface::Plugins::
762 _ReadData( const std::string& parent )
764 if( !( this->HasActiveFilter( ) ) )
767 TStringContainer outputs;
768 if( this->UpdateActiveFilter( outputs, parent ) )
769 return( *( outputs.begin( ) ) );
774 // ------------------------------------------------------------------------
775 bool cpPlugins::Interface::Plugins::
776 _WriteData( const std::string& name )
778 if( !( this->HasActiveFilter( ) ) )
781 TStringContainer inputs;
782 this->GetActiveFilterInputsNames( inputs );
784 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
785 r &= this->ConnectInputInActiveFilter( name, *iIt );
789 TStringContainer outputs;
790 return( this->UpdateActiveFilter( outputs, "" ) );
796 // -------------------------------------------------------------------------
797 bool cpPlugins::Interface::Plugins::
798 _InsertNewData( TDataObject* dobj, const std::string& parent )
802 auto oIt = this->m_DataObjects.find( parent );
803 if( oIt == this->m_DataObjects.end( ) )
807 this->m_DataObjects[ dobj->GetName( ) ] = _TTreeNode( parent, dobj );