]> Creatis software - creaImageIO.git/blob - src/creaImageIOWxSimpleDlg.cpp
#3131 creaImageIO Feature New Normal - branch changeWx28to30 compilation with wxWid...
[creaImageIO.git] / src / creaImageIOWxSimpleDlg.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28 #include "creaImageIOWxSimpleDlg.h"
29
30
31 namespace creaImageIO
32 {
33
34
35         ///Ctor
36    WxSimpleDlg::WxSimpleDlg(wxWindow *parent, 
37                             wxString i_title,  
38                             const std::string i_namedescp , 
39                             const std::string i_namedb)
40     : wxDialog(parent, -1,_T("SELECT IMAGE(S)"), wxDefaultPosition, wxSize(230,150))
41    {
42                 namedescp       = i_namedescp; 
43                 namedb          = i_namedb;
44
45            if(!i_title.empty())
46            {
47                         this->SetTitle(i_title);  
48            }
49            // Button to select file(s)
50            wxButton *fileBut = new wxButton(this, -1,_T("Select a single file"), wxPoint(10,7) );
51            Connect( fileBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxSimpleDlg::OnReadFile ); 
52
53            // Button to select directory
54            wxButton *directoryBut = new wxButton(this, -1,_T("Select all the files in a directory"), wxPoint(10,40) );
55            Connect( directoryBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxSimpleDlg::OnReadDirectory ); 
56
57            // button to select creaImageIO
58            wxButton *gimmickBut = new wxButton(this, -1,_T("Select thru Dicom browser"), wxPoint(10,70) );
59            Connect( gimmickBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxSimpleDlg::OnReadGimmick ); 
60
61            /// \TODO  Button to select Bruker directory
62            bInfo = false;
63         Layout(); 
64         
65         }
66 //////////////////////////////////////////////////////////////////////
67 //                                                                  //
68 //////////////////////////////////////////////////////////////////////
69           void WxSimpleDlg::OnReadFile(wxCommandEvent& event)
70           {
71                   int resultShowModal;
72 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
73 #if wxMAJOR_VERSION <= 2
74                   wxFileDialog* fileDlg = new wxFileDialog( 0,  _T("Select file"), _T(""), _T(""), crea::std2wx("*"), wxOPEN |wxFD_MULTIPLE, wxDefaultPosition);
75 #else
76                   wxFileDialog* fileDlg = new wxFileDialog( 0,  _T("Select file"), _T(""), _T(""), crea::std2wx("*"), wxFD_OPEN |wxFD_MULTIPLE, wxDefaultPosition);
77 #endif
78     
79                     resultShowModal = fileDlg->ShowModal();
80                         if ( resultShowModal==wxID_OK )
81                         {
82                                 wxArrayString wxArray;
83                                 fileDlg->GetPaths(wxArray);
84                                 if(wxArray.size() >0)
85                                 {
86                                         m_results.clear();
87                                         infoimage               =       wxArray[0];
88                                         infoimageSource =       _T("FILE(s)");
89                                         std::vector<std::string> files;
90                                         for( int i = 0; i < wxArray.GetCount(); i++)
91                                         {
92                                                 files.push_back( crea::wx2std(wxArray[i]));
93                                         }
94                                         if( !m_view.readFile(files , m_attrDicomTags , m_resultsDicomAtr , m_results) )
95                                         {
96                                                 //TO DO WARNING MESSAGE;
97                                         }
98                                 } else {
99                                         // TO DO WARNING MESSAGES
100                                 }
101                         }
102                   SetReturnCode( resultShowModal );
103 //               Close();
104                  EndModal( resultShowModal );
105           }
106
107  //////////////////////////////////////////////////////////////////////
108 //                                                                                                                                      //
109 //////////////////////////////////////////////////////////////////////
110
111           void WxSimpleDlg::OnReadDirectory(wxCommandEvent &event)
112           {
113                   int resultShowModal;
114              long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
115                  wxDirDialog* dirDlg = new wxDirDialog( 0, _T("Select the directory to display"), _T(""), style);
116                  
117                  resultShowModal = dirDlg->ShowModal();
118                  if ( resultShowModal==wxID_OK )
119                  {  
120                          m_results.clear();
121                         infoimage               =       dirDlg->GetPath();
122                         infoimageSource =       _T("DIRECTORY");
123                         if(! m_view.readDirectory(crea::wx2std(dirDlg->GetPath()) , m_attrDicomTags , m_resultsDicomAtr , m_results))
124                         {
125                                 //TO DO WARNING MESSAGE;
126                         }
127                  }
128                  SetReturnCode( resultShowModal );
129 //               Close();
130                  EndModal( resultShowModal );
131           }
132
133           //////////////////////////////////////////////////////////////////////
134 //                                                                                                                                      //
135 //////////////////////////////////////////////////////////////////////
136
137           void WxSimpleDlg::OnReadGimmick(wxCommandEvent &event)
138           {
139                   // Run Gimmick
140                    WxGimmickReaderDialog dlg(0,-1, 
141                                    namedescp,
142                                    namedb, 
143                                    _T("Select image(s)        - Gimmick! (c) CREATIS-LRMN 2008"),
144                    wxDefaultPosition,
145                    wxSize(810,750),
146                    GIMMICK_2D_IMAGE_SELECTION,
147                    GIMMICK_3D_IMAGE_SELECTION,
148                    _3D,
149                                    1);
150                  dlg.ShowModal();
151                  if (dlg.GetReturnCode() == wxID_OK)
152              {
153                                 dlg.stopReading();
154
155                                 std::vector<creaImageIO::OutStrGimmick> outStrGimmick;
156                                 dlg.getSelected(outStrGimmick, m_attrDicomTags,true,"");
157
158                         m_results.clear();
159                         int size=(int)outStrGimmick.size();
160                         int ii;
161 //                      if(!bInfo)
162 //                      {
163                                 for (ii=0;ii<size;ii++)
164                                 {
165                                         m_results.push_back(outStrGimmick[ii].img);
166                                         m_resultsDicomAtr.push_back( outStrGimmick[ii].infos );
167                                 }
168 //                      } else {
169 //                              for (ii=0;ii<size;ii++)
170 //                              {
171 //                                      m_resultsInfo.push_back(outStrGimmick[ii]);
172 //                              }
173 //                      }
174
175                         infoimage               =       _T("EED Missing info <void> ???");
176                         infoimageSource =       _T("GIMMICK");
177
178                         dlg.OnExit();
179                  }
180                  SetReturnCode( dlg.GetReturnCode() );
181                  EndModal( dlg.GetReturnCode() );
182            }
183
184 //-----------------------------------------------------------------------------   
185         wxString WxSimpleDlg::getInfoImage()
186         {
187                 return infoimage;
188         }
189
190 //-----------------------------------------------------------------------------   
191         wxString WxSimpleDlg::getInfoImageSource()
192         {
193                 return infoimageSource;
194         }
195
196  //////////////////////////////////////////////////////////////////////
197 // Return the results vector                                                                            //
198 //////////////////////////////////////////////////////////////////////
199           std::vector<vtkImageData*> WxSimpleDlg::getImagesSelected()
200           {
201                   return m_results;
202           }
203
204 //-----------------------------------------------------------------------------   
205         std::vector< std::map<std::string, std::string> >  WxSimpleDlg::getDicomInfoImagesSelected()
206         {
207                 return m_resultsDicomAtr;
208         }
209
210 //-----------------------------------------------------------------------------   
211         void WxSimpleDlg::SetAttrDicomTags( std::vector<std::string> attr )
212         {
213                 m_attrDicomTags=attr;
214         }
215           
216
217 //-----------------------------------------------------------------------------   
218         vtkImageData*  WxSimpleDlg::getVolumeSelected()
219         {
220                 vtkImageData *result;
221
222 //        creaImageIO::WxSimpleDlg dlg(0,crea::std2wx(bbGetInputTitle()),"localdatabase_Descriptor.dscp","Local Database"); 
223 //              dlg.SetAttrDicomTags( bbGetInputDicomTags() );
224 //              dlg.ShowModal(); 
225 //              bbSetOutputDicomInfo( dlg.getDicomInfoImagesSelected() );
226
227                 if (getImagesSelected().size()!=0){ 
228 //                bbSetOutputFileName( std::string( dlg.getInfoImage().mb_str() ) );
229                   if(getImagesSelected().size() ==1)
230                   {
231                         result = getImagesSelected()[0] ;  
232                   } else {
233                                 // FCY: it will be a big problem if we have several kind of data in the same folder.
234                                 // creation of a huge vtkImageData!!!!
235                                 vtkImageData* first = getImagesSelected()[0];
236                                 int dim[3];
237                                 first->GetDimensions(dim);
238                                 if (dim[2]==1)
239                                 {
240                                         vtkImageData *out;
241                                         out  = vtkImageData::New();
242                                         out->SetScalarType(first->GetScalarType());
243                                         out->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
244                                         int ext[6];
245                                         first->GetWholeExtent(ext); 
246                                         if(ext[5] == 0)
247                                         {
248                                            ext[5] = getImagesSelected().size()-1;
249                                          } else {
250                                                 ext[5] = ext[5] * getImagesSelected().size()-1; // to deal with multiframes 
251                                         } // ext
252                                         out->SetExtent(ext);
253                                         double spac[3];
254                                         first->GetDimensions(dim);
255                                         first->GetSpacing(spac);
256                                         if (spac[0]==spac[1])
257                                         {
258                                                 spac[2]=spac[0];  
259                                 }
260                                         out->SetSpacing(spac);
261                                         out->SetDimensions(dim[0], dim[1], getImagesSelected().size() );
262                                         out->AllocateScalars();
263                                         out->Update();
264                                         unsigned long imsize = dim[0] * dim[1];
265                                         imsize = imsize * dim[2];  // deal with multiframes here
266                                         // differents formats char , short, etc...
267                                         // differents components 1..3  ex. jpg ->RGB 3
268                                         imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
269                                 int slice,sizeImageVector=getImagesSelected().size();
270                                         for (slice=0 ; slice<sizeImageVector ; slice++)
271                                         {
272                                                 vtkImageData *img = getImagesSelected()[slice];
273                                                 memcpy(out->GetScalarPointer(0,0,slice), img->GetScalarPointer(0,0,0), imsize);
274                                                 //img->Delete();
275                                         } // for slice  
276
277 //EED This is NOT working. The iteration over the pointer vtkImageData
278 //                              int slice=0;
279 //                              std::vector<vtkImageData*>::iterator it;
280 //                              for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it) 
281 //                                      {
282 //                                                memcpy(out->GetScalarPointer(0,0,slice), (*it)->GetScalarPointer(0,0,0), imsize);
283 //                                                slice++;
284 //                                      }       
285 //                              //for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it) 
286 //                                      //{
287 //                                      //              (*it)->Delete();
288 //                                      //}
289
290                                         result =out;
291
292                                 } else {
293                                         result = first;
294                                 } // dim
295                         } // if getImagesSelected size
296                 } else { 
297                 result = NULL;  
298                 } // getImagesSelected().size()!=0
299
300
301                 return result;
302
303                 }
304
305
306
307 } // namespace creaImageIO
308