]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOWxGimmickView.cpp
*** empty log message ***
[creaImageIO.git] / src2 / creaImageIOWxGimmickView.cpp
index 7e79bc320c94b25983120a56ab82a33f1c660b6c..0c7229c08930a013711dd166c4d87de455be6e70 100644 (file)
@@ -19,7 +19,7 @@ namespace creaImageIO
   // The ids of the different tools
   enum
     {
-      TOOL_ADDFILE_ID = 1,
+      TOOL_ADDFILES_ID = 1,
       TOOL_ADDDIR_ID = 2,
       TOOL_REMOVE_ID = 3,
       TOOL_ADDDATABASE_ID = 4,
@@ -61,13 +61,23 @@ namespace creaImageIO
                               wxWindow *parent, 
                               const wxWindowID id,
                               const wxPoint& pos, const wxSize& size,
-                              int image_type,
+                              int min_dim,
+                                  int max_dim,
                               int number_of_threads)
     : wxPanel(parent,id,pos,size),
-      GimmickView(gimmick)
+      GimmickView(gimmick, number_of_threads),
+      mProgressDialog(0),
+      mConstructed(false)
   {
     GimmickDebugMessage(1,"WxGimmickView::WxGimmickView"
                        <<std::endl);
+    // Sets the current directory to the home dir
+    mCurrentDirectory =  std2wx(gimmick->GetHomeDirectory());
+
+     // Connect the AddProgress callback
+    gimmick->ConnectAddProgressObserver
+      ( boost::bind( &WxGimmickView::OnAddProgress , this, _1 ) );
+
     // Create the list of icons (mIcon)
     CreateIconList();
 
@@ -87,18 +97,35 @@ namespace creaImageIO
     mNotebook = new wxNotebook(mSplitter,
                               -1,wxDefaultPosition, wxDefaultSize, 0);
 
-       //Gimmick
-       mGimmick=gimmick;
+    //Gimmick
+    mGimmick=gimmick;
 
+      
+    mSelectionMaxDimension= max_dim;
+    mSelectionMinDimension= min_dim;
+    
     // Create the views
     CreateTreeViews();
 
     // Bottom panel 
     mBottomPanel = new wxPanel(mSplitter,-1);
+    
+    wxBoxSizer *bottom_sizer = new wxBoxSizer(wxHORIZONTAL);
+    
+    mText = new wxStaticText(mBottomPanel, wxID_ANY, wxT("Status: Welcome to Gimmick!"));
+    bottom_sizer->Add(mText,1,wxGROW,0);
+    
+    // Previewer
+    mViewer = new WxViewer(mBottomPanel, wxID_ANY, wxT("Gimmick! Viewer"),wxDefaultPosition, wxDefaultSize );
+
+    bottom_sizer->Add(mViewer,1,wxGROW,0);
+    //    mViewer->Show();
+
+    mBottomPanel->SetSizer(bottom_sizer);
 
     // Splitting
     int hsize = size.GetHeight();
-    int bottom_minsize = 200;
+    int bottom_minsize = 40;
 
     mSplitter->SetMinimumPaneSize( bottom_minsize );
     mSplitter->SplitHorizontally( mNotebook, mBottomPanel, 
@@ -110,7 +137,8 @@ namespace creaImageIO
     SetSizer( sizer );     
     SetAutoLayout(true);
     Layout();
-
+    
+    mConstructed = true;
   }
   //======================================================================
 
@@ -131,10 +159,10 @@ namespace creaImageIO
     mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize,
                             style);
 
