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/create-database.xpm"
13 #include "icons/help.xpm"
14 #include "icons/synchronize.xpm"
16 #include <wx/imaglist.h>
17 #include <wx/popupwin.h>
18 #include<boost/filesystem/operations.hpp>
23 //======================================================================
24 // The ids of the different tools
30 TOOL_ADDDATABASE_ID = 4,
32 TOOL_SYNCHRONIZE_ID = 6,
35 //======================================================================
37 //================================================================
39 const int icon_number = 9;
54 //================================================================
56 //================================================================
58 const icon_id Icon[5] = { Icon_Database,
64 //================================================================
67 //======================================================================
69 WxGimmickView::WxGimmickView(Gimmick* gimmick,
72 const wxPoint& pos, const wxSize& size,
75 int number_of_threads)
76 : wxPanel(parent,id,pos,size),
77 GimmickView(gimmick, number_of_threads),
81 GimmickDebugMessage(1,"WxGimmickView::WxGimmickView"
83 // Sets the current directory to the home dir
84 mCurrentDirectory = std2wx(gimmick->GetHomeDirectory());
86 // Connect the AddProgress callback
87 gimmick->ConnectAddProgressObserver
88 ( boost::bind( &WxGimmickView::OnAddProgress , this, _1 ) );
90 // Create the list of icons (mIcon)
94 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
96 // Create the tool bar
98 sizer->Add( mToolBar ,0, wxGROW ,0);
100 // Split part below toolbar into notebook for views and panel
101 // for preview, messages...
102 mSplitter = new wxSplitterWindow( this , -1);
106 mNotebook = new wxNotebook(mSplitter,
107 -1,wxDefaultPosition, wxDefaultSize, 0);
113 mSelectionMaxDimension= max_dim;
114 mSelectionMinDimension= min_dim;
120 mBottomPanel = new wxPanel(mSplitter,-1);
122 wxBoxSizer *bottom_sizer = new wxBoxSizer(wxVERTICAL); //HORIZONTAL);
126 mViewer = new WxViewer(mBottomPanel, wxID_ANY, wxT("Gimmick! Viewer"),wxDefaultPosition, wxDefaultSize );
127 pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
128 mViewer->SetImageVector(pointers);
129 mViewer->StartPlayer();
132 bottom_sizer->Add(mViewer,1,wxGROW,1);
135 mText = new wxStaticText(mBottomPanel, wxID_ANY, wxT("Welcome to Gimmick!"));
136 bottom_sizer->Add(mText,0,wxGROW,0);
140 mBottomPanel->SetSizer(bottom_sizer);
143 int hsize = size.GetHeight();
145 int top_minsize = 450;
146 int bottom_minsize = 50;
148 mSplitter->SetMinimumPaneSize( bottom_minsize );
149 mSplitter->SplitHorizontally( mNotebook, mBottomPanel,
152 sizer->Add( mSplitter,1,wxGROW ,0);
161 //======================================================================
163 //======================================================================
165 WxGimmickView::~WxGimmickView()
167 // stop the viewer before application exit.
168 mViewer->StopPlayer();
169 GimmickDebugMessage(1,"WxGimmickView::~WxGimmickView"
172 //======================================================================
174 //======================================================================
175 /// Creates the tool bar
176 void WxGimmickView::CreateToolBar()
178 long style = wxTB_HORIZONTAL | wxNO_BORDER | wxTB_TEXT;
179 mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize,
182 mToolAddFile = mToolBar->AddTool( TOOL_CREATEDB_ID,
183 _T("Create database"),
184 mIcon->GetBitmap(Icon_create_database),
185 _T("Create DB from an Attributes Descriptor file")
187 mToolAddFile = mToolBar->AddTool( TOOL_ADDFILES_ID,
189 mIcon->GetBitmap(Icon_page_down),
190 _T("Add one or more file to database")
192 mToolAddDir = mToolBar->AddTool( TOOL_ADDDIR_ID,
194 mIcon->GetBitmap(Icon_folder_down),
195 _T("Add the content of a folder to database")
197 mToolAddDatabase = mToolBar->AddTool( TOOL_ADDDATABASE_ID,
199 mIcon->GetBitmap(Icon_database_add),
200 _T("Open a local or distant database")
202 mToolRemove = mToolBar->AddTool( TOOL_REMOVE_ID,
204 mIcon->GetBitmap(Icon_remove),
205 _T("Remove selected items")
207 mToolSynchronize = mToolBar->AddTool( TOOL_SYNCHRONIZE_ID,
209 mIcon->GetBitmap(Icon_synchronize),
210 _T("Synchronizes the database with disk")
212 mToolHelp = mToolBar->AddTool( TOOL_HELP_ID,
214 mIcon->GetBitmap(Icon_help),
215 _T("Open help window")
217 //const wxBitmap& bitmap1, const wxString& shortHelpString = "", wxItemKind kind = wxITEM_NORMAL)
221 //======================================================================
224 //======================================================================
225 /// Create the tree view for TreeHandler provided
226 void WxGimmickView::CreateTreeView( TreeHandler* h)
228 std::string name(h->GetTree().GetAttribute("Name"));
229 GimmickMessage(2,"Creating the tree view for '"<<
230 name<<"'"<<std::endl);
231 // Create the WxTreeView
232 WxTreeView* view = new WxTreeView(h,this,mNotebook,-1);
234 // TO DO : TEST THAT A VIEW WITH SAME NAME IS NOT
235 // ALREADY IN THE MAP
236 GetTreeViewMap()[name] = view;
239 mNotebook->AddPage( view, crea::std2wx(name) );
243 //======================================================================
244 void WxGimmickView::GetSelectedImages(std::vector<vtkImageData*>& s, int dim)
246 std::vector<std::string> files;
247 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(files);
248 ReadImagesNotThreaded(s,files,dim);
250 //======================================================================
252 //======================================================================
253 void WxGimmickView::GetSelectedFiles(std::vector<std::string>& s)
255 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(s);
257 //======================================================================
259 //======================================================================
260 void WxGimmickView::GetImages(int dim,
261 const std::vector<std::string>& files,
262 std::vector<vtkImageData*>& s)
264 ReadImagesNotThreaded(s,files,dim);
266 //======================================================================
269 //=================================================
270 void WxGimmickView::CreateIconList()
272 // Size of the icons;
276 // should correspond to Icon_xxx enum
277 icons[Icon_accept] = wxIcon(accept_xpm);
278 icons[Icon_add] = wxIcon(add_xpm);
279 icons[Icon_folder_down] = wxIcon(folder_down_xpm);
280 icons[Icon_page_down] = wxIcon(page_down_xpm);
281 icons[Icon_remove] = wxIcon(remove_xpm);
282 icons[Icon_database_add] = wxIcon(database_add_xpm);
283 icons[Icon_help] = wxIcon(help_xpm);
284 icons[Icon_synchronize] = wxIcon(synchronize_xpm);
285 icons[Icon_create_database] = wxIcon(create_database_xpm);
287 // unsigned int NbIcons = 8;
288 // Make an image list containing small icons
289 mIcon = new wxImageList(size,size,true);
291 // Make all icons the same size = size of the first one
292 int sizeOrig = icons[0].GetWidth();
293 for ( size_t i = 0; i < icon_number; i++ )
295 if ( size == sizeOrig )
297 mIcon->Add(icons[i]);
301 mIcon->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
305 //=================================================
308 //=================================================
309 void WxGimmickView::OnAddFiles(wxCommandEvent& event)
311 mViewer->StopPlayer();
312 long style = wxOPEN | wxFILE_MUST_EXIST | wxFD_MULTIPLE;
313 std::string wc("*.*");
314 wxFileDialog* FD = new wxFileDialog( 0,
322 if (FD->ShowModal()==wxID_OK)
329 std::vector<std::string> filenames;
330 for (i=0;i<files.GetCount();++i)
332 filenames.push_back(wx2std(files[i]));
333 GimmickMessage(2,"Adding File "<<files[i]<<"."<<std::endl);
337 new wxProgressDialog(_T("Adding file(s)"),
342 // wxPD_ESTIMATED_TIME |
343 // wxPD_REMAINING_TIME |
346 // TO DO : select the current tree handler
347 mGimmick->AddFiles(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),filenames);
349 mProgressDialog->Pulse(_T("Updating view..."));
351 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
352 delete mProgressDialog;
356 mViewer->StartPlayer();
358 //=================================================
360 //=================================================
361 void WxGimmickView::OnAddDir(wxCommandEvent& event)
363 mViewer->StopPlayer();
364 std::string name = crea::wx2std(mNotebook->GetCurrentPage()->GetName());
365 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
368 _T("Select directory"),
372 if (FD->ShowModal()==wxID_OK)
376 std::string dirname = wx2std (FD->GetPath());
377 bool recurse = isNeedRecursive(dirname);
380 recurse = wxMessageBox(_T("Recurse into sub-directories ?"), _T("Scan directory"), wxYES_NO,this ) == wxYES ? true : false;
384 wxString title(_T("Adding directory"));
386 title = _T("Adding directory (recursive)");
388 new wxProgressDialog(_T("Adding directory"),
393 // wxPD_ESTIMATED_TIME |
394 // wxPD_REMAINING_TIME |
397 mCurrentDirectory = FD->GetPath();
398 mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),dirname,recurse);
399 mProgressDialog->Pulse(_T("Updating view..."));
401 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
402 delete mProgressDialog;
406 mViewer->StartPlayer();
409 //=================================================
410 // Test a directory to know if contains sub-directory to analyze
411 bool WxGimmickView::isNeedRecursive(std::string i_name)
413 boost::filesystem::directory_iterator iter(i_name), end_iter;
414 bool bfindir = false;
415 for(; iter != end_iter; ++iter)
417 if(boost::filesystem::is_directory(*iter))
424 //=================================================
426 //=================================================
427 void WxGimmickView::OnSelectionChange(const std::vector<tree::Node*>& sel, bool isSelection, int selection, bool needProcess)
429 GimmickDebugMessage(5,
430 "WxGimmickView::OnSelectionChange"
437 valid= ValidateSelected(NULL,
438 mSelectionMinDimension,
439 mSelectionMaxDimension );
444 std::vector<tree::Node*>::const_iterator i;
445 for(i=sel.begin();i!=sel.end()&&valid;++i)
447 valid= ValidateSelected((*i),
448 mSelectionMinDimension,
449 mSelectionMaxDimension );
454 valid= ValidateSelected(sel.front(),
455 mSelectionMinDimension,
456 mSelectionMaxDimension );
461 std::vector<tree::Node*>::const_iterator i;
462 for(i=sel.begin();i!=sel.end()&&valid;++i)
464 valid= ValidateSelected((*i),
465 mSelectionMinDimension,
466 mSelectionMaxDimension );
469 mText->SetLabel(crea::std2wx(GetMessage()));
472 ReadImageThreaded(sel);
478 ReadImageThreaded(sel);
483 //==================================================
485 //==================================================
486 ///Reads Images (Threaded)
487 void WxGimmickView::ReadImageThreaded(const std::vector<tree::Node*>& sel)
489 GimmickDebugMessage(5,
492 int maxprio = GetMaximalPriority();
493 int prio = maxprio + 2000;
497 //First load the selected images
498 mCurImageItemToShow = sel.front();
501 std::vector<tree::Node*>::const_iterator selected;
502 for(selected=sel.begin();selected!=sel.end();++selected)
504 GimmickDebugMessage(5,
505 "Requesting image from selected "
506 <<(*selected)->GetAttribute("FullFileName")
508 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
509 pointers.push_back(ph);
510 RequestReading(*selected,prio,index,ph);
511 // AddEntryToMap(*selected);
515 mViewer->SetImageVector(pointers);
518 std::vector<tree::Node*> up;
519 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(up,true);
520 std::vector<tree::Node*>::iterator iterUp;
521 for(iterUp=up.begin();iterUp!=up.end();++iterUp)
523 GimmickDebugMessage(5,
524 "Requesting image from neighbors up "
525 <<(*iterUp)->GetAttribute("FullFileName")
527 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
528 RequestReading(*iterUp,prio,-1,ph);
529 // AddEntryToMap(*iterUp);
531 if (prio == maxprio) break;
536 std::vector<tree::Node*> down;
537 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(down,false);
538 std::vector<tree::Node*>::iterator iterDown;
539 for(iterDown=down.begin();iterDown!=down.end();++iterDown)
541 GimmickDebugMessage(5,
542 "Requesting image from neighbors down "
543 <<(*iterDown)->GetAttribute("FullFileName")
545 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
546 RequestReading(*iterDown,prio,-1,ph);
547 // AddEntryToMap(*iterDown);
549 if (prio == maxprio) break;
555 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
556 pointers.push_back(ph);
557 mViewer->SetImageVector(pointers);
561 //==================================================
563 //==================================================
565 //==================================================
566 void WxGimmickView::OnInternalIdle()
568 if (!mConstructed) return;
569 static bool first_time = true;
574 // GimmickMessage(1,"WxGimmickView : Refresh viewer"<<std::endl);
575 // mViewer->StartPlayer();
578 mViewer->RefreshIfNecessary();
582 //==================================================
584 //==================================================
585 void WxGimmickView::ClearSelection()
588 pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
589 mViewer->SetImageVector(pointers);
590 mViewer->RefreshIfNecessary();
594 //=================================================
596 //=================================================
597 void WxGimmickView::OnRemove(wxCommandEvent& event)
599 //TODO Select current tree handler
601 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->RemoveSelected();
604 //=================================================
607 //=================================================
608 void WxGimmickView::AddIgnoreFile(tree::Node* toRemove)
610 mGimmick->RemoveFile(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),toRemove);
611 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->UpdateLevel(1);
614 //=================================================
615 void WxGimmickView::CopyFiles(const std::vector<std::string>& filenames)
617 mGimmick->CopyFiles(filenames, crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())));
618 wxMessageBox(std2wx("The selected files have been copied"),_T("Copy files"),wxOK,this);
621 //=================================================
622 void WxGimmickView::OnSynchronize(wxCommandEvent& event)
625 const wxString choices[] = { _T("Check database for files deletion and addition and give a report."),
626 _T("Check database for files deletion, addition and attributes change. Then give a report."),
627 _T("Repair database (remove deleted files and add new files)."),
628 _T("Repair database (remove deleted files, add new files and reset changed attributes).") } ;
630 wxSingleChoiceDialog dialog(this,
631 _T("Select one of the following synchronization actions:\n")
632 _T("Please note that, due to the heavy amount of operations required, this action might take a while."),
633 _T("Synchronization Settings"),
634 WXSIZEOF(choices), choices);
636 //dialog.SetSelection(0);
638 if (dialog.ShowModal() == wxID_OK)
640 int sel=dialog.GetSelection();
642 bool checkAttributes=false;
643 if(sel==2 || sel==3){repair=true;}
644 if(sel==1 || sel==3){checkAttributes=true;}
645 std::string mess=mGimmick->Synchronize(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),repair, checkAttributes);
646 wxMessageBox(std2wx(mess),_T("Synchronization result"),wxOK,this);
647 if(sel==2 || sel==3){
648 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->UpdateLevel(1);
653 //=================================================
655 //=================================================
656 /// AddProgress Gimmick callback
657 void WxGimmickView::OnAddProgress( Gimmick::AddProgress& p)
662 sprintf(mess,"%i dirs : %i files :\n %i handled - %i added",
663 p.GetNumberScannedDirs(),
664 p.GetNumberScannedFiles(),
665 p.GetNumberHandledFiles(),
666 p.GetNumberAddedFiles());
667 // std::cout << "OnAddProgress "<<mess<<std::endl;
668 wxString s(wxString::From8BitData(mess));
669 // std::cout << "Pulse"<<std::endl;
670 if (!mProgressDialog->Pulse(s))
674 // std::cout << "OnAddProgress ok"<<std::endl;
676 //=================================================
678 //=================================================
679 void WxGimmickView::DisplayAddSummary()
681 const Gimmick::AddProgress& p = mGimmick->GetAddProgress();
682 std::stringstream mess;
683 mess << "Dirs \tscanned\t: " << p.GetNumberScannedDirs() << "\n";
684 mess << "Files\tscanned\t: " << p.GetNumberScannedFiles() << "\n";
685 mess << "Files\thandled\t: " << p.GetNumberHandledFiles() << "\n\n";
686 mess << "Files\tadded \t: " << p.GetNumberAddedFiles() << "\n\n";
689 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",
691 (int)( summary.parse_time*100./summary.total_time),
692 summary.file_scan_time,
693 (int)(summary.file_scan_time*100./summary.total_time),
694 summary.update_structs_time,
695 (int)(summary.update_structs_time*100./summary.total_time),
696 summary.update_database_time,
697 (int)(summary.update_database_time*100./summary.total_time),
698 summary.total_time );
702 wxMessageBox(std2wx(mess.str()),_T("Addition result"),wxOK,this);
705 //////////////////////////////////////////////////
706 // Add a DB to application //
707 // @param event : WxEvent //
709 //////////////////////////////////////////////////
710 void WxGimmickView::OnAddDB(wxCommandEvent& event)
714 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
715 std::string wc("*.sqlite3*");
716 wxFileDialog* FD = new wxFileDialog( 0,
724 if (FD->ShowModal()==wxID_OK)
729 std::stringstream st;
730 for(int i = 0; i< files.size(); i++)
732 //get name of DB (file name)
733 size_t pos = files[i].find_last_of(_T("\\"));
734 std::string name = crea::wx2std(files[i].substr(pos+1));
735 pos = name.find_last_of(".");
736 name = name.substr(0,pos);
738 mGimmick->addDB(name, crea::wx2std(files[i]));
740 CreateSingleTreeView(name);
744 ////////////////////////////////////////////////////
745 // Create a DB from an Attributes Descriptor files //
746 // @param event : WxEvent //
748 //////////////////////////////////////////////////
749 void WxGimmickView::OnCreateDB(wxCommandEvent& event)
753 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
754 std::string wc("*.txt");
755 wxFileDialog* FD = new wxFileDialog( 0,
763 if (FD->ShowModal()==wxID_OK)
768 //get name of DB (file name)
769 std::string file = crea::wx2std(afile[0]);
770 size_t pos = file.find_last_of("\\");
771 std::string name = file.substr(pos+1);
772 std::string directory = file.substr(0,pos);
773 pos = name.find_last_of(".");
774 name = name.substr(0,pos);
775 //get directory to store DB
776 directory += "\\" + name + ".sqlite3";
778 mGimmick->createDB(name, file,directory);
780 mGimmick->addDB(name, directory);
782 CreateSingleTreeView(name);
786 //=================================================
788 //=================================================
789 BEGIN_EVENT_TABLE(WxGimmickView, wxPanel)
790 EVT_TOOL(TOOL_CREATEDB_ID, WxGimmickView::OnCreateDB)
791 EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnAddFiles)
792 EVT_TOOL(TOOL_ADDDIR_ID, WxGimmickView::OnAddDir)
793 EVT_TOOL(TOOL_ADDDATABASE_ID, WxGimmickView::OnAddDB)
794 EVT_TOOL(TOOL_REMOVE_ID, WxGimmickView::OnRemove)
795 EVT_TOOL(TOOL_SYNCHRONIZE_ID, WxGimmickView::OnSynchronize)
797 //=================================================
799 } // EO namespace creaImageIO