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 )
146 this->BlockWidget( );
148 // Load all plugins from given folder
149 std::list< std::string > files =
150 this->m_Interface.LoadFromFolder( path, r );
152 // Update a simple track
154 if( files.size( ) > 0 )
156 for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
158 this->m_LoadedPlugins.insert( *fIt );
159 this->m_LastLoadedPlugin = *fIt;
163 this->_UpdateLoadedPluginsInformation( );
167 this->UnblockWidget( );
171 // -------------------------------------------------------------------------
172 bool cpPlugins::Interface::Plugins::
173 LoadPlugins( const std::string& fname )
175 this->BlockWidget( );
177 // Is it already loaded?
179 if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
181 // Was it succesfully loaded?
182 ret = this->m_Interface.Load( fname );
184 // Update a simple track
187 this->m_LoadedPlugins.insert( fname );
188 this->m_LastLoadedPlugin = fname;
189 this->_UpdateLoadedPluginsInformation( );
195 this->UnblockWidget( );
199 // -------------------------------------------------------------------------
200 bool cpPlugins::Interface::Plugins::
201 LoadPluginsConfigurationFile( const std::string& fname )
203 // Load file into a char buffer
205 fname.c_str( ), std::ios::in | std::ios::binary | std::ios::ate
210 std::streampos size = in.tellg( );
211 char* buffer = new char[ size ];
212 in.seekg( 0, std::ios::beg );
213 in.read( buffer, size );
217 std::stringstream in_stream( buffer );
221 in_stream.getline( line, 4096 );
222 this->LoadPlugins( line );
230 // -------------------------------------------------------------------------
231 const cpPlugins::Interface::Plugins::
232 TStringContainer& cpPlugins::Interface::Plugins::
233 GetLoadedPlugins( ) const
235 return( this->m_LoadedPlugins );
238 // -------------------------------------------------------------------------
239 void cpPlugins::Interface::Plugins::
240 GetLoadedCategories( TStringContainer& categories ) const
243 auto fIt = this->m_LoadedFilters.begin( );
244 for( ; fIt != this->m_LoadedFilters.end( ); ++fIt )
245 categories.insert( fIt->first );
248 // -------------------------------------------------------------------------
249 void cpPlugins::Interface::Plugins::
250 GetLoadedFilters( TStringContainer& filters ) const
253 auto pIt = this->m_LoadedFilters.begin( );
254 for( ; pIt != this->m_LoadedFilters.end( ); ++pIt )
255 for( auto fIt = pIt->second.begin( ); fIt != pIt->second.end( ); ++fIt )
256 filters.insert( *fIt );
259 // -------------------------------------------------------------------------
260 const cpPlugins::Interface::Plugins::
261 TStringContainer& cpPlugins::Interface::Plugins::
262 GetLoadedFilters( const std::string& category ) const
264 static const TStringContainer EMPTY;
265 auto pIt = this->m_LoadedFilters.find( category );
266 if( pIt != this->m_LoadedFilters.end( ) )
267 return( pIt->second );
272 // -------------------------------------------------------------------------
273 void cpPlugins::Interface::Plugins::
274 AddInteractor( vtkRenderWindowInteractor* interactor )
276 this->m_Interactors.insert( interactor );
279 // -------------------------------------------------------------------------
280 void cpPlugins::Interface::Plugins::
281 RemoveInteractor( vtkRenderWindowInteractor* interactor )
283 this->m_Interactors.erase( interactor );
286 // -------------------------------------------------------------------------
287 void cpPlugins::Interface::Plugins::
290 this->m_Interactors.clear( );
293 // -------------------------------------------------------------------------
294 #define cpPlugins_Plugins_HasMacro( F ) \
295 bool cpPlugins::Interface::Plugins:: \
298 return( this->m_IOFilters.find( #F ) != this->m_IOFilters.end( ) ); \
301 cpPlugins_Plugins_HasMacro( ImageReader );
302 cpPlugins_Plugins_HasMacro( DicomSeriesReader );
303 cpPlugins_Plugins_HasMacro( MeshReader );
304 cpPlugins_Plugins_HasMacro( ImageWriter );
305 cpPlugins_Plugins_HasMacro( MeshWriter );
307 // -------------------------------------------------------------------------
308 std::string cpPlugins::Interface::Plugins::
309 ReadImage( const std::string& fname, const std::string& parent )
311 std::vector< std::string > fnames( 1, fname );
312 return( this->ReadImage( fnames, parent ) );
315 // -------------------------------------------------------------------------
316 std::string cpPlugins::Interface::Plugins::
318 const std::vector< std::string >& fnames, const std::string& parent
322 this->_ActivateIOFilter( "ImageReader" );
325 TParameters* params = this->GetActiveFilterParameters( );
326 params->ClearStringList( "FileNames" );
327 for( auto nIt = fnames.begin( ); nIt != fnames.end( ); ++nIt )
328 params->AddToStringList( "FileNames", *nIt );
331 return( this->_ReadData( parent ) );
334 // -------------------------------------------------------------------------
335 std::string cpPlugins::Interface::Plugins::
336 ReadImage( const std::string& parent )
339 this->_ActivateIOFilter( "ImageReader" );
341 // Try to configure source
342 if( this->ConfigureActiveFilter( ) == TProcessObject::DialogResult_Cancel )
344 this->DeactivateFilter( );
350 return( this->_ReadData( parent ) );
353 // -------------------------------------------------------------------------
354 std::string cpPlugins::Interface::Plugins::
355 ReadDicomSeries( const std::string& parent )
358 this->_ActivateIOFilter( "DicomSeriesReader" );
360 // Try to configure source
361 if( this->ConfigureActiveFilter( ) == TProcessObject::DialogResult_Cancel )
363 this->DeactivateFilter( );
369 return( this->_ReadData( parent ) );
372 // -------------------------------------------------------------------------
373 std::string cpPlugins::Interface::Plugins::
374 ReadMesh( const std::string& fname, const std::string& parent )
377 this->_ActivateIOFilter( "MeshReader" );
380 TParameters* params = this->GetActiveFilterParameters( );
381 params->SetString( "FileName", fname );
384 return( this->_ReadData( parent ) );
387 // -------------------------------------------------------------------------
388 std::string cpPlugins::Interface::Plugins::
389 ReadMesh( const std::string& parent )
392 this->_ActivateIOFilter( "MeshReader" );
394 // Try to configure source
395 if( this->ConfigureActiveFilter( ) == TProcessObject::DialogResult_Cancel )
397 this->DeactivateFilter( );
403 return( this->_ReadData( parent ) );
406 // -------------------------------------------------------------------------
407 bool cpPlugins::Interface::Plugins::
408 WriteDataObject( const std::string& fname, const std::string& name )
410 typedef cpPlugins::Interface::Image _TImage;
411 typedef cpPlugins::Interface::Mesh _TMesh;
414 TDataObject* obj = this->GetData< TDataObject >( name );
415 if( dynamic_cast< cpPlugins::Interface::Image* >( obj ) != NULL )
416 this->_ActivateIOFilter( "ImageWriter" );
417 else if( dynamic_cast< cpPlugins::Interface::Mesh* >( obj ) != NULL )
418 this->_ActivateIOFilter( "MeshWriter" );
421 this->DeactivateFilter( );
427 TParameters* params = this->GetActiveFilterParameters( );
428 params->SetString( "FileName", fname );
431 return( this->_WriteData( name ) );
434 // -------------------------------------------------------------------------
435 bool cpPlugins::Interface::Plugins::
436 WriteDataObject( const std::string& name )
438 typedef cpPlugins::Interface::Image _TImage;
439 typedef cpPlugins::Interface::Mesh _TMesh;
442 TDataObject* obj = this->GetData< TDataObject >( name );
443 if( dynamic_cast< cpPlugins::Interface::Image* >( obj ) != NULL )
444 this->_ActivateIOFilter( "ImageWriter" );
445 else if( dynamic_cast< cpPlugins::Interface::Mesh* >( obj ) != NULL )
446 this->_ActivateIOFilter( "MeshWriter" );
449 this->DeactivateFilter( );
454 // Try to configure source
455 if( this->ConfigureActiveFilter( ) == TProcessObject::DialogResult_Cancel )
457 this->DeactivateFilter( );
463 return( this->_WriteData( name ) );
466 // -------------------------------------------------------------------------
467 void cpPlugins::Interface::Plugins::
470 this->m_DataObjects.clear( );
473 // -------------------------------------------------------------------------
474 void cpPlugins::Interface::Plugins::
475 DeleteDataObject( const std::string& name )
477 // Find and delete object
478 auto oIt = this->m_DataObjects.find( name );
479 if( oIt == this->m_DataObjects.end( ) )
481 this->m_DataObjects.erase( oIt );
484 TStringContainer children;
485 this->GetChildren( children, name );
486 auto cIt = children.begin( );
487 for( ; cIt != children.end( ); ++cIt )
488 this->DeleteDataObject( *cIt );
491 // -------------------------------------------------------------------------
492 void cpPlugins::Interface::Plugins::
493 GetDataObjects( TStringContainer& names )
496 auto oIt = this->m_DataObjects.begin( );
497 for( ; oIt != this->m_DataObjects.end( ); ++oIt )
498 names.insert( oIt->first );
501 // -------------------------------------------------------------------------
502 std::string cpPlugins::Interface::Plugins::
503 GetParent( const std::string& name ) const
505 // Find and delete object
506 auto oIt = this->m_DataObjects.find( name );
507 if( oIt != this->m_DataObjects.end( ) )
508 return( oIt->second.first );
513 // -------------------------------------------------------------------------
514 void cpPlugins::Interface::Plugins::
515 GetChildren( TStringContainer& names, const std::string& name ) const
518 auto oIt = this->m_DataObjects.begin( );
519 for( ; oIt != this->m_DataObjects.end( ); ++oIt )
520 if( oIt->second.first == name )
521 names.insert( oIt->first );
524 // -------------------------------------------------------------------------
525 void cpPlugins::Interface::Plugins::
526 GetRoots( TStringContainer& names ) const
528 this->GetChildren( names, "" );
531 // -------------------------------------------------------------------------
532 bool cpPlugins::Interface::Plugins::
533 ActivateFilter( const std::string& name )
535 this->m_ActiveFilter = this->m_Interface.CreateObject( name );
536 if( this->m_ActiveFilter.IsNotNull( ) )
538 this->m_ActiveFilter->SetPlugins( this );
539 auto iIt = this->m_Interactors.begin( );
540 for( ; iIt != this->m_Interactors.end( ); ++iIt )
541 this->m_ActiveFilter->AddInteractor( *iIt );
548 // -------------------------------------------------------------------------
549 void cpPlugins::Interface::Plugins::
552 if( this->m_ActiveFilter.IsNotNull( ) )
553 this->m_ActiveFilter->DisconnectOutputs( );
554 this->m_ActiveFilter = NULL;
557 // -------------------------------------------------------------------------
558 bool cpPlugins::Interface::Plugins::
559 HasActiveFilter( ) const
561 return( this->m_ActiveFilter.IsNotNull( ) );
564 // -------------------------------------------------------------------------
565 bool cpPlugins::Interface::Plugins::
566 IsActiveFilterInteractive( ) const
568 if( this->m_ActiveFilter.IsNotNull( ) )
569 return( this->m_ActiveFilter->GetInteractive( ) );
574 // -------------------------------------------------------------------------
575 void cpPlugins::Interface::Plugins::
576 GetActiveFilterInputsNames( TStringContainer& names ) const
578 if( this->m_ActiveFilter.IsNotNull( ) )
579 this->m_ActiveFilter->GetInputsNames( names );
582 // -------------------------------------------------------------------------
583 void cpPlugins::Interface::Plugins::
584 GetActiveFilterOutputsNames( TStringContainer& names ) const
586 if( this->m_ActiveFilter.IsNotNull( ) )
587 this->m_ActiveFilter->GetOutputsNames( names );
590 // -------------------------------------------------------------------------
591 bool cpPlugins::Interface::Plugins::
592 ConnectInputInActiveFilter(
593 const std::string& object_name, const std::string& input_name
596 if( this->m_ActiveFilter.IsNotNull( ) )
598 TDataObject* dobj = this->GetData< TDataObject >( object_name );
601 this->m_ActiveFilter->SetInput( input_name, dobj );
610 // -------------------------------------------------------------------------
611 bool cpPlugins::Interface::Plugins::
612 SetOutputNameInActiveFilter(
613 const std::string& new_object_name, const std::string& output_name
616 if( this->m_ActiveFilter.IsNotNull( ) )
618 this->m_ActiveFilter->SetOutputObjectName(
619 new_object_name, output_name
626 // -------------------------------------------------------------------------
627 cpPlugins::Interface::Plugins::
628 TParameters* cpPlugins::Interface::Plugins::
629 GetActiveFilterParameters( )
631 if( this->m_ActiveFilter.IsNotNull( ) )
632 return( this->m_ActiveFilter->GetParameters( ) );
637 // -------------------------------------------------------------------------
638 const cpPlugins::Interface::Plugins::
639 TParameters* cpPlugins::Interface::Plugins::
640 GetActiveFilterParameters( ) const
642 if( this->m_ActiveFilter.IsNotNull( ) )
643 return( this->m_ActiveFilter->GetParameters( ) );
648 // -------------------------------------------------------------------------
649 cpPlugins::Interface::Plugins::
650 TProcessObject::DialogResult cpPlugins::Interface::Plugins::
651 ConfigureActiveFilter( )
653 if( this->m_ActiveFilter.IsNotNull( ) )
654 return( this->m_ActiveFilter->ExecConfigurationDialog( this->m_Widget ) );
656 return( TProcessObject::DialogResult_Cancel );
659 // -------------------------------------------------------------------------
660 bool cpPlugins::Interface::Plugins::
661 UpdateActiveFilter( TStringContainer& outputs, const std::string& parent )
663 if( this->m_ActiveFilter.IsNull( ) )
667 this->BlockWidget( );
668 std::string err = this->m_ActiveFilter->Update( );
669 this->UnblockWidget( );
675 std::set< std::string > names;
676 this->m_ActiveFilter->GetOutputsNames( names );
677 for( auto oIt = names.begin( ); oIt != names.end( ); ++oIt )
680 this->m_ActiveFilter->GetOutput< TDataObject >( *oIt );
682 if( std::string( dobj->GetName( ) ) == std::string( "" ) )
684 this->m_ActiveFilter->GetName( ) + std::string( "_" ) + *oIt
686 this->_InsertNewData( dobj, parent );
687 outputs.insert( dobj->GetName( ) );
690 // this->m_ActiveFilter->DisconnectOutputs( );
695 #ifdef cpPlugins_Interface_QT4
696 if( this->m_Widget != NULL )
697 QMessageBox::critical(
699 QMessageBox::tr( "Error reading image." ),
700 QMessageBox::tr( err.c_str( ) )
703 throw std::runtime_error(
704 std::string( "Error reading image: " ) + err
706 #else // cpPlugins_Interface_QT4
707 throw std::runtime_error(
708 std::string( "Error reading image: " ) + err
710 #endif // cpPlugins_Interface_QT4
716 // -------------------------------------------------------------------------
717 void cpPlugins::Interface::Plugins::
718 _UpdateLoadedPluginsInformation( )
720 this->m_LoadedFilters.clear( );
721 const TInterface::TClasses& cls = this->m_Interface.GetClasses( );
722 for( auto i = cls.begin( ); i != cls.end( ); ++i )
724 TProcessObject::Pointer o = this->m_Interface.CreateObject( i->first );
725 std::string name = o->GetClassName( );
726 std::string category = o->GetClassCategory( );
729 category == "ImageReader" ||
730 category == "ImageWriter" ||
731 category == "MeshReader" ||
732 category == "MeshWriter" ||
733 category == "DicomSeriesReader"
735 this->m_IOFilters[ category ] = o;
737 this->m_LoadedFilters[ category ].insert( name );
742 // -------------------------------------------------------------------------
743 bool cpPlugins::Interface::Plugins::
744 _ActivateIOFilter( const std::string& filter )
747 auto fIt = this->m_IOFilters.find( filter );
748 if( fIt != this->m_IOFilters.end( ) )
750 this->m_ActiveFilter = fIt->second;
757 // -------------------------------------------------------------------------
758 std::string cpPlugins::Interface::Plugins::
759 _ReadData( const std::string& parent )
761 if( !( this->HasActiveFilter( ) ) )
764 TStringContainer outputs;
765 if( this->UpdateActiveFilter( outputs, parent ) )
766 return( *( outputs.begin( ) ) );
771 // ------------------------------------------------------------------------
772 bool cpPlugins::Interface::Plugins::
773 _WriteData( const std::string& name )
775 if( !( this->HasActiveFilter( ) ) )
778 TStringContainer inputs;
779 this->GetActiveFilterInputsNames( inputs );
781 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
782 r &= this->ConnectInputInActiveFilter( name, *iIt );
786 TStringContainer outputs;
787 return( this->UpdateActiveFilter( outputs, "" ) );
793 // -------------------------------------------------------------------------
794 bool cpPlugins::Interface::Plugins::
795 _InsertNewData( TDataObject* dobj, const std::string& parent )
799 auto oIt = this->m_DataObjects.find( parent );
800 if( oIt == this->m_DataObjects.end( ) )
804 this->m_DataObjects[ dobj->GetName( ) ] = _TTreeNode( parent, dobj );