-    mToolAddFile = mToolBar->AddTool( TOOL_ADDFILE_ID, 
+    mToolAddFile = mToolBar->AddTool( TOOL_ADDFILES_ID, 
                                      _T("Add file(s)"),
                                      mIcon->GetBitmap(Icon_page_down),
-                                     _T("Add some file(s) to database")
+                                     _T("Add one or more file to database")
                                      );
     mToolAddDir = mToolBar->AddTool( TOOL_ADDDIR_ID, 
                                      _T("Add folder"),
@@ -171,7 +199,7 @@ namespace creaImageIO
     GimmickMessage(2,"Creating the tree view for '"<<
                   name<<"'"<<std::endl);
     // Create the WxTreeView
-    WxTreeView* view = new WxTreeView(h,mNotebook,-1);
+    WxTreeView* view = new WxTreeView(h,this,mNotebook,-1);
 
     // TO DO : TEST THAT A VIEW WITH SAME NAME IS NOT
     // ALREADY IN THE MAP
@@ -182,9 +210,27 @@ namespace creaImageIO
 
   }
   //======================================================================
+  /// Returns the selected Image so that it complies with the given parameter(<4D)
+  vtkImageData* WxGimmickView::GetSelectedImage(int dim)
+  {
+         return NULL;
+                 //GetTreeViewMap()["Local database"]->GetSelectedImage(dim);
+  }
 
-
-
+  //======================================================================
+  /// Returns the selected Images so that they comply with the given parameter(4D)
+  void WxGimmickView::GetSelectedImages(std::vector<vtkImageData*>& s, int dim)
+  {
+       int level=GetTreeViewMap()["Local database"]->GetNumberOfLevels();
+       std::vector<tree::Node*> im=GetTreeViewMap()["Local database"]->GetSelected(level+1);
+       ReadImagesNotThreaded(s,im,dim);
+  }
+  //======================================================================
+  /// Returns the selected Images so that they comply with the given parameter(4D)
+  void WxGimmickView::GetSelectedFiles(std::vector<std::string>& s)
+  {
+       GetTreeViewMap()["Local database"]->GetSelectedAsString(s);
+  }
   //=================================================
   void WxGimmickView::CreateIconList()
   {
@@ -219,23 +265,25 @@ namespace creaImageIO
          }
       }
   }
+  //=================================================
 
 
   //=================================================
-  void WxGimmickView::OnAddFile(wxCommandEvent& event)
+  void WxGimmickView::OnAddFiles(wxCommandEvent& event)
   {
    long style = wxOPEN | wxFILE_MUST_EXIST | wxFD_MULTIPLE;
     std::string wc("*.*");
     wxFileDialog* FD = new wxFileDialog( 0, 
                                         _T("Select file"),
-                                        "",
                                         _T(""),
-                                        std2wx(wc),
+                                        _T(""),
+                                        crea::std2wx(wc),
                                         style,
                                         wxDefaultPosition);
     
     if (FD->ShowModal()==wxID_OK)
       {
+       wxBusyCursor busy;
 
        wxArrayString files;
        FD->GetPaths(files);
@@ -247,38 +295,326 @@ namespace creaImageIO
          GimmickMessage(2,"Adding File "<<files[i]<<"."<<std::endl);
        }
 
-       
-    /*   
-       TreeItemData *data = 
-         (TreeItemData *)
-         mTreeListCtrl->GetItemData(mItemOfMenu);
-       DicomDatabase* db = data->GetDicomNode()->GetDicomDatabase();
-       DicomDatabase::UpdateSummary summary;
-       wxProgressDialog* progress = 
+       mProgressDialog = 
          new wxProgressDialog(_T("Adding file(s)"),
                               _T(""),
                               1000,
                               this,
                               wxPD_ELAPSED_TIME |
-                              wxPD_ESTIMATED_TIME | 
-                              wxPD_REMAINING_TIME |
+                              //                              wxPD_ESTIMATED_TIME | 
+                              //                              wxPD_REMAINING_TIME |
                               wxPD_CAN_ABORT );
+
+       // TO DO : select the current tree handler
+       mGimmick->AddFiles("Local database",filenames);
+
+       mProgressDialog->Pulse(_T("Updating view..."));
+
+       UpdateTreeViewLevel("Local database",1);
+       delete mProgressDialog;
+       DisplayAddSummary();    
+      }
+       
+  }
+  //=================================================
+
+  //=================================================
+  void WxGimmickView::OnAddDir(wxCommandEvent& event)
+  {
+    long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
+    wxDirDialog* FD = 
+      new wxDirDialog( 0, 
+                      _T("Select directory"),
+                      mCurrentDirectory,
+                      style);
+    
+    if (FD->ShowModal()==wxID_OK)
+      {
+       
+       bool recurse = false;
+       if (wxMessageBox(_T("Recurse into sub-directories ?"),
+                        _T("Scan directory"),
+                        wxYES_NO,this ) == wxYES)
+         {
+           recurse = true;
+         }
        
-       db->AddFiles(filenames,progress,summary);
+       wxBusyCursor busy;
+       wxString title(_T("Adding directory"));
+       if (recurse) 
+         title = _T("Adding directory (recursive)");
+       mProgressDialog = 
+         new wxProgressDialog(_T("Adding directory"),
+                              _T(""),
+                              1000,
+                              this,
+                              wxPD_ELAPSED_TIME |
+                              //                              wxPD_ESTIMATED_TIME | 
+                              //                              wxPD_REMAINING_TIME |
+                              wxPD_CAN_ABORT );
+       std::string dirname = wx2std (FD->GetPath()) ;
+       mCurrentDirectory = FD->GetPath();  
+       
+       // TO DO : select the current tree handler
+       mGimmick->AddDir("Local database",dirname,recurse);
        
-       progress->Pulse(_T("Updating view..."));
-       UpdateDicomDatabaseView(db);
-       delete progress;
-       DisplayUpdateSummary(summary,this);
-       */
+       mProgressDialog->Pulse(_T("Updating view..."));
+       
+       UpdateTreeViewLevel("Local database",1);
+       delete mProgressDialog;
+       DisplayAddSummary();
       }
