1 #include <creaImageIOWxGimmickView.h>
2 #include <creaImageIOWxTreeView.h>
3 #include <creaImageIOSystem.h>
6 #include "icons/accept.xpm"
7 #include "icons/add.xpm"
8 #include "icons/folder-down.xpm"
9 #include "icons/page-down.xpm"
10 #include "icons/remove.xpm"
11 #include "icons/database-add.xpm"
12 #include "icons/help.xpm"
13 #include "icons/synchronize.xpm"
15 #include <wx/imaglist.h>
16 #include <wx/popupwin.h>
17 #include<boost/filesystem/operations.hpp>
22 //======================================================================
23 // The ids of the different tools
29 TOOL_ADDDATABASE_ID = 4,
31 TOOL_SYNCHRONIZE_ID = 6
33 //======================================================================
35 //================================================================
37 const int icon_number = 8;
51 //================================================================
53 //================================================================
55 const icon_id Icon[5] = { Icon_Database,
61 //================================================================
64 //======================================================================
66 WxGimmickView::WxGimmickView(Gimmick* gimmick,
69 const wxPoint& pos, const wxSize& size,
72 int number_of_threads)
73 : wxPanel(parent,id,pos,size),
74 GimmickView(gimmick, number_of_threads),
78 GimmickDebugMessage(1,"WxGimmickView::WxGimmickView"
80 // Sets the current directory to the home dir
81 mCurrentDirectory = std2wx(gimmick->GetHomeDirectory());
83 // Connect the AddProgress callback
84 gimmick->ConnectAddProgressObserver
85 ( boost::bind( &WxGimmickView::OnAddProgress , this, _1 ) );
87 // Create the list of icons (mIcon)
91 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
93 // Create the tool bar
95 sizer->Add( mToolBar ,0, wxGROW ,0);
97 // Split part below toolbar into notebook for views and panel
98 // for preview, messages...
99 mSplitter = new wxSplitterWindow( this , -1);
103 mNotebook = new wxNotebook(mSplitter,
104 -1,wxDefaultPosition, wxDefaultSize, 0);
110 mSelectionMaxDimension= max_dim;
111 mSelectionMinDimension= min_dim;
117 mBottomPanel = new wxPanel(mSplitter,-1);
119 wxBoxSizer *bottom_sizer = new wxBoxSizer(wxVERTICAL); //HORIZONTAL);
123 mViewer = new WxViewer(mBottomPanel, wxID_ANY, wxT("Gimmick! Viewer"),wxDefaultPosition, wxDefaultSize );
124 pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
125 mViewer->SetImageVector(pointers);
126 mViewer->StartPlayer();
129 bottom_sizer->Add(mViewer,1,wxGROW,1);
132 mText = new wxStaticText(mBottomPanel, wxID_ANY, wxT("Welcome to Gimmick!"));
133 bottom_sizer->Add(mText,0,wxGROW,0);
137 mBottomPanel->SetSizer(bottom_sizer);
140 int hsize = size.GetHeight();
142 int top_minsize = 450;
143 int bottom_minsize = 50;
145 mSplitter->SetMinimumPaneSize( bottom_minsize );
146 mSplitter->SplitHorizontally( mNotebook, mBottomPanel,
149 sizer->Add( mSplitter,1,wxGROW ,0);
158 //======================================================================
160 //======================================================================
162 WxGimmickView::~WxGimmickView()
164 // stop the viewer before application exit.
165 mViewer->StopPlayer();
166 GimmickDebugMessage(1,"WxGimmickView::~WxGimmickView"
169 //======================================================================
171 //======================================================================
172 /// Creates the tool bar
173 void WxGimmickView::CreateToolBar()
175 long style = wxTB_HORIZONTAL | wxNO_BORDER | wxTB_TEXT;
176 mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize,
179 mToolAddFile = mToolBar->AddTool( TOOL_ADDFILES_ID,
181 mIcon->GetBitmap(Icon_page_down),
182 _T("Add one or more file to database")
184 mToolAddDir = mToolBar->AddTool( TOOL_ADDDIR_ID,
186 mIcon->GetBitmap(Icon_folder_down),
187 _T("Add the content of a folder to database")
189 mToolRemove = mToolBar->AddTool( TOOL_REMOVE_ID,
191 mIcon->GetBitmap(Icon_remove),
192 _T("Remove selected items")
194 mToolAddDatabase = mToolBar->AddTool( TOOL_ADDDATABASE_ID,
196 mIcon->GetBitmap(Icon_database_add),
197 _T("Open a local or distant database")
199 mToolHelp = mToolBar->AddTool( TOOL_HELP_ID,
201 mIcon->GetBitmap(Icon_help),
202 _T("Open help window")
204 mToolSynchronize = mToolBar->AddTool( TOOL_SYNCHRONIZE_ID,
206 mIcon->GetBitmap(Icon_synchronize),
207 _T("Synchronizes the database with disk")
209 //const wxBitmap& bitmap1, const wxString& shortHelpString = "", wxItemKind kind = wxITEM_NORMAL)
213 //======================================================================
216 //======================================================================
217 /// Create the tree view for TreeHandler provided
218 void WxGimmickView::CreateTreeView( TreeHandler* h)
220 std::string name(h->GetTree().GetAttribute("Name"));
221 GimmickMessage(2,"Creating the tree view for '"<<
222 name<<"'"<<std::endl);
223 // Create the WxTreeView
224 WxTreeView* view = new WxTreeView(h,this,mNotebook,-1);
226 // TO DO : TEST THAT A VIEW WITH SAME NAME IS NOT
227 // ALREADY IN THE MAP
228 GetTreeViewMap()[name] = view;
231 mNotebook->AddPage( view, crea::std2wx(name) );
235 //======================================================================
236 void WxGimmickView::GetSelectedImages(std::vector<vtkImageData*>& s, int dim)
238 std::vector<std::string> files;
239 GetTreeViewMap()["Local database"]->GetSelectedAsString(files);
240 ReadImagesNotThreaded(s,files,dim);
242 //======================================================================
244 //======================================================================
245 void WxGimmickView::GetSelectedFiles(std::vector<std::string>& s)
247 GetTreeViewMap()["Local database"]->GetSelectedAsString(s);
249 //======================================================================
251 //======================================================================
252 void WxGimmickView::GetImages(int dim,
253 const std::vector<std::string>& files,
254 std::vector<vtkImageData*>& s)
256 ReadImagesNotThreaded(s,files,dim);
258 //======================================================================
261 //=================================================
262 void WxGimmickView::CreateIconList()
264 // Size of the icons;
268 // should correspond to Icon_xxx enum
269 icons[Icon_accept] = wxIcon(accept_xpm);
270 icons[Icon_add] = wxIcon(add_xpm);
271 icons[Icon_folder_down] = wxIcon(folder_down_xpm);
272 icons[Icon_page_down] = wxIcon(page_down_xpm);
273 icons[Icon_remove] = wxIcon(remove_xpm);
274 icons[Icon_database_add] = wxIcon(database_add_xpm);
275 icons[Icon_help] = wxIcon(help_xpm);
276 icons[Icon_synchronize] = wxIcon(synchronize_xpm);
278 // unsigned int NbIcons = 8;
279 // Make an image list containing small icons
280 mIcon = new wxImageList(size,size,true);
282 // Make all icons the same size = size of the first one
283 int sizeOrig = icons[0].GetWidth();
284 for ( size_t i = 0; i < icon_number; i++ )
286 if ( size == sizeOrig )
288 mIcon->Add(icons[i]);
292 mIcon->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
296 //=================================================
299 //=================================================
300 void WxGimmickView::OnAddFiles(wxCommandEvent& event)
302 mViewer->StopPlayer();
303 long style = wxOPEN | wxFILE_MUST_EXIST | wxFD_MULTIPLE;
304 std::string wc("*.*");
305 wxFileDialog* FD = new wxFileDialog( 0,
313 if (FD->ShowModal()==wxID_OK)
320 std::vector<std::string> filenames;
321 for (i=0;i<files.GetCount();++i)
323 filenames.push_back(wx2std(files[i]));
324 GimmickMessage(2,"Adding File "<<files[i]<<"."<<std::endl);
328 new wxProgressDialog(_T("Adding file(s)"),
333 // wxPD_ESTIMATED_TIME |
334 // wxPD_REMAINING_TIME |
337 // TO DO : select the current tree handler
338 mGimmick->AddFiles(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),filenames);
340 mProgressDialog->Pulse(_T("Updating view..."));
342 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
343 delete mProgressDialog;
347 mViewer->StartPlayer();
349 //=================================================
351 //=================================================
352 void WxGimmickView::OnAddDir(wxCommandEvent& event)
354 mViewer->StopPlayer();
355 std::string name = crea::wx2std(mNotebook->GetCurrentPage()->GetName());
356 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
359 _T("Select directory"),
363 if (FD->ShowModal()==wxID_OK)
367 std::string dirname = wx2std (FD->GetPath());
368 bool recurse = isNeedRecursive(dirname);
371 recurse = wxMessageBox(_T("Recurse into sub-directories ?"), _T("Scan directory"), wxYES_NO,this ) == wxYES ? true : false;
375 wxString title(_T("Adding directory"));
377 title = _T("Adding directory (recursive)");
379 new wxProgressDialog(_T("Adding directory"),
384 // wxPD_ESTIMATED_TIME |
385 // wxPD_REMAINING_TIME |
388 mCurrentDirectory = FD->GetPath();
389 mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),dirname,recurse);
390 mProgressDialog->Pulse(_T("Updating view..."));
392 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
393 delete mProgressDialog;
397 mViewer->StartPlayer();
400 //=================================================
401 // Test a directory to know if contains sub-directory to analyze
402 bool WxGimmickView::isNeedRecursive(std::string i_name)
404 boost::filesystem::directory_iterator iter(i_name), end_iter;
405 bool bfindir = false;
406 for(; iter != end_iter; ++iter)
408 if(boost::filesystem::is_directory(*iter))
415 //=================================================
417 //=================================================
418 void WxGimmickView::OnSelectionChange(const std::vector<tree::Node*>& sel, bool isSelection, int selection, bool needProcess)
420 GimmickDebugMessage(5,
421 "WxGimmickView::OnSelectionChange"
428 valid= ValidateSelected(NULL,
429 mSelectionMinDimension,
430 mSelectionMaxDimension );
435 std::vector<tree::Node*>::const_iterator i;
436 for(i=sel.begin();i!=sel.end()&&valid;++i)
438 valid= ValidateSelected((*i),
439 mSelectionMinDimension,
440 mSelectionMaxDimension );
445 valid= ValidateSelected(sel.front(),
446 mSelectionMinDimension,
447 mSelectionMaxDimension );
452 std::vector<tree::Node*>::const_iterator i;
453 for(i=sel.begin();i!=sel.end()&&valid;++i)
455 valid= ValidateSelected((*i),
456 mSelectionMinDimension,
457 mSelectionMaxDimension );
461 mText->SetLabel(crea::std2wx(GetMessage()));
464 ReadImageThreaded(sel);
470 ReadImageThreaded(sel);
475 //==================================================
477 //==================================================
478 ///Reads Images (Threaded)
479 void WxGimmickView::ReadImageThreaded(const std::vector<tree::Node*>& sel)
481 GimmickDebugMessage(5,
484 int maxprio = GetMaximalPriority();
485 int prio = maxprio + 2000;
489 //First load the selected images
490 mCurImageItemToShow = sel.front();
493 std::vector<tree::Node*>::const_iterator selected;
494 for(selected=sel.begin();selected!=sel.end();++selected)
496 GimmickDebugMessage(5,
497 "Requesting image from selected "
498 <<(*selected)->GetAttribute("FullFileName")
500 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
501 pointers.push_back(ph);
502 RequestReading(*selected,prio,index,ph);
503 // AddEntryToMap(*selected);
507 mViewer->SetImageVector(pointers);
510 std::vector<tree::Node*> up;
511 GetTreeViewMap()["Local database"]->GetNodes(up,true);
512 std::vector<tree::Node*>::iterator iterUp;
513 for(iterUp=up.begin();iterUp!=up.end();++iterUp)
515 GimmickDebugMessage(5,
516 "Requesting image from neighbors up "
517 <<(*iterUp)->GetAttribute("FullFileName")
519 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
520 RequestReading(*iterUp,prio,-1,ph);
521 // AddEntryToMap(*iterUp);
523 if (prio == maxprio) break;
528 std::vector<tree::Node*> down;
529 GetTreeViewMap()["Local database"]->GetNodes(down,false);
530 std::vector<tree::Node*>::iterator iterDown;
531 for(iterDown=down.begin();iterDown!=down.end();++iterDown)
533 GimmickDebugMessage(5,
534 "Requesting image from neighbors down "
535 <<(*iterDown)->GetAttribute("FullFileName")
537 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
538 RequestReading(*iterDown,prio,-1,ph);
539 // AddEntryToMap(*iterDown);
541 if (prio == maxprio) break;
547 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
548 pointers.push_back(ph);
549 mViewer->SetImageVector(pointers);
553 //==================================================
555 //==================================================
557 //==================================================
558 void WxGimmickView::OnInternalIdle()
560 if (!mConstructed) return;
561 static bool first_time = true;
566 // GimmickMessage(1,"WxGimmickView : Refresh viewer"<<std::endl);
567 // mViewer->StartPlayer();
570 mViewer->RefreshIfNecessary();
574 //==================================================
576 //==================================================
577 void WxGimmickView::ClearSelection()
580 pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
581 mViewer->SetImageVector(pointers);
582 mViewer->RefreshIfNecessary();
586 //=================================================
588 //=================================================
589 void WxGimmickView::OnRemove(wxCommandEvent& event)
591 //TODO Select current tree handler
593 GetTreeViewMap()["Local database"]->RemoveSelected();
596 //=================================================
599 //=================================================
600 void WxGimmickView::AddIgnoreFile(const tree::Node* & toRemove)
602 mGimmick->RemoveFile("Local database",toRemove);
603 GetTreeViewMap()["Local database"]->UpdateLevel(1);
606 //=================================================
607 void WxGimmickView::OnSynchronize(wxCommandEvent& event)
610 const wxString choices[] = { _T("Check database for file deletion or addition and give a report."),
611 _T("Check database for file deletion or addition and attribute change and give a report."),
612 _T("Repair database (removes deleted files and adds new files)."),
613 _T("Repair database (removes deleted files, adds new files and resets changed attributes).") } ;
615 wxSingleChoiceDialog dialog(this,
616 _T("Select one of the following synchronization actions:\n")
617 _T("Plase note that, due to the heavy amount of operations required, this action might take a while."),
618 _T("Synchronization Settings"),
619 WXSIZEOF(choices), choices);
621 //dialog.SetSelection(0);
623 if (dialog.ShowModal() == wxID_OK)
625 int sel=dialog.GetSelection();
627 bool checkAttributes=false;
628 if(sel==2 || sel==3){repair=true;}
629 if(sel==1 || sel==3){checkAttributes=true;}
630 std::string mess=mGimmick->Synchronize(repair, checkAttributes);
631 wxMessageBox(std2wx(mess),_T("Synchronization result"),wxOK,this);
632 GetTreeViewMap()["Local database"]->UpdateLevel(1);
636 //=================================================
638 //=================================================
639 /// AddProgress Gimmick callback
640 void WxGimmickView::OnAddProgress( Gimmick::AddProgress& p)
645 sprintf(mess,"%i dirs : %i files :\n %i handled - %i added",
646 p.GetNumberScannedDirs(),
647 p.GetNumberScannedFiles(),
648 p.GetNumberHandledFiles(),
649 p.GetNumberAddedFiles());
650 // std::cout << "OnAddProgress "<<mess<<std::endl;
651 wxString s(wxString::From8BitData(mess));
652 // std::cout << "Pulse"<<std::endl;
653 if (!mProgressDialog->Pulse(s))
657 // std::cout << "OnAddProgress ok"<<std::endl;
659 //=================================================
661 //=================================================
662 void WxGimmickView::DisplayAddSummary()
664 const Gimmick::AddProgress& p = mGimmick->GetAddProgress();
665 std::stringstream mess;
666 mess << "Dirs \tscanned\t: " << p.GetNumberScannedDirs() << "\n";
667 mess << "Files\tscanned\t: " << p.GetNumberScannedFiles() << "\n";
668 mess << "Files\thandled\t: " << p.GetNumberHandledFiles() << "\n\n";
669 mess << "Files\tadded \t: " << p.GetNumberAddedFiles() << "\n\n";
672 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",
674 (int)( summary.parse_time*100./summary.total_time),
675 summary.file_scan_time,
676 (int)(summary.file_scan_time*100./summary.total_time),
677 summary.update_structs_time,
678 (int)(summary.update_structs_time*100./summary.total_time),
679 summary.update_database_time,
680 (int)(summary.update_database_time*100./summary.total_time),
681 summary.total_time );
685 wxMessageBox(std2wx(mess.str()),_T("Addition result"),wxOK,this);
688 //////////////////////////////////////////////////
689 // Add a DB to application //
690 // @param event : WxEvent //
692 //////////////////////////////////////////////////
693 void WxGimmickView::OnAddDB(wxCommandEvent& event)
697 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
698 std::string wc("*.sqlite3*");
699 wxFileDialog* FD = new wxFileDialog( 0,
707 if (FD->ShowModal()==wxID_OK)
712 std::stringstream st;
713 for(int i = 0; i< files.size(); i++)
715 st<< "remote_" << i+1;
717 mGimmick->addDB(st.str(), crea::wx2std(files[i]));
719 CreateSingleTreeView(st.str());
726 //=================================================
728 //=================================================
729 BEGIN_EVENT_TABLE(WxGimmickView, wxPanel)
730 EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnAddFiles)
731 EVT_TOOL(TOOL_ADDDIR_ID, WxGimmickView::OnAddDir)
732 EVT_TOOL(TOOL_REMOVE_ID, WxGimmickView::OnRemove)
733 EVT_TOOL(TOOL_SYNCHRONIZE_ID, WxGimmickView::OnSynchronize)
734 EVT_TOOL(TOOL_ADDDATABASE_ID, WxGimmickView::OnAddDB)
736 //=================================================
738 } // EO namespace creaImageIO