2 #include "ui_ImageMPR.h"
4 #include <cpPlugins/Interface/ParametersQtDialog.h>
6 #include <vtkProperty.h>
7 #include <vtkRenderWindow.h>
10 #include <QMessageBox>
13 # define PLUGIN_PREFIX ""
14 # define PLUGIN_EXT "dll"
15 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
17 # define PLUGIN_PREFIX "lib"
18 # define PLUGIN_EXT "so"
19 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
22 // -------------------------------------------------------------------------
23 ImageMPR::ImageMPR( QWidget* parent )
24 : QMainWindow( parent ),
25 m_UI( new Ui::ImageMPR ),
26 m_ImageReaderClass( "" ),
27 m_ImageWriterClass( "" ),
30 this->m_UI->setupUi( this );
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( )
43 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
44 this, SLOT( _triggered_actionOpenPlugins( ) )
47 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
48 this, SLOT( _triggered_actionOpenInputImage( ) )
51 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
52 this, SLOT( _triggered_actionOpenSegmentation( ) )
55 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
56 this, SLOT( _triggered_actionOpenInputPolyData( ) )
59 // Start: load all disponible plugins
61 std::string( PLUGIN_PREFIX ) +
62 std::string( "cpPlugins." ) +
63 std::string( PLUGIN_EXT )
67 // -------------------------------------------------------------------------
71 // Close all connections
72 this->m_Plugins.UnloadAll( );
78 // -------------------------------------------------------------------------
80 _LoadPlugins( const std::string& filename )
82 QApplication::setOverrideCursor( Qt::WaitCursor );
83 this->setEnabled( false );
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( );
92 this->m_Plugins.UnloadAll( );
93 if( !( this->m_Plugins.Load( filename ) ) )
95 this->m_Plugins.UnloadAll( );
100 typedef TPluginsInterface::TClasses _TClasses;
101 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
102 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
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" )
118 this->m_ImageToImageFilters.insert( name );
120 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
122 action, SIGNAL( triggered( ) ),
123 this, SLOT( _triggered_actionImageToImage( ) )
126 else if( category == "ImageToMeshFilter" )
128 this->m_ImageToMeshFilters.insert( name );
130 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
132 action, SIGNAL( triggered( ) ),
133 this, SLOT( _triggered_actionImageToMesh( ) )
139 QApplication::restoreOverrideCursor( );
140 this->setEnabled( true );
145 // -------------------------------------------------------------------------
147 _triggered_actionOpenPlugins( )
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( ) ) )
158 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
159 if( !( _LoadPlugins( fname ) ) )
160 QMessageBox::critical(
162 tr( "Ignoring plugin" ),
167 // -------------------------------------------------------------------------
169 _triggered_actionOpenInputImage( )
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 (*)" )
178 dialog.setDefaultSuffix( tr( "mhd" ) );
179 if( !( dialog.exec( ) ) )
182 this->m_InputImage = NULL;
184 // Get a reader from plugins
185 TPluginFilter::Pointer reader =
186 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
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 );
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 );
203 // Assign fresh image, if any
206 this->m_InputImage = reader->GetOutput< TPluginImage >( 0 );
207 reader->DisconnectOutputs( );
208 if( this->m_InputImage.IsNotNull( ) )
210 vtkImageData* vtk_id = this->m_InputImage->GetVTKImageData( );
213 this->m_MPRObjects->SetImage( vtk_id );
214 this->m_MPRObjects->ActivateInteractors( );
215 this->m_MPRObjects->ResetCameras( );
216 this->m_MPRObjects->RenderAll( );
219 QMessageBox::critical(
221 tr( "Error message" ),
222 tr( "Read image does not have a valid VTK converter." )
228 QMessageBox::critical(
230 tr( "Error reading single image" ),
235 // -------------------------------------------------------------------------
237 _triggered_actionOpenSegmentation( )
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 (*)" )
246 dialog.setDefaultSuffix( tr( "mhd" ) );
247 if( !( dialog.exec( ) ) )
250 this->m_InputImage = NULL;
252 // Get a reader from plugins
253 TPluginFilter::Pointer reader =
254 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
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 );
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 );
271 // Assign fresh image, if any
274 this->m_InputSegmentation = reader->GetOutput< TPluginImage >( 0 );
275 reader->DisconnectOutputs( );
276 if( this->m_InputSegmentation.IsNotNull( ) )
278 vtkImageData* vtk_id = this->m_InputSegmentation->GetVTKImageData( );
282 this->m_MPRObjects->SetImage( vtk_id );
283 this->m_MPRObjects->ResetCameras( );
284 this->m_MPRObjects->RenderAll( );
288 QMessageBox::critical(
290 tr( "Error message" ),
291 tr( "Read image does not have a valid VTK converter." )
297 QMessageBox::critical(
299 tr( "Error reading single image" ),
304 // -------------------------------------------------------------------------
306 _triggered_actionOpenInputPolyData( )
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 (*)" )
315 dialog.setDefaultSuffix( tr( "vtk" ) );
316 if( !( dialog.exec( ) ) )
319 this->m_InputMesh = NULL;
321 // Get a reader from plugins
322 TPluginFilter::Pointer reader =
323 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
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 );
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 );
341 // Assign fresh image, if any
344 this->m_InputMesh = reader->GetOutput< TPluginMesh >( 0 );
345 reader->DisconnectOutputs( );
346 if( this->m_InputMesh.IsNotNull( ) )
348 vtkActor* vtk_actor = this->m_InputMesh->GetVTKActor( );
349 if( vtk_actor != NULL )
351 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
352 this->m_MPRObjects->Render( 4 );
355 QMessageBox::critical(
357 tr( "Error message" ),
358 tr( "Read mesh does not have a valid vtkActor." )
364 QMessageBox::critical(
366 tr( "Error reading mesh" ),
371 // -------------------------------------------------------------------------
373 _triggered_actionImageToImage( )
375 if( this->m_InputImage.IsNull( ) )
379 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
382 std::string name = action->text( ).toStdString( );
385 TPluginFilter::Pointer filter =
386 this->m_Plugins.CreateProcessObject( name );
387 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
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 );
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( ) );
409 QMessageBox::critical(
411 tr( "Error executing filter" ),
416 // -------------------------------------------------------------------------
418 _triggered_actionImageToMesh( )
420 if( this->m_InputImage.IsNull( ) )
424 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
427 std::string name = action->text( ).toStdString( );
430 TPluginFilter::Pointer filter =
431 this->m_Plugins.CreateProcessObject( name );
432 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
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 );
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( )
456 QMessageBox::critical(
458 tr( "Error executing filter" ),