]> 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->actionOpenInputPolyData, SIGNAL( triggered( ) ),
52     this, SLOT( _triggered_actionOpenInputPolyData( ) )
53     );
54
55   // Start: load all disponible plugins
56   this->_LoadPlugins(
57     std::string( PLUGIN_PREFIX ) +
58     std::string( "cpPlugins." ) +
59     std::string( PLUGIN_EXT )
60     );
61 }
62
63 // -------------------------------------------------------------------------
64 ImageMPR::
65 ~ImageMPR( )
66 {
67   // Close all connections
68   this->m_Plugins.UnloadAll( );
69
70   // Delete objects
71   delete this->m_UI;
72 }
73
74 // -------------------------------------------------------------------------
75 bool ImageMPR::
76 _LoadPlugins( const std::string& filename )
77 {
78   QApplication::setOverrideCursor( Qt::WaitCursor );
79   this->setEnabled( false );
80
81   this->m_ImageReaderClass = "";
82   this->m_ImageWriterClass = "";
83   this->m_MeshReaderClass = "";
84   this->m_MeshWriterClass = "";
85   this->m_ImageToImageFilters.clear( );
86   this->m_ImageToMeshFilters.clear( );
87
88   this->m_Plugins.UnloadAll( );
89   if( !( this->m_Plugins.Load( filename ) ) )
90   {
91     this->m_Plugins.UnloadAll( );
92     return( false );
93
94   } // fi
95
96   typedef TPluginsInterface::TClasses _TClasses;
97   _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
98   for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
99   {
100     TPluginFilter::Pointer o =
101       this->m_Plugins.CreateProcessObject( cIt->first );
102     std::string name = o->GetClassName( );
103     std::string category = o->GetClassCategory( );
104     if( category == "ImageReader" )
105       this->m_ImageReaderClass = name;
106     else if( category == "ImageWriter" )
107       this->m_ImageWriterClass = name;
108     else if( category == "MeshReader" )
109       this->m_MeshReaderClass = name;
110     else if( category == "MeshWriter" )
111       this->m_MeshWriterClass = name;
112     else if( category == "ImageToImageFilter" )
113     {
114       this->m_ImageToImageFilters.insert( name );
115       QAction* action =
116         this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
117       QObject::connect(
118         action, SIGNAL( triggered( ) ),
119         this, SLOT( _triggered_actionImageToImage( ) )
120         );
121     }
122     else if( category == "ImageToMeshFilter" )
123     {
124       this->m_ImageToMeshFilters.insert( name );
125       QAction* action =
126         this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
127       QObject::connect(
128         action, SIGNAL( triggered( ) ),
129         this, SLOT( _triggered_actionImageToMesh( ) )
130         );
131
132     } // fi
133
134   } // rof
135   QApplication::restoreOverrideCursor( );
136   this->setEnabled( true );
137
138   return( true );
139 }
140
141 // -------------------------------------------------------------------------
142 void ImageMPR::
143 _triggered_actionOpenPlugins( )
144 {
145   // Show dialog and check if it was accepted
146   QFileDialog dialog( this );
147   dialog.setFileMode( QFileDialog::ExistingFile );
148   dialog.setDirectory( "." );
149   dialog.setNameFilter( tr( PLUGIN_REGEX ) );
150   dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
151   if( !( dialog.exec( ) ) )
152     return;
153   
154   std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
155   if( !( _LoadPlugins( fname ) ) )
156     QMessageBox::critical(
157       this,
158       tr( "Ignoring plugin" ),
159       tr( fname.c_str( ) )
160       );
161 }
162
163 // -------------------------------------------------------------------------
164 void ImageMPR::
165 _triggered_actionOpenInputImage( )
166 {
167   // Show dialog and check if it was accepted
168   QFileDialog dialog( this );
169   dialog.setFileMode( QFileDialog::ExistingFiles );
170   dialog.setDirectory( tr( "." ) );
171   dialog.setNameFilter(
172     tr( "Medical image files (*.mhd *.bin *.dcm);;All files (*)" )
173     );
174   dialog.setDefaultSuffix( tr( "mhd" ) );
175   if( !( dialog.exec( ) ) )
176     return;
177   
178   this->m_InputImage = NULL;
179
180   // Get a reader from plugins
181   TPluginFilter::Pointer reader =
182     this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
183
184   // Configure reader
185   TParameters reader_params = reader->GetDefaultParameters( );
186   QStringList q_fnames = dialog.selectedFiles( );
187   QStringList::const_iterator qIt = q_fnames.begin( );
188   for( ; qIt != q_fnames.end( ); ++qIt )
189     reader_params.AddValueToStringList( "FileNames", qIt->toStdString( ) );
190   reader->SetParameters( reader_params );
191
192   // Execute and get error message, if any
193   QApplication::setOverrideCursor( Qt::WaitCursor );
194   this->setEnabled( false );
195   std::string err = reader->Update( );
196   QApplication::restoreOverrideCursor( );
197   this->setEnabled( true );
198
199   // Assign fresh image, if any
200   if( err == "" )
201   {
202     this->m_InputImage = reader->GetOutput< TPluginImage >( 0 );
203     reader->DisconnectOutputs( );
204     if( this->m_InputImage.IsNotNull( ) )
205     {
206       vtkImageData* vtk_id = this->m_InputImage->GetVTKImageData( );
207       if( vtk_id != NULL )
208       {
209         this->m_MPRObjects->SetImage( vtk_id );
210         this->m_MPRObjects->ResetCameras( );
211         this->m_MPRObjects->RenderAll( );
212       }
213       else
214         QMessageBox::critical(
215           this,
216           tr( "Error message" ),
217           tr( "Read image does not have a valid VTK converter." )
218           );
219
220     } // fi
221   }
222   else
223     QMessageBox::critical(
224       this,
225       tr( "Error reading single image" ),
226       tr( err.c_str( ) )
227       );
228 }
229
230 // -------------------------------------------------------------------------
231 void ImageMPR::
232 _triggered_actionOpenInputPolyData( )
233 {
234   // Show dialog and check if it was accepted
235   QFileDialog dialog( this );
236   dialog.setFileMode( QFileDialog::ExistingFile );
237   dialog.setDirectory( tr( "." ) );
238   dialog.setNameFilter(
239     tr( "Mesh files (*.vtk *.obj);;All files (*)" )
240     );
241   dialog.setDefaultSuffix( tr( "vtk" ) );
242   if( !( dialog.exec( ) ) )
243     return;
244   
245   this->m_InputMesh = NULL;
246
247   // Get a reader from plugins
248   TPluginFilter::Pointer reader =
249     this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
250
251   // Configure reader
252   TParameters reader_params = reader->GetDefaultParameters( );
253   QStringList q_fnames = dialog.selectedFiles( );
254   QStringList::const_iterator qIt = q_fnames.begin( );
255   for( ; qIt != q_fnames.end( ); ++qIt )
256     reader_params.SetValueAsString( "FileName", qIt->toStdString( ) );
257   reader->SetParameters( reader_params );
258
259   // Execute and get error message, if any
260   QApplication::setOverrideCursor( Qt::WaitCursor );
261   this->setEnabled( false );
262   std::string err = reader->Update( );
263   QApplication::restoreOverrideCursor( );
264   this->setEnabled( true );
265
266
267   // Assign fresh image, if any
268   if( err == "" )
269   {
270     this->m_InputMesh = reader->GetOutput< TPluginMesh >( 0 );
271     reader->DisconnectOutputs( );
272     if( this->m_InputMesh.IsNotNull( ) )
273     {
274       vtkActor* vtk_actor = this->m_InputMesh->GetVTKActor( );
275       if( vtk_actor != NULL )
276       {
277         this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
278         this->m_MPRObjects->Render( 4 );
279       }
280       else
281         QMessageBox::critical(
282           this,
283           tr( "Error message" ),
284           tr( "Read mesh does not have a valid vtkActor." )
285           );
286
287     } // fi
288   }
289   else
290     QMessageBox::critical(
291       this,
292       tr( "Error reading mesh" ),
293       tr( err.c_str( ) )
294       );
295 }
296
297 // -------------------------------------------------------------------------
298 void ImageMPR::
299 _triggered_actionImageToImage( )
300 {
301   if( this->m_InputImage.IsNull( ) )
302     return;
303
304   // Get filter name
305   QAction* action = dynamic_cast< QAction* >( this->sender( ) );
306   if( action == NULL )
307     return;
308   std::string name = action->text( ).toStdString( );
309
310   // Configure filter
311   TPluginFilter::Pointer filter =
312     this->m_Plugins.CreateProcessObject( name );
313   TParameters parameters = filter->GetDefaultParameters( );
314   bool dlg_ok =
315     cpPlugins::Interface::ParametersQtDialog(
316       parameters, filter->GetClassName( )
317       );
318   if( !dlg_ok )
319     return;
320
321   // Execute filter
322   QApplication::setOverrideCursor( Qt::WaitCursor );
323   this->setEnabled( false );
324   filter->SetParameters( parameters );
325   filter->SetInput( 0, this->m_InputImage );
326   std::string err = filter->Update( );
327   QApplication::restoreOverrideCursor( );
328   this->setEnabled( true );
329
330   // Update image
331   if( err == "" )
332   {
333     TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
334     result->DisconnectPipeline( );
335     this->m_InputImage = result;
336     if( this->m_InputImage.IsNotNull( ) )
337       this->m_MPRObjects->SetImage( this->m_InputImage->GetVTKImageData( ) );
338   }
339   else
340     QMessageBox::critical(
341       this,
342       tr( "Error executing filter" ),
343       tr( err.c_str( ) )
344       );
345 }
346
347 // -------------------------------------------------------------------------
348 void ImageMPR::
349 _triggered_actionImageToMesh( )
350 {
351   if( this->m_InputImage.IsNull( ) )
352     return;
353
354   // Get filter name
355   QAction* action = dynamic_cast< QAction* >( this->sender( ) );
356   if( action == NULL )
357     return;
358   std::string name = action->text( ).toStdString( );
359
360   // Configure filter
361   TPluginFilter::Pointer filter =
362     this->m_Plugins.CreateProcessObject( name );
363   TParameters parameters = filter->GetDefaultParameters( );
364   bool dlg_ok =
365     cpPlugins::Interface::ParametersQtDialog(
366       parameters, filter->GetClassName( )
367       );
368   if( !dlg_ok )
369     return;
370
371   // Execute filter
372   QApplication::setOverrideCursor( Qt::WaitCursor );
373   this->setEnabled( false );
374   filter->SetParameters( parameters );
375   filter->SetInput( 0, this->m_InputImage );
376   std::string err = filter->Update( );
377   QApplication::restoreOverrideCursor( );
378   this->setEnabled( true );
379
380   // Update image
381   if( err == "" )
382   {
383     TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
384     result->DisconnectPipeline( );
385     this->m_InputMesh = result;
386     if( this->m_InputMesh.IsNotNull( ) )
387       this->m_MPRObjects->Get3DRenderer( )->AddActor(
388         this->m_InputMesh->GetVTKActor( )
389         );
390   }
391   else
392     QMessageBox::critical(
393       this,
394       tr( "Error executing filter" ),
395       tr( err.c_str( ) )
396       );
397 }
398
399 // eof - $RCSfile$