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);
106 level.wxSplitter->Show(false);
108 // level.wxSplitter->SetMinimumPaneSize(100);
110 wxListCtrl* ctrl = new wxListCtrl(level.wxSplitter,
116 level.wxSplitter->Initialize(ctrl);
119 // Create the columns : one for each attribute of the level
122 tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
123 for (a = handler->GetTree().GetAttributeDescriptorList(i).begin();
124 a != handler->GetTree().GetAttributeDescriptorList(i).end();
128 GimmickDebugMessage(5,"Creating column "<<col<<" : "
132 if(a->GetFlags()!=creaImageIO::tree::AttributeDescriptor::PRIVATE)
135 if(a->GetName()=="UNKNOWN")
138 title += handler->GetTree().GetLevelDescriptor(i+1).GetName();
139 if (title[title.size()-1]!='s')
147 ctrl->InsertColumn(col,
150 level.key.push_back(a->GetKey());
151 // ctrl->SetColumnWidth(col, wxLIST_AUTOSIZE );
157 mLevelList.push_back(level);
163 wxMenuItem* m1=menu->Append(wxID_ANY, _T("&Sort ascending"));
164 wxMenuItem* m2=menu->Append(wxID_ANY, _T("&Sort descending"));
165 wxMenuItem* m3=menu->Append(wxID_ANY, _T("&Filter"));
166 mAscendingID=m1->GetId();
167 mDescendingID=m2->GetId();
168 mFilterID=m3->GetId();
169 Connect( mAscendingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupSort) );
170 Connect( mDescendingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupSort) );
171 Connect( mFilterID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupFilter) );
173 #endif // wxUSE_MENUS
176 /// Initialize the first level splitter
178 sizer->Add( mLevelList[0].wxSplitter ,1, wxGROW ,0);
182 mSelectionMade=false;
184 //CreateColorPalette();
192 //=====================================================================
194 //=====================================================================
196 WxTreeView::~WxTreeView()
198 GimmickDebugMessage(1,"WxTreeView::~WxTreeView"
201 //=====================================================================
204 //=====================================================================
211 //=====================================================================
212 std::vector<tree::Node*> WxTreeView::GetSelected(int level)
215 // the selection of upper level
216 std::vector<tree::Node*> sel;
220 sel.push_back(GetTreeHandler()->GetTree().GetTree());
224 int n = GetCtrl(l-1)->GetItemCount();
225 for (int i = 0; i < n; i++)
227 if ( GetCtrl(l-1)->GetItemState(i,wxLIST_STATE_SELECTED))
229 long adr = GetCtrl(l-1)->GetItemData(i);
230 tree::Node* n = ((ItemData*)adr)->node;
233 std::vector<tree::Node*>::iterator it;
235 it = sel.insert ( it , n );
248 //=====================================================================
250 ///Removes selected nodes on given level
251 void WxTreeView::RemoveSelected( int level )
253 std::vector<tree::Node*> sel=GetSelected(level+1);
255 if (wxMessageBox(_T("Delete file(s) ?"),
257 wxYES_NO,this ) == wxYES)
263 std::vector<tree::Node*>::iterator i;
264 for (i=sel.begin(); i!=sel.end(); ++i)
266 GimmickDebugMessage(2,
271 GetTreeHandler()->Remove(*i);
280 //=====================================================================
283 //=====================================================================
284 /// Updates a level of the view (adds or removes children, proganizes, etc.)
285 void WxTreeView::UpdateLevel( int level )
287 GimmickDebugMessage(1,
288 GetTreeHandler()->GetTree().GetLabel()
289 <<" view : updating level "<<level
293 RecursiveUpdateLevel(level);
295 for (i=0; i<level-1; i++)
297 if (!GetSplitter(i)->IsSplit())
298 GetSplitter(i)->SplitVertically( GetCtrl(i), GetSplitter(i+1),
301 if (GetSplitter(i)->IsSplit()) GetSplitter(i)->Unsplit();
304 //=====================================================================
306 /// Recursive method called upon by UpdateLevel to refresh all windows
307 void WxTreeView::RecursiveUpdateLevel( int level )
309 GimmickDebugMessage(2,
310 GetTreeHandler()->GetTree().GetLabel()
311 <<" view : updating level (recursive)"<<level
315 std::vector<tree::Node*> sel=GetSelected(level);
319 // to speed up inserting we hide the control temporarily
321 GetCtrl(l)->DeleteAllItems();
323 std::vector<tree::Node*>::iterator i;
325 for (i=sel.begin(); i!=sel.end(); ++i)
327 GimmickDebugMessage(2,
328 "adding children of '"
334 //Adds items and sets their attributes
335 GetCtrl(l)->InsertItem(0, _T(""));
336 GetTreeHandler()->LoadChildren(*i,1);
337 tree::Node::ChildrenListType::reverse_iterator j;
338 for (j = (*i)->GetChildrenList().rbegin();
339 j!= (*i)->GetChildrenList().rend();
342 wxListItem* item= new wxListItem;
343 item->SetMask(wxLIST_MASK_STATE |
345 // wxLIST_MASK_IMAGE |
347 // wxLIST_MASK_WIDTH |
351 ItemData* data = new ItemData;
360 GetCtrl(l)->InsertItem(*item);
364 for (int k=0; k<GetCtrl(l)->GetColumnCount(); k++)
366 std::string val = (*j)->GetAttribute(mLevelList[l].key[k]);
367 if (val.size()==0) val = "?";
368 item->SetText( crea::std2wx(val));
370 GetCtrl(l)->SetItem(*item);
374 GetCtrl(l)->DeleteItem(0);
380 //=====================================================================
383 //================================================================
384 void WxTreeView::OnSelectedChanged(wxListEvent& event)
386 GimmickDebugMessage(1,
387 GetTreeHandler()->GetTree().GetLabel()
388 <<" view : item selected "
392 info.m_itemId = event.m_itemIndex;
393 mLastSelected = event.m_itemIndex;
394 // retrieve the level
395 wxObject* obj = event.GetEventObject();
396 unsigned int level = 0;
397 for (level = 0; level<mLevelList.size(); ++level)
399 if ( GetCtrl(level) == obj ) break;
401 GimmickDebugMessage(1,
405 // Update the children level (if selection not at last level)
406 if (level<mLevelList.size()-1)
408 UpdateLevel( level + 2 );
409 // Reset the viewer setting the default image
410 GetGimmickView()->ClearSelection();
412 // Select all images if the selection is at series level
413 if (level==mLevelList.size()-2) SelectLowerLevel();
414 // Validate selected images if the selection is at image level
415 if (level==(mLevelList.size()-1)&&mProcess)
417 if(event.GetEventType()==10145)
419 ValidateSelectedImages (true);
423 ValidateSelectedImages (false);
428 //================================================================
430 //================================================================
431 void WxTreeView::SelectLowerLevel()
434 int level=mLevelList.size()-1;
437 item = GetCtrl(level)->GetNextItem(item,
442 if(item==(GetCtrl(level)->GetItemCount()-1))
450 GetCtrl(level)->SetItemState(item,wxLIST_STATE_SELECTED, wxLIST_MASK_STATE
451 | wxLIST_MASK_TEXT |wxLIST_MASK_IMAGE | wxLIST_MASK_DATA | wxLIST_MASK_WIDTH | wxLIST_MASK_FORMAT);
455 //================================================================
456 //================================================================
458 void WxTreeView::OnColClick(wxListEvent& event)
460 mColumnSelected=event.m_col;
462 clientpt.x = wxGetMousePosition().x - this->GetScreenPosition().x;
463 clientpt.y = wxGetMousePosition().y - this->GetScreenPosition().y;
464 senderCtrl = event.GetEventObject();
465 unsigned int level = 0;
466 for (level = 0; level<mLevelList.size(); ++level)
468 if ( GetCtrl(level) == senderCtrl ) break;
470 PopupMenu(menu, clientpt);
474 //================================================================
475 //================================================================
477 void WxTreeView::OnPopupFilter(wxCommandEvent& event)
480 GimmickDebugMessage(7,
481 "WxTreeView::OnEndLabelEdit"
483 wxObject* ctrl = event.GetEventObject();
484 unsigned int level = 0;
485 for (level = 0; level<mLevelList.size(); ++level)
487 if ( GetCtrl(level) == senderCtrl ) break;
489 std::string filter = crea::wx2std(wxGetTextFromUser(_T("Enter the filter to apply"), _T("Filter On Column")));
494 UpdateLevel(level+1);
496 std::vector<long> items;
501 it = GetCtrl(level)->GetNextItem(it,
506 long adr = GetCtrl(level)->GetItemData(it);
507 tree::Node* nod = ((ItemData*)adr)->node;
508 att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected]);
511 if(att.find(filter)>900)
523 items.push_back(it-del);
527 std::vector<long>::iterator iter;
528 for(iter=items.begin();iter!=items.end();++iter)
530 GetCtrl(level)->DeleteItem(*iter);
532 GetGimmickView()->ClearSelection();
534 //================================================================
536 //================================================================
537 void WxTreeView::OnPopupSort(wxCommandEvent& event)
540 unsigned int level = 0;
541 for (level = 0; level<mLevelList.size(); ++level)
543 if ( GetCtrl(level) == senderCtrl ) break;
545 if(event.GetId()==mAscendingID)
549 else if(event.GetId()==mDescendingID)
556 //================================================================
557 void WxTreeView::OnSort(int level)
559 //Obtain the column name and the level that needs to be organized
562 //Sets the data for the items to be sorted
565 int n = GetCtrl(level)->GetItemCount();
566 for (int i = 0; i < n; i++)
569 //Gets current item data
570 long adr = GetCtrl(level)->GetItemData(i);
572 //Extracts the node and the type of attribute
573 tree::Node* nod = ((ItemData*)adr)->node;
576 (*nod).GetAttributeDescriptor(mLevelList[level].key[mColumnSelected]).DecodeType(ty);
578 //Obtains the organizing attribute
579 att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected]);
581 char* d= new char [att.size()+1];
582 strcpy (d, att.c_str());
585 long* lp= new long[2];
589 //Sets it as the data
590 GetCtrl(level)->SetItemData(i,(long)lp);
597 GetCtrl(level)->SortItems(CompareFunctionInts, 0);
601 GetCtrl(level)->SortItems(CompareFunctionStrings, 0);
609 GetCtrl(level)->SortItems(CompareFunctionInts, 1);
613 GetCtrl(level)->SortItems(CompareFunctionStrings, 1);
617 //Resets original data
621 it = GetCtrl(level)->GetNextItem(it,
626 //Gets current item data, extracts the node and resets it
627 long item = GetCtrl(level)->GetItemData(it);
628 GetCtrl(level)->SetItemData(it,((long*)item)[0]);
629 //tree::Node* n= ((ItemData*)((long*)item)[0])->node;
635 //================================================================
636 void WxTreeView::ValidateSelectedImages(bool isSelection)
638 GimmickDebugMessage(7,
639 "WxTreeView::ValidateSelectedImages"
641 std::vector<tree::Node*> sel(GetSelected(mLevelList.size()+1));
642 GetGimmickView()->OnSelectionChange(sel,isSelection,(mLastSelected-1), mProcess);
645 //================================================================
648 //================================================================
649 void WxTreeView::GetNodes(std::vector<tree::Node*>& nodes, bool direction)
651 long item = mLastSelected;
652 int level=mLevelList.size()-1;
653 //Gets current item data
654 long adr = GetCtrl(level)->GetItemData(item);
656 tree::Node* nod = ((ItemData*)adr)->node;
661 item = GetCtrl(level)->GetNextItem(item,
666 item = GetCtrl(level)->GetNextItem(item,
673 if(GetCtrl(level)->GetItemState(item, wxLIST_STATE_SELECTED)==0 /*&& item!=0*/)
675 adr = GetCtrl(level)->GetItemData(item);
676 nod = ((ItemData*)adr)->node;
677 nodes.push_back(nod);
683 //================================================================
684 void WxTreeView::GetSelectedAsString(std::vector<std::string>&s)
686 int level=mLevelList.size();
687 std::vector<tree::Node*> sel=GetSelected(level+1);
688 std::vector<tree::Node*>::iterator i;
690 for (i=sel.begin(); i!=sel.end(); ++i)
692 std::string filename=(*i)->GetAttribute("FullFileName");
693 s.push_back(filename);
697 //================================================================
698 void WxTreeView::SetColor(int l, int item)
701 GetCtrl(l)->SetItemTextColour(item, wxColourDatabase().Find
702 (crea::std2wx(mColorPalette[colorId])));
703 GetCtrl(l)->SetItemState(item,wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED); /*
705 //Setting the color according to the parent
708 item.SetBackgroundColour
709 (wxColourDatabase().Find
710 (crea::std2wx(mColorPalette[colorId])));
713 (*j,wxColourDatabase().Find
714 (crea::std2wx(mColorPalette[colorId]))));
724 else if(l!=mLevelList.size()-1)
726 item.SetBackgroundColour(mColorMap[*i]);
727 mColorMap.insert(NodeColorPair(*j,mColorMap[*i]));
731 item.SetBackgroundColour(mColorMap[*i]);
734 //================================================================
735 void WxTreeView::CreateColorPalette()
737 GimmickDebugMessage(6,"WxTreeView::CreateColorPalette");
738 mColorPalette.push_back("WHITE");
739 mColorPalette.push_back("LIGHT GREY");
740 mColorPalette.push_back("AQUAMARINE");
741 mColorPalette.push_back("MEDIUM FOREST GREEN");
742 mColorPalette.push_back("INDIAN RED");
743 mColorPalette.push_back("KHAKI");
744 mColorPalette.push_back("ORANGE");
745 mColorPalette.push_back("LIGHT BLUE");
746 mColorPalette.push_back("LIGHT STEEL BLUE");
747 mColorPalette.push_back("PINK");
748 mColorPalette.push_back("PLUM");
749 mColorPalette.push_back("PURPLE");
750 mColorPalette.push_back("RED");
751 mColorPalette.push_back("SEA GREEN");
752 mColorPalette.push_back("SIENNA");
753 mColorPalette.push_back("SKY BLUE");
754 mColorPalette.push_back("SLATE BLUE");
755 mColorPalette.push_back("SPRING GREEN");
756 mColorPalette.push_back("TAN");
757 mColorPalette.push_back("THISTLE");
758 mColorPalette.push_back("TURQUOISE");
759 mColorPalette.push_back("VIOLET");
760 mColorPalette.push_back("VIOLET RED");
761 mColorPalette.push_back("WHEAT");
762 mColorPalette.push_back("YELLOW");
763 mColorPalette.push_back("YELLOW GREEN");
764 mColorPalette.push_back("BLUE");
765 mColorPalette.push_back("BLUE VIOLET");
766 mColorPalette.push_back("BROWN");
767 mColorPalette.push_back("CADET BLUE");
768 mColorPalette.push_back("CORAL");
769 mColorPalette.push_back("CORNFLOWER BLUE");
770 mColorPalette.push_back("CYAN");
771 mColorPalette.push_back("DARK GREY");
772 mColorPalette.push_back("DARK GREEN");
773 mColorPalette.push_back("DARK OLIVE GREEN");
774 mColorPalette.push_back("DARK ORCHID");
775 mColorPalette.push_back("DARK SLATE BLUE");
776 mColorPalette.push_back("DARK SLATE GREY");
777 mColorPalette.push_back("DARK TURQUOISE");
778 mColorPalette.push_back("FIREBRICK");
779 mColorPalette.push_back("FOREST GREEN");
780 mColorPalette.push_back("GOLD");
781 mColorPalette.push_back("GOLDENROD");
782 mColorPalette.push_back("GREY");
783 mColorPalette.push_back("GREEN");
784 mColorPalette.push_back("GREEN YELLOW");
785 mColorPalette.push_back("LIME GREEN");
786 mColorPalette.push_back("MAGENTA");
787 mColorPalette.push_back("MAROON");
788 mColorPalette.push_back("MEDIUM AQUAMARINE");
789 mColorPalette.push_back("MEDIUM BLUE");
790 mColorPalette.push_back("MEDIUM GOLDENROD");
791 mColorPalette.push_back("MEDIUM ORCHID");
792 mColorPalette.push_back("MEDIUM SEA GREEN");
793 mColorPalette.push_back("MEDIUM SLATE BLUE");
794 mColorPalette.push_back("MEDIUM SPRING GREEN");
795 mColorPalette.push_back("MEDIUM TURQUOISE");
796 mColorPalette.push_back("MEDIUM VIOLET RED");
797 mColorPalette.push_back("MIDNIGHT BLUE");
798 mColorPalette.push_back("NAVY");
799 mColorPalette.push_back("ORANGE RED");
800 mColorPalette.push_back("ORCHID, PALE GREEN");
801 mColorPalette.push_back("STEEL BLUE");
802 mColorPalette.push_back("BLACK");
806 //================================================================
807 BEGIN_EVENT_TABLE(WxTreeView, wxPanel)
809 EVT_SIZE(MyFrame::OnSize)
811 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
812 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
813 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
814 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
815 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
816 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
817 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
818 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
819 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
820 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
822 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
823 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
824 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
825 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
826 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
827 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
828 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
829 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
830 EVT_MENU(LIST_SORT, MyFrame::OnSort)
831 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
832 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
833 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
834 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
835 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
836 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
837 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
838 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
839 EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
841 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
842 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
845 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
846 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
847 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
849 EVT_LIST_BEGIN_LABEL_EDIT(-1, WxTreeView::OnBeginLabelEdit)
850 EVT_LIST_END_LABEL_EDIT(-1, WxTreeView::OnEndLabelEdit)
852 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
853 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
854 #if WXWIN_COMPATIBILITY_2_4
855 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
856 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
859 EVT_LIST_ITEM_SELECTED(-1, WxTreeView::OnSelectedChanged)
861 EVT_LIST_ITEM_DESELECTED(-1, WxTreeView::OnSelectedChanged)
863 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
864 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
865 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
867 EVT_LIST_COL_RIGHT_CLICK(-1, WxTreeView::OnColClick)
869 EVT_LIST_COL_CLICK(-1, WxTreeView::OnColClick)
871 //EVT_LEFT_DOWN(WxTreeView::OnMouseClick)
873 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
874 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
875 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
877 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
880 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
882 EVT_CHAR(MyListCtrl::OnChar)
884 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
888 } // EO namespace creaImageIO