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<<" : "
127 if(a->GetName()=="UNKNOWN")
130 title += handler->GetTree().GetLevelDescriptor(i+1).GetName();
131 if (title[title.size()-1]!='s')
139 ctrl->InsertColumn(col,
142 level.key.push_back(a->GetKey());
143 // ctrl->SetColumnWidth(col, wxLIST_AUTOSIZE );
149 mLevelList.push_back(level);
155 wxMenuItem* m1=menu->Append(wxID_ANY, _T("&Sort ascending"));
156 wxMenuItem* m2=menu->Append(wxID_ANY, _T("&Sort descending"));
157 wxMenuItem* m3=menu->Append(wxID_ANY, _T("&Filter"));
158 mAscendingID=m1->GetId();
159 mDescendingID=m2->GetId();
160 mFilterID=m3->GetId();
161 Connect( mAscendingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupSort) );
162 Connect( mDescendingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupSort) );
163 Connect( mFilterID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupFilter) );
165 #endif // wxUSE_MENUS
168 /// Initialize the first level splitter
170 sizer->Add( mLevelList[0].wxSplitter ,1, wxGROW ,0);
174 mSelectionMade=false;
176 //CreateColorPalette();
184 //=====================================================================
186 //=====================================================================
188 WxTreeView::~WxTreeView()
190 GimmickDebugMessage(1,"WxTreeView::~WxTreeView"
193 //=====================================================================
196 //=====================================================================
203 //=====================================================================
204 std::vector<tree::Node*> WxTreeView::GetSelected(int level)
207 // the selection of upper level
208 std::vector<tree::Node*> sel;
212 sel.push_back(GetTreeHandler()->GetTree().GetTree());
216 int n = GetCtrl(l-1)->GetItemCount();
217 for (int i = 0; i < n; i++)
219 if ( GetCtrl(l-1)->GetItemState(i,wxLIST_STATE_SELECTED))
221 long adr = GetCtrl(l-1)->GetItemData(i);
222 tree::Node* n = ((ItemData*)adr)->node;
225 std::vector<tree::Node*>::iterator it;
227 it = sel.insert ( it , n );
240 //=====================================================================
242 ///Removes selected nodes on given level
243 void WxTreeView::RemoveSelected( int level )
245 std::vector<tree::Node*> sel=GetSelected(level+1);
247 if (wxMessageBox(_T("Delete file(s) ?"),
249 wxYES_NO,this ) == wxYES)
255 std::vector<tree::Node*>::iterator i;
256 for (i=sel.begin(); i!=sel.end(); ++i)
258 GimmickDebugMessage(2,
263 GetTreeHandler()->Remove(*i);
272 //=====================================================================
275 //=====================================================================
276 /// Updates a level of the view (adds or removes children, proganizes, etc.)
277 void WxTreeView::UpdateLevel( int level )
279 GimmickDebugMessage(1,
280 GetTreeHandler()->GetTree().GetLabel()
281 <<" view : updating level "<<level
285 RecursiveUpdateLevel(level);
287 for (i=0; i<level-1; i++)
289 if (!GetSplitter(i)->IsSplit())
290 GetSplitter(i)->SplitVertically( GetCtrl(i), GetSplitter(i+1),
293 if (GetSplitter(i)->IsSplit()) GetSplitter(i)->Unsplit();
296 //=====================================================================
298 /// Recursive method called upon by UpdateLevel to refresh all windows
299 void WxTreeView::RecursiveUpdateLevel( int level )
301 GimmickDebugMessage(2,
302 GetTreeHandler()->GetTree().GetLabel()
303 <<" view : updating level (recursive)"<<level
307 std::vector<tree::Node*> sel=GetSelected(level);
311 // to speed up inserting we hide the control temporarily
313 GetCtrl(l)->DeleteAllItems();
315 std::vector<tree::Node*>::iterator i;
317 for (i=sel.begin(); i!=sel.end(); ++i)
319 GimmickDebugMessage(2,
320 "adding children of '"
326 //Adds items and sets their attributes
327 GetCtrl(l)->InsertItem(0, _T(""));
328 GetTreeHandler()->LoadChildren(*i,1);
329 tree::Node::ChildrenListType::reverse_iterator j;
330 for (j = (*i)->GetChildrenList().rbegin();
331 j!= (*i)->GetChildrenList().rend();
334 wxListItem* item= new wxListItem;
335 item->SetMask(wxLIST_MASK_STATE |
337 // wxLIST_MASK_IMAGE |
339 // wxLIST_MASK_WIDTH |
343 ItemData* data = new ItemData;
352 GetCtrl(l)->InsertItem(*item);
356 for (int k=0; k<GetCtrl(l)->GetColumnCount(); k++)
358 std::string val = (*j)->GetAttribute(mLevelList[l].key[k]);
359 if (val.size()==0) val = "?";
360 item->SetText( crea::std2wx(val));
362 GetCtrl(l)->SetItem(*item);
366 GetCtrl(l)->DeleteItem(0);
372 //=====================================================================
375 //================================================================
376 void WxTreeView::OnSelectedChanged(wxListEvent& event)
378 GimmickDebugMessage(1,
379 GetTreeHandler()->GetTree().GetLabel()
380 <<" view : item selected "
384 info.m_itemId = event.m_itemIndex;
385 mLastSelected = event.m_itemIndex;
386 // retrieve the level
387 wxObject* obj = event.GetEventObject();
388 unsigned int level = 0;
389 for (level = 0; level<mLevelList.size(); ++level)
391 if ( GetCtrl(level) == obj ) break;
393 GimmickDebugMessage(1,
397 // Update the children level (if selection not at last level)
398 if (level<mLevelList.size()-1)
400 UpdateLevel( level + 2 );
401 // Reset the viewer setting the default image
402 GetGimmickView()->ClearSelection();
404 // Select all images if the selection is at series level
405 if (level==mLevelList.size()-2) SelectLowerLevel();
406 // Validate selected images if the selection is at image level
407 if (level==(mLevelList.size()-1)&&mProcess)
409 if(event.GetEventType()==10145)
411 ValidateSelectedImages (true);
415 ValidateSelectedImages (false);
420 //================================================================
422 //================================================================
423 void WxTreeView::SelectLowerLevel()
426 int level=mLevelList.size()-1;
429 item = GetCtrl(level)->GetNextItem(item,
434 if(item==(GetCtrl(level)->GetItemCount()-1))
442 GetCtrl(level)->SetItemState(item,wxLIST_STATE_SELECTED, wxLIST_MASK_STATE
443 | wxLIST_MASK_TEXT |wxLIST_MASK_IMAGE | wxLIST_MASK_DATA | wxLIST_MASK_WIDTH | wxLIST_MASK_FORMAT);
447 //================================================================
448 //================================================================
450 void WxTreeView::OnColClick(wxListEvent& event)
452 mColumnSelected=event.m_col;
454 clientpt.x = wxGetMousePosition().x - this->GetScreenPosition().x;
455 clientpt.y = wxGetMousePosition().y - this->GetScreenPosition().y;
456 senderCtrl = event.GetEventObject();
457 unsigned int level = 0;
458 for (level = 0; level<mLevelList.size(); ++level)
460 if ( GetCtrl(level) == senderCtrl ) break;
462 PopupMenu(menu, clientpt);
466 //================================================================
467 //================================================================
469 void WxTreeView::OnPopupFilter(wxCommandEvent& event)
472 GimmickDebugMessage(7,
473 "WxTreeView::OnEndLabelEdit"
475 wxObject* ctrl = event.GetEventObject();
476 unsigned int level = 0;
477 for (level = 0; level<mLevelList.size(); ++level)
479 if ( GetCtrl(level) == senderCtrl ) break;
481 std::string filter = crea::wx2std(wxGetTextFromUser(_T("Enter the filter to apply"), _T("Filter On Column")));
486 UpdateLevel(level+1);
488 std::vector<long> items;
493 it = GetCtrl(level)->GetNextItem(it,
498 long adr = GetCtrl(level)->GetItemData(it);
499 tree::Node* nod = ((ItemData*)adr)->node;
500 att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected]);
503 if(att.find(filter)>900)
515 items.push_back(it-del);
519 std::vector<long>::iterator iter;
520 for(iter=items.begin();iter!=items.end();++iter)
522 GetCtrl(level)->DeleteItem(*iter);
524 GetGimmickView()->ClearSelection();
526 //================================================================
528 //================================================================
529 void WxTreeView::OnPopupSort(wxCommandEvent& event)
532 unsigned int level = 0;
533 for (level = 0; level<mLevelList.size(); ++level)
535 if ( GetCtrl(level) == senderCtrl ) break;
537 if(event.GetId()==mAscendingID)
541 else if(event.GetId()==mDescendingID)
548 //================================================================
549 void WxTreeView::OnSort(int level)
551 //Obtain the column name and the level that needs to be organized
554 //Sets the data for the items to be sorted
557 int n = GetCtrl(level)->GetItemCount();
558 for (int i = 0; i < n; i++)
561 //Gets current item data
562 long adr = GetCtrl(level)->GetItemData(i);
564 //Extracts the node and the type of attribute
565 tree::Node* nod = ((ItemData*)adr)->node;
568 (*nod).GetAttributeDescriptor(mLevelList[level].key[mColumnSelected]).DecodeType(ty);
570 //Obtains the organizing attribute
571 att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected]);
573 char* d= new char [att.size()+1];
574 strcpy (d, att.c_str());
577 long* lp= new long[2];
581 //Sets it as the data
582 GetCtrl(level)->SetItemData(i,(long)lp);
589 GetCtrl(level)->SortItems(CompareFunctionInts, 0);
593 GetCtrl(level)->SortItems(CompareFunctionStrings, 0);
601 GetCtrl(level)->SortItems(CompareFunctionInts, 1);
605 GetCtrl(level)->SortItems(CompareFunctionStrings, 1);
609 //Resets original data
611 std::vector<tree::Node*>::iterator selection;
612 std::vector<long> change;
616 it = GetCtrl(level)->GetNextItem(it,
621 //Gets current item data, extracts the node and resets it
622 long item = GetCtrl(level)->GetItemData(it);
623 GetCtrl(level)->SetItemData(it,((long*)item)[0]);
624 tree::Node* n= ((ItemData*)((long*)item)[0])->node;
630 //================================================================
631 void WxTreeView::ValidateSelectedImages(bool isSelection)
633 GimmickDebugMessage(7,
634 "WxTreeView::ValidateSelectedImages"
636 std::vector<tree::Node*> sel(GetSelected(mLevelList.size()+1));
637 GetGimmickView()->OnSelectionChange(sel,isSelection,(mLastSelected-1), mProcess);
640 //================================================================
643 //================================================================
644 void WxTreeView::GetNodes(std::vector<tree::Node*>& nodes, bool direction)
646 long item = mLastSelected;
647 int level=mLevelList.size()-1;
648 //Gets current item data
649 long adr = GetCtrl(level)->GetItemData(item);
651 tree::Node* nod = ((ItemData*)adr)->node;
656 item = GetCtrl(level)->GetNextItem(item,
661 item = GetCtrl(level)->GetNextItem(item,
668 if(GetCtrl(level)->GetItemState(item, wxLIST_STATE_SELECTED)==0 && item!=0)
670 adr = GetCtrl(level)->GetItemData(item);
671 nod = ((ItemData*)adr)->node;
672 nodes.push_back(nod);
678 //================================================================
679 void WxTreeView::GetSelectedAsString(std::vector<std::string>&s)
681 int level=mLevelList.size();
682 std::vector<tree::Node*> sel=GetSelected(level+1);
683 std::vector<tree::Node*>::iterator i;
685 for (i=sel.begin(); i!=sel.end(); ++i)
687 std::string filename=(*i)->GetAttribute("FullFileName");
688 s.push_back(filename);
692 //================================================================
693 void WxTreeView::SetColor(int l, int item)
696 GetCtrl(l)->SetItemTextColour(item, wxColourDatabase().Find
697 (crea::std2wx(mColorPalette[colorId])));
698 GetCtrl(l)->SetItemState(item,wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED); /*
700 //Setting the color according to the parent
703 item.SetBackgroundColour
704 (wxColourDatabase().Find
705 (crea::std2wx(mColorPalette[colorId])));
708 (*j,wxColourDatabase().Find
709 (crea::std2wx(mColorPalette[colorId]))));
719 else if(l!=mLevelList.size()-1)
721 item.SetBackgroundColour(mColorMap[*i]);
722 mColorMap.insert(NodeColorPair(*j,mColorMap[*i]));
726 item.SetBackgroundColour(mColorMap[*i]);
729 //================================================================
730 void WxTreeView::CreateColorPalette()
732 GimmickDebugMessage(6,"WxTreeView::CreateColorPalette");
733 mColorPalette.push_back("WHITE");
734 mColorPalette.push_back("LIGHT GREY");
735 mColorPalette.push_back("AQUAMARINE");
736 mColorPalette.push_back("MEDIUM FOREST GREEN");
737 mColorPalette.push_back("INDIAN RED");
738 mColorPalette.push_back("KHAKI");
739 mColorPalette.push_back("ORANGE");
740 mColorPalette.push_back("LIGHT BLUE");
741 mColorPalette.push_back("LIGHT STEEL BLUE");
742 mColorPalette.push_back("PINK");
743 mColorPalette.push_back("PLUM");
744 mColorPalette.push_back("PURPLE");
745 mColorPalette.push_back("RED");
746 mColorPalette.push_back("SEA GREEN");
747 mColorPalette.push_back("SIENNA");
748 mColorPalette.push_back("SKY BLUE");
749 mColorPalette.push_back("SLATE BLUE");
750 mColorPalette.push_back("SPRING GREEN");
751 mColorPalette.push_back("TAN");
752 mColorPalette.push_back("THISTLE");
753 mColorPalette.push_back("TURQUOISE");
754 mColorPalette.push_back("VIOLET");
755 mColorPalette.push_back("VIOLET RED");
756 mColorPalette.push_back("WHEAT");
757 mColorPalette.push_back("YELLOW");
758 mColorPalette.push_back("YELLOW GREEN");
759 mColorPalette.push_back("BLUE");
760 mColorPalette.push_back("BLUE VIOLET");
761 mColorPalette.push_back("BROWN");
762 mColorPalette.push_back("CADET BLUE");
763 mColorPalette.push_back("CORAL");
764 mColorPalette.push_back("CORNFLOWER BLUE");
765 mColorPalette.push_back("CYAN");
766 mColorPalette.push_back("DARK GREY");
767 mColorPalette.push_back("DARK GREEN");
768 mColorPalette.push_back("DARK OLIVE GREEN");
769 mColorPalette.push_back("DARK ORCHID");
770 mColorPalette.push_back("DARK SLATE BLUE");
771 mColorPalette.push_back("DARK SLATE GREY");
772 mColorPalette.push_back("DARK TURQUOISE");
773 mColorPalette.push_back("FIREBRICK");
774 mColorPalette.push_back("FOREST GREEN");
775 mColorPalette.push_back("GOLD");
776 mColorPalette.push_back("GOLDENROD");
777 mColorPalette.push_back("GREY");
778 mColorPalette.push_back("GREEN");
779 mColorPalette.push_back("GREEN YELLOW");
780 mColorPalette.push_back("LIME GREEN");
781 mColorPalette.push_back("MAGENTA");
782 mColorPalette.push_back("MAROON");
783 mColorPalette.push_back("MEDIUM AQUAMARINE");
784 mColorPalette.push_back("MEDIUM BLUE");
785 mColorPalette.push_back("MEDIUM GOLDENROD");
786 mColorPalette.push_back("MEDIUM ORCHID");
787 mColorPalette.push_back("MEDIUM SEA GREEN");
788 mColorPalette.push_back("MEDIUM SLATE BLUE");
789 mColorPalette.push_back("MEDIUM SPRING GREEN");
790 mColorPalette.push_back("MEDIUM TURQUOISE");
791 mColorPalette.push_back("MEDIUM VIOLET RED");
792 mColorPalette.push_back("MIDNIGHT BLUE");
793 mColorPalette.push_back("NAVY");
794 mColorPalette.push_back("ORANGE RED");
795 mColorPalette.push_back("ORCHID, PALE GREEN");
796 mColorPalette.push_back("STEEL BLUE");
797 mColorPalette.push_back("BLACK");
801 //================================================================
802 BEGIN_EVENT_TABLE(WxTreeView, wxPanel)
804 EVT_SIZE(MyFrame::OnSize)
806 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
807 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
808 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
809 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
810 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
811 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
812 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
813 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
814 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
815 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
817 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
818 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
819 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
820 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
821 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
822 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
823 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
824 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
825 EVT_MENU(LIST_SORT, MyFrame::OnSort)
826 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
827 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
828 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
829 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
830 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
831 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
832 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
833 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
834 EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
836 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
837 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
840 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
841 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
842 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
844 EVT_LIST_BEGIN_LABEL_EDIT(-1, WxTreeView::OnBeginLabelEdit)
845 EVT_LIST_END_LABEL_EDIT(-1, WxTreeView::OnEndLabelEdit)
847 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
848 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
849 #if WXWIN_COMPATIBILITY_2_4
850 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
851 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
854 EVT_LIST_ITEM_SELECTED(-1, WxTreeView::OnSelectedChanged)
856 EVT_LIST_ITEM_DESELECTED(-1, WxTreeView::OnSelectedChanged)
858 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
859 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
860 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
862 EVT_LIST_COL_RIGHT_CLICK(-1, WxTreeView::OnColClick)
864 EVT_LIST_COL_CLICK(-1, WxTreeView::OnColClick)
866 //EVT_LEFT_DOWN(WxTreeView::OnMouseClick)
868 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
869 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
870 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
872 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
875 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
877 EVT_CHAR(MyListCtrl::OnChar)
879 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
883 } // EO namespace creaImageIO