]> Creatis software - cpPlugins.git/blob - appli/ImageMPR/ImageMPR.cxx
MPR almost working with multiple images
[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->ActivateInteractors( );
215         this->m_MPRObjects->ResetCameras( );
216         this->m_MPRObjects->RenderAll( );
217       }
218       else
219         QMessageBox::critical(
220           this,
221           tr( "Error message" ),
222           tr( "Read image does not have a valid VTK converter." )
223           );
224
225     } // fi
226   }
227   else
228     QMessageBox::critical(
229       this,
230       tr( "Error reading single image" ),
231       tr( err.c_str( ) )
232       );
233 }
234
235 // -------------------------------------------------------------------------
236 void ImageMPR::
237 _triggered_actionOpenSegmentation( )
238 {
239   // Show dialog and check if it was accepted
240   QFileDialog dialog( this );
241   dialog.setFileMode( QFileDialog::ExistingFiles );
242   dialog.setDirectory( tr( "." ) );
243   dialog.setNameFilter(
244     tr( "Medical image files (*.mhd *.bin *.dcm);;All files (*)" )
245     );
246   dialog.setDefaultSuffix( tr( "mhd" ) );
247   if( !( dialog.exec( ) ) )
248     return;
249   
250   this->m_InputImage = NULL;
251
252   // Get a reader from plugins
253   TPluginFilter::Pointer reader =
254     this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
255
256   // Configure reader
257   TParameters reader_params = reader->GetDefaultParameters( );
258   QStringList q_fnames = dialog.selectedFiles( );
259   QStringList::const_iterator qIt = q_fnames.begin( );
260   for( ; qIt != q_fnames.end( ); ++qIt )
261     reader_params.AddValueToStringList( "FileNames", qIt->toStdString( ) );
262   reader->SetParameters( reader_params );
263
264   // Execute and get error message, if any
265   QApplication::setOverrideCursor( Qt::WaitCursor );
266   this->setEnabled( false );
267   std::string err = reader->Update( );
268   QApplication::restoreOverrideCursor( );
269   this->setEnabled( true );
270
271   // Assign fresh image, if any
272   if( err == "" )
273   {
274     this->m_InputSegmentation = reader->GetOutput< TPluginImage >( 0 );
275     reader->DisconnectOutputs( );
276     if( this->m_InputSegmentation.IsNotNull( ) )
277     {
278       vtkImageData* vtk_id = this->m_InputSegmentation->GetVTKImageData( );
279       if( vtk_id != NULL )
280       {
281         /*
282           this->m_MPRObjects->SetImage( vtk_id );
283           this->m_MPRObjects->ResetCameras( );
284           this->m_MPRObjects->RenderAll( );
285         */
286       }
287       else
288         QMessageBox::critical(
289           this,
290           tr( "Error message" ),
291           tr( "Read image does not have a valid VTK converter." )
292           );
293
294     } // fi
295   }
296   else
297     QMessageBox::critical(
298       this,
299       tr( "Error reading single image" ),
300       tr( err.c_str( ) )
301       );
302 }
303
304 // -------------------------------------------------------------------------
305 void ImageMPR::
306 _triggered_actionOpenInputPolyData( )
307 {
308   // Show dialog and check if it was accepted
309   QFileDialog dialog( this );
310   dialog.setFileMode( QFileDialog::ExistingFile );
311   dialog.setDirectory( tr( "." ) );
312   dialog.setNameFilter(
313     tr( "Mesh files (*.vtk *.obj);;All files (*)" )
314     );
315   dialog.setDefaultSuffix( tr( "vtk" ) );
316   if( !( dialog.exec( ) ) )
317     return;
318   
319   this->m_InputMesh = NULL;
320
321   // Get a reader from plugins
322   TPluginFilter::Pointer reader =
323     this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
324
325   // Configure reader
326   TParameters reader_params = reader->GetDefaultParameters( );
327   QStringList q_fnames = dialog.selectedFiles( );
328   QStringList::const_iterator qIt = q_fnames.begin( );
329   for( ; qIt != q_fnames.end( ); ++qIt )
330     reader_params.SetValueAsString( "FileName", qIt->toStdString( ) );
331   reader->SetParameters( reader_params );
332
333   // Execute and get error message, if any
334   QApplication::setOverrideCursor( Qt::WaitCursor );
335   this->setEnabled( false );
336   std::string err = reader->Update( );
337   QApplication::restoreOverrideCursor( );
338   this->setEnabled( true );
339
340
341   // Assign fresh image, if any
342   if( err == "" )
343   {
344     this->m_InputMesh = reader->GetOutput< TPluginMesh >( 0 );
345     reader->DisconnectOutputs( );
346     if( this->m_InputMesh.IsNotNull( ) )
347     {
348       vtkActor* vtk_actor = this->m_InputMesh->GetVTKActor( );
349       if( vtk_actor != NULL )
350       {
351         this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
352         this->m_MPRObjects->Render( 4 );
353       }
354       else
355         QMessageBox::critical(
356           this,
357           tr( "Error message" ),
358           tr( "Read mesh does not have a valid vtkActor." )
359           );
360
361     } // fi
362   }
363   else
364     QMessageBox::critical(
365       this,
366       tr( "Error reading mesh" ),
367       tr( err.c_str( ) )
368       );
369 }
370
371 // -------------------------------------------------------------------------
372 void ImageMPR::
373 _triggered_actionImageToImage( )
374 {
375   if( this->m_InputImage.IsNull( ) )
376     return;
377
378   // Get filter name
379   QAction* action = dynamic_cast< QAction* >( this->sender( ) );
380   if( action == NULL )
381     return;
382   std::string name = action->text( ).toStdString( );
383
384   // Configure filter
385   TPluginFilter::Pointer filter =
386     this->m_Plugins.CreateProcessObject( name );
387   bool dlg_ok = filter->ExecConfigurationDialog( NULL );
388   if( !dlg_ok )
389     return;
390
391   // Execute filter
392   QApplication::setOverrideCursor( Qt::WaitCursor );
393   this->setEnabled( false );
394   filter->SetInput( 0, this->m_InputImage );
395   std::string err = filter->Update( );
396   QApplication::restoreOverrideCursor( );
397   this->setEnabled( true );
398
399   // Update image
400   if( err == "" )
401   {
402     TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
403     result->DisconnectPipeline( );
404     this->m_InputImage = result;
405     if( this->m_InputImage.IsNotNull( ) )
406       this->m_MPRObjects->SetImage( this->m_InputImage->GetVTKImageData( ) );
407   }
408   else
409     QMessageBox::critical(
410       this,
411       tr( "Error executing filter" ),
412       tr( err.c_str( ) )
413       );
414 }
415
416 // -------------------------------------------------------------------------
417 void ImageMPR::
418 _triggered_actionImageToMesh( )
419 {
420   if( this->m_InputImage.IsNull( ) )
421     return;
422
423   // Get filter name
424   QAction* action = dynamic_cast< QAction* >( this->sender( ) );
425   if( action == NULL )
426     return;
427   std::string name = action->text( ).toStdString( );
428
429   // Configure filter
430   TPluginFilter::Pointer filter =
431     this->m_Plugins.CreateProcessObject( name );
432   bool dlg_ok = filter->ExecConfigurationDialog( NULL );
433   if( !dlg_ok )
434     return;
435
436   // Execute filter
437   QApplication::setOverrideCursor( Qt::WaitCursor );
438   this->setEnabled( false );
439   filter->SetInput( 0, this->m_InputImage );
440   std::string err = filter->Update( );
441   QApplication::restoreOverrideCursor( );
442   this->setEnabled( true );
443
444   // Update image
445   if( err == "" )
446   {
447     TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
448     result->DisconnectPipeline( );
449     this->m_InputMesh = result;
450     if( this->m_InputMesh.IsNotNull( ) )
451       this->m_MPRObjects->Get3DRenderer( )->AddActor(
452         this->m_InputMesh->GetVTKActor( )
453         );
454   }
455   else
456     QMessageBox::critical(
457       this,
458       tr( "Error executing filter" ),
459       tr( err.c_str( ) )
460       );
461 }
462
463 // eof - $RCSfile$