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