]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Plugins.cxx
Merge branch 'master' of ssh://git.creatis.insa-lyon.fr/cpPlugins
[cpPlugins.git] / lib / cpPlugins / Interface / Plugins.cxx
1 #include <cpPlugins/Interface/Plugins.h>
2 #include <cpPlugins/Interface/Config.h>
3
4 #include <fstream>
5 #include <sstream>
6
7 #ifdef cpPlugins_Interface_QT4
8
9 #include <QApplication>
10 #include <QFileDialog>
11 #include <QMenu>
12 #include <QMessageBox>
13 #include <QWidget>
14
15 #ifdef _WIN32
16 #  define PLUGIN_PREFIX ""
17 #  define PLUGIN_EXT "dll"
18 #  define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
19 #else // Linux
20 #  define PLUGIN_PREFIX "lib"
21 #  define PLUGIN_EXT "so"
22 #  define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
23 #endif // _WIN32
24
25 #endif // cpPlugins_Interface_QT4
26
27 // -------------------------------------------------------------------------
28 cpPlugins::Interface::Plugins::
29 Plugins( QWidget* widget )
30   : m_Widget( widget ),
31     m_LastLoadedPlugin( "." )
32 {
33 }
34
35 // -------------------------------------------------------------------------
36 cpPlugins::Interface::Plugins::
37 ~Plugins( )
38 {
39   // TODO: this causes a segfault? this->m_Interface.UnloadAll( );
40 }
41
42 // -------------------------------------------------------------------------
43 QWidget* cpPlugins::Interface::Plugins::
44 GetWidget( )
45 {
46   return( this->m_Widget );
47 }
48
49 // -------------------------------------------------------------------------
50 const QWidget* cpPlugins::Interface::Plugins::
51 GetWidget( ) const
52 {
53   return( this->m_Widget );
54 }
55
56 // -------------------------------------------------------------------------
57 void cpPlugins::Interface::Plugins::
58 SetWidget( QWidget* widget )
59 {
60   this->m_Widget = widget;
61 }
62
63 // -------------------------------------------------------------------------
64 void cpPlugins::Interface::Plugins::
65 BlockWidget( )
66 {
67 #ifdef cpPlugins_Interface_QT4
68   if( this->m_Widget != NULL )
69   {
70     QApplication::setOverrideCursor( Qt::WaitCursor );
71     this->m_Widget->setEnabled( false );
72
73   } // fi
74 #endif // cpPlugins_Interface_QT4
75 }
76
77 // -------------------------------------------------------------------------
78 void cpPlugins::Interface::Plugins::
79 UnblockWidget( )
80 {
81 #ifdef cpPlugins_Interface_QT4
82   if( this->m_Widget != NULL )
83   {
84     QApplication::restoreOverrideCursor( );
85     this->m_Widget->setEnabled( true );
86
87   } // fi
88 #endif // cpPlugins_Interface_QT4
89 }
90
91 // -------------------------------------------------------------------------
92 void cpPlugins::Interface::Plugins::
93 DialogLoadPlugins( )
94 {
95 #ifdef cpPlugins_Interface_QT4
96   if( this->m_Widget != NULL )
97   {
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( ) ) )
104       return;
105
106     std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
107     if( !( this->LoadPlugins( fname ) ) )
108       QMessageBox::critical(
109         this->m_Widget,
110         QMessageBox::tr( "Ignoring plugin" ),
111         QMessageBox::tr( fname.c_str( ) )
112         );
113
114   } // fi
115 #endif // cpPlugins_Interface_QT4
116 }
117
118 // -------------------------------------------------------------------------
119 void cpPlugins::Interface::Plugins::
120 AssociatePluginsToMenu( QMenu* menu, QObject* obj, const char* slot )
121 {
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;
125
126   menu->clear( );
127   for( i = this->m_Filters.begin( ); i != this->m_Filters.end( ); i++ )
128   {
129     QMenu* newMenu = menu->addMenu( i->first.c_str( ) );
130     for( j = i->second.begin( ); j != i->second.end( ); ++j )
131     {
132       QAction* a = newMenu->addAction( j->c_str( ) );
133       QObject::connect( a, SIGNAL( triggered( ) ), obj, slot );
134
135     } // rof
136
137   } // rof
138 #endif // cpPlugins_Interface_QT4
139 }
140
141 // -------------------------------------------------------------------------
142 bool cpPlugins::Interface::Plugins::
143 LoadPlugins( const std::string& fname )
144 {
145   this->BlockWidget( );
146
147   // Is it already loaded?
148   bool ret = true;
149   if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
150   {
151     // Was it succesfully loaded?
152     ret = this->m_Interface.Load( fname );
153
154     // Update a simple track
155     if( ret )
156     {
157       this->m_LoadedPlugins.insert( fname );
158       this->m_LastLoadedPlugin = fname;
159       this->_Update( );
160
161     } // fi
162
163   } // fi
164
165   this->UnblockWidget( );
166   return( ret );
167 }
168
169 // -------------------------------------------------------------------------
170 bool cpPlugins::Interface::Plugins::
171 LoadPluginsConfigurationFile( const std::string& fname )
172 {
173   // Load file into a char buffer
174   std::ifstream in(
175     fname.c_str( ), std::ios::in | std::ios::binary | std::ios::ate
176     );
177   if( !in.is_open( ) )
178     return( false );
179
180   std::streampos size = in.tellg( );
181   char* buffer = new char[ size ];
182   in.seekg( 0, std::ios::beg );
183   in.read( buffer, size );
184   in.close( );
185
186   // Read stream
187   std::stringstream in_stream( buffer );
188   char line[ 4096 ];
189   while( in_stream )
190   {
191     in_stream.getline( line, 4096 );
192     this->LoadPlugins( line );
193
194   } // elihw
195   delete buffer;
196
197   return( true );
198 }
199
200 // -------------------------------------------------------------------------
201 cpPlugins::Interface::Plugins::
202 TParameters* cpPlugins::Interface::Plugins::
203 GetImageReaderParameters( )
204 {
205   return( this->m_ImageReader->GetParameters( ) );
206 }
207
208 // -------------------------------------------------------------------------
209 cpPlugins::Interface::Plugins::
210 TParameters* cpPlugins::Interface::Plugins::
211 GetMeshReaderParameters( )
212 {
213   return( this->m_MeshReader->GetParameters( ) );
214 }
215
216 // -------------------------------------------------------------------------
217 cpPlugins::Interface::Plugins::
218 TParameters* cpPlugins::Interface::Plugins::
219 GetImageWriterParameters( )
220 {
221   return( this->m_ImageWriter->GetParameters( ) );
222 }
223
224 // -------------------------------------------------------------------------
225 cpPlugins::Interface::Plugins::
226 TParameters* cpPlugins::Interface::Plugins::
227 GetMeshWriterParameters( )
228 {
229   return( this->m_MeshWriter->GetParameters( ) );
230 }
231
232 // -------------------------------------------------------------------------
233 const cpPlugins::Interface::Plugins::
234 TParameters* cpPlugins::Interface::Plugins::
235 GetImageReaderParameters( ) const
236 {
237   return( this->m_ImageReader->GetParameters( ) );
238 }
239
240 // -------------------------------------------------------------------------
241 const cpPlugins::Interface::Plugins::
242 TParameters* cpPlugins::Interface::Plugins::
243 GetMeshReaderParameters( ) const
244 {
245   return( this->m_MeshReader->GetParameters( ) );
246 }
247
248 // -------------------------------------------------------------------------
249 const cpPlugins::Interface::Plugins::
250 TParameters* cpPlugins::Interface::Plugins::
251 GetImageWriterParameters( ) const
252 {
253   return( this->m_ImageWriter->GetParameters( ) );
254 }
255
256 // -------------------------------------------------------------------------
257 const cpPlugins::Interface::Plugins::
258 TParameters* cpPlugins::Interface::Plugins::
259 GetMeshWriterParameters( ) const
260 {
261   return( this->m_MeshWriter->GetParameters( ) );
262 }
263
264 // -------------------------------------------------------------------------
265 bool cpPlugins::Interface::Plugins::
266 ReadImage( TImage::Pointer& image, bool exec_qt )
267 {
268   std::string ret = "";
269   if( this->m_ImageReader.IsNull( ) )
270     ret = "Plugins: No valid image reader. Please load a valid plugin.";
271
272   if( ret == "" )
273   {
274     bool execute = true;
275     if( exec_qt )
276       execute = this->m_ImageReader->ExecConfigurationDialog( this->m_Widget );
277     if( execute )
278     {
279       this->BlockWidget( );
280       ret = this->m_ImageReader->Update( );
281       this->UnblockWidget( );
282       if( ret == "" )
283       {
284         image = this->m_ImageReader->GetOutput< TImage >( "Output" );
285         this->m_ImageReader->DisconnectOutputs( );
286         this->m_ImageReader->GetParameters( )->ClearStringList( "FileNames" );
287       }
288       else
289         image = NULL;
290
291     } // fi
292
293   } // fi
294
295   // Show an error message and return
296   if( ret != "" )
297   {
298 #ifdef cpPlugins_Interface_QT4
299     if( this->m_Widget != NULL )
300       QMessageBox::critical(
301         this->m_Widget,
302         QMessageBox::tr( "Error reading image." ),
303         QMessageBox::tr( ret.c_str( ) )
304         );
305 #endif // cpPlugins_Interface_QT4
306     return( false );
307   }
308   else
309     return( true );
310 }
311
312 // -------------------------------------------------------------------------
313 bool cpPlugins::Interface::Plugins::
314 ReadDicomSeries( TImage::Pointer& image )
315 {
316   std::string ret = "";
317   if( this->m_DicomSeriesReader.IsNull( ) )
318     ret = "Plugins: No valid dicom series reader. Please load a valid plugin.";
319
320   if( ret == "" )
321   {
322     if( this->m_DicomSeriesReader->ExecConfigurationDialog( this->m_Widget ) )
323     {
324       this->BlockWidget( );
325       ret = this->m_DicomSeriesReader->Update( );
326       this->UnblockWidget( );
327       if( ret == "" )
328       {
329         image = this->m_DicomSeriesReader->GetOutput< TImage >( "Output" );
330         this->m_DicomSeriesReader->DisconnectOutputs( );
331         this->m_DicomSeriesReader->GetParameters( )->
332           ClearStringList( "FileNames" );
333       }
334       else
335         image = NULL;
336
337     } // fi
338
339   } // fi
340
341   // Show an error message and return
342   if( ret != "" )
343   {
344 #ifdef cpPlugins_Interface_QT4
345     if( this->m_Widget != NULL )
346       QMessageBox::critical(
347         this->m_Widget,
348         QMessageBox::tr( "Error reading dicom series." ),
349         QMessageBox::tr( ret.c_str( ) )
350         );
351 #endif // cpPlugins_Interface_QT4
352     return( false );
353   }
354   else
355     return( true );
356 }
357
358 // -------------------------------------------------------------------------
359 bool cpPlugins::Interface::Plugins::
360 ReadMesh( TMesh::Pointer& mesh, bool exec_qt )
361 {
362   std::string ret = "";
363   if( this->m_MeshReader.IsNull( ) )
364     ret = "Plugins: No valid mesh reader. Please load a valid plugin.";
365
366   if( ret == "" )
367   {
368     bool execute = true;
369     if( exec_qt )
370       execute = this->m_MeshReader->ExecConfigurationDialog( this->m_Widget );
371     if( execute )
372     {
373       this->BlockWidget( );
374       ret = this->m_MeshReader->Update( );
375       this->UnblockWidget( );
376       if( ret == "" )
377       {
378         mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" );
379         this->m_MeshReader->DisconnectOutputs( );
380       }
381       else
382         mesh = NULL;
383
384     } // fi
385
386   } // fi
387
388   // Show an error message and return
389   if( ret != "" )
390   {
391 #ifdef cpPlugins_Interface_QT4
392     if( this->m_Widget != NULL )
393       QMessageBox::critical(
394         this->m_Widget,
395         QMessageBox::tr( "Error reading image." ),
396         QMessageBox::tr( ret.c_str( ) )
397         );
398 #endif // cpPlugins_Interface_QT4
399     return( false );
400   }
401   else
402     return( true );
403 }
404
405 // -------------------------------------------------------------------------
406 bool cpPlugins::Interface::Plugins::
407 WriteImage( TImage* image, bool exec_qt )
408 {
409   std::string ret = "";
410   if( this->m_ImageWriter.IsNull( ) )
411     ret = "Plugins: No valid image writer. Please load a valid plugin.";
412
413   if( ret == "" )
414   {
415     bool execute = true;
416     if( exec_qt )
417       execute = this->m_ImageWriter->ExecConfigurationDialog( this->m_Widget );
418     if( execute )
419     {
420       this->m_ImageWriter->SetInput( "Input", image );
421       this->BlockWidget( );
422       ret = this->m_ImageWriter->Update( );
423       this->UnblockWidget( );
424
425     } // fi
426
427   } // fi
428
429   // Show an error message and return
430   if( ret != "" )
431   {
432 #ifdef cpPlugins_Interface_QT4
433     if( this->m_Widget != NULL )
434       QMessageBox::critical(
435         this->m_Widget,
436         QMessageBox::tr( "Error reading image." ),
437         QMessageBox::tr( ret.c_str( ) )
438         );
439 #endif // cpPlugins_Interface_QT4
440     return( false );
441   }
442   else
443     return( true );
444 }
445
446 // -------------------------------------------------------------------------
447 bool cpPlugins::Interface::Plugins::
448 WriteMesh( TMesh* mesh, bool exec_qt )
449 {
450   std::string ret = "";
451   if( this->m_MeshWriter.IsNull( ) )
452     ret = "Plugins: No valid mesh writer. Please load a valid plugin.";
453
454   if( ret == "" )
455   {
456     bool execute = true;
457     if( exec_qt )
458       execute = this->m_MeshReader->ExecConfigurationDialog( this->m_Widget );
459     if( execute )
460     {
461       this->m_MeshWriter->SetInput( 0, mesh );
462       this->BlockWidget( );
463       ret = this->m_MeshWriter->Update( );
464       this->UnblockWidget( );
465
466     } // fi
467
468   } // fi
469
470   // Show an error message and return
471   if( ret != "" )
472   {
473 #ifdef cpPlugins_Interface_QT4
474     if( this->m_Widget != NULL )
475       QMessageBox::critical(
476         this->m_Widget,
477         QMessageBox::tr( "Error reading image." ),
478         QMessageBox::tr( ret.c_str( ) )
479         );
480 #endif // cpPlugins_Interface_QT4
481     return( false );
482   }
483   else
484     return( true );
485 }
486
487 // -------------------------------------------------------------------------
488 bool cpPlugins::Interface::Plugins::
489 CreateFilter( TProcessObject::Pointer& filter, const std::string& name )
490 {
491   filter = this->m_Interface.CreateProcessObject( name );
492   return( filter.IsNotNull( ) );
493 }
494
495 // -------------------------------------------------------------------------
496 void cpPlugins::Interface::Plugins::
497 _Update( )
498 {
499   typedef TInterface::TClasses _C;
500
501   this->m_Filters.clear( );
502   _C& classes = this->m_Interface.GetClasses( );
503   for( _C::const_iterator i = classes.begin( ); i != classes.end( ); ++i )
504   {
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;
519     else
520       this->m_Filters[ category ].insert( name );
521
522   } // rof
523 }
524
525 // eof - $RCSfile$