]> Creatis software - cpPlugins.git/blob - appli/ImageMPR/ImageMPR.cxx
Garbage collector added
[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 }
50
51 // -------------------------------------------------------------------------
52 void ImageMPR::
53 _triggered_actionOpenPlugins( )
54 {
55   // Show dialog and check if it was accepted
56   QFileDialog dialog( this );
57   dialog.setFileMode( QFileDialog::ExistingFile );
58   dialog.setDirectory( "." );
59   dialog.setNameFilter(
60     tr( "Plugins file (*.so);;All files (*)" )
61     );
62   dialog.setDefaultSuffix( tr( "so" ) );
63   if( !( dialog.exec( ) ) )
64     return;
65   
66   std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
67   this->m_Plugins.UnloadAll( );
68   if( !( this->m_Plugins.Load( fname ) ) )
69   {
70     QMessageBox::critical(
71       this,
72       tr( "Ignoring plugin" ),
73       tr( fname.c_str( ) )
74       );
75     this->m_Plugins.UnloadAll( );
76     return;
77
78   } // fi
79
80   this->m_BaseClasses[ "ImageReader" ] =
81     "cpPlugins::Plugins::ImageReader";
82   this->m_BaseClasses[ "ImageSeriesReader" ] =
83     "cpPlugins::Plugins::ImageSeriesReader";
84 }
85
86 // -------------------------------------------------------------------------
87 void ImageMPR::
88 _triggered_actionOpenInputImage( )
89 {
90   // Show dialog and check if it was accepted
91   QFileDialog dialog( this );
92   dialog.setFileMode( QFileDialog::ExistingFiles );
93   dialog.setDirectory( tr( "." ) );
94   dialog.setNameFilter(
95     tr( "Medical image files (*.mhd *.bin *.dcm);;All files (*)" )
96     );
97   dialog.setDefaultSuffix( tr( "mhd" ) );
98   if( !( dialog.exec( ) ) )
99     return;
100   
101   this->m_InputImage = NULL;
102   unsigned int nFiles = dialog.selectedFiles( ).size( );
103   if( nFiles == 1 )
104   {
105     std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
106
107     TPlugin::Pointer reader =
108       this->m_Plugins.CreateProcessObject(
109         this->m_BaseClasses[ "ImageReader" ]
110         );
111
112     TParameters reader_params = reader->GetDefaultParameters( );
113     reader_params[ "FileName" ].second = fname;
114     reader_params[ "PixelType" ].second = "short";
115     reader_params[ "ImageDimension" ].second = "3";
116     reader_params[ "IsColorImage" ].second = "0";
117     reader->SetParameters( reader_params );
118     std::string err = reader->Update( );
119
120     if( err == "" )
121     {
122       this->m_InputImage =
123         dynamic_cast< TPluginImage* >( reader->GetOutput( 0 ) );
124       reader->DisconnectOutputs( );
125     }
126     else
127       QMessageBox::critical(
128         this,
129         tr( "Error reading single image" ),
130         tr( err.c_str( ) )
131         );
132   }
133   else if( nFiles > 1 )
134   {
135     /* TODO
136        if( this->m_ImageSeriesReaderClassName == "" )
137        {
138        QMessageBox::critical(
139        this,
140        tr( "No plugin to read an image series found!" ),
141        tr( "No plugin to read an image series found!" )
142        );
143        return( ret );
144
145        } // fi
146        std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
147        this->m_LastOpenedFile = fname;
148        return( ret );
149     */
150
151   } // fi
152
153   if( this->m_InputImage.IsNotNull( ) )
154     this->m_MPR->SetImage( this->m_InputImage->GetVTKImageData( ) );
155 }
156
157 // eof - $RCSfile$