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 // TODO: this causes a segfault? 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.";
274 TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal;
276 dret = this->m_ImageReader->ExecConfigurationDialog( this->m_Widget );
277 if( dret != TProcessObject::DialogResult_Cancel )
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 TProcessObject::DialogResult dret =
323 this->m_DicomSeriesReader->ExecConfigurationDialog( this->m_Widget );
324 if( dret != TProcessObject::DialogResult_Cancel )
326 this->BlockWidget( );
327 ret = this->m_DicomSeriesReader->Update( );
328 this->UnblockWidget( );
331 image = this->m_DicomSeriesReader->GetOutput< TImage >( "Output" );
332 this->m_DicomSeriesReader->DisconnectOutputs( );
333 this->m_DicomSeriesReader->GetParameters( )->
334 ClearStringList( "FileNames" );
343 // Show an error message and return
346 #ifdef cpPlugins_Interface_QT4
347 if( this->m_Widget != NULL )
348 QMessageBox::critical(
350 QMessageBox::tr( "Error reading dicom series." ),
351 QMessageBox::tr( ret.c_str( ) )
353 #endif // cpPlugins_Interface_QT4
360 // -------------------------------------------------------------------------
361 bool cpPlugins::Interface::Plugins::
362 ReadMesh( TMesh::Pointer& mesh, bool exec_qt )
364 std::string ret = "";
365 if( this->m_MeshReader.IsNull( ) )
366 ret = "Plugins: No valid mesh reader. Please load a valid plugin.";
370 TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal;
372 dret = this->m_MeshReader->ExecConfigurationDialog( this->m_Widget );
373 if( dret != TProcessObject::DialogResult_Cancel )
375 this->BlockWidget( );
376 ret = this->m_MeshReader->Update( );
377 this->UnblockWidget( );
380 mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" );
381 this->m_MeshReader->DisconnectOutputs( );
390 // Show an error message and return
393 #ifdef cpPlugins_Interface_QT4
394 if( this->m_Widget != NULL )
395 QMessageBox::critical(
397 QMessageBox::tr( "Error reading image." ),
398 QMessageBox::tr( ret.c_str( ) )
400 #endif // cpPlugins_Interface_QT4
407 // -------------------------------------------------------------------------
408 bool cpPlugins::Interface::Plugins::
409 WriteImage( TImage* image, bool exec_qt )
411 std::string ret = "";
412 if( this->m_ImageWriter.IsNull( ) )
413 ret = "Plugins: No valid image writer. Please load a valid plugin.";
417 TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal;
419 dret = this->m_ImageWriter->ExecConfigurationDialog( this->m_Widget );
420 if( dret != TProcessObject::DialogResult_Cancel )
422 this->m_ImageWriter->SetInput( "Input", image );
423 this->BlockWidget( );
424 ret = this->m_ImageWriter->Update( );
425 this->UnblockWidget( );
431 // Show an error message and return
434 #ifdef cpPlugins_Interface_QT4
435 if( this->m_Widget != NULL )
436 QMessageBox::critical(
438 QMessageBox::tr( "Error reading image." ),
439 QMessageBox::tr( ret.c_str( ) )
441 #endif // cpPlugins_Interface_QT4
448 // -------------------------------------------------------------------------
449 bool cpPlugins::Interface::Plugins::
450 WriteMesh( TMesh* mesh, bool exec_qt )
452 std::string ret = "";
453 if( this->m_MeshWriter.IsNull( ) )
454 ret = "Plugins: No valid mesh writer. Please load a valid plugin.";
458 TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal;
460 dret = this->m_MeshWriter->ExecConfigurationDialog( this->m_Widget );
461 if( dret != TProcessObject::DialogResult_Cancel )
463 this->m_MeshWriter->SetInput( "Input", mesh );
464 this->BlockWidget( );
465 ret = this->m_MeshWriter->Update( );
466 this->UnblockWidget( );
472 // Show an error message and return
475 #ifdef cpPlugins_Interface_QT4
476 if( this->m_Widget != NULL )
477 QMessageBox::critical(
479 QMessageBox::tr( "Error reading image." ),
480 QMessageBox::tr( ret.c_str( ) )
482 #endif // cpPlugins_Interface_QT4
489 // -------------------------------------------------------------------------
490 bool cpPlugins::Interface::Plugins::
491 CreateFilter( TProcessObject::Pointer& filter, const std::string& name )
493 filter = this->m_Interface.CreateProcessObject( name );
494 return( filter.IsNotNull( ) );
497 // -------------------------------------------------------------------------
498 void cpPlugins::Interface::Plugins::
501 typedef TInterface::TClasses _C;
503 this->m_Filters.clear( );
504 _C& classes = this->m_Interface.GetClasses( );
505 for( _C::const_iterator i = classes.begin( ); i != classes.end( ); ++i )
507 TProcessObject::Pointer o =
508 this->m_Interface.CreateProcessObject( i->first );
509 std::string name = o->GetClassName( );
510 std::string category = o->GetClassCategory( );
511 if( category == "ImageReader" )
512 this->m_ImageReader = o;
513 else if( category == "ImageWriter" )
514 this->m_ImageWriter = o;
515 else if( category == "MeshReader" )
516 this->m_MeshReader = o;
517 else if( category == "MeshWriter" )
518 this->m_MeshWriter = o;
519 else if( category == "DicomSeriesReader" )
520 this->m_DicomSeriesReader = o;
522 this->m_Filters[ category ].insert( name );