]> Creatis software - creaImageIO.git/blob - src2/creaImageIOWxGimmickView.cpp
Fixed a problem with the Listener
[creaImageIO.git] / src2 / creaImageIOWxGimmickView.cpp
1 #include <creaImageIOWxGimmickView.h>
2 #include <creaImageIOWxTreeView.h>
3 #include <creaImageIOSystem.h>
4 #include <creaImageIOWxCustomizeConfigPanel.h>
5 #include <creaImageIOWxListenerPanel.h>
6
7 using namespace crea;
8 // Icons
9 #include "icons/accept.xpm"
10 #include "icons/add.xpm"
11 #include "icons/folder-down.xpm"
12 #include "icons/page-down.xpm"
13 #include "icons/remove.xpm"
14 #include "icons/database-add.xpm"
15 #include "icons/create-database.xpm"
16 #include "icons/help.xpm"
17 #include "icons/synchronize.xpm"
18 #include "icons/settings.xpm"
19 #include "icons/tools.xpm"
20
21 #include <wx/imaglist.h>
22 #include <wx/popupwin.h>
23 #include<boost/filesystem/operations.hpp>
24
25 namespace creaImageIO
26 {
27    
28   //======================================================================
29   // The ids of the different tools
30   enum
31     {
32           TOOL_CREATEDB_ID = 1,
33       TOOL_ADDFILES_ID = 2,
34       TOOL_ADDDIR_ID = 3,
35           TOOL_ADDDATABASE_ID = 4,
36       TOOL_REMOVE_ID = 5,
37           TOOL_SYNCHRONIZE_ID = 6,
38       TOOL_HELP_ID = 7,
39           TOOL_SETTINGS_ID = 8,
40           TOOL_TOOLS_ID = 9
41     };
42   //======================================================================
43
44   //================================================================
45   // 
46   const int icon_number = 11;
47   // Icon ids
48   typedef enum
49     {
50           Icon_create_database,
51       Icon_accept,
52       Icon_add,
53       Icon_folder_down,
54       Icon_page_down,
55           Icon_database_add,
56       Icon_remove,
57           Icon_synchronize,
58       Icon_help,
59           Icon_settings,
60       Icon_tools
61     }
62     icon_id;
63   //================================================================
64
65   //================================================================
66   /*
67   const icon_id Icon[5] = { Icon_Database,  
68                             Icon_Patient,
69                             Icon_Study,
70                             Icon_Series,
71                             Icon_Image };
72   */
73   //================================================================
74
75
76   //======================================================================
77   // CTor
78   WxGimmickView::WxGimmickView(Gimmick* gimmick,
79                                wxWindow *parent, 
80                                const wxWindowID id,
81                                const wxPoint& pos, const wxSize& size,
82                                int min_dim,
83                                    int max_dim,
84                                int number_of_threads)
85     : wxPanel(parent,id,pos,size),
86       GimmickView(gimmick, number_of_threads),
87       mProgressDialog(0),
88       mConstructed(false)
89   {
90     GimmickDebugMessage(1,"WxGimmickView::WxGimmickView"
91                         <<std::endl);
92     // Sets the current directory to the home dir
93     mCurrentDirectory =  std2wx(gimmick->GetHomeDirectory());
94
95      // Connect the AddProgress callback
96     gimmick->ConnectAddProgressObserver
97       ( boost::bind( &WxGimmickView::OnAddProgress , this, _1 ) );
98
99     // Create the list of icons (mIcon)
100     CreateIconList();
101
102     // Global sizer
103     wxBoxSizer  *sizer = new wxBoxSizer(wxVERTICAL);
104
105     // Create the tool bar
106     CreateToolBar(); 
107     sizer->Add( mToolBar ,0, wxGROW  ,0);
108
109     // Split part below toolbar into notebook for views and panel
110     // for preview, messages...
111     mSplitter = new wxSplitterWindow( this , -1);
112  
113    
114     // Notebook
115     mNotebook = new wxNotebook(mSplitter,
116                                -1,wxDefaultPosition, wxDefaultSize, 0);
117
118     //Gimmick
119     mGimmick=gimmick;
120
121       
122     mSelectionMaxDimension= max_dim;
123     mSelectionMinDimension= min_dim;
124     
125     // Create the views
126     CreateTreeViews();
127
128     // Bottom panel 
129     mBottomPanel = new wxPanel(mSplitter,-1);
130     
131           wxBoxSizer    *bottom_sizer = new wxBoxSizer(wxVERTICAL); //HORIZONTAL);
132     
133     
134     // Previewer
135     mViewer = new WxViewer(mBottomPanel, wxID_ANY, wxT("Gimmick! Viewer"),wxDefaultPosition, wxDefaultSize );
136         pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
137         mViewer->SetImageVector(pointers);
138         mViewer->StartPlayer();
139
140
141     bottom_sizer->Add(mViewer,1,wxGROW,1);
142     //    mViewer->Show();
143
144           mText = new wxStaticText(mBottomPanel, wxID_ANY, wxT("Welcome to Gimmick!"));
145           bottom_sizer->Add(mText,0,wxGROW,0);
146
147           
148           
149     mBottomPanel->SetSizer(bottom_sizer);
150
151     // Splitting
152     int hsize = size.GetHeight();
153
154     int top_minsize = 450;
155     int bottom_minsize = 50;
156
157     mSplitter->SetMinimumPaneSize( bottom_minsize );
158     mSplitter->SplitHorizontally( mNotebook, mBottomPanel, 
159                                   top_minsize);
160
161     sizer->Add( mSplitter,1,wxGROW  ,0);
162
163         mProgressDialog=0;
164     SetSizer( sizer );     
165     SetAutoLayout(true);
166     Layout();
167         mListener=new Listener();
168         mListener->ConnectObserver(boost::bind( &WxGimmickView::OnDriveMount , this, _1 ) );
169         mListener->Create();
170         mListener->Run();
171         mListener->Pause();
172     
173     mConstructed = true;
174   }
175   //======================================================================
176
177   //======================================================================
178   /// Destructor
179   WxGimmickView::~WxGimmickView()
180   {
181         // stop the viewer before application exit.
182         mViewer->StopPlayer();
183     GimmickDebugMessage(1,"WxGimmickView::~WxGimmickView"
184                         <<std::endl);
185         if(mListener->IsAlive())
186         {
187          mListener->Delete();
188         }
189   }
190   //======================================================================
191   
192   //======================================================================
193   /// Creates the tool bar
194   void WxGimmickView::CreateToolBar()
195   {
196     long style = wxTB_HORIZONTAL | wxNO_BORDER | wxTB_TEXT;
197     mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize,
198                              style);
199
200         mToolAddFile = mToolBar->AddTool( TOOL_CREATEDB_ID, 
201                                       _T("Create database"),
202                                       mIcon->GetBitmap(Icon_create_database),
203                                       _T("Create DB from an Attributes Descriptor file")
204                                       );
205     mToolAddFile = mToolBar->AddTool( TOOL_ADDFILES_ID, 
206                                       _T("Add file(s)"),
207                                       mIcon->GetBitmap(Icon_page_down),
208                                       _T("Add one or more file to database")
209                                       );
210     mToolAddDir = mToolBar->AddTool( TOOL_ADDDIR_ID, 
211                                       _T("Add folder"),
212                                       mIcon->GetBitmap(Icon_folder_down),
213                                       _T("Add the content of a folder to database")
214                                       );
215         mToolAddDatabase = mToolBar->AddTool( TOOL_ADDDATABASE_ID, 
216                                       _T("Open database"),
217                                       mIcon->GetBitmap(Icon_database_add),
218                                       _T("Open a local or distant database")
219                                       );
220     mToolRemove = mToolBar->AddTool( TOOL_REMOVE_ID, 
221                                       _T("Remove"),
222                                       mIcon->GetBitmap(Icon_remove),
223                                       _T("Remove selected items")
224                                       );
225     mToolSynchronize = mToolBar->AddTool( TOOL_SYNCHRONIZE_ID, 
226                                       _T("Synchronize"),
227                                       mIcon->GetBitmap(Icon_synchronize),
228                                       _T("Synchronizes the database with disk")
229                                       );
230         mToolHelp = mToolBar->AddTool( TOOL_HELP_ID, 
231                                       _T("Help"),
232                                       mIcon->GetBitmap(Icon_help),
233                                       _T("Open help window")
234                                       );
235         mToolSettings = mToolBar->AddTool( TOOL_SETTINGS_ID, 
236                                       _T("System settings"),
237                                       mIcon->GetBitmap(Icon_settings),
238                                       _T("Allows the modification of various system settings")
239                                       );
240         mToolTools = mToolBar->AddTool( TOOL_TOOLS_ID, 
241                                       _T("Tools"),
242                                       mIcon->GetBitmap(Icon_tools),
243                                       _T("Applies tools to images")
244                                       );
245     //const wxBitmap& bitmap1, const wxString& shortHelpString = "", wxItemKind kind = wxITEM_NORMAL)
246
247     mToolBar->Realize();
248   }
249   //======================================================================
250
251  
252   //======================================================================
253   /// Create the tree view for TreeHandler provided
254   void WxGimmickView::CreateTreeView( TreeHandler* h)
255   {
256     std::string name(h->GetTree().GetAttribute("Name"));
257     GimmickMessage(2,"Creating the tree view for '"<<
258                    name<<"'"<<std::endl);
259     // Create the WxTreeView
260     WxTreeView* view = new WxTreeView(h,this,mNotebook,-1);
261
262     // TO DO : TEST THAT A VIEW WITH SAME NAME IS NOT
263     // ALREADY IN THE MAP
264     GetTreeViewMap()[name] = view;
265
266     // Add Notebook page
267     mNotebook->AddPage( view, crea::std2wx(name) );
268         
269   }
270
271   //======================================================================
272   void WxGimmickView::GetSelectedImages(std::vector<vtkImageData*>& s, int dim)
273   {
274         std::vector<std::string> files;
275         GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(files);
276         ReadImagesNotThreaded(s,files,dim);
277   }
278   //======================================================================
279
280   //======================================================================
281   void WxGimmickView::GetSelectedFiles(std::vector<std::string>& s)
282   {
283         GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(s);
284   }
285   //======================================================================
286
287   //======================================================================
288   void WxGimmickView::GetImages(int dim, 
289                                 const std::vector<std::string>& files, 
290                                 std::vector<vtkImageData*>& s)
291   {
292         ReadImagesNotThreaded(s,files,dim);
293   }
294   //======================================================================
295
296
297   //=================================================
298   void WxGimmickView::CreateIconList()
299   {
300     // Size of the icons;
301     int size = 16;
302
303     wxIcon icons[20];
304     // should correspond to Icon_xxx enum
305     icons[Icon_accept] = wxIcon(accept_xpm);
306     icons[Icon_add] = wxIcon(add_xpm);
307     icons[Icon_folder_down] = wxIcon(folder_down_xpm);
308     icons[Icon_page_down] = wxIcon(page_down_xpm);
309     icons[Icon_remove] = wxIcon(remove_xpm);
310     icons[Icon_database_add] = wxIcon(database_add_xpm);
311     icons[Icon_help] = wxIcon(help_xpm);
312         icons[Icon_synchronize] = wxIcon(synchronize_xpm);
313         icons[Icon_create_database] = wxIcon(create_database_xpm);
314         icons[Icon_settings] = wxIcon(settings_xpm);
315         icons[Icon_tools] = wxIcon(tools_xpm);
316
317     //   unsigned int NbIcons = 8;
318     // Make an image list containing small icons
319     mIcon = new wxImageList(size,size,true);
320     
321     // Make all icons the same size = size of the first one
322     int sizeOrig = icons[0].GetWidth();
323     for ( size_t i = 0; i < icon_number; i++ )
324       {
325         if ( size == sizeOrig )
326           {
327             mIcon->Add(icons[i]);
328           }
329         else
330           {
331             mIcon->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
332           }
333       }
334   }
335   //=================================================
336
337
338   //=================================================
339   void WxGimmickView::OnAddFiles(wxCommandEvent& event)
340   {
341     mViewer->StopPlayer();
342    long style = wxOPEN | wxFILE_MUST_EXIST | wxFD_MULTIPLE;
343     std::string wc("*.*");
344     wxFileDialog* FD = new wxFileDialog( 0, 
345                                          _T("Select file"),
346                                          _T(""),
347                                          _T(""),
348                                          crea::std2wx(wc),
349                                          style,
350                                          wxDefaultPosition);
351     
352     if (FD->ShowModal()==wxID_OK)
353       {
354         wxBusyCursor busy;
355
356         wxArrayString files;
357         FD->GetPaths(files);
358         unsigned int i;
359         std::vector<std::string> filenames;
360         for (i=0;i<files.GetCount();++i)
361         {
362           filenames.push_back(wx2std(files[i]));
363           GimmickMessage(2,"Adding File "<<files[i]<<"."<<std::endl);
364         }
365
366         mProgressDialog = 
367           new wxProgressDialog(_T("Adding file(s)"),
368                                _T(""),
369                                1000,
370                                this,
371                                wxPD_ELAPSED_TIME |
372                                //                              wxPD_ESTIMATED_TIME | 
373                                //                              wxPD_REMAINING_TIME |
374                                wxPD_CAN_ABORT );
375
376         // TO DO : select the current tree handler
377         mGimmick->AddFiles(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),filenames);
378
379         mProgressDialog->Pulse(_T("Updating view..."));
380
381         UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
382         delete mProgressDialog;
383         DisplayAddSummary();    
384
385       }
386         mViewer->StartPlayer(); 
387   }
388   //=================================================
389
390   //=================================================
391   void WxGimmickView::OnAddDir(wxCommandEvent& event)
392   {
393     mViewer->StopPlayer();
394         std::string name = crea::wx2std(mNotebook->GetCurrentPage()->GetName());
395     long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
396     wxDirDialog* FD = 
397       new wxDirDialog( 0, 
398                        _T("Select directory"),
399                        mCurrentDirectory,
400                        style);
401     
402     if (FD->ShowModal()==wxID_OK)
403       {
404
405         
406                   std::string dirname = wx2std (FD->GetPath());
407                   bool recurse =  isNeedRecursive(dirname);
408                   if (recurse)
409                  {
410              recurse = wxMessageBox(_T("Recurse into sub-directories ?"),  _T("Scan directory"),         wxYES_NO,this ) == wxYES ? true : false;
411                  }
412                 
413                 wxBusyCursor busy;
414                 wxString title(_T("Adding directory"));
415                 if (recurse) 
416                 title = _T("Adding directory (recursive)");
417                 mProgressDialog = 
418                 new wxProgressDialog(_T("Adding directory"),
419                                         _T(""),
420                                         1000,
421                                         this,
422                                         wxPD_ELAPSED_TIME |
423                                         //                             wxPD_ESTIMATED_TIME | 
424                                         //                             wxPD_REMAINING_TIME |
425                                         wxPD_CAN_ABORT );
426                 
427                 mCurrentDirectory = FD->GetPath();  
428                 mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),dirname,recurse);
429                 mProgressDialog->Pulse(_T("Updating view..."));
430                 
431                 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
432                 delete mProgressDialog;
433                 DisplayAddSummary();
434
435           }
436     mViewer->StartPlayer();
437   }
438
439     //=================================================
440    // Test a directory to know if contains sub-directory to analyze
441   bool WxGimmickView::isNeedRecursive(std::string i_name)
442   {
443       boost::filesystem::directory_iterator iter(i_name), end_iter;
444           bool bfindir = false;
445                   for(; iter != end_iter; ++iter)
446                   {
447                           if(boost::filesystem::is_directory(*iter))
448                           {
449                                   return true;
450                           }
451                   }
452                   return false;
453   }
454   //=================================================
455
456   //=================================================
457   void WxGimmickView::OnSelectionChange(const std::vector<tree::Node*>& sel, bool isSelection, int selection, bool needProcess)
458   {      
459     GimmickDebugMessage(5,
460                         "WxGimmickView::OnSelectionChange"
461                         <<std::endl);
462     wxBusyCursor busy;
463         bool valid=true;
464         
465         if(sel.size()==0)
466         {
467                 valid= ValidateSelected(NULL,
468                                 mSelectionMinDimension,
469                                 mSelectionMaxDimension );
470         }
471         else if(needProcess)
472         {
473                 ResetExtent();
474                 std::vector<tree::Node*>::const_iterator i;
475                 for(i=sel.begin();i!=sel.end()&&valid;++i)
476                 {
477                         valid= ValidateSelected((*i),
478                                 mSelectionMinDimension,
479                                 mSelectionMaxDimension );
480                 }
481         }
482         else if(isSelection)
483         {
484                 valid= ValidateSelected(sel.front(),
485                                 mSelectionMinDimension,
486                                 mSelectionMaxDimension );
487         }
488         else
489         {
490                 ResetExtent();
491                 std::vector<tree::Node*>::const_iterator i;
492                 for(i=sel.begin();i!=sel.end()&&valid;++i)
493                 {
494                         valid= ValidateSelected((*i),
495                                 mSelectionMinDimension,
496                                 mSelectionMaxDimension );
497                 }
498         }
499         mText->SetLabel(crea::std2wx(GetMessage()));
500     /*if(valid)
501       {
502         ReadImageThreaded(sel);
503       }
504     else
505       {
506                   ClearSelection();
507       }*/
508         ReadImageThreaded(sel);
509
510     
511    }
512
513   //==================================================
514
515   //==================================================
516   ///Reads Images (Threaded)
517   void WxGimmickView::ReadImageThreaded(const std::vector<tree::Node*>& sel)
518   {     
519    GimmickDebugMessage(5,
520                        "ReadImageThreaded"
521                        <<std::endl);
522    int maxprio = GetMaximalPriority();
523    int prio = maxprio + 2000;
524
525    if(sel.size()>0)
526    {
527    //First load the selected images
528    mCurImageItemToShow = sel.front();
529    pointers.clear();
530    int index = 0;
531    std::vector<tree::Node*>::const_iterator selected;
532    for(selected=sel.begin();selected!=sel.end();++selected)
533      {
534        GimmickDebugMessage(5,
535                            "Requesting image from selected "
536                            <<(*selected)->GetAttribute("FullFileName")
537                            <<std::endl);
538            ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
539            pointers.push_back(ph);
540        RequestReading(*selected,prio,index,ph);
541        //       AddEntryToMap(*selected);
542        prio--;
543        index++;
544      }
545         mViewer->SetImageVector(pointers);
546         //Going up
547         prio = maxprio + 20;
548         std::vector<tree::Node*> up;
549         GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(up,true);
550         std::vector<tree::Node*>::iterator iterUp;
551         for(iterUp=up.begin();iterUp!=up.end();++iterUp)
552         {
553                 GimmickDebugMessage(5,
554                                 "Requesting image from neighbors up "
555                                 <<(*iterUp)->GetAttribute("FullFileName")
556                                 <<std::endl);
557                 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
558                 RequestReading(*iterUp,prio,-1,ph);
559                 //              AddEntryToMap(*iterUp);
560                 prio--;
561                 if (prio == maxprio) break;
562         }
563
564         //Going down
565         prio = maxprio + 19;
566         std::vector<tree::Node*> down;
567         GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(down,false);
568         std::vector<tree::Node*>::iterator iterDown;
569         for(iterDown=down.begin();iterDown!=down.end();++iterDown)
570         {
571                 GimmickDebugMessage(5,
572                                 "Requesting image from neighbors down "
573                                 <<(*iterDown)->GetAttribute("FullFileName")
574                                 <<std::endl);
575                 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
576                 RequestReading(*iterDown,prio,-1,ph);
577                 //              AddEntryToMap(*iterDown);
578                 prio--;
579                 if (prio == maxprio) break;
580         }
581    }
582    else
583    {
584            pointers.clear();
585            ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
586            pointers.push_back(ph);
587            mViewer->SetImageVector(pointers);
588    }
589   }
590
591   //==================================================
592
593   //==================================================
594
595   //==================================================
596    void  WxGimmickView::OnInternalIdle()
597   {
598    if (!mConstructed) return;
599    static bool first_time = true;
600    if (false)
601    {
602         first_time = false;
603      }
604    //   GimmickMessage(1,"WxGimmickView : Refresh viewer"<<std::endl);
605         //  mViewer->StartPlayer();
606    if(mViewer)
607    {
608            mViewer->RefreshIfNecessary();
609    }
610   }
611
612    //==================================================
613
614   //==================================================
615    void  WxGimmickView::ClearSelection()
616   {
617         pointers.clear();
618         pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
619         mViewer->SetImageVector(pointers);
620         mViewer->RefreshIfNecessary();
621         ResetExtent();
622   }
623   
624   //=================================================
625  
626   //=================================================
627   void WxGimmickView::OnRemove(wxCommandEvent& event)
628   {
629         //TODO Select current tree handler       
630     wxBusyCursor busy;
631     GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->RemoveSelected();
632         ClearSelection();
633   }
634   //=================================================
635
636   
637   //=================================================
638   void WxGimmickView::AddIgnoreFile(tree::Node* toRemove)
639   {
640           mGimmick->RemoveFile(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),toRemove);
641           GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->UpdateLevel(1);
642   }
643
644   //=================================================
645   void WxGimmickView::CopyFiles(const std::vector<std::string>& filenames)
646   {
647           mGimmick->CopyFiles(filenames, crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())));
648           wxMessageBox(std2wx("The selected files have been copied"),_T("Copy files"),wxOK,this);
649   }
650
651    //=================================================
652   void WxGimmickView::OnSynchronize(wxCommandEvent& event)
653   {       
654     wxBusyCursor busy;
655         const wxString choices[] = { _T("Check database for files deletion and addition and give a report."), 
656                                                                 _T("Check database for files deletion, addition and attributes change. Then give a report."), 
657                                                                 _T("Repair database (remove deleted files and add new files)."), 
658                                                                 _T("Repair database (remove deleted files, add new files and reset changed attributes).") } ;
659
660     wxSingleChoiceDialog dialog(this,
661                                                                 _T("Select one of the following synchronization actions:\n")
662                                 _T("Please note that, due to the heavy amount of operations required, this action might take a while."),
663                                 _T("Synchronization Settings"),
664                                 WXSIZEOF(choices), choices);
665
666     //dialog.SetSelection(0);
667
668     if (dialog.ShowModal() == wxID_OK)
669     {
670         int sel=dialog.GetSelection();
671                 bool repair=false;
672                 bool checkAttributes=false;
673                 if(sel==2 || sel==3){repair=true;}
674                 if(sel==1 || sel==3){checkAttributes=true;}
675                 std::string mess=mGimmick->Synchronize(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),repair, checkAttributes);
676                 wxMessageBox(std2wx(mess),_T("Synchronization result"),wxOK,this);
677                 if(sel==2 || sel==3){
678                 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->UpdateLevel(1);
679                 }
680                 
681     }
682   }
683   //=================================================
684
685   //=================================================
686   void WxGimmickView::OnSettings(wxCommandEvent& event)
687   {
688     wxDialog* dial= new wxDialog (this,-1,_T("System Settings"),wxDefaultPosition, wxSize(450,220));
689     wxBoxSizer  *siz = new wxBoxSizer(wxVERTICAL);
690     // Notebook
691     wxNotebook* nb= new wxNotebook(dial, -1, wxDefaultPosition, wxDefaultSize, 0);
692     
693     siz->Add( nb,1,wxGROW  ,0);  
694     CreateSettingsDialog(nb,dial);
695     dial->SetSizer(siz);
696     dial->ShowModal();
697   }
698   //=================================================
699
700   //=================================================
701   //AndresDonadio
702   void WxGimmickView::OnTools(wxCommandEvent& event)
703   {
704                 mViewer->StopPlayer();
705                 
706                 wxDialog* dial = new wxDialog (this,-1,"Tools",wxDefaultPosition, wxSize(300,250));
707
708                 wxSizer* buttonsSizer = dial->CreateSeparatedButtonSizer(wxOK|wxCANCEL);
709                 wxNotebook* nb= new wxNotebook(dial, -1, wxDefaultPosition, wxDefaultSize, 0);
710
711                 wxBoxSizer *dialSizer = new wxBoxSizer(wxVERTICAL);     
712                 dialSizer->Add(nb,1,wxGROW,0);
713                 dialSizer->Add(buttonsSizer,0,wxGROW);
714
715                 //First page: Bruker Image Reader
716                 WxGimmickTools * gimmickTools = new WxGimmickTools(nb, mCurrentDirectory);
717                 nb->AddPage( gimmickTools, crea::std2wx("Bruker Image Reader") );
718
719                 dial->SetSizer(dialSizer, true);
720                 dial->Layout();
721                 dial->ShowModal();
722
723                 if (dial->GetReturnCode() == wxID_OK)
724                         {
725                                 if (nb->GetSelection()==0)//Selection: Bruker Image Reader
726                                 {
727                                         std::string inputDir = crea::wx2std(gimmickTools->getInputDir());
728                                         std::string outputDir = crea::wx2std(gimmickTools->getOutputDir());
729                                         bool addToDB = gimmickTools->getCheckBoxValue();
730
731                                         if (inputDir.compare("")!=0 && outputDir.compare("")!=0)
732                                         {
733                                                 /*
734                                                 Bruker2Dicom b2d;    
735                                                 b2d.SetInputDirectory(crea::wx2std(inputDir));
736                                                 b2d.SetOutputDirectory(crea::wx2std(outputDir));
737                                                 b2d.SetConvertModeToDicom();
738                                                 b2d.Execute();
739                                                 */
740
741                                                 if (addToDB)
742                                                 {
743                                                         mProgressDialog = new wxProgressDialog(_T("Adding directory"),_T(""),1000,this,wxPD_ELAPSED_TIME |wxPD_CAN_ABORT );
744                                                         mCurrentDirectory = outputDir;  
745                                                         mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),crea::wx2std(outputDir),true);
746                                                         mProgressDialog->Pulse(_T("Updating view..."));
747                                                         
748                                                         UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
749                                                         delete mProgressDialog;
750                                                         DisplayAddSummary();
751                                                 }       
752                                         }
753
754                                         else
755                                         {
756                                                 wxMessageBox(_T("One or both of the directory fields are empty"),_T("Empty Fields"),wxOK,this);
757                                         }
758                                 }
759                         }
760
761                 mViewer->StartPlayer();
762   }
763   //=================================================
764
765   void WxGimmickView::CreateSettingsDialog(wxNotebook* nb, wxDialog* dial)
766   {
767           //First page: Customization of configurations
768           //Copy Path string
769           std::string cp;
770           mGimmick->GetSetting(SETTINGS_COPY_PATH,cp);
771           //Database Path String
772           std::string dp;
773           mGimmick->GetSetting(SETTINGS_DBPATH,dp);
774           //Syncronization Event String
775           std::string se;
776           mGimmick->GetSetting(SETTINGS_SYNC_EVENT,se);
777           //Syncronization Frequency String
778           std::string sf;
779           mGimmick->GetSetting(SETTINGS_SYNC_FREQ,sf);
780
781           WxCustomizeConfigPanel * customConfig=new WxCustomizeConfigPanel(nb,dial,this,cp,dp,se,sf);
782
783           nb->AddPage( customConfig, crea::std2wx("Customize Configuration") );
784
785           //Second page: Creation of Databases
786           wxPanel* databaseCreation=new wxPanel(nb);
787           nb->AddPage( databaseCreation, crea::std2wx("Create Database") );
788
789           //Third page: CD/DVD Watch
790           WxListenerPanel* cdWatch=new WxListenerPanel(nb,dial, this, mListener->IsPaused());
791           nb->AddPage( cdWatch, crea::std2wx("CD/DVD") );
792
793           //Fourth page: Selection of attributes to show (edit)
794           wxPanel* attSelection=new wxPanel(nb);
795           nb->AddPage( attSelection, crea::std2wx("Selection of Attributes") );
796   }
797
798   //===================================================================
799   void WxGimmickView::OnSaveSettingsCallback(const std::string& copyPath,
800           const std::string& dbPath,
801           const std::string& syncEvent,
802           const std::string& syncFreq)
803   {
804           mGimmick->UpdateSetting(SETTINGS_COPY_PATH,copyPath);
805           mGimmick->UpdateSetting(SETTINGS_DBPATH,dbPath);
806           mGimmick->UpdateSetting(SETTINGS_SYNC_EVENT,syncEvent);
807           mGimmick->UpdateSetting(SETTINGS_SYNC_FREQ,syncFreq);
808   }
809
810   //===================================================================
811   void WxGimmickView::OnListenerCallback(const std::string& drive, bool addFiles, bool removeFiles)
812   {
813          mListener->SetMonitoredDrive(drive);
814          mListener->SetAddFilesState(addFiles);
815          mListener->SetRemoveFilesState(removeFiles);
816   }
817
818   //========================================================================
819
820   void WxGimmickView::OnDriveMount(bool mount)
821   {
822           GimmickMessage(1, "Gimmick::OnDriveMount"<<std::endl);
823           std::string drive;
824           mListener->GetMonitoredDrive(drive);
825           
826           if(mount)
827           {
828                 mViewer->StopPlayer();
829                 wxBusyCursor busy;
830                 wxString title(_T("Adding drive"));
831                 mProgressDialog = 
832                 new wxProgressDialog(_T("Adding drive"),
833                                         _T(""),
834                                         1000,
835                                         this,
836                                         wxPD_ELAPSED_TIME |
837                                         //                             wxPD_ESTIMATED_TIME | 
838                                         //                             wxPD_REMAINING_TIME |
839                                         wxPD_CAN_ABORT );
840                 mCurrentDirectory = crea::std2wx(drive);
841                 mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),drive,true);
842                 mProgressDialog->Pulse(_T("Updating view..."));
843                 
844                 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
845                 delete mProgressDialog;
846                 DisplayAddSummary();
847                 mViewer->StartPlayer();
848                   
849           }
850           else
851           {  
852                   mGimmick->DeleteDrive(drive);
853                   UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
854           }
855          
856   }
857
858    //========================================================================
859
860   void WxGimmickView::StartListeningThread()
861   {
862           mListener->Resume();
863   }
864
865    //========================================================================
866
867   void WxGimmickView::StopListeningThread()
868   {
869           mListener->Pause();
870   }
871
872
873   //=================================================
874   /// AddProgress Gimmick callback
875   void WxGimmickView::OnAddProgress( Gimmick::AddProgress& p)
876   {
877
878     char mess[200];
879    
880         sprintf(mess,"%i dirs : %i files :\n            %i handled - %i added",
881            p.GetNumberScannedDirs(),
882            p.GetNumberScannedFiles(),
883            p.GetNumberHandledFiles(),
884            p.GetNumberAddedFiles());
885     //    std::cout << "OnAddProgress "<<mess<<std::endl;
886     wxString s(wxString::From8BitData(mess));
887     //  std::cout << "Pulse"<<std::endl;
888     if (!mProgressDialog->Pulse(s)) 
889       {
890         p.SetStop();
891       }
892     //  std::cout << "OnAddProgress ok"<<std::endl;
893   }
894   //=================================================
895
896   //=================================================
897   void WxGimmickView::DisplayAddSummary()
898   {
899     const Gimmick::AddProgress& p = mGimmick->GetAddProgress();
900     std::stringstream mess;
901     mess << "Dirs \tscanned\t: " << p.GetNumberScannedDirs()  << "\n";
902     mess << "Files\tscanned\t: " << p.GetNumberScannedFiles() << "\n";
903     mess << "Files\thandled\t: " << p.GetNumberHandledFiles() << "\n\n";
904     mess << "Files\tadded  \t: " << p.GetNumberAddedFiles()   << "\n\n";
905
906     /*    char times[500];
907     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",
908             summary.parse_time,
909             (int)( summary.parse_time*100./summary.total_time),
910             summary.file_scan_time,
911             (int)(summary.file_scan_time*100./summary.total_time),
912             summary.update_structs_time,
913             (int)(summary.update_structs_time*100./summary.total_time),
914             summary.update_database_time,
915             (int)(summary.update_database_time*100./summary.total_time),
916             summary.total_time );
917     
918     mess << times;
919     */
920     wxMessageBox(std2wx(mess.str()),_T("Addition result"),wxOK,this);
921   }
922
923   //////////////////////////////////////////////////
924   // Add a DB to application                                      //
925   // @param event : WxEvent                                               //
926   // @return : -                                                                  //
927   //////////////////////////////////////////////////
928   void WxGimmickView::OnAddDB(wxCommandEvent& event)
929   {
930     
931           //Select DB
932           long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
933           std::string wc("*.sqlite3*");
934           wxFileDialog* FD = new wxFileDialog( 0, 
935                                          _T("Select file"),
936                                          _T(""),
937                                          _T(""),
938                                          crea::std2wx(wc),
939                                          style,
940                                          wxDefaultPosition);
941     
942     if (FD->ShowModal()==wxID_OK)
943         {
944                 wxBusyCursor busy;
945                 wxArrayString files;
946                 FD->GetPaths(files);
947                 std::stringstream st;
948                 for(int i = 0; i< files.size(); i++)
949                 {
950                         //get name of DB (file name)
951                   size_t pos = files[i].find_last_of(_T("\\"));
952                   std::string name = crea::wx2std(files[i].substr(pos+1));
953                   pos = name.find_last_of(".");
954                   name = name.substr(0,pos);
955                   //create TreeHandler
956                   mGimmick->addDB(name, crea::wx2std(files[i]));
957                   //create TreeView
958                   CreateSingleTreeView(name);
959                 }
960         }
961   }
962     ////////////////////////////////////////////////////
963   // Create a DB from an Attributes Descriptor files  //
964   // @param event : WxEvent                                               //
965   // @return : -                                                                  //
966   //////////////////////////////////////////////////
967   void WxGimmickView::OnCreateDB(wxCommandEvent& event)
968   {
969     
970           //Select DB
971           long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
972           std::string wc("*.txt");
973           wxFileDialog* FD = new wxFileDialog( 0, 
974                                          _T("Select file"),
975                                          _T(""),
976                                          _T(""),
977                                          crea::std2wx(wc),
978                                          style,
979                                          wxDefaultPosition);
980     
981     if (FD->ShowModal()==wxID_OK)
982         {
983                 wxBusyCursor busy;
984                 wxArrayString afile;
985                 FD->GetPaths(afile);
986                 //get name of DB (file name)
987                 std::string file = crea::wx2std(afile[0]);
988                 size_t pos = file.find_last_of("\\");
989                 std::string name = file.substr(pos+1);
990                 std::string directory = file.substr(0,pos);
991                 pos = name.find_last_of(".");
992                 name = name.substr(0,pos);
993                 //get directory to store DB
994                 directory +=  "\\" + name + ".sqlite3";
995                 //create createDB
996                 mGimmick->createDB(name, file,directory);
997                 //create TreeHandler
998                 mGimmick->addDB(name, directory);
999                 //create TreeView
1000                 CreateSingleTreeView(name);
1001         }
1002   }
1003
1004    //=================================================
1005
1006    //=================================================
1007   BEGIN_EVENT_TABLE(WxGimmickView, wxPanel)
1008     EVT_TOOL(TOOL_CREATEDB_ID, WxGimmickView::OnCreateDB)
1009     EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnAddFiles)
1010     EVT_TOOL(TOOL_ADDDIR_ID, WxGimmickView::OnAddDir)
1011         EVT_TOOL(TOOL_ADDDATABASE_ID, WxGimmickView::OnAddDB)
1012         EVT_TOOL(TOOL_REMOVE_ID, WxGimmickView::OnRemove)
1013         EVT_TOOL(TOOL_SYNCHRONIZE_ID, WxGimmickView::OnSynchronize)
1014         EVT_TOOL(TOOL_SETTINGS_ID, WxGimmickView::OnSettings)
1015         EVT_TOOL(TOOL_TOOLS_ID, WxGimmickView::OnTools)
1016   END_EVENT_TABLE()
1017   //=================================================
1018
1019 } // EO namespace creaImageIO
1020
1021