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