]> Creatis software - creaImageIO.git/blob - src2/creaImageIOWxGimmickView.cpp
Added validation function (visible in ok button being enabled or not)
[creaImageIO.git] / src2 / creaImageIOWxGimmickView.cpp
1 #include <creaImageIOWxGimmickView.h>
2 #include <creaImageIOWxTreeView.h>
3 #include <creaImageIOSystem.h>
4 using namespace crea;
5 // Icons
6 #include "icons/accept.xpm"
7 #include "icons/add.xpm"
8 #include "icons/folder-down.xpm"
9 #include "icons/page-down.xpm"
10 #include "icons/remove.xpm"
11 #include "icons/database-add.xpm"
12 #include "icons/help.xpm"
13
14 #include <wx/imaglist.h>
15
16 namespace creaImageIO
17 {
18   //======================================================================
19   // The ids of the different tools
20   enum
21     {
22       TOOL_ADDFILES_ID = 1,
23       TOOL_ADDDIR_ID = 2,
24       TOOL_REMOVE_ID = 3,
25       TOOL_ADDDATABASE_ID = 4,
26       TOOL_HELP_ID = 5
27     };
28   //======================================================================
29
30   //================================================================
31   // 
32   const int icon_number = 7;
33   // Icon ids
34   typedef enum
35     {
36       Icon_accept,
37       Icon_add,
38       Icon_folder_down,
39       Icon_page_down,
40       Icon_remove,
41       Icon_database_add,
42       Icon_help
43     }
44     icon_id;
45   //================================================================
46
47   //================================================================
48   /*
49   const icon_id Icon[5] = { Icon_Database,  
50                             Icon_Patient,
51                             Icon_Study,
52                             Icon_Series,
53                             Icon_Image };
54   */
55   //================================================================
56
57
58   //======================================================================
59   // CTor
60   WxGimmickView::WxGimmickView(Gimmick* gimmick,
61                                wxWindow *parent, 
62                                const wxWindowID id,
63                                const wxPoint& pos, const wxSize& size,
64                                int min_dim,
65                                    int max_dim,
66                                    int out_dim,
67                                int number_of_threads)
68     : wxPanel(parent,id,pos,size),
69       GimmickView(gimmick),
70       mProgressDialog(0)
71   {
72     GimmickDebugMessage(1,"WxGimmickView::WxGimmickView"
73                         <<std::endl);
74     // Sets the current directory to the home dir
75     mCurrentDirectory =  std2wx(gimmick->GetHomeDirectory());
76
77      // Connect the AddProgress callback
78     gimmick->ConnectAddProgressObserver
79       ( boost::bind( &WxGimmickView::OnAddProgress , this, _1 ) );
80
81     // Create the list of icons (mIcon)
82     CreateIconList();
83
84     // Global sizer
85     wxBoxSizer  *sizer = new wxBoxSizer(wxVERTICAL);
86
87     // Create the tool bar
88     CreateToolBar(); 
89     sizer->Add( mToolBar ,0, wxGROW  ,0);
90
91     // Split part below toolbar into notebook for views and panel
92     // for preview, messages...
93     mSplitter = new wxSplitterWindow( this , -1);
94  
95    
96     // Notebook
97     mNotebook = new wxNotebook(mSplitter,
98                                -1,wxDefaultPosition, wxDefaultSize, 0);
99
100         //Gimmick
101         mGimmick=gimmick;
102
103         mSelectionMaxDimension=max_dim;
104         mSelectionMinDimension=min_dim;
105         mOutputDimension=out_dim;
106
107     // Create the views
108     CreateTreeViews();
109
110     // Bottom panel 
111     mBottomPanel = new wxPanel(mSplitter,-1);
112
113     // Splitting
114     int hsize = size.GetHeight();
115     int bottom_minsize = 200;
116
117     mSplitter->SetMinimumPaneSize( bottom_minsize );
118     mSplitter->SplitHorizontally( mNotebook, mBottomPanel, 
119                                    hsize - bottom_minsize);
120   
121     sizer->Add( mSplitter,1,wxGROW  ,0);
122
123
124     SetSizer( sizer );     
125     SetAutoLayout(true);
126     Layout();
127
128   }
129   //======================================================================
130
131   //======================================================================
132   /// Destructor
133   WxGimmickView::~WxGimmickView()
134   {
135     GimmickDebugMessage(1,"WxGimmickView::~WxGimmickView"
136                         <<std::endl);
137   }
138   //======================================================================
139   
140   //======================================================================
141   /// Creates the tool bar
142   void WxGimmickView::CreateToolBar()
143   {
144     long style = wxTB_HORIZONTAL | wxNO_BORDER | wxTB_TEXT;
145     mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize,
146                              style);
147
148     mToolAddFile = mToolBar->AddTool( TOOL_ADDFILES_ID, 
149                                       _T("Add file(s)"),
150                                       mIcon->GetBitmap(Icon_page_down),
151                                       _T("Add one or more file to database")
152                                       );
153     mToolAddDir = mToolBar->AddTool( TOOL_ADDDIR_ID, 
154                                       _T("Add folder"),
155                                       mIcon->GetBitmap(Icon_folder_down),
156                                       _T("Add the content of a folder to database")
157                                       );
158     mToolRemove = mToolBar->AddTool( TOOL_REMOVE_ID, 
159                                       _T("Remove"),
160                                       mIcon->GetBitmap(Icon_remove),
161                                       _T("Remove selected items")
162                                       );
163     mToolAddDatabase = mToolBar->AddTool( TOOL_ADDDATABASE_ID, 
164                                       _T("Open database"),
165                                       mIcon->GetBitmap(Icon_database_add),
166                                       _T("Open a local or distant database")
167                                       );
168     mToolHelp = mToolBar->AddTool( TOOL_HELP_ID, 
169                                       _T("Help"),
170                                       mIcon->GetBitmap(Icon_help),
171                                       _T("Open help window")
172                                       );
173     //const wxBitmap& bitmap1, const wxString& shortHelpString = "", wxItemKind kind = wxITEM_NORMAL)
174
175     mToolBar->Realize();
176   }
177   //======================================================================
178
179  
180   //======================================================================
181   /// Create the tree view for TreeHandler provided
182   void WxGimmickView::CreateTreeView( TreeHandler* h)
183   {
184     std::string name(h->GetTree().GetAttribute("Name"));
185     GimmickMessage(2,"Creating the tree view for '"<<
186                    name<<"'"<<std::endl);
187     // Create the WxTreeView
188     WxTreeView* view = new WxTreeView(h,mNotebook,-1);
189         
190         view->SetMaxDimension(mSelectionMaxDimension);
191         view->SetMinDimension(mSelectionMinDimension);
192         view->SetOutputDimension(mOutputDimension);
193
194     // TO DO : TEST THAT A VIEW WITH SAME NAME IS NOT
195     // ALREADY IN THE MAP
196     GetTreeViewMap()[name] = view;
197
198     // Add Notebook page
199     mNotebook->AddPage( view, crea::std2wx(name) );
200
201   }
202   //======================================================================
203   /// Returns the selected Images so that they comply with the given parameter(<4D)
204   vtkImageData* WxGimmickView::GetSelectedImage(int dim)
205   {
206           return NULL;
207   }
208
209   //======================================================================
210   /// Returns the selected Images so that they comply with the given parameter(4D)
211   void WxGimmickView::GetSelectedImages(std::vector<vtkImageData*>& s, int dim)
212   {
213         
214                 //GetTreeViewMap()["Local database"]->GetImageData()
215                 //return NULL   ;
216         
217   }
218
219   //=================================================
220   void WxGimmickView::CreateIconList()
221   {
222     // Size of the icons;
223     int size = 24;
224
225     wxIcon icons[20];
226     // should correspond to Icon_xxx enum
227     icons[Icon_accept] = wxIcon(accept_xpm);
228     icons[Icon_add] = wxIcon(add_xpm);
229     icons[Icon_folder_down] = wxIcon(folder_down_xpm);
230     icons[Icon_page_down] = wxIcon(page_down_xpm);
231     icons[Icon_remove] = wxIcon(remove_xpm);
232     icons[Icon_database_add] = wxIcon(database_add_xpm);
233     icons[Icon_help] = wxIcon(help_xpm);
234
235     //   unsigned int NbIcons = 8;
236     // Make an image list containing small icons
237     mIcon = new wxImageList(size,size,true);
238     
239     // Make all icons the same size = size of the first one
240     int sizeOrig = icons[0].GetWidth();
241     for ( size_t i = 0; i < icon_number; i++ )
242       {
243         if ( size == sizeOrig )
244           {
245             mIcon->Add(icons[i]);
246           }
247         else
248           {
249             mIcon->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
250           }
251       }
252   }
253   //=================================================
254
255
256   //=================================================
257   void WxGimmickView::OnAddFiles(wxCommandEvent& event)
258   {
259    long style = wxOPEN | wxFILE_MUST_EXIST | wxFD_MULTIPLE;
260     std::string wc("*.*");
261     wxFileDialog* FD = new wxFileDialog( 0, 
262                                          _T("Select file"),
263                                          _T(""),
264                                          _T(""),
265                                          crea::std2wx(wc),
266                                          style,
267                                          wxDefaultPosition);
268     
269     if (FD->ShowModal()==wxID_OK)
270       {
271         wxBusyCursor busy;
272
273         wxArrayString files;
274         FD->GetPaths(files);
275         unsigned int i;
276         std::vector<std::string> filenames;
277         for (i=0;i<files.GetCount();++i)
278         {
279           filenames.push_back(wx2std(files[i]));
280           GimmickMessage(2,"Adding File "<<files[i]<<"."<<std::endl);
281         }
282
283         mProgressDialog = 
284           new wxProgressDialog(_T("Adding file(s)"),
285                                _T(""),
286                                1000,
287                                this,
288                                wxPD_ELAPSED_TIME |
289                                //                              wxPD_ESTIMATED_TIME | 
290                                //                              wxPD_REMAINING_TIME |
291                                wxPD_CAN_ABORT );
292
293         // TO DO : select the current tree handler
294         mGimmick->AddFiles("Local database",filenames);
295
296         mProgressDialog->Pulse(_T("Updating view..."));
297
298         UpdateTreeViewLevel("Local database",1);
299         delete mProgressDialog;
300         DisplayAddSummary();    
301       }
302         
303   }
304   //=================================================
305
306   //=================================================
307   void WxGimmickView::OnAddDir(wxCommandEvent& event)
308   {
309     long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
310     wxDirDialog* FD = 
311       new wxDirDialog( 0, 
312                        _T("Select directory"),
313                        mCurrentDirectory,
314                        style);
315     
316     if (FD->ShowModal()==wxID_OK)
317       {
318         
319         bool recurse = false;
320         if (wxMessageBox(_T("Recurse into sub-directories ?"),
321                          _T("Scan directory"),
322                          wxYES_NO,this ) == wxYES)
323           {
324             recurse = true;
325           }
326        
327         wxBusyCursor busy;
328         wxString title(_T("Adding directory"));
329         if (recurse) 
330           title = _T("Adding directory (recursive)");
331         mProgressDialog = 
332           new wxProgressDialog(_T("Adding directory"),
333                                _T(""),
334                                1000,
335                                this,
336                                wxPD_ELAPSED_TIME |
337                                //                              wxPD_ESTIMATED_TIME | 
338                                //                              wxPD_REMAINING_TIME |
339                                wxPD_CAN_ABORT );
340         std::string dirname = wx2std (FD->GetPath()) ;
341         mCurrentDirectory = FD->GetPath();  
342         
343         // TO DO : select the current tree handler
344         mGimmick->AddDir("Local database",dirname,recurse);
345         
346         mProgressDialog->Pulse(_T("Updating view..."));
347         
348         UpdateTreeViewLevel("Local database",1);
349         delete mProgressDialog;
350         DisplayAddSummary();
351       }
352   }
353   //=================================================
354
355    //=================================================
356   void WxGimmickView::OnRemove(wxCommandEvent& event)
357   {
358         //TODO Select current tree handler       
359     wxBusyCursor busy;
360     GetTreeViewMap()["Local database"]->RemoveSelected(1);
361   }
362   //=================================================
363
364   //=================================================
365   /// AddProgress Gimmick callback
366   void WxGimmickView::OnAddProgress( Gimmick::AddProgress& p)
367   {
368
369     char mess[200];
370     sprintf(mess,"%i dirs - %i files - %i handled - %i added",
371            p.GetNumberScannedDirs(),
372            p.GetNumberScannedFiles(),
373            p.GetNumberHandledFiles(),
374            p.GetNumberAddedFiles());
375     //    std::cout << "OnAddProgress "<<mess<<std::endl;
376     wxString s(wxString::From8BitData(mess));
377     //  std::cout << "Pulse"<<std::endl;
378     if (!mProgressDialog->Pulse(s)) 
379       {
380         p.SetStop();
381       }
382     //  std::cout << "OnAddProgress ok"<<std::endl;
383   }
384   //=================================================
385
386   //=================================================
387   void WxGimmickView::DisplayAddSummary()
388   {
389     const Gimmick::AddProgress& p = mGimmick->GetAddProgress();
390     std::stringstream mess;
391     mess << "Dirs \tscanned\t: " << p.GetNumberScannedDirs()  << "\n";
392     mess << "Files\tscanned\t: " << p.GetNumberScannedFiles() << "\n";
393     mess << "Files\thandled\t: " << p.GetNumberHandledFiles() << "\n\n";
394     mess << "Files\tadded  \t: " << p.GetNumberAddedFiles()   << "\n\n";
395
396     /*    char times[500];
397     sprintf(times,"Time to parse dir \t\t: %ld ms \t%d°/o\nTime to read files info \t: %ld ms \t%d°/o\nTime to update structs \t: %ld ms \t%d°/o\nTime to update database \t: %ld ms \t%d°/o\nTotal time \t\t\t: %ld ms",
398             summary.parse_time,
399             (int)( summary.parse_time*100./summary.total_time),
400             summary.file_scan_time,
401             (int)(summary.file_scan_time*100./summary.total_time),
402             summary.update_structs_time,
403             (int)(summary.update_structs_time*100./summary.total_time),
404             summary.update_database_time,
405             (int)(summary.update_database_time*100./summary.total_time),
406             summary.total_time );
407     
408     mess << times;
409     */
410     wxMessageBox(std2wx(mess.str()),_T("Addition result"),wxOK,this);
411   }
412    //=================================================
413
414    //=================================================
415   BEGIN_EVENT_TABLE(WxGimmickView, wxPanel)
416     EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnAddFiles)
417     EVT_TOOL(TOOL_ADDDIR_ID, WxGimmickView::OnAddDir)
418         EVT_TOOL(TOOL_REMOVE_ID, WxGimmickView::OnRemove)
419     END_EVENT_TABLE()
420   //=================================================
421
422 } // EO namespace creaImageIO
423
424