]> Creatis software - cpPlugins.git/blob - appli/ImageMPR/ImageMPR.cxx
...
[cpPlugins.git] / appli / ImageMPR / ImageMPR.cxx
1 #include "ImageMPR.h"
2 #include "ui_ImageMPR.h"
3
4 #include <cpPlugins/Interface/ParametersQtDialog.h>
5
6 #include <vtkProperty.h>
7 #include <vtkRenderWindow.h>
8
9 #include <QFileDialog>
10 #include <QMessageBox>
11
12 #ifdef _WIN32
13 #  define PLUGIN_PREFIX ""
14 #  define PLUGIN_EXT "dll"
15 #  define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
16 #else
17 #  define PLUGIN_PREFIX "lib"
18 #  define PLUGIN_EXT "so"
19 #  define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
20 #endif // _WIN32
21
22 // -------------------------------------------------------------------------
23 ImageMPR::ImageMPR( QWidget* parent )
24   : QMainWindow( parent ),
25     m_UI( new Ui::ImageMPR ),
26     m_ImageReaderClass( "" ),
27     m_ImageWriterClass( "" ),
28     m_InputImage( NULL )
29 {
30   this->m_UI->setupUi( this );
31
32   // Create and associate renderers
33   this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
34   this->m_MPRObjects->SetRenderWindows(
35     this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
36     this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
37     this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
38     this->m_UI->m_3DVTK->GetRenderWindow( )
39     );
40
41   // signals <-> slots
42   QObject::connect(
43     this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
44     this, SLOT( _triggered_actionOpenPlugins( ) )
45     );
46   QObject::connect(
47     this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
48     this, SLOT( _triggered_actionOpenInputImage( ) )
49     );
50   QObject::connect(
51     this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
52     this, SLOT( _triggered_actionOpenSegmentation( ) )
53     );
54   QObject::connect(
55     this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
56     this, SLOT( _triggered_actionOpenInputPolyData( ) )
57     );
58
59   // Start: load all disponible plugins
60   this->_LoadPlugins(
61     std::string( PLUGIN_PREFIX ) +
62     std::string( "cpPlugins." ) +
63     std::string( PLUGIN_EXT )
64     );
65 }
66
67 // -------------------------------------------------------------------------
68 ImageMPR::
69 ~ImageMPR( )
70 {
71   // Close all connections
72   this->m_Plugins.UnloadAll( );
73
74   // Delete objects
75   delete this->m_UI;
76 }
77
78 // -------------------------------------------------------------------------
79 bool ImageMPR::
80 _LoadPlugins( const std::string& filename )
81 {
82   QApplication::setOverrideCursor( Qt::WaitCursor );
83   this->setEnabled( false );
84
85   this->m_ImageReaderClass = "";
86   this->m_ImageWriterClass = "";
87   this->m_MeshReaderClass = "";
88   this->m_MeshWriterClass = "";
89   this->m_ImageToImageFilters.clear( );
90   this->m_ImageToMeshFilters.clear( );
91
92   this->m_Plugins.UnloadAll( );
93   if( !( this->m_Plugins.Load( filename ) ) )
94   {
95     this->m_Plugins.UnloadAll( );
96     return( false );
97
98   } // fi
99
100   typedef TPluginsInterface::TClasses _TClasses;
101   _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
102   for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
103   {
104     TPluginFilter::Pointer o =
105       this->m_Plugins.CreateProcessObject( cIt->first );
106     std::string name = o->GetClassName( );
107     std::string category = o->GetClassCategory( );
108     if( category == "ImageReader" )
109       this->m_ImageReaderClass = name;
110     else if( category == "ImageWriter" )
111       this->m_ImageWriterClass = name;
112     else if( category == "MeshReader" )
113       this->m_MeshReaderClass = name;
114     else if( category == "MeshWriter" )
115       this->m_MeshWriterClass = name;
116     else if( category == "ImageToImageFilter" )
117     {
118       this->m_ImageToImageFilters.insert( name );
119       QAction* action =
120         this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
121       QObject::connect(
122         action, SIGNAL( triggered( ) ),
123         this, SLOT( _triggered_actionImageToImage( ) )
124         );
125     }
126     else if( category == "ImageToMeshFilter" )
127     {
128       this->m_ImageToMeshFilters.insert( name );
129       QAction* action =
130         this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
131       QObject::connect(
132         action, SIGNAL( triggered( ) ),
133         this, SLOT( _triggered_actionImageToMesh( ) )
134         );
135
136     } // fi
137
138   } // rof
139   QApplication::restoreOverrideCursor( );
140   this->setEnabled( true );
141
142   return( true );
143 }
144
145 // -------------------------------------------------------------------------
146 void ImageMPR::
147 _triggered_actionOpenPlugins( )
148 {
149   // Show dialog and check if it was accepted
150   QFileDialog dialog( this );
151   dialog.setFileMode( QFileDialog::ExistingFile );
152   dialog.setDirectory( "." );
153   dialog.setNameFilter( tr( PLUGIN_REGEX ) );
154   dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
155   if( !( dialog.exec( ) ) )
156     return;
157   
158   std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
159   if( !( _LoadPlugins( fname ) ) )
160     QMessageBox::critical(
161       this,
162       tr( "Ignoring plugin" ),
163       tr( fname.c_str( ) )
164       );
165 }
166
167 // -------------------------------------------------------------------------
168 void ImageMPR::
169 _triggered_actionOpenInputImage( )
170 {
171   // Show dialog and check if it was accepted
172   QFileDialog dialog( this );
173   dialog.setFileMode( QFileDialog::ExistingFiles );
174   dialog.setDirectory( tr( "." ) );
175   dialog.setNameFilter(
176     tr( "Medical image files (*.mhd *.bin *.dcm);;All files (*)" )
177     );
178   dialog.setDefaultSuffix( tr( "mhd" ) );
179   if( !( dialog.exec( ) ) )
180     return;
181   
182   this->m_InputImage = NULL;
183
184   // Get a reader from plugins
185   TPluginFilter::Pointer reader =
186     this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
187
188   // Configure reader
189   TParameters reader_params = reader->GetDefaultParameters( );
190   QStringList q_fnames = dialog.selectedFiles( );
191   QStringList::const_iterator qIt = q_fnames.begin( );
192   for( ; qIt != q_fnames.end( ); ++qIt )
193     reader_params.AddValueToStringList( "FileNames", qIt->toStdString( ) );
194   reader->SetParameters( reader_params );
195
196   // Execute and get error message, if any
197   QApplication::setOverrideCursor( Qt::WaitCursor );
198   this->setEnabled( false );
199   std::string err = reader->Update( );
200   QApplication::restoreOverrideCursor( );
201   this->setEnabled( true );
202
203   // Assign fresh image, if any
204   if( err == "" )
205   {
206     this->m_InputImage = reader->GetOutput< TPluginImage >( 0 );
207     reader->DisconnectOutputs( );
208     if( this->m_InputImage.IsNotNull( ) )
209     {
210       vtkImageData* vtk_id = this->m_InputImage->GetVTKImageData( );
211       if( vtk_id != NULL )
212       {
213         this->m_MPRObjects->SetImage( vtk_id );
214         this->m_MPRObjects->ResetCameras( );
215         this->m_MPRObjects->RenderAll( );
216       }
217       else
218         QMessageBox::critical(
219           this,
220           tr( "Error message" ),
221           tr( "Read image does not have a valid VTK converter." )
222           );
223
224     } // fi
225   }
226   else
227     QMessageBox::critical(
228       this,
229       tr( "Error reading single image" ),
230       tr( err.c_str( ) )
231       );
232 }
233
234 // -------------------------------------------------------------------------
235 void ImageMPR::
236 _triggered_actionOpenSegmentation( )
237 {
238   // Show dialog and check if it was accepted
239   QFileDialog dialog( this );
240   dialog.setFileMode( QFileDialog::ExistingFiles );
241   dialog.setDirectory( tr( "." ) );
242   dialog.setNameFilter(
243     tr( "Medical image files (*.mhd *.bin *.dcm);;All files (*)" )
244     );
245   dialog.setDefaultSuffix( tr( "mhd" ) );
246   if( !( dialog.exec( ) ) )
247     return;
248   
249   this->m_InputImage = NULL;
250
251   // Get a reader from plugins
252   TPluginFilter::Pointer reader =
253     this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
254
255   // Configure reader
256   TParameters reader_params = reader->GetDefaultParameters( );
257   QStringList q_fnames = dialog.selectedFiles( );
258   QStringList::const_iterator qIt = q_fnames.begin( );
259   for( ; qIt != q_fnames.end( ); ++qIt )
260     reader_params.AddValueToStringList( "FileNames", qIt->toStdString( ) );
261   reader->SetParameters( reader_params );
262
263   // Execute and get error message, if any
264   QApplication::setOverrideCursor( Qt::WaitCursor );
265   this->setEnabled( false );
266   std::string err = reader->Update( );
267   QApplication::restoreOverrideCursor( );
268   this->setEnabled( true );
269
270   // Assign fresh image, if any
271   if( err == "" )
272   {
273     this->m_InputSegmentation = reader->GetOutput< TPluginImage >( 0 );
274     reader->DisconnectOutputs( );
275     if( this->m_InputSegmentation.IsNotNull( ) )
276     {
277       vtkImageData* vtk_id = this->m_InputSegmentation->GetVTKImageData( );
278       if( vtk_id != NULL )
279       {
280         /*
281           this->m_MPRObjects->SetImage( vtk_id );
282           this->m_MPRObjects->ResetCameras( );
283           this->m_MPRObjects->RenderAll( );
284         */
285       }
286       else
287         QMessageBox::critical(
288           this,
289           tr( "Error message" ),
290           tr( "Read image does not have a valid VTK converter." )
291           );
292
293     } // fi
294   }
295   else
296     QMessageBox::critical(
297       this,
298       tr( "Error reading single image" ),
299       tr( err.c_str( ) )
300       );
301 }
302
303 // -------------------------------------------------------------------------
304 void ImageMPR::
305 _triggered_actionOpenInputPolyData( )
306 {
307   // Show dialog and check if it was accepted
308   QFileDialog dialog( this );
309   dialog.setFileMode( QFileDialog::ExistingFile );
310   dialog.setDirectory( tr( "." ) );
311   dialog.setNameFilter(
312     tr( "Mesh files (*.vtk *.obj);;All files (*)" )
313     );
314   dialog.setDefaultSuffix( tr( "vtk" ) );
315   if( !( dialog.exec( ) ) )
316     return;
317   
318   this->m_InputMesh = NULL;
319
320   // Get a reader from plugins
321   TPluginFilter::Pointer reader =
322     this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
323
324   // Configure reader
325   TParameters reader_params = reader->GetDefaultParameters( );
326   QStringList q_fnames = dialog.selectedFiles( );
327   QStringList::const_iterator qIt = q_fnames.begin( );
328   for( ; qIt != q_fnames.end( ); ++qIt )
329     reader_params.SetValueAsString( "FileName", qIt->toStdString( ) );
330   reader->SetParameters( reader_params );
331
332   // Execute and get error message, if any
333   QApplication::setOverrideCursor( Qt::WaitCursor );
334   this->setEnabled( false );
335   std::string err = reader->Update( );
336   QApplication::restoreOverrideCursor( );
337   this->setEnabled( true );
338
339
340   // Assign fresh image, if any
341   if( err == "" )
342   {
343     this->m_InputMesh = reader->GetOutput< TPluginMesh >( 0 );
344     reader->DisconnectOutputs( );
345     if( this->m_InputMesh.IsNotNull( ) )
346     {
347       vtkActor* vtk_actor = this->m_InputMesh->GetVTKActor( );
348       if( vtk_actor != NULL )
349       {
350         this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
351         this->m_MPRObjects->Render( 4 );
352       }
353       else
354         QMessageBox::critical(
355           this,
356           tr( "Error message" ),
357           tr( "Read mesh does not have a valid vtkActor." )
358           );
359
360     } // fi
361   }
362   else
363     QMessageBox::critical(
364       this,
365       tr( "Error reading mesh" ),
366       tr( err.c_str( ) )
367       );
368 }
369
370 // -------------------------------------------------------------------------
371 void ImageMPR::
372 _triggered_actionImageToImage( )
373 {
374   if( this->m_InputImage.IsNull( ) )
375     return;
376
377   // Get filter name
378   QAction* action = dynamic_cast< QAction* >( this->sender( ) );
379   if( action == NULL )
380     return;
381   std::string name = action->text( ).toStdString( );
382
383   // Configure filter
384   TPluginFilter::Pointer filter =
385     this->m_Plugins.CreateProcessObject( name );
386   bool dlg_ok = filter->ExecConfigurationDialog( NULL );
387   if( !dlg_ok )
388     return;
389
390   // Execute filter
391   QApplication::setOverrideCursor( Qt::WaitCursor );
392   this->setEnabled( false );
393   filter->SetInput( 0, this->m_InputImage );
394   std::string err = filter->Update( );
395   QApplication::restoreOverrideCursor( );
396   this->setEnabled( true );
397
398   // Update image
399   if( err == "" )
400   {
401     TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
402     result->DisconnectPipeline( );
403     this->m_InputImage = result;
404     if( this->m_InputImage.IsNotNull( ) )
405       this->m_MPRObjects->SetImage( this->m_InputImage->GetVTKImageData( ) );
406   }
407   else
408     QMessageBox::critical(
409       this,
410       tr( "Error executing filter" ),
411       tr( err.c_str( ) )
412       );
413 }
414
415 // -------------------------------------------------------------------------
416 void ImageMPR::
417 _triggered_actionImageToMesh( )
418 {
419   if( this->m_InputImage.IsNull( ) )
420     return;
421
422   // Get filter name
423   QAction* action = dynamic_cast< QAction* >( this->sender( ) );
424   if( action == NULL )
425     return;
426   std::string name = action->text( ).toStdString( );
427
428   // Configure filter
429   TPluginFilter::Pointer filter =
430     this->m_Plugins.CreateProcessObject( name );
431   bool dlg_ok = filter->ExecConfigurationDialog( NULL );
432   if( !dlg_ok )
433     return;
434
435   // Execute filter
436   QApplication::setOverrideCursor( Qt::WaitCursor );
437   this->setEnabled( false );
438   filter->SetInput( 0, this->m_InputImage );
439   std::string err = filter->Update( );
440   QApplication::restoreOverrideCursor( );
441   this->setEnabled( true );
442
443   // Update image
444   if( err == "" )
445   {
446     TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
447     result->DisconnectPipeline( );
448     this->m_InputMesh = result;
449     if( this->m_InputMesh.IsNotNull( ) )
450       this->m_MPRObjects->Get3DRenderer( )->AddActor(
451         this->m_InputMesh->GetVTKActor( )
452         );
453   }
454   else
455     QMessageBox::critical(
456       this,
457       tr( "Error executing filter" ),
458       tr( err.c_str( ) )
459       );
460 }
461
462 // eof - $RCSfile$