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