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