1 #include <creaImageIOWxTreeView.h>
2 #include <creaImageIOGimmickView.h>
3 #include <creaImageIOSystem.h>
4 #include <wx/splitter.h>
6 ///Comparing function for ordering algorithm. Takes parameters as strings.
7 int wxCALLBACK CompareFunctionStrings(long item1, long item2, long sortData)
9 std::string s1((char*)((long*)item1)[1]);
10 std::string s2((char*)((long*)item2)[1]);
34 ///Comparing function for ordering algorithm. Takes parameters as ints.
35 int wxCALLBACK CompareFunctionInts(long item1, long item2, long sortData)
37 int val1=atoi((char*)((long*)item1)[1]);
38 int val2=atoi((char*)((long*)item2)[1]);
64 //=====================================================================
66 WxTreeView::WxTreeView(TreeHandler* handler,
71 TreeView(handler,gimmick)
73 GimmickDebugMessage(1,"WxTreeView::WxTreeView"
77 // Split part below toolbar into notebook for views and panel
78 // for preview, messages...
80 // mSplitter = new wxSplitterWindow( this , -1);
83 wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
85 int ctrl_style = wxLC_REPORT | wxLC_VRULES;
86 int col_style = wxLIST_FORMAT_LEFT;
88 // Creating the ListCtrl for the levels > 0 (not for Root level)
90 i < handler->GetTree().GetNumberOfLevels();
94 GimmickDebugMessage(5,"Creating view for level "<<i
98 // If the first level : parent = this
99 wxWindow* sparent = this;
100 // else parent = last splitter
101 if (i>1) sparent = mLevelList[i-2].wxSplitter;
103 level.wxSplitter = new wxSplitterWindow( sparent , -1);
104 // level.wxSplitter->SetMinimumPaneSize(100);
106 wxListCtrl* ctrl = new wxListCtrl(level.wxSplitter,
112 level.wxSplitter->Initialize(ctrl);
115 // Create the columns : one for each attribute of the level
118 tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
119 for (a = handler->GetTree().GetAttributeDescriptorList(i).begin();
120 a != handler->GetTree().GetAttributeDescriptorList(i).end();
124 GimmickDebugMessage(5,"Creating column "<<col<<" : "
128 if(a->GetFlags()!=creaImageIO::tree::AttributeDescriptor::PRIVATE)
131 if(a->GetName()=="UNKNOWN")
134 title += handler->GetTree().GetLevelDescriptor(i+1).GetName();
135 if (title[title.size()-1]!='s')
143 ctrl->InsertColumn(col,
146 level.key.push_back(a->GetKey());
147 // ctrl->SetColumnWidth(col, wxLIST_AUTOSIZE );
153 mLevelList.push_back(level);
159 wxMenuItem* m1=menu->Append(wxID_ANY, _T("&Sort ascending"));
160 wxMenuItem* m2=menu->Append(wxID_ANY, _T("&Sort descending"));
161 wxMenuItem* m3=menu->Append(wxID_ANY, _T("&Filter"));
162 mAscendingID=m1->GetId();
163 mDescendingID=m2->GetId();
164 mFilterID=m3->GetId();
165 Connect( mAscendingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupSort) );
166 Connect( mDescendingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupSort) );
167 Connect( mFilterID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupFilter) );
169 #endif // wxUSE_MENUS
172 /// Initialize the first level splitter
174 sizer->Add( mLevelList[0].wxSplitter ,1, wxGROW ,0);
178 mSelectionMade=false;
180 //CreateColorPalette();
188 //=====================================================================
190 //=====================================================================
192 WxTreeView::~WxTreeView()
194 GimmickDebugMessage(1,"WxTreeView::~WxTreeView"
197 //=====================================================================
200 //=====================================================================
207 //=====================================================================
208 std::vector<tree::Node*> WxTreeView::GetSelected(int level)
211 // the selection of upper level
212 std::vector<tree::Node*> sel;
216 sel.push_back(GetTreeHandler()->GetTree().GetTree());
220 int n = GetCtrl(l-1)->GetItemCount();
221 for (int i = 0; i < n; i++)
223 if ( GetCtrl(l-1)->GetItemState(i,wxLIST_STATE_SELECTED))
225 long adr = GetCtrl(l-1)->GetItemData(i);
226 tree::Node* n = ((ItemData*)adr)->node;
229 std::vector<tree::Node*>::iterator it;
231 it = sel.insert ( it , n );
244 //=====================================================================
246 ///Removes selected nodes on given level
247 void WxTreeView::RemoveSelected( int level )
249 std::vector<tree::Node*> sel=GetSelected(level+1);
251 if (wxMessageBox(_T("Delete file(s) ?"),
253 wxYES_NO,this ) == wxYES)
259 std::vector<tree::Node*>::iterator i;
260 for (i=sel.begin(); i!=sel.end(); ++i)
262 GimmickDebugMessage(2,
267 GetTreeHandler()->Remove(*i);
276 //=====================================================================
279 //=====================================================================
280 /// Updates a level of the view (adds or removes children, proganizes, etc.)
281 void WxTreeView::UpdateLevel( int level )
283 GimmickDebugMessage(1,
284 GetTreeHandler()->GetTree().GetLabel()
285 <<" view : updating level "<<level
289 RecursiveUpdateLevel(level);
291 for (i=0; i<level-1; i++)
293 if (!GetSplitter(i)->IsSplit())
294 GetSplitter(i)->SplitVertically( GetCtrl(i), GetSplitter(i+1),
297 if (GetSplitter(i)->IsSplit()) GetSplitter(i)->Unsplit();
300 //=====================================================================
302 /// Recursive method called upon by UpdateLevel to refresh all windows
303 void WxTreeView::RecursiveUpdateLevel( int level )
305 GimmickDebugMessage(2,
306 GetTreeHandler()->GetTree().GetLabel()
307 <<" view : updating level (recursive)"<<level
311 std::vector<tree::Node*> sel=GetSelected(level);
315 // to speed up inserting we hide the control temporarily
317 GetCtrl(l)->DeleteAllItems();
319 std::vector<tree::Node*>::iterator i;
321 for (i=sel.begin(); i!=sel.end(); ++i)
323 GimmickDebugMessage(2,
324 "adding children of '"
330 //Adds items and sets their attributes
331 GetCtrl(l)->InsertItem(0, _T(""));
332 GetTreeHandler()->LoadChildren(*i,1);
333 tree::Node::ChildrenListType::reverse_iterator j;
334 for (j = (*i)->GetChildrenList().rbegin();
335 j!= (*i)->GetChildrenList().rend();
338 wxListItem* item= new wxListItem;
339 item->SetMask(wxLIST_MASK_STATE |
341 // wxLIST_MASK_IMAGE |
343 // wxLIST_MASK_WIDTH |
347 ItemData* data = new ItemData;
356 GetCtrl(l)->InsertItem(*item);
360 for (int k=0; k<GetCtrl(l)->GetColumnCount(); k++)
362 std::string val = (*j)->GetAttribute(mLevelList[l].key[k]);
363 if (val.size()==0) val = "?";
364 item->SetText( crea::std2wx(val));
366 GetCtrl(l)->SetItem(*item);
370 GetCtrl(l)->DeleteItem(0);
376 //=====================================================================
379 //================================================================
380 void WxTreeView::OnSelectedChanged(wxListEvent& event)
382 GimmickDebugMessage(1,
383 GetTreeHandler()->GetTree().GetLabel()
384 <<" view : item selected "
388 info.m_itemId = event.m_itemIndex;
389 mLastSelected = event.m_itemIndex;
390 // retrieve the level
391 wxObject* obj = event.GetEventObject();
392 unsigned int level = 0;
393 for (level = 0; level<mLevelList.size(); ++level)
395 if ( GetCtrl(level) == obj ) break;
397 GimmickDebugMessage(1,
401 // Update the children level (if selection not at last level)
402 if (level<mLevelList.size()-1)
404 UpdateLevel( level + 2 );
405 // Reset the viewer setting the default image
406 GetGimmickView()->ClearSelection();
408 // Select all images if the selection is at series level
409 if (level==mLevelList.size()-2) SelectLowerLevel();
410 // Validate selected images if the selection is at image level
411 if (level==(mLevelList.size()-1)&&mProcess)
413 if(event.GetEventType()==10145)
415 ValidateSelectedImages (true);
419 ValidateSelectedImages (false);
424 //================================================================
426 //================================================================
427 void WxTreeView::SelectLowerLevel()
430 int level=mLevelList.size()-1;
433 item = GetCtrl(level)->GetNextItem(item,
438 if(item==(GetCtrl(level)->GetItemCount()-1))
446 GetCtrl(level)->SetItemState(item,wxLIST_STATE_SELECTED, wxLIST_MASK_STATE
447 | wxLIST_MASK_TEXT |wxLIST_MASK_IMAGE | wxLIST_MASK_DATA | wxLIST_MASK_WIDTH | wxLIST_MASK_FORMAT);
451 //================================================================
452 //================================================================
454 void WxTreeView::OnColClick(wxListEvent& event)
456 mColumnSelected=event.m_col;
458 clientpt.x = wxGetMousePosition().x - this->GetScreenPosition().x;
459 clientpt.y = wxGetMousePosition().y - this->GetScreenPosition().y;
460 senderCtrl = event.GetEventObject();
461 unsigned int level = 0;
462 for (level = 0; level<mLevelList.size(); ++level)
464 if ( GetCtrl(level) == senderCtrl ) break;
466 PopupMenu(menu, clientpt);
470 //================================================================
471 //================================================================
473 void WxTreeView::OnPopupFilter(wxCommandEvent& event)
476 GimmickDebugMessage(7,
477 "WxTreeView::OnEndLabelEdit"
479 wxObject* ctrl = event.GetEventObject();
480 unsigned int level = 0;
481 for (level = 0; level<mLevelList.size(); ++level)
483 if ( GetCtrl(level) == senderCtrl ) break;
485 std::string filter = crea::wx2std(wxGetTextFromUser(_T("Enter the filter to apply"), _T("Filter On Column")));
490 UpdateLevel(level+1);
492 std::vector<long> items;
497 it = GetCtrl(level)->GetNextItem(it,
502 long adr = GetCtrl(level)->GetItemData(it);
503 tree::Node* nod = ((ItemData*)adr)->node;
504 att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected]);
507 if(att.find(filter)>900)
519 items.push_back(it-del);
523 std::vector<long>::iterator iter;
524 for(iter=items.begin();iter!=items.end();++iter)
526 GetCtrl(level)->DeleteItem(*iter);
528 GetGimmickView()->ClearSelection();
530 //================================================================
532 //================================================================
533 void WxTreeView::OnPopupSort(wxCommandEvent& event)
536 unsigned int level = 0;
537 for (level = 0; level<mLevelList.size(); ++level)
539 if ( GetCtrl(level) == senderCtrl ) break;
541 if(event.GetId()==mAscendingID)
545 else if(event.GetId()==mDescendingID)
552 //================================================================
553 void WxTreeView::OnSort(int level)
555 //Obtain the column name and the level that needs to be organized
558 //Sets the data for the items to be sorted
561 int n = GetCtrl(level)->GetItemCount();
562 for (int i = 0; i < n; i++)
565 //Gets current item data
566 long adr = GetCtrl(level)->GetItemData(i);
568 //Extracts the node and the type of attribute
569 tree::Node* nod = ((ItemData*)adr)->node;
572 (*nod).GetAttributeDescriptor(mLevelList[level].key[mColumnSelected]).DecodeType(ty);
574 //Obtains the organizing attribute
575 att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected]);
577 char* d= new char [att.size()+1];
578 strcpy (d, att.c_str());
581 long* lp= new long[2];
585 //Sets it as the data
586 GetCtrl(level)->SetItemData(i,(long)lp);
593 GetCtrl(level)->SortItems(CompareFunctionInts, 0);
597 GetCtrl(level)->SortItems(CompareFunctionStrings, 0);
605 GetCtrl(level)->SortItems(CompareFunctionInts, 1);
609 GetCtrl(level)->SortItems(CompareFunctionStrings, 1);
613 //Resets original data
615 std::vector<tree::Node*>::iterator selection;
616 std::vector<long> change;
620 it = GetCtrl(level)->GetNextItem(it,
625 //Gets current item data, extracts the node and resets it
626 long item = GetCtrl(level)->GetItemData(it);
627 GetCtrl(level)->SetItemData(it,((long*)item)[0]);
628 tree::Node* n= ((ItemData*)((long*)item)[0])->node;
634 //================================================================
635 void WxTreeView::ValidateSelectedImages(bool isSelection)
637 GimmickDebugMessage(7,
638 "WxTreeView::ValidateSelectedImages"
640 std::vector<tree::Node*> sel(GetSelected(mLevelList.size()+1));
641 GetGimmickView()->OnSelectionChange(sel,isSelection,(mLastSelected-1), mProcess);
644 //================================================================
647 //================================================================
648 void WxTreeView::GetNodes(std::vector<tree::Node*>& nodes, bool direction)
650 long item = mLastSelected;
651 int level=mLevelList.size()-1;
652 //Gets current item data
653 long adr = GetCtrl(level)->GetItemData(item);
655 tree::Node* nod = ((ItemData*)adr)->node;
660 item = GetCtrl(level)->GetNextItem(item,
665 item = GetCtrl(level)->GetNextItem(item,
672 if(GetCtrl(level)->GetItemState(item, wxLIST_STATE_SELECTED)==0 /*&& item!=0*/)
674 adr = GetCtrl(level)->GetItemData(item);
675 nod = ((ItemData*)adr)->node;
676 nodes.push_back(nod);
682 //================================================================
683 void WxTreeView::GetSelectedAsString(std::vector<std::string>&s)
685 int level=mLevelList.size();
686 std::vector<tree::Node*> sel=GetSelected(level+1);
687 std::vector<tree::Node*>::iterator i;
689 for (i=sel.begin(); i!=sel.end(); ++i)
691 std::string filename=(*i)->GetAttribute("FullFileName");
692 s.push_back(filename);
696 //================================================================
697 void WxTreeView::SetColor(int l, int item)
700 GetCtrl(l)->SetItemTextColour(item, wxColourDatabase().Find
701 (crea::std2wx(mColorPalette[colorId])));
702 GetCtrl(l)->SetItemState(item,wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED); /*
704 //Setting the color according to the parent
707 item.SetBackgroundColour
708 (wxColourDatabase().Find
709 (crea::std2wx(mColorPalette[colorId])));
712 (*j,wxColourDatabase().Find
713 (crea::std2wx(mColorPalette[colorId]))));
723 else if(l!=mLevelList.size()-1)
725 item.SetBackgroundColour(mColorMap[*i]);
726 mColorMap.insert(NodeColorPair(*j,mColorMap[*i]));
730 item.SetBackgroundColour(mColorMap[*i]);
733 //================================================================
734 void WxTreeView::CreateColorPalette()
736 GimmickDebugMessage(6,"WxTreeView::CreateColorPalette");
737 mColorPalette.push_back("WHITE");
738 mColorPalette.push_back("LIGHT GREY");
739 mColorPalette.push_back("AQUAMARINE");
740 mColorPalette.push_back("MEDIUM FOREST GREEN");
741 mColorPalette.push_back("INDIAN RED");
742 mColorPalette.push_back("KHAKI");
743 mColorPalette.push_back("ORANGE");
744 mColorPalette.push_back("LIGHT BLUE");
745 mColorPalette.push_back("LIGHT STEEL BLUE");
746 mColorPalette.push_back("PINK");
747 mColorPalette.push_back("PLUM");
748 mColorPalette.push_back("PURPLE");
749 mColorPalette.push_back("RED");
750 mColorPalette.push_back("SEA GREEN");
751 mColorPalette.push_back("SIENNA");
752 mColorPalette.push_back("SKY BLUE");
753 mColorPalette.push_back("SLATE BLUE");
754 mColorPalette.push_back("SPRING GREEN");
755 mColorPalette.push_back("TAN");
756 mColorPalette.push_back("THISTLE");
757 mColorPalette.push_back("TURQUOISE");
758 mColorPalette.push_back("VIOLET");
759 mColorPalette.push_back("VIOLET RED");
760 mColorPalette.push_back("WHEAT");
761 mColorPalette.push_back("YELLOW");
762 mColorPalette.push_back("YELLOW GREEN");
763 mColorPalette.push_back("BLUE");
764 mColorPalette.push_back("BLUE VIOLET");
765 mColorPalette.push_back("BROWN");
766 mColorPalette.push_back("CADET BLUE");
767 mColorPalette.push_back("CORAL");
768 mColorPalette.push_back("CORNFLOWER BLUE");
769 mColorPalette.push_back("CYAN");
770 mColorPalette.push_back("DARK GREY");
771 mColorPalette.push_back("DARK GREEN");
772 mColorPalette.push_back("DARK OLIVE GREEN");
773 mColorPalette.push_back("DARK ORCHID");
774 mColorPalette.push_back("DARK SLATE BLUE");
775 mColorPalette.push_back("DARK SLATE GREY");
776 mColorPalette.push_back("DARK TURQUOISE");
777 mColorPalette.push_back("FIREBRICK");
778 mColorPalette.push_back("FOREST GREEN");
779 mColorPalette.push_back("GOLD");
780 mColorPalette.push_back("GOLDENROD");
781 mColorPalette.push_back("GREY");
782 mColorPalette.push_back("GREEN");
783 mColorPalette.push_back("GREEN YELLOW");
784 mColorPalette.push_back("LIME GREEN");
785 mColorPalette.push_back("MAGENTA");
786 mColorPalette.push_back("MAROON");
787 mColorPalette.push_back("MEDIUM AQUAMARINE");
788 mColorPalette.push_back("MEDIUM BLUE");
789 mColorPalette.push_back("MEDIUM GOLDENROD");
790 mColorPalette.push_back("MEDIUM ORCHID");
791 mColorPalette.push_back("MEDIUM SEA GREEN");
792 mColorPalette.push_back("MEDIUM SLATE BLUE");
793 mColorPalette.push_back("MEDIUM SPRING GREEN");
794 mColorPalette.push_back("MEDIUM TURQUOISE");
795 mColorPalette.push_back("MEDIUM VIOLET RED");
796 mColorPalette.push_back("MIDNIGHT BLUE");
797 mColorPalette.push_back("NAVY");
798 mColorPalette.push_back("ORANGE RED");
799 mColorPalette.push_back("ORCHID, PALE GREEN");
800 mColorPalette.push_back("STEEL BLUE");
801 mColorPalette.push_back("BLACK");
805 //================================================================
806 BEGIN_EVENT_TABLE(WxTreeView, wxPanel)
808 EVT_SIZE(MyFrame::OnSize)
810 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
811 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
812 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
813 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
814 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
815 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
816 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
817 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
818 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
819 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
821 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
822 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
823 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
824 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
825 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
826 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
827 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
828 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
829 EVT_MENU(LIST_SORT, MyFrame::OnSort)
830 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
831 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
832 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
833 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
834 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
835 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
836 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
837 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
838 EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
840 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
841 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
844 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
845 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
846 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
848 EVT_LIST_BEGIN_LABEL_EDIT(-1, WxTreeView::OnBeginLabelEdit)
849 EVT_LIST_END_LABEL_EDIT(-1, WxTreeView::OnEndLabelEdit)
851 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
852 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
853 #if WXWIN_COMPATIBILITY_2_4
854 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
855 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
858 EVT_LIST_ITEM_SELECTED(-1, WxTreeView::OnSelectedChanged)
860 EVT_LIST_ITEM_DESELECTED(-1, WxTreeView::OnSelectedChanged)
862 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
863 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
864 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
866 EVT_LIST_COL_RIGHT_CLICK(-1, WxTreeView::OnColClick)
868 EVT_LIST_COL_CLICK(-1, WxTreeView::OnColClick)
870 //EVT_LEFT_DOWN(WxTreeView::OnMouseClick)
872 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
873 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
874 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
876 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
879 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
881 EVT_CHAR(MyListCtrl::OnChar)
883 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
887 } // EO namespace creaImageIO