]> Creatis software - cpPlugins.git/blob - appli/ImageMPR/ImageMPR.cxx
76daaa514f58e8bb235b91dceb8b8f09ea9c34b1
[cpPlugins.git] / appli / ImageMPR / ImageMPR.cxx
1 #include "ImageMPR.h"
2 #include "ui_ImageMPR.h"
3
4 #include <vtkRenderWindow.h>
5
6 #include <QFileDialog>
7 #include <QMessageBox>
8
9 #ifdef _WIN32
10 #  define PLUGIN_EXT "dll"
11 #  define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
12 #else
13 #  define PLUGIN_EXT "so"
14 #  define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
15 #endif // _WIN32
16
17 // -------------------------------------------------------------------------
18 ImageMPR::ImageMPR( QWidget* parent )
19   : QMainWindow( parent ),
20     m_UI( new Ui::ImageMPR ),
21     m_InputImage( NULL )
22 {
23   this->m_UI->setupUi( this );
24
25   // Create and associate renderers
26   this->m_MPR = new TMPR(
27     this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
28     this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
29     this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
30     this->m_UI->m_3DVTK->GetRenderWindow( )
31     );
32
33   // signals <-> slots
34   QObject::connect(
35     this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
36     this, SLOT( _triggered_actionOpenPlugins( ) )
37     );
38   QObject::connect(
39     this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
40     this, SLOT( _triggered_actionOpenInputImage( ) )
41     );
42
43   // Start: load all disponible plugins
44   this->_triggered_actionOpenPlugins( );
45 }
46
47 // -------------------------------------------------------------------------
48 ImageMPR::
49 ~ImageMPR( )
50 {
51   // Close all connections
52   this->m_Plugins.UnloadAll( );
53
54   // Delete objects
55   delete this->m_UI;
56   delete this->m_MPR;
57 }
58
59 // -------------------------------------------------------------------------
60 void ImageMPR::
61 _triggered_actionOpenPlugins( )
62 {
63   // Show dialog and check if it was accepted
64   QFileDialog dialog( this );
65   dialog.setFileMode( QFileDialog::ExistingFile );
66   dialog.setDirectory( "." );
67   dialog.setNameFilter( tr( PLUGIN_REGEX ) );
68   dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
69   if( !( dialog.exec( ) ) )
70     return;
71   
72   std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
73   this->m_Plugins.UnloadAll( );
74   if( !( this->m_Plugins.Load( fname ) ) )
75   {
76     QMessageBox::critical(
77       this,
78       tr( "Ignoring plugin" ),
79       tr( fname.c_str( ) )
80       );
81     this->m_Plugins.UnloadAll( );
82     return;
83
84   } // fi
85
86   this->m_BaseClasses[ "ImageReader" ] =
87     "cpPlugins::Plugins::ImageReader";
88   this->m_BaseClasses[ "ImageSeriesReader" ] =
89     "cpPlugins::Plugins::ImageSeriesReader";
90 }
91
92 // -------------------------------------------------------------------------
93 void ImageMPR::
94 _triggered_actionOpenInputImage( )
95 {
96   // Show dialog and check if it was accepted
97   QFileDialog dialog( this );
98   dialog.setFileMode( QFileDialog::ExistingFiles );
99   dialog.setDirectory( tr( "." ) );
100   dialog.setNameFilter(
101     tr( "Medical image files (*.mhd *.bin *.dcm);;All files (*)" )
102     );
103   dialog.setDefaultSuffix( tr( "mhd" ) );
104   if( !( dialog.exec( ) ) )
105     return;
106   
107   this->m_InputImage = NULL;
108   unsigned int nFiles = dialog.selectedFiles( ).size( );
109   if( nFiles == 1 )
110   {
111     std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
112
113     TPlugin::Pointer reader =
114       this->m_Plugins.CreateProcessObject(
115         this->m_BaseClasses[ "ImageReader" ]
116         );
117
118     TParameters reader_params = reader->GetDefaultParameters( );
119     reader_params.SetValueAsString( "FileName", fname );
120     reader_params.SetValueAsString( "PixelType", "short" );
121     reader_params.SetValueAsUint( "ImageDimension", 3 );
122     reader_params.SetValueAsUint( "IsColorImage", 0 );
123     reader->SetParameters( reader_params );
124     std::string err = reader->Update( );
125
126     if( err == "" )
127     {
128       this->m_InputImage =
129         dynamic_cast< TPluginImage* >( reader->GetOutput( 0 ) );
130       reader->DisconnectOutputs( );
131     }
132     else
133       QMessageBox::critical(
134         this,
135         tr( "Error reading single image" ),
136         tr( err.c_str( ) )
137         );
138   }
139   else if( nFiles > 1 )
140   {
141     /* TODO
142        if( this->m_ImageSeriesReaderClassName == "" )
143        {
144        QMessageBox::critical(
145        this,
146        tr( "No plugin to read an image series found!" ),
147        tr( "No plugin to read an image series found!" )
148        );
149        return( ret );
150
151        } // fi
152        std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
153        this->m_LastOpenedFile = fname;
154        return( ret );
155     */
156
157   } // fi
158
159   if( this->m_InputImage.IsNotNull( ) )
160     this->m_MPR->SetImage( this->m_InputImage->GetVTKImageData( ) );
161 }
162
163 // eof - $RCSfile$