]> Creatis software - cpPlugins.git/blob - appli/ImageMPR/ImageMPR.cxx
8074f7ea3f8c920938376525e5f82f54a73ae679
[cpPlugins.git] / appli / ImageMPR / ImageMPR.cxx
1 #include "ImageMPR.h"
2 #include "ui_ImageMPR.h"
3
4 #include <vtkProperty.h>
5 #include <vtkRenderWindow.h>
6
7 #include <QDoubleSpinBox>
8 #include <QFileDialog>
9 #include <QLabel>
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     m_ParametersDlg( NULL )
30 {
31   this->m_UI->setupUi( this );
32
33   // Create and associate renderers
34   this->m_MPR = new TMPR(
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   delete this->m_MPR;
73   if( this->m_ParametersDlg != NULL )
74   {
75     this->m_ParametersDlg->close( );
76     delete this->m_ParametersDlg;
77
78   } // fi
79 }
80
81 // -------------------------------------------------------------------------
82 bool ImageMPR::
83 _LoadPlugins( const std::string& filename )
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   return( true );
140 }
141
142 // -------------------------------------------------------------------------
143 bool ImageMPR::
144 _ParametersDialog( TPluginFilter* filter )
145 {
146   if( this->m_ParametersDlg != NULL ) 
147     this->m_ParametersDlg->close( );
148   this->m_ParametersDlg = new QWidget( NULL );
149   this->m_ParametersDlg->setWindowFlags( Qt::FramelessWindowHint ); 
150   this->m_ParametersDlg->setWindowFlags( Qt::WindowTitleHint );
151
152   QGridLayout* gridLayout = new QGridLayout( this->m_ParametersDlg );
153   QVBoxLayout* verticalLayout = new QVBoxLayout( );
154
155   // Put a title
156   QLabel* title = new QLabel( this->m_ParametersDlg );
157   title->setText( filter->GetClassName( ).c_str( ) );
158   verticalLayout->addWidget( title );
159
160   // Put values
161   TParameters parameters = filter->GetDefaultParameters( );
162   std::vector< std::string > names = parameters.GetParameters( );
163   std::vector< std::string >::const_iterator nIt = names.begin( );
164   for( ; nIt != names.end( ); ++nIt )
165   {
166     std::string par_name = *nIt;
167     TParameters::Type par_type = parameters.GetParameterType( par_name );
168
169     /*
170       enum Type
171       {
172       String = 0,
173       Bool,
174       Int,
175       Uint,
176       Real,
177       Index,
178       Point,
179       StringList,
180       BoolList,
181       IntList,
182       UintList,
183       RealList,
184       IndexList,
185       PointList,
186       NoType
187       };
188     */
189
190     QHBoxLayout* horizontalLayout = new QHBoxLayout( );
191     QLabel* label = new QLabel( this->m_ParametersDlg );
192     label->setText( QString( par_name.c_str( ) ) );
193     horizontalLayout->addWidget( label );
194
195     if( par_type == TParameters::Uint )
196     {
197       QSpinBox* v_uint =
198         new QSpinBox( this->m_ParametersDlg );
199       v_uint->setMinimum( 0 );
200       v_uint->setMaximum( std::numeric_limits< unsigned long >::max( ) );
201       v_uint->setValue( parameters.GetValueAsUint( par_name ) );
202       v_uint->setObjectName( QString( par_name.c_str( ) ) );
203       horizontalLayout->addWidget( v_uint );
204       verticalLayout->addLayout( horizontalLayout );
205     }
206     else if( par_type == TParameters::Int )
207     {
208       QSpinBox* v_int =
209         new QSpinBox( this->m_ParametersDlg );
210       v_int->setMinimum( -std::numeric_limits< long >::max( ) );
211       v_int->setMaximum( std::numeric_limits< long >::max( ) );
212       v_int->setValue( parameters.GetValueAsInt( par_name ) );
213       v_int->setObjectName( QString( par_name.c_str( ) ) );
214       horizontalLayout->addWidget( v_int );
215       verticalLayout->addLayout( horizontalLayout );
216     }
217     else if( par_type == TParameters::Real )
218     {
219       QDoubleSpinBox* v_double =
220         new QDoubleSpinBox( this->m_ParametersDlg );
221       v_double->setDecimals( 3 );
222       v_double->setMinimum( -( std::numeric_limits< double >::max( ) ) );
223       v_double->setMaximum( std::numeric_limits< double >::max( ) );
224       v_double->setValue( parameters.GetValueAsReal( par_name ) );
225       v_double->setObjectName( QString( par_name.c_str( ) ) );
226       horizontalLayout->addWidget( v_double );
227       verticalLayout->addLayout( horizontalLayout );
228
229     } // fi
230
231   } // rof
232   gridLayout->addLayout( verticalLayout, 0, 0, 1, 1 );
233
234   // Infere plugin type
235   /* TODO
236      TParameters::const_iterator seedIt = parameters.find( "Seed" );
237      TParameters::const_iterator radiusIt = parameters.find( "Radius" );
238      TParameters::const_iterator endIt = parameters.end( );
239      if( seedIt == endIt && radiusIt == endIt )
240      this->m_ActivePluginType = Self::GlobalPluginType;
241      else if( seedIt != endIt && radiusIt == endIt )
242      this->m_ActivePluginType = Self::DoubleClickPluginType;
243      else if( seedIt != endIt && radiusIt != endIt )
244      this->m_ActivePluginType = Self::SpherePluginType;
245      else
246      this->m_ActivePluginType = Self::NonePluginType;
247   */
248
249   QMetaObject::connectSlotsByName( this->m_ParametersDlg );
250   this->m_ParametersDlg->show( );
251
252   return( false );
253 }
254
255 // -------------------------------------------------------------------------
256 void ImageMPR::
257 _triggered_actionOpenPlugins( )
258 {
259   // Show dialog and check if it was accepted
260   QFileDialog dialog( this );
261   dialog.setFileMode( QFileDialog::ExistingFile );
262   dialog.setDirectory( "." );
263   dialog.setNameFilter( tr( PLUGIN_REGEX ) );
264   dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
265   if( !( dialog.exec( ) ) )
266     return;
267   
268   std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
269   if( !( _LoadPlugins( fname ) ) )
270     QMessageBox::critical(
271       this,
272       tr( "Ignoring plugin" ),
273       tr( fname.c_str( ) )
274       );
275 }
276
277 // -------------------------------------------------------------------------
278 void ImageMPR::
279 _triggered_actionOpenInputImage( )
280 {
281   // Show dialog and check if it was accepted
282   QFileDialog dialog( this );
283   dialog.setFileMode( QFileDialog::ExistingFiles );
284   dialog.setDirectory( tr( "." ) );
285   dialog.setNameFilter(
286     tr( "Medical image files (*.mhd *.bin *.dcm *.nrri);;All files (*)" )
287     );
288   dialog.setDefaultSuffix( tr( "mhd" ) );
289   if( !( dialog.exec( ) ) )
290     return;
291   
292   this->m_InputImage = NULL;
293
294   // Get a reader from plugins
295   TPluginFilter::Pointer reader =
296     this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
297
298   // Configure reader
299   TParameters reader_params = reader->GetDefaultParameters( );
300   QStringList q_fnames = dialog.selectedFiles( );
301   QStringList::const_iterator qIt = q_fnames.begin( );
302   for( ; qIt != q_fnames.end( ); ++qIt )
303     reader_params.AddValueToStringList( "FileNames", qIt->toStdString( ) );
304   reader->SetParameters( reader_params );
305
306   // Execute and get error message, if any
307   std::string err = reader->Update( );
308
309   // Assign fresh image, if any
310   if( err == "" )
311   {
312     this->m_InputImage =
313       dynamic_cast< TPluginImage* >( reader->GetOutput( 0 ) );
314     reader->DisconnectOutputs( );
315     if( this->m_InputImage.IsNotNull( ) )
316       this->m_MPR->SetImage( this->m_InputImage->GetVTKImageData( ) );
317   }
318   else
319     QMessageBox::critical(
320       this,
321       tr( "Error reading single image" ),
322       tr( err.c_str( ) )
323       );
324 }
325
326 // -------------------------------------------------------------------------
327 void ImageMPR::
328 _triggered_actionOpenInputPolyData( )
329 {
330   /*
331   // Show dialog and check if it was accepted
332   QFileDialog dialog( this );
333   dialog.setFileMode( QFileDialog::ExistingFile );
334   dialog.setDirectory( "." );
335   dialog.setNameFilter( tr( "VTK file (*.vtk);;All files (*)" ) );
336   dialog.setDefaultSuffix( tr( "vtk" ) );
337   if( !( dialog.exec( ) ) )
338     return;
339   
340   std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
341
342   this->m_InputMesh = NULL;
343
344   // Get a reader from plugins
345   TPluginFilter::Pointer reader =
346     this->m_Plugins.CreateProcessObject(
347       this->m_BaseClasses[ "MeshReader" ]
348       );
349
350   // Configure plugin
351   TParameters reader_params = reader->GetDefaultParameters( );
352   reader_params.SetValueAsString( "FileName", fname );
353   reader->SetParameters( reader_params );
354
355   // Execute and get error message, if any
356   std::string err = reader->Update( );
357
358   // Assign fresh image, if any
359   if( err == "" )
360   {
361     this->m_InputMesh =
362       dynamic_cast< TPluginMesh* >( reader->GetOutput( 0 ) );
363     reader->DisconnectOutputs( );
364     if( this->m_InputMesh.IsNotNull( ) )
365     {
366       this->m_InputMeshMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
367       this->m_InputMeshMapper->SetInputData( this->m_InputMesh->GetVTKPolyData( ) );
368       this->m_InputMeshActor = vtkSmartPointer< vtkActor >::New( );
369       this->m_InputMeshActor->SetMapper( this->m_InputMeshMapper );
370       this->m_MPR->Add3DActor( this->m_InputMeshActor );
371
372     } // fi
373   }
374   else
375     QMessageBox::critical(
376       this,
377       tr( "Error reading polydata" ),
378       tr( err.c_str( ) )
379       );
380   */
381 }
382
383 // -------------------------------------------------------------------------
384 void ImageMPR::
385 _triggered_actionImageToImage( )
386 {
387   if( this->m_InputImage.IsNull( ) )
388     return;
389
390   // Get filter name
391   QAction* action = dynamic_cast< QAction* >( this->sender( ) );
392   if( action == NULL )
393     return;
394   std::string name = action->text( ).toStdString( );
395
396   // Configure filter
397   TPluginFilter::Pointer filter =
398     this->m_Plugins.CreateProcessObject( name );
399   this->_ParametersDialog( filter );
400 }
401
402 // -------------------------------------------------------------------------
403 void ImageMPR::
404 _triggered_actionImageToMesh( )
405 {
406   if( this->m_InputImage.IsNull( ) )
407     return;
408
409   // Get filter name
410   QAction* action = dynamic_cast< QAction* >( this->sender( ) );
411   if( action == NULL )
412     return;
413   std::string name = action->text( ).toStdString( );
414 }
415
416 // eof - $RCSfile$