]> Creatis software - creaImageIO.git/blob - src2/creaImageIOWxGimmickView.cpp
9ada3c16b67eaf2f7738c1a6b0b10fff7739c66d
[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::AddDir(std::string dirName)
658   {
659                 mProgressDialog = new wxProgressDialog(_T("Adding directory"),_T(""),1000,this,wxPD_ELAPSED_TIME |wxPD_CAN_ABORT );
660                 mCurrentDirectory = crea::std2wx(dirName);
661                 mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),dirName,true);
662                 mProgressDialog->Pulse(_T("Updating view..."));
663                                                                         
664                 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
665                 delete mProgressDialog;
666                 DisplayAddSummary();
667   }
668
669    //=================================================
670   void WxGimmickView::OnSynchronize(wxCommandEvent& event)
671   {       
672     wxBusyCursor busy;
673         const wxString choices[] = { _T("Check database for files deletion and addition and give a report."), 
674                                                                 _T("Check database for files deletion, addition and attributes change. Then give a report."), 
675                                                                 _T("Repair database (remove deleted files and add new files)."), 
676                                                                 _T("Repair database (remove deleted files, add new files and reset changed attributes).") } ;
677
678     wxSingleChoiceDialog dialog(this,
679                                                                 _T("Select one of the following synchronization actions:\n")
680                                 _T("Please note that, due to the heavy amount of operations required, this action might take a while."),
681                                 _T("Synchronization Settings"),
682                                 WXSIZEOF(choices), choices);
683
684     //dialog.SetSelection(0);
685
686     if (dialog.ShowModal() == wxID_OK)
687     {
688         int sel=dialog.GetSelection();
689                 bool repair=false;
690                 bool checkAttributes=false;
691                 if(sel==2 || sel==3){repair=true;}
692                 if(sel==1 || sel==3){checkAttributes=true;}
693                 std::string mess=mGimmick->Synchronize(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),repair, checkAttributes);
694                 wxMessageBox(std2wx(mess),_T("Synchronization result"),wxOK,this);
695                 if(sel==2 || sel==3){
696                 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->UpdateLevel(1);
697                 }
698                 
699     }
700   }
701   //=================================================
702
703   //=================================================
704   void WxGimmickView::OnSettings(wxCommandEvent& event)
705   {
706     wxDialog* dial= new wxDialog (this,-1,_T("System Settings"),wxDefaultPosition, wxSize(450,220));
707     wxBoxSizer  *siz = new wxBoxSizer(wxVERTICAL);
708     // Notebook
709     wxNotebook* nb= new wxNotebook(dial, -1, wxDefaultPosition, wxDefaultSize, 0);
710     
711     siz->Add( nb,1,wxGROW  ,0);  
712     CreateSettingsDialog(nb,dial);
713     dial->SetSizer(siz);
714     dial->ShowModal();
715   }
716   //=================================================
717
718   //=================================================
719   //AndresDonadio
720   void WxGimmickView::OnTools(wxCommandEvent& event)
721   {
722                 mViewer->StopPlayer();
723                 
724                 wxDialog* dial = new wxDialog (this,-1,_T("Tools"),wxDefaultPosition, wxSize(300,250));
725
726                 wxSizer* buttonsSizer = dial->CreateSeparatedButtonSizer(wxOK|wxCANCEL);
727                 wxNotebook* nb= new wxNotebook(dial, -1, wxDefaultPosition, wxDefaultSize, 0);
728
729                 wxBoxSizer *dialSizer = new wxBoxSizer(wxVERTICAL);     
730                 dialSizer->Add(nb,1,wxGROW,0);
731                 dialSizer->Add(buttonsSizer,0,wxGROW);
732
733 #if defined(BUILD_BRUKER)
734                         //First page: Bruker Image Reader
735                         WxGimmickTools * gimmickTools = new WxGimmickTools(nb, mCurrentDirectory);
736                         nb->AddPage( gimmickTools, _T("Bruker Image Reader") );
737 #endif
738
739                 dial->SetSizer(dialSizer, true);
740                 dial->Layout();
741                 dial->ShowModal();
742
743                 if (dial->GetReturnCode() == wxID_OK)
744                         {
745 #if defined(BUILD_BRUKER)
746                                         if (nb->GetSelection()==0)//Selection: Bruker Image Reader
747                                         {
748                                                 std::string inputDir = crea::wx2std(gimmickTools->getInputDir());
749                                                 std::string outputDir = crea::wx2std(gimmickTools->getOutputDir());
750                                                 bool addToDB = gimmickTools->getCheckBoxValue();
751
752                                                 if (inputDir.compare("")!=0 && outputDir.compare("")!=0)
753                                                 {
754                                                         if ( wxMessageBox(_T("Depending on the amount of Data the process can take between 1 and 5 minutes. Do you want to continue?"),
755                                                                         _T("Please confirm"), wxICON_QUESTION | wxYES_NO) == wxYES )
756                                                         {
757                                                                 Bruker2Dicom b2d;    
758                                                                 b2d.SetInputDirectory(inputDir);
759                                                                 b2d.SetOutputDirectory(outputDir);
760                                                                 b2d.SetConvertModeToDicom();
761                                                                 b2d.Execute();
762
763                                                                 if (addToDB)
764                                                                 {
765                                                                         mProgressDialog = new wxProgressDialog(_T("Adding directory"),_T(""),1000,this,wxPD_ELAPSED_TIME |wxPD_CAN_ABORT );
766                                                                         mCurrentDirectory = gimmickTools->getOutputDir();
767                                                                         mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),outputDir,true);
768                                                                         mProgressDialog->Pulse(_T("Updating view..."));
769                                                                         
770                                                                         UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
771                                                                         delete mProgressDialog;
772                                                                         DisplayAddSummary();
773                                                                 }       
774                                                         }
775                                                 }
776
777                                                 else
778                                                 {
779                                                         wxMessageBox(_T("One or both of the directory fields are empty"),_T("Empty Fields"),wxOK,this);
780                                                 }
781                                 }
782                                         delete gimmickTools;
783 #endif
784                         }
785
786                 mViewer->StartPlayer();
787   }
788   //=================================================
789
790   void WxGimmickView::CreateSettingsDialog(wxNotebook* nb, wxDialog* dial)
791   {
792           //First page: Customization of configurations
793           //Copy Path string
794           std::string cp;
795           mGimmick->GetSetting(SETTINGS_COPY_PATH,cp);
796           //Database Path String
797           std::string dp;
798           mGimmick->GetSetting(SETTINGS_DBPATH,dp);
799           //Syncronization Event String
800           std::string se;
801           mGimmick->GetSetting(SETTINGS_SYNC_EVENT,se);
802           //Syncronization Frequency String
803           std::string sf;
804           mGimmick->GetSetting(SETTINGS_SYNC_FREQ,sf);
805
806           WxCustomizeConfigPanel * customConfig=new WxCustomizeConfigPanel(nb,dial,this,cp,dp,se,sf);
807
808           nb->AddPage( customConfig, crea::std2wx("Customize Configuration") );
809
810           //Second page: Creation of Databases
811           wxPanel* databaseCreation=new wxPanel(nb);
812           nb->AddPage( databaseCreation, crea::std2wx("Create Database") );
813
814           //Third page: CD/DVD Watch
815           WxListenerPanel* cdWatch=new WxListenerPanel(nb,dial, this, mListener->IsPaused());
816           nb->AddPage( cdWatch, crea::std2wx("CD/DVD") );
817
818           //Fourth page: Selection of attributes to show
819           std::vector<std::string> shown;
820       std::vector<std::string> nShown;
821           GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetAttributes(shown,nShown,1);
822           int nLev=GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNumberOfLevels();
823           WxAttributeSelectionPanel* attSelection=new WxAttributeSelectionPanel(nb,dial,this,shown,nShown,nLev);
824           nb->AddPage( attSelection, crea::std2wx("Selection of Attributes") );
825   }
826
827   //===================================================================
828   void WxGimmickView::GetVisibleAttributes(std::vector<std::string>& shown, 
829           std::vector<std::string>& nShown, int level)
830   {
831           GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetAttributes(shown,nShown,level);
832   }
833
834   //===================================================================
835   void WxGimmickView::OnAttributesChanged(const std::vector<std::string>& nShown, int level)
836   {
837           GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->SetNonVisibleAttributes(nShown,level);
838           std::vector<std::string> n=nShown;
839           GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->CreateCtrl(n,level);
840   }
841   //===================================================================
842   void WxGimmickView::OnSaveSettingsCallback(const std::string& copyPath,
843           const std::string& dbPath,
844           const std::string& syncEvent,
845           const std::string& syncFreq)
846   {
847           mGimmick->UpdateSetting(SETTINGS_COPY_PATH,copyPath);
848           mGimmick->UpdateSetting(SETTINGS_DBPATH,dbPath);
849           mGimmick->UpdateSetting(SETTINGS_SYNC_EVENT,syncEvent);
850           mGimmick->UpdateSetting(SETTINGS_SYNC_FREQ,syncFreq);
851   }
852
853   //===================================================================
854   void WxGimmickView::OnListenerCallback(const std::string& drive, bool addFiles, bool removeFiles)
855   {
856          mListener->SetMonitoredDrive(drive);
857          mListener->SetAddFilesState(addFiles);
858          mListener->SetRemoveFilesState(removeFiles);
859   }
860
861   //========================================================================
862
863   void WxGimmickView::OnDriveMount(bool mount)
864   {
865           GimmickMessage(1, "Gimmick::OnDriveMount"<<std::endl);
866           std::string drive;
867           mListener->GetMonitoredDrive(drive);
868           
869           if(mount)
870           {
871                 mViewer->StopPlayer();
872                 wxBusyCursor busy;
873                 wxString title(_T("Adding drive"));
874                 mProgressDialog = 
875                 new wxProgressDialog(_T("Adding drive"),
876                                         _T(""),
877                                         1000,
878                                         this,
879                                         wxPD_ELAPSED_TIME |
880                                         //                             wxPD_ESTIMATED_TIME | 
881                                         //                             wxPD_REMAINING_TIME |
882                                         wxPD_CAN_ABORT );
883                 mCurrentDirectory = crea::std2wx(drive);
884                 mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),drive,true);
885                 mProgressDialog->Pulse(_T("Updating view..."));
886                 
887                 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
888                 delete mProgressDialog;
889                 DisplayAddSummary();
890                 mViewer->StartPlayer();
891                   
892           }
893           else
894           {  
895                   mGimmick->DeleteDrive(drive);
896                   UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
897           }
898          
899   }
900
901    //========================================================================
902
903   void WxGimmickView::StartListeningThread()
904   {
905           mListener->Resume();
906   }
907
908    //========================================================================
909
910   void WxGimmickView::StopListeningThread()
911   {
912           mListener->Pause();
913   }
914
915   //========================================================================
916   void WxGimmickView::CreateEditFieldsDialog(tree::Node* node, std::vector<std::string> names, std::vector<std::string> keys)
917   {
918           wxDialog* dial= new wxDialog (this,-1,_T("Edit Fields for node "+node->GetLabel()),wxDefaultPosition, wxSize(350,155));
919     wxBoxSizer  *siz = new wxBoxSizer(wxVERTICAL);
920     WxEditFieldsPanel* ef = new WxEditFieldsPanel(dial, dial, this, node, names, keys);
921
922     siz->Add( ef,1,wxGROW  ,0); 
923     dial->SetSizer(siz);
924     dial->ShowModal();  
925   }
926
927   //========================================================================
928   void WxGimmickView::OnFieldsEdited(tree::Node* node, const std::string& name, const std::string& key, const std::string& val)
929   {
930           mGimmick->EditField(node, crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())), name, key, val);
931           UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
932   }
933
934   //=================================================
935   /// AddProgress Gimmick callback
936   void WxGimmickView::OnAddProgress( Gimmick::AddProgress& p)
937   {
938     char mess[200];
939         sprintf(mess,"%i dirs : %i files :\n            %i handled - %i added",
940            p.GetNumberScannedDirs(),
941            p.GetNumberScannedFiles(),
942            p.GetNumberHandledFiles(),
943            p.GetNumberAddedFiles());
944     //    std::cout << "OnAddProgress "<<mess<<std::endl;
945     wxString s(wxString::From8BitData(mess));
946     //  std::cout << "Pulse"<<std::endl;
947     if (!mProgressDialog->Pulse(s)) 
948       {
949         p.SetStop();
950       }
951     //  std::cout << "OnAddProgress ok"<<std::endl;
952   }
953   //=================================================
954
955   //=================================================
956   void WxGimmickView::DisplayAddSummary()
957   {
958     const Gimmick::AddProgress& p = mGimmick->GetAddProgress();
959     std::stringstream mess;
960     mess << "Dirs \tscanned\t: " << p.GetNumberScannedDirs()  << "\n";
961     mess << "Files\tscanned\t: " << p.GetNumberScannedFiles() << "\n";
962     mess << "Files\thandled\t: " << p.GetNumberHandledFiles() << "\n\n";
963     mess << "Files\tadded  \t: " << p.GetNumberAddedFiles()   << "\n\n";
964
965     /*    char times[500];
966     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",
967             summary.parse_time,
968             (int)( summary.parse_time*100./summary.total_time),
969             summary.file_scan_time,
970             (int)(summary.file_scan_time*100./summary.total_time),
971             summary.update_structs_time,
972             (int)(summary.update_structs_time*100./summary.total_time),
973             summary.update_database_time,
974             (int)(summary.update_database_time*100./summary.total_time),
975             summary.total_time );
976     
977     mess << times;
978     */
979     wxMessageBox(std2wx(mess.str()),_T("Addition result"),wxOK,this);
980   }
981
982   //////////////////////////////////////////////////
983   // Add a DB to application                                      //
984   // @param event : WxEvent                                               //
985   // @return : -                                                                  //
986   //////////////////////////////////////////////////
987   void WxGimmickView::OnAddDB(wxCommandEvent& event)
988   {
989     
990           //Select DB
991           long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
992           std::string wc("*.sqlite3*");
993           wxFileDialog* FD = new wxFileDialog( 0, 
994                                          _T("Select file"),
995                                          _T(""),
996                                          _T(""),
997                                          crea::std2wx(wc),
998                                          style,
999                                          wxDefaultPosition);
1000     
1001     if (FD->ShowModal()==wxID_OK)
1002         {
1003                 wxBusyCursor busy;
1004                 wxArrayString files;
1005                 FD->GetPaths(files);
1006                 std::stringstream st;
1007                 for(int i = 0; i< files.size(); i++)
1008                 {
1009                         //get name of DB (file name)
1010                   size_t pos = files[i].find_last_of(_T("\\"));
1011                   std::string name = crea::wx2std(files[i].substr(pos+1));
1012                   pos = name.find_last_of(".");
1013                   name = name.substr(0,pos);
1014                   //create TreeHandler
1015                   mGimmick->addDB(name, crea::wx2std(files[i]));
1016                   //create TreeView
1017                   CreateSingleTreeView(name);
1018                 }
1019         }
1020   }
1021     ////////////////////////////////////////////////////
1022   // Create a DB from an Attributes Descriptor files  //
1023   // @param event : WxEvent                                               //
1024   // @return : -                                                                  //
1025   //////////////////////////////////////////////////
1026   void WxGimmickView::OnCreateDB(wxCommandEvent& event)
1027   {
1028     
1029           //Select DB
1030           long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
1031           std::string wc("*.txt");
1032           wxFileDialog* FD = new wxFileDialog( 0, 
1033                                          _T("Select file"),
1034                                          _T(""),
1035                                          _T(""),
1036                                          crea::std2wx(wc),
1037                                          style,
1038                                          wxDefaultPosition);
1039     
1040     if (FD->ShowModal()==wxID_OK)
1041         {
1042                 wxBusyCursor busy;
1043                 wxArrayString afile;
1044                 FD->GetPaths(afile);
1045                 //get name of DB (file name)
1046                 std::string file = crea::wx2std(afile[0]);
1047                 size_t pos = file.find_last_of("\\");
1048                 std::string name = file.substr(pos+1);
1049                 std::string directory = file.substr(0,pos);
1050                 pos = name.find_last_of(".");
1051                 name = name.substr(0,pos);
1052                 //get directory to store DB
1053                 directory +=  "\\" + name + ".sqlite3";
1054                 //create createDB
1055                 mGimmick->createDB(name, file,directory);
1056                 //create TreeHandler
1057                 mGimmick->addDB(name, directory);
1058                 //create TreeView
1059                 CreateSingleTreeView(name);
1060         }
1061   }
1062
1063    //=================================================
1064
1065    //=================================================
1066   BEGIN_EVENT_TABLE(WxGimmickView, wxPanel)
1067     EVT_TOOL(TOOL_CREATEDB_ID, WxGimmickView::OnCreateDB)
1068     EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnAddFiles)
1069     EVT_TOOL(TOOL_ADDDIR_ID, WxGimmickView::OnAddDir)
1070         EVT_TOOL(TOOL_ADDDATABASE_ID, WxGimmickView::OnAddDB)
1071         EVT_TOOL(TOOL_REMOVE_ID, WxGimmickView::OnRemove)
1072         EVT_TOOL(TOOL_SYNCHRONIZE_ID, WxGimmickView::OnSynchronize)
1073         EVT_TOOL(TOOL_SETTINGS_ID, WxGimmickView::OnSettings)
1074         EVT_TOOL(TOOL_TOOLS_ID, WxGimmickView::OnTools)
1075   END_EVENT_TABLE()
1076   //=================================================
1077
1078 } // EO namespace creaImageIO
1079
1080