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