]> Creatis software - cpPlugins.git/blob - appli/ImageMPR/ImageMPR.cxx
vtkPolyData support added
[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 <QFileDialog>
8 #include <QMessageBox>
9
10 #ifdef _WIN32
11 #  define PLUGIN_EXT "dll"
12 #  define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
13 #else
14 #  define PLUGIN_EXT "so"
15 #  define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
16 #endif // _WIN32
17
18 // -------------------------------------------------------------------------
19 ImageMPR::ImageMPR( QWidget* parent )
20   : QMainWindow( parent ),
21     m_UI( new Ui::ImageMPR ),
22     m_InputImage( NULL )
23 {
24   this->m_UI->setupUi( this );
25
26   // Create and associate renderers
27   this->m_MPR = new TMPR(
28     this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
29     this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
30     this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
31     this->m_UI->m_3DVTK->GetRenderWindow( )
32     );
33
34   // signals <-> slots
35   QObject::connect(
36     this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
37     this, SLOT( _triggered_actionOpenPlugins( ) )
38     );
39   QObject::connect(
40     this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
41     this, SLOT( _triggered_actionOpenInputImage( ) )
42     );
43   QObject::connect(
44     this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
45     this, SLOT( _triggered_actionOpenInputPolyData( ) )
46     );
47
48   // Start: load all disponible plugins
49   this->_triggered_actionOpenPlugins( );
50 }
51
52 // -------------------------------------------------------------------------
53 ImageMPR::
54 ~ImageMPR( )
55 {
56   // Close all connections
57   this->m_Plugins.UnloadAll( );
58
59   // Delete objects
60   delete this->m_UI;
61   delete this->m_MPR;
62 }
63
64 // -------------------------------------------------------------------------
65 void ImageMPR::
66 _triggered_actionOpenPlugins( )
67 {
68   // Show dialog and check if it was accepted
69   QFileDialog dialog( this );
70   dialog.setFileMode( QFileDialog::ExistingFile );
71   dialog.setDirectory( "." );
72   dialog.setNameFilter( tr( PLUGIN_REGEX ) );
73   dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
74   if( !( dialog.exec( ) ) )
75     return;
76   
77   std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
78   this->m_Plugins.UnloadAll( );
79   if( !( this->m_Plugins.Load( fname ) ) )
80   {
81     QMessageBox::critical(
82       this,
83       tr( "Ignoring plugin" ),
84       tr( fname.c_str( ) )
85       );
86     this->m_Plugins.UnloadAll( );
87     return;
88
89   } // fi
90
91   this->m_BaseClasses[ "ImageReader" ] =
92     "cpPlugins::Plugins::ImageReader";
93   this->m_BaseClasses[ "PolyDataReader" ] =
94     "cpPlugins::Plugins::PolyDataReader";
95 }
96
97 // -------------------------------------------------------------------------
98 void ImageMPR::
99 _triggered_actionOpenInputImage( )
100 {
101   // Show dialog and check if it was accepted
102   QFileDialog dialog( this );
103   dialog.setFileMode( QFileDialog::ExistingFiles );
104   dialog.setDirectory( tr( "." ) );
105   dialog.setNameFilter(
106     tr( "Medical image files (*.mhd *.bin *.dcm);;All files (*)" )
107     );
108   dialog.setDefaultSuffix( tr( "mhd" ) );
109   if( !( dialog.exec( ) ) )
110     return;
111   
112   this->m_InputImage = NULL;
113
114   // Get a reader from plugins
115   TPlugin::Pointer reader =
116     this->m_Plugins.CreateProcessObject(
117       this->m_BaseClasses[ "ImageReader" ]
118       );
119
120   // Configure plugins
121   TParameters reader_params = reader->GetDefaultParameters( );
122
123   // File names
124   QStringList q_fnames = dialog.selectedFiles( );
125   QStringList::const_iterator qIt = q_fnames.begin( );
126   for( ; qIt != q_fnames.end( ); ++qIt )
127     reader_params.AddValueToStringList( "FileNames", qIt->toStdString( ) );
128
129   // Other parameters
130   reader_params.SetValueAsString( "PixelType", "short" );
131   reader_params.SetValueAsUint( "ImageDimension", 3 );
132   reader_params.SetValueAsUint( "IsColorImage", 0 );
133   reader->SetParameters( reader_params );
134
135   // Execute and get error message, if any
136   std::string err = reader->Update( );
137
138   // Assign fresh image, if any
139   if( err == "" )
140   {
141     this->m_InputImage =
142       dynamic_cast< TPluginImage* >( reader->GetOutput( 0 ) );
143     reader->DisconnectOutputs( );
144     if( this->m_InputImage.IsNotNull( ) )
145       this->m_MPR->SetImage( this->m_InputImage->GetVTKImageData( ) );
146   }
147   else
148     QMessageBox::critical(
149       this,
150       tr( "Error reading single image" ),
151       tr( err.c_str( ) )
152       );
153 }
154
155 // -------------------------------------------------------------------------
156 void ImageMPR::
157 _triggered_actionOpenInputPolyData( )
158 {
159   // Show dialog and check if it was accepted
160   QFileDialog dialog( this );
161   dialog.setFileMode( QFileDialog::ExistingFile );
162   dialog.setDirectory( "." );
163   dialog.setNameFilter( tr( "VTK file (*.vtk);;All files (*)" ) );
164   dialog.setDefaultSuffix( tr( "vtk" ) );
165   if( !( dialog.exec( ) ) )
166     return;
167   
168   std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
169
170   this->m_InputPolyData = NULL;
171
172   // Get a reader from plugins
173   TPlugin::Pointer reader =
174     this->m_Plugins.CreateProcessObject(
175       this->m_BaseClasses[ "PolyDataReader" ]
176       );
177
178   // Configure plugin
179   TParameters reader_params = reader->GetDefaultParameters( );
180   reader_params.SetValueAsString( "FileName", fname );
181   reader->SetParameters( reader_params );
182
183   // Execute and get error message, if any
184   std::string err = reader->Update( );
185
186   // Assign fresh image, if any
187   if( err == "" )
188   {
189     this->m_InputPolyData =
190       dynamic_cast< TPluginPolyData* >( reader->GetOutput( 0 ) );
191     reader->DisconnectOutputs( );
192     if( this->m_InputPolyData.IsNotNull( ) )
193     {
194       this->m_InputPolyData->GetActor( )->GetProperty( )->SetColor( 1, 0, 1 );
195       this->m_MPR->Add3DActor( this->m_InputPolyData->GetActor( ) );
196
197     } // fi
198   }
199   else
200     QMessageBox::critical(
201       this,
202       tr( "Error reading polydata" ),
203       tr( err.c_str( ) )
204       );
205 }
206
207 // eof - $RCSfile$