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