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