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