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