1 #include <cpPlugins/Interface/Plugins.h>
2 #include <cpPlugins/Interface/Config.h>
7 #ifdef cpPlugins_Interface_QT4
9 #include <QApplication>
10 #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::
29 Plugins( QWidget* widget )
31 m_LastLoadedPlugin( "." )
35 // -------------------------------------------------------------------------
36 cpPlugins::Interface::Plugins::
39 this->m_Interface.UnloadAll( );
42 // -------------------------------------------------------------------------
43 QWidget* cpPlugins::Interface::Plugins::
46 return( this->m_Widget );
49 // -------------------------------------------------------------------------
50 const QWidget* cpPlugins::Interface::Plugins::
53 return( this->m_Widget );
56 // -------------------------------------------------------------------------
57 void cpPlugins::Interface::Plugins::
58 SetWidget( QWidget* widget )
60 this->m_Widget = widget;
63 // -------------------------------------------------------------------------
64 void cpPlugins::Interface::Plugins::
67 #ifdef cpPlugins_Interface_QT4
68 if( this->m_Widget != NULL )
70 QApplication::setOverrideCursor( Qt::WaitCursor );
71 this->m_Widget->setEnabled( false );
74 #endif // cpPlugins_Interface_QT4
77 // -------------------------------------------------------------------------
78 void cpPlugins::Interface::Plugins::
81 #ifdef cpPlugins_Interface_QT4
82 if( this->m_Widget != NULL )
84 QApplication::restoreOverrideCursor( );
85 this->m_Widget->setEnabled( true );
88 #endif // cpPlugins_Interface_QT4
91 // -------------------------------------------------------------------------
92 void cpPlugins::Interface::Plugins::
95 #ifdef cpPlugins_Interface_QT4
96 if( this->m_Widget != NULL )
98 QFileDialog dialog( this->m_Widget );
99 dialog.setFileMode( QFileDialog::ExistingFile );
100 dialog.setDirectory( this->m_LastLoadedPlugin.c_str( ) );
101 dialog.setNameFilter( QFileDialog::tr( PLUGIN_REGEX ) );
102 dialog.setDefaultSuffix( QFileDialog::tr( PLUGIN_EXT ) );
103 if( !( dialog.exec( ) ) )
106 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
107 if( !( this->LoadPlugins( fname ) ) )
108 QMessageBox::critical(
110 QMessageBox::tr( "Ignoring plugin" ),
111 QMessageBox::tr( fname.c_str( ) )
115 #endif // cpPlugins_Interface_QT4
118 // -------------------------------------------------------------------------
119 void cpPlugins::Interface::Plugins::
120 AssociatePluginsToMenu( QMenu* menu, QObject* obj, const char* slot )
122 #ifdef cpPlugins_Interface_QT4
123 std::map< std::string, std::set< std::string > >::const_iterator i;
124 std::set< std::string >::const_iterator j;
127 for( i = this->m_Filters.begin( ); i != this->m_Filters.end( ); i++ )
129 QMenu* newMenu = menu->addMenu( i->first.c_str( ) );
130 for( j = i->second.begin( ); j != i->second.end( ); ++j )
132 QAction* a = newMenu->addAction( j->c_str( ) );
133 QObject::connect( a, SIGNAL( triggered( ) ), obj, slot );
138 #endif // cpPlugins_Interface_QT4
141 // -------------------------------------------------------------------------
142 bool cpPlugins::Interface::Plugins::
143 LoadPlugins( const std::string& fname )
145 this->BlockWidget( );
147 // Is it already loaded?
149 if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
151 // Was it succesfully loaded?
152 ret = this->m_Interface.Load( fname );
154 // Update a simple track
157 this->m_LoadedPlugins.insert( fname );
158 this->m_LastLoadedPlugin = fname;
165 this->UnblockWidget( );
169 // -------------------------------------------------------------------------
170 bool cpPlugins::Interface::Plugins::
171 LoadPluginsConfigurationFile( const std::string& fname )
173 // Load file into a char buffer
175 fname.c_str( ), std::ios::in | std::ios::binary | std::ios::ate
180 std::streampos size = in.tellg( );
181 char* buffer = new char[ size ];
182 in.seekg( 0, std::ios::beg );
183 in.read( buffer, size );
187 std::stringstream in_stream( buffer );
191 in_stream.getline( line, 4096 );
192 this->LoadPlugins( line );
200 // -------------------------------------------------------------------------
201 cpPlugins::Interface::Plugins::
202 TParameters* cpPlugins::Interface::Plugins::
203 GetImageReaderParameters( )
205 return( this->m_ImageReader->GetParameters( ) );
208 // -------------------------------------------------------------------------
209 cpPlugins::Interface::Plugins::
210 TParameters* cpPlugins::Interface::Plugins::
211 GetMeshReaderParameters( )
213 return( this->m_MeshReader->GetParameters( ) );
216 // -------------------------------------------------------------------------
217 cpPlugins::Interface::Plugins::
218 TParameters* cpPlugins::Interface::Plugins::
219 GetImageWriterParameters( )
221 return( this->m_ImageWriter->GetParameters( ) );
224 // -------------------------------------------------------------------------
225 cpPlugins::Interface::Plugins::
226 TParameters* cpPlugins::Interface::Plugins::
227 GetMeshWriterParameters( )
229 return( this->m_MeshWriter->GetParameters( ) );
232 // -------------------------------------------------------------------------
233 const cpPlugins::Interface::Plugins::
234 TParameters* cpPlugins::Interface::Plugins::
235 GetImageReaderParameters( ) const
237 return( this->m_ImageReader->GetParameters( ) );
240 // -------------------------------------------------------------------------
241 const cpPlugins::Interface::Plugins::
242 TParameters* cpPlugins::Interface::Plugins::
243 GetMeshReaderParameters( ) const
245 return( this->m_MeshReader->GetParameters( ) );
248 // -------------------------------------------------------------------------
249 const cpPlugins::Interface::Plugins::
250 TParameters* cpPlugins::Interface::Plugins::
251 GetImageWriterParameters( ) const
253 return( this->m_ImageWriter->GetParameters( ) );
256 // -------------------------------------------------------------------------
257 const cpPlugins::Interface::Plugins::
258 TParameters* cpPlugins::Interface::Plugins::
259 GetMeshWriterParameters( ) const
261 return( this->m_MeshWriter->GetParameters( ) );
264 // -------------------------------------------------------------------------
265 bool cpPlugins::Interface::Plugins::
266 ReadImage( TImage::Pointer& image, bool exec_qt )
268 std::string ret = "";
269 if( this->m_ImageReader.IsNull( ) )
270 ret = "Plugins: No valid image reader. Please load a valid plugin.";
276 execute = this->m_ImageReader->ExecConfigurationDialog( this->m_Widget );
279 this->BlockWidget( );
280 ret = this->m_ImageReader->Update( );
281 this->UnblockWidget( );
284 image = this->m_ImageReader->GetOutput< TImage >( "Output" );
285 this->m_ImageReader->DisconnectOutputs( );
286 this->m_ImageReader->GetParameters( )->ClearStringList( "FileNames" );
295 // Show an error message and return
298 #ifdef cpPlugins_Interface_QT4
299 if( this->m_Widget != NULL )
300 QMessageBox::critical(
302 QMessageBox::tr( "Error reading image." ),
303 QMessageBox::tr( ret.c_str( ) )
305 #endif // cpPlugins_Interface_QT4
312 // -------------------------------------------------------------------------
313 bool cpPlugins::Interface::Plugins::
314 ReadDicomSeries( TImage::Pointer& image )
316 std::string ret = "";
317 if( this->m_DicomSeriesReader.IsNull( ) )
318 ret = "Plugins: No valid dicom series reader. Please load a valid plugin.";
322 if( this->m_DicomSeriesReader->ExecConfigurationDialog( this->m_Widget ) )
324 this->BlockWidget( );
325 ret = this->m_DicomSeriesReader->Update( );
326 this->UnblockWidget( );
329 image = this->m_DicomSeriesReader->GetOutput< TImage >( "Output" );
330 this->m_DicomSeriesReader->DisconnectOutputs( );
331 this->m_DicomSeriesReader->GetParameters( )->
332 ClearStringList( "FileNames" );
341 // Show an error message and return
344 #ifdef cpPlugins_Interface_QT4
345 if( this->m_Widget != NULL )
346 QMessageBox::critical(
348 QMessageBox::tr( "Error reading dicom series." ),
349 QMessageBox::tr( ret.c_str( ) )
351 #endif // cpPlugins_Interface_QT4
358 // -------------------------------------------------------------------------
359 bool cpPlugins::Interface::Plugins::
360 ReadMesh( TMesh::Pointer& mesh, bool exec_qt )
362 std::string ret = "";
363 if( this->m_MeshReader.IsNull( ) )
364 ret = "Plugins: No valid mesh reader. Please load a valid plugin.";
370 execute = this->m_MeshReader->ExecConfigurationDialog( this->m_Widget );
373 this->BlockWidget( );
374 ret = this->m_MeshReader->Update( );
375 this->UnblockWidget( );
378 mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" );
379 this->m_MeshReader->DisconnectOutputs( );
388 // Show an error message and return
391 #ifdef cpPlugins_Interface_QT4
392 if( this->m_Widget != NULL )
393 QMessageBox::critical(
395 QMessageBox::tr( "Error reading image." ),
396 QMessageBox::tr( ret.c_str( ) )
398 #endif // cpPlugins_Interface_QT4
405 // -------------------------------------------------------------------------
406 bool cpPlugins::Interface::Plugins::
407 WriteImage( TImage* image, bool exec_qt )
409 std::string ret = "";
410 if( this->m_ImageWriter.IsNull( ) )
411 ret = "Plugins: No valid image writer. Please load a valid plugin.";
417 execute = this->m_ImageWriter->ExecConfigurationDialog( this->m_Widget );
420 this->m_ImageWriter->SetInput( "Input", image );
421 this->BlockWidget( );
422 ret = this->m_ImageWriter->Update( );
423 this->UnblockWidget( );
429 // Show an error message and return
432 #ifdef cpPlugins_Interface_QT4
433 if( this->m_Widget != NULL )
434 QMessageBox::critical(
436 QMessageBox::tr( "Error reading image." ),
437 QMessageBox::tr( ret.c_str( ) )
439 #endif // cpPlugins_Interface_QT4
446 // -------------------------------------------------------------------------
447 bool cpPlugins::Interface::Plugins::
448 WriteMesh( TMesh* mesh, bool exec_qt )
450 std::string ret = "";
451 if( this->m_MeshWriter.IsNull( ) )
452 ret = "Plugins: No valid mesh writer. Please load a valid plugin.";
458 execute = this->m_MeshReader->ExecConfigurationDialog( this->m_Widget );
461 this->m_MeshWriter->SetInput( 0, mesh );
462 this->BlockWidget( );
463 ret = this->m_MeshWriter->Update( );
464 this->UnblockWidget( );
470 // Show an error message and return
473 #ifdef cpPlugins_Interface_QT4
474 if( this->m_Widget != NULL )
475 QMessageBox::critical(
477 QMessageBox::tr( "Error reading image." ),
478 QMessageBox::tr( ret.c_str( ) )
480 #endif // cpPlugins_Interface_QT4
487 // -------------------------------------------------------------------------
488 bool cpPlugins::Interface::Plugins::
489 CreateFilter( TProcessObject::Pointer& filter, const std::string& name )
491 filter = this->m_Interface.CreateProcessObject( name );
492 return( filter.IsNotNull( ) );
495 // -------------------------------------------------------------------------
496 void cpPlugins::Interface::Plugins::
499 typedef TInterface::TClasses _C;
501 this->m_Filters.clear( );
502 _C& classes = this->m_Interface.GetClasses( );
503 for( _C::const_iterator i = classes.begin( ); i != classes.end( ); ++i )
505 TProcessObject::Pointer o =
506 this->m_Interface.CreateProcessObject( i->first );
507 std::string name = o->GetClassName( );
508 std::string category = o->GetClassCategory( );
509 if( category == "ImageReader" )
510 this->m_ImageReader = o;
511 else if( category == "ImageWriter" )
512 this->m_ImageWriter = o;
513 else if( category == "MeshReader" )
514 this->m_MeshReader = o;
515 else if( category == "MeshWriter" )
516 this->m_MeshWriter = o;
517 else if( category == "DicomSeriesReader" )
518 this->m_DicomSeriesReader = o;
520 this->m_Filters[ category ].insert( name );