2 #include "ui_ImageMPR.h"
4 #include <vtkProperty.h>
5 #include <vtkRenderWindow.h>
7 #include <QDoubleSpinBox>
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( "" ),
29 m_ParametersDlg( NULL )
31 this->m_UI->setupUi( this );
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( )
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->actionOpenInputPolyData, SIGNAL( triggered( ) ),
52 this, SLOT( _triggered_actionOpenInputPolyData( ) )
55 // Start: load all disponible plugins
57 std::string( PLUGIN_PREFIX ) +
58 std::string( "cpPlugins." ) +
59 std::string( PLUGIN_EXT )
63 // -------------------------------------------------------------------------
67 // Close all connections
68 this->m_Plugins.UnloadAll( );
73 if( this->m_ParametersDlg != NULL )
75 this->m_ParametersDlg->close( );
76 delete this->m_ParametersDlg;
81 // -------------------------------------------------------------------------
83 _LoadPlugins( const std::string& filename )
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( ) )
142 // -------------------------------------------------------------------------
144 _ParametersDialog( TPluginFilter* filter )
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 );
152 QGridLayout* gridLayout = new QGridLayout( this->m_ParametersDlg );
153 QVBoxLayout* verticalLayout = new QVBoxLayout( );
156 QLabel* title = new QLabel( this->m_ParametersDlg );
157 title->setText( filter->GetClassName( ).c_str( ) );
158 verticalLayout->addWidget( title );
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 )
166 std::string par_name = *nIt;
167 TParameters::Type par_type = parameters.GetParameterType( par_name );
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 );
195 if( par_type == TParameters::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 );
206 else if( par_type == TParameters::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 );
217 else if( par_type == TParameters::Real )
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 );
232 gridLayout->addLayout( verticalLayout, 0, 0, 1, 1 );
234 // Infere plugin type
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;
246 this->m_ActivePluginType = Self::NonePluginType;
249 QMetaObject::connectSlotsByName( this->m_ParametersDlg );
250 this->m_ParametersDlg->show( );
255 // -------------------------------------------------------------------------
257 _triggered_actionOpenPlugins( )
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( ) ) )
268 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
269 if( !( _LoadPlugins( fname ) ) )
270 QMessageBox::critical(
272 tr( "Ignoring plugin" ),
277 // -------------------------------------------------------------------------
279 _triggered_actionOpenInputImage( )
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 (*)" )
288 dialog.setDefaultSuffix( tr( "mhd" ) );
289 if( !( dialog.exec( ) ) )
292 this->m_InputImage = NULL;
294 // Get a reader from plugins
295 TPluginFilter::Pointer reader =
296 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
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 );
306 // Execute and get error message, if any
307 std::string err = reader->Update( );
309 // Assign fresh image, if any
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( ) );
319 QMessageBox::critical(
321 tr( "Error reading single image" ),
326 // -------------------------------------------------------------------------
328 _triggered_actionOpenInputPolyData( )
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( ) ) )
340 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
342 this->m_InputMesh = NULL;
344 // Get a reader from plugins
345 TPluginFilter::Pointer reader =
346 this->m_Plugins.CreateProcessObject(
347 this->m_BaseClasses[ "MeshReader" ]
351 TParameters reader_params = reader->GetDefaultParameters( );
352 reader_params.SetValueAsString( "FileName", fname );
353 reader->SetParameters( reader_params );
355 // Execute and get error message, if any
356 std::string err = reader->Update( );
358 // Assign fresh image, if any
362 dynamic_cast< TPluginMesh* >( reader->GetOutput( 0 ) );
363 reader->DisconnectOutputs( );
364 if( this->m_InputMesh.IsNotNull( ) )
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 );
375 QMessageBox::critical(
377 tr( "Error reading polydata" ),
383 // -------------------------------------------------------------------------
385 _triggered_actionImageToImage( )
387 if( this->m_InputImage.IsNull( ) )
391 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
394 std::string name = action->text( ).toStdString( );
397 TPluginFilter::Pointer filter =
398 this->m_Plugins.CreateProcessObject( name );
399 this->_ParametersDialog( filter );
402 // -------------------------------------------------------------------------
404 _triggered_actionImageToMesh( )
406 if( this->m_InputImage.IsNull( ) )
410 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
413 std::string name = action->text( ).toStdString( );