]> 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         
278     /*   
279         TreeItemData *data = 
280           (TreeItemData *)
281           mTreeListCtrl->GetItemData(mItemOfMenu);
282         DicomDatabase* db = data->GetDicomNode()->GetDicomDatabase();
283         DicomDatabase::UpdateSummary summary;
284         wxProgressDialog* progress = 
285           new wxProgressDialog(_T("Adding file(s)"),
286                                _T(""),
287                                1000,
288                                this,
289                                wxPD_ELAPSED_TIME |
290                                wxPD_ESTIMATED_TIME | 
291                                wxPD_REMAINING_TIME |
292                                wxPD_CAN_ABORT );
293        
294         db->AddFiles(filenames,progress,summary);
295         
296         progress->Pulse(_T("Updating view..."));
297         UpdateDicomDatabaseView(db);
298         delete progress;
299         DisplayUpdateSummary(summary,this);
300         */
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         
351       }
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   BEGIN_EVENT_TABLE(WxGimmickView, wxPanel)
379     EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnAddFiles)
380     EVT_TOOL(TOOL_ADDDIR_ID, WxGimmickView::OnAddDir)
381     END_EVENT_TABLE()
382   //=================================================
383
384 } // EO namespace creaImageIO
385
386