+  }
+  //=================================================
+
+  //=================================================
+  void WxGimmickView::OnSelectionChange(std::vector<tree::Node*>& sel)
+  {      
+    GimmickDebugMessage(5,
+                       "WxGimmickView::OnSelectionChange"
+                       <<std::endl);
+    wxBusyCursor busy;
+    bool valid = ValidateSelected(sel,
+                                 mSelectionMinDimension,
+                                 mSelectionMaxDimension );
+    mText->SetLabel(_T("Status: ")+crea::std2wx(GetMessage()));
+    if(valid)
+      {
+       ReadImageThreaded(sel);
+      }
+    else
+      {
+       mViewer->SetMovieSize(1);
+       mViewer->SetImage(0,GetDefaultImage());
+       //      mViewer->Hide();
+      }
+
+    
+   }
+
+  //==================================================
+
+  //==================================================
+  ///Reads Images (Threaded)
+  void WxGimmickView::ReadImageThreaded(std::vector<tree::Node*> sel)
+  {    
+   GimmickDebugMessage(5,
+                      "ReadImageThreaded"
+                      <<std::endl);
+   int maxprio = GetMaximalPriority();
+   int prio = maxprio + 2000;
+
+   mViewer->SetMovieSize(sel.size());//ClearImages();
+   
+   //First load the selected images
+   mCurImageItemToShow = sel.front();
+   int index = 0;
+   std::vector<tree::Node*>::iterator selected;
+   for(selected=sel.begin();selected!=sel.end();++selected)
+     {
+       GimmickDebugMessage(5,
+                          "Requesting image from selected "
+                          <<(*selected)->GetAttribute("FullFileName")
+                          <<std::endl);
+       RequestReading(*selected,prio,index);
+       //       AddEntryToMap(*selected);
+       prio--;
+       index++;
+     }
        
+       //Going up
+       prio = maxprio + 20;
+       std::vector<tree::Node*> up;
+       GetTreeViewMap()["Local database"]->GetNodes(up,true);
+       std::vector<tree::Node*>::iterator iterUp;
+       for(iterUp=up.begin();iterUp!=up.end();++iterUp)
+       {
+               GimmickDebugMessage(5,
+                               "Requesting image from neighbors up "
+                               <<(*iterUp)->GetAttribute("FullFileName")
+                               <<std::endl);
+               RequestReading(*iterUp,prio,-1);
+               //              AddEntryToMap(*iterUp);
+               prio--;
+               if (prio == maxprio) break;
+       }
+
+       //Going down
+       prio = maxprio + 19;
+       std::vector<tree::Node*> down;
+       GetTreeViewMap()["Local database"]->GetNodes(down,false);
+       std::vector<tree::Node*>::iterator iterDown;
+       for(iterDown=down.begin();iterDown!=down.end();++iterDown)
+       {
+               GimmickDebugMessage(5,
+                               "Requesting image from neighbors down "
+                               <<(*iterDown)->GetAttribute("FullFileName")
+                               <<std::endl);
+               RequestReading(*iterDown,prio,-1);
+               //              AddEntryToMap(*iterDown);
+               prio--;
+               if (prio == maxprio) break;
+       }       
   }
+
+  //==================================================
+
+  //==================================================
+  /// Processes the queue of image events 
+  void WxGimmickView::ProcessImageEvents()
+  {
+    if (!mConstructed) return;
+
+    //    int level=GetTreeViewMap()["Local database"]->GetNumberOfLevels();
+    //    std::vector<tree::Node*> sel=GetTreeViewMap()["Local database"]->GetSelected(level+1);
+    /*
+    GimmickDebugMessage(5,
+                       "Processing Images. Lock Started"
+                       <<std::endl);
+    */
+    MultiThreadImageReaderEventLock();
+    
+
+    while (!IsQueueEmpty())
+      {
+       GimmickDebugMessage(5,
+                           "Queue not empty"
+                           <<std::endl);
+       
+       vtkImageData* image = GetNextImageQueued();
+       if( image!=0 ) 
+         {
+           int index =  GetNextSelectionIndexQueued();
+           if (index>=0) 
+             {
+               mViewer->SetImage(index,image);
+             }
+           /*
+           tree::Node* node=GetNextNodeQueued();
+           
+
+           bool found=false;
+           std::vector<tree::Node*>::iterator i;
+           for(i=sel.begin();i!=sel.end()&&!found;++i)
+             {
+               if((*i)==node)
+                 {
+                   mViewer->AddImage(image);
+                   found=true;
+                 }
+             }
+           */
+           
+         }
+       UnqueueNext();
+      }
+    /*
+    if(!(mViewer->ImagesEmpty()))
+      {
+       GimmickDebugMessage(5,
+                           "Showing images"
+                           <<std::endl);
+       //      mViewer->ShowImages();
+       //      mViewer->Show();
+      }
+    */
+    ClearQueue();
+       
+
+    MultiThreadImageReaderEventUnlock();
+    /*
+       GimmickDebugMessage(5,
+                               "Processing Images. Lock Ended"
+                               <<std::endl);
+    */
+  }
+  //==================================================
+
+  //==================================================
+   void  WxGimmickView::OnInternalIdle()
+  {
+   if (!mConstructed) return;
+   ProcessImageEvents();
+   static bool first_time = true;
+   if (false)
+     {
+       mViewer->SetMovieSize(1);
+       mViewer->SetImage(0,GetDefaultImage());
+       first_time = false;
+     }
+   //  mViewer->Refresh();
+  }
+  
+  //=================================================
+  //=================================================
+  void WxGimmickView::OnRemove(wxCommandEvent& event)
+  {
+       //TODO Select current tree handler       
+    wxBusyCursor busy;
+    GetTreeViewMap()["Local database"]->RemoveSelected(1);
+  }
+  //=================================================
+
   //=================================================
+  /// AddProgress Gimmick callback
+  void WxGimmickView::OnAddProgress( Gimmick::AddProgress& p)
+  {
 
+    char mess[200];
+    sprintf(mess,"%i dirs - %i files - %i handled - %i added",
+          p.GetNumberScannedDirs(),
+          p.GetNumberScannedFiles(),
+          p.GetNumberHandledFiles(),
+          p.GetNumberAddedFiles());
+    //    std::cout << "OnAddProgress "<<mess<<std::endl;
+    wxString s(wxString::From8BitData(mess));
+    //  std::cout << "Pulse"<<std::endl;
+    if (!mProgressDialog->Pulse(s)) 
+      {
+       p.SetStop();
+      }
+    //  std::cout << "OnAddProgress ok"<<std::endl;
+  }
+  //=================================================
+
+  //=================================================
+  void WxGimmickView::DisplayAddSummary()
+  {
+    const Gimmick::AddProgress& p = mGimmick->GetAddProgress();
+    std::stringstream mess;
+    mess << "Dirs \tscanned\t: " << p.GetNumberScannedDirs()  << "\n";
+    mess << "Files\tscanned\t: " << p.GetNumberScannedFiles() << "\n";
+    mess << "Files\thandled\t: " << p.GetNumberHandledFiles() << "\n\n";
+    mess << "Files\tadded  \t: " << p.GetNumberAddedFiles()   << "\n\n";
+
+    /*    char times[500];
+    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",
+           summary.parse_time,
+           (int)( summary.parse_time*100./summary.total_time),
+           summary.file_scan_time,
+           (int)(summary.file_scan_time*100./summary.total_time),
+           summary.update_structs_time,
+           (int)(summary.update_structs_time*100./summary.total_time),
+           summary.update_database_time,
+           (int)(summary.update_database_time*100./summary.total_time),
+           summary.total_time );
+    
+    mess << times;
+    */
+    wxMessageBox(std2wx(mess.str()),_T("Addition result"),wxOK,this);
+  }
+
+   //=================================================
+
+   //=================================================
   BEGIN_EVENT_TABLE(WxGimmickView, wxPanel)
-  EVT_TOOL(TOOL_ADDFILE_ID, WxGimmickView::OnAddFile)
+    EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnAddFiles)
+    EVT_TOOL(TOOL_ADDDIR_ID, WxGimmickView::OnAddDir)
+       EVT_TOOL(TOOL_REMOVE_ID, WxGimmickView::OnRemove)
   END_EVENT_TABLE()
+  //=================================================
 
 } // EO namespace creaImageIO