]>
Commit | Line | Data |
---|---|---|
be05b434 RD |
1 | import wx |
2 | import data | |
3 | ||
4 | class TestFrame(wx.Frame): | |
5 | def __init__(self): | |
6 | wx.Frame.__init__(self, None, title="tree: misc tests", size=(400,500)) | |
7 | ||
8 | # Create an image list | |
9 | il = wx.ImageList(16,16) | |
10 | ||
11 | # Get some standard images from the art provider and add them | |
12 | # to the image list | |
13 | self.fldridx = il.Add( | |
14 | wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, (16,16))) | |
15 | self.fldropenidx = il.Add( | |
16 | wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, (16,16))) | |
17 | self.fileidx = il.Add( | |
18 | wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, (16,16))) | |
19 | ||
20 | ||
21 | # Create the tree | |
22 | self.tree = wx.TreeCtrl(self, style=wx.TR_DEFAULT_STYLE | wx.TR_EDIT_LABELS) | |
23 | ||
24 | # Give it the image list | |
25 | self.tree.AssignImageList(il) | |
26 | ||
27 | ||
28 | # Add a root node and assign it some images | |
29 | root = self.tree.AddRoot("wx.Object") | |
30 | self.tree.SetItemPyData(root, None) | |
31 | self.tree.SetItemImage(root, self.fldridx, | |
32 | wx.TreeItemIcon_Normal) | |
33 | self.tree.SetItemImage(root, self.fldropenidx, | |
34 | wx.TreeItemIcon_Expanded) | |
35 | ||
36 | # Add nodes from our data set | |
37 | self.AddTreeNodes(root, data.tree) | |
38 | ||
39 | # Bind some interesting events | |
40 | self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnItemExpanded, self.tree) | |
41 | self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnItemCollapsed, self.tree) | |
42 | self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.tree) | |
43 | self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated, self.tree) | |
44 | ||
45 | self.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, self.OnBeginEdit, self.tree) | |
46 | ||
47 | ||
48 | # Expand the first level | |
49 | self.tree.Expand(root) | |
50 | ||
51 | ||
52 | menu = wx.Menu() | |
53 | mi = menu.Append(-1, "Edit Item") | |
54 | self.Bind(wx.EVT_MENU, self.OnEditItem, mi) | |
55 | mi = menu.Append(-1, "Sort Children") | |
56 | self.Bind(wx.EVT_MENU, self.OnSortChildren, mi) | |
57 | mi = menu.Append(-1, "Delete Children") | |
58 | self.Bind(wx.EVT_MENU, self.OnDeleteChildren, mi) | |
59 | mi = menu.Append(-1, "Find all Children") | |
60 | self.Bind(wx.EVT_MENU, self.OnFindChildren, mi) | |
61 | mb = wx.MenuBar() | |
62 | mb.Append(menu, "Tests") | |
63 | self.SetMenuBar(mb) | |
64 | ||
65 | ||
66 | def AddTreeNodes(self, parentItem, items): | |
67 | """ | |
68 | Recursively traverses the data structure, adding tree nodes to | |
69 | match it. | |
70 | """ | |
71 | for item in items: | |
72 | if type(item) == str: | |
73 | newItem = self.tree.AppendItem(parentItem, item) | |
74 | self.tree.SetItemPyData(newItem, None) | |
75 | self.tree.SetItemImage(newItem, self.fileidx, | |
76 | wx.TreeItemIcon_Normal) | |
77 | else: | |
78 | newItem = self.tree.AppendItem(parentItem, item[0]) | |
79 | self.tree.SetItemPyData(newItem, None) | |
80 | self.tree.SetItemImage(newItem, self.fldridx, | |
81 | wx.TreeItemIcon_Normal) | |
82 | self.tree.SetItemImage(newItem, self.fldropenidx, | |
83 | wx.TreeItemIcon_Expanded) | |
84 | ||
85 | self.AddTreeNodes(newItem, item[1]) | |
86 | ||
87 | ||
88 | def GetItemText(self, item): | |
89 | if item: | |
90 | return self.tree.GetItemText(item) | |
91 | else: | |
92 | return "" | |
93 | ||
94 | def OnItemExpanded(self, evt): | |
95 | print "OnItemExpanded: ", self.GetItemText(evt.GetItem()) | |
96 | ||
97 | def OnItemCollapsed(self, evt): | |
98 | print "OnItemCollapsed:", self.GetItemText(evt.GetItem()) | |
99 | ||
100 | def OnSelChanged(self, evt): | |
101 | print "OnSelChanged: ", self.GetItemText(evt.GetItem()) | |
102 | ||
103 | def OnActivated(self, evt): | |
104 | print "OnActivated: ", self.GetItemText(evt.GetItem()) | |
105 | ||
106 | ||
107 | def OnBeginEdit(self, evt): | |
108 | print "OnBeginEdit: ", self.GetItemText(evt.GetItem()) | |
109 | # we can prevent nodes from being edited, for example let's | |
110 | # not let the root node be edited... | |
111 | item = evt.GetItem() | |
112 | if item == self.tree.GetRootItem(): | |
113 | evt.Veto() | |
114 | print "*** Edit was vetoed!" | |
115 | ||
116 | ||
117 | def OnEditItem(self, evt): | |
118 | item = self.tree.GetSelection() | |
119 | if item: | |
120 | self.tree.EditLabel(item) | |
121 | ||
122 | def OnSortChildren(self, evt): | |
123 | item = self.tree.GetSelection() | |
124 | if item: | |
125 | self.tree.SortChildren(item) | |
126 | ||
127 | def OnDeleteChildren(self, evt): | |
128 | item = self.tree.GetSelection() | |
129 | if item: | |
130 | self.tree.DeleteChildren(item) | |
131 | ||
132 | def OnFindChildren(self, evt): | |
133 | item = self.tree.GetSelection() | |
134 | if item: | |
135 | self.PrintAllItems(item) | |
136 | ||
137 | def PrintAllItems(self, parent, indent=0): | |
138 | text = self.tree.GetItemText(parent) | |
139 | print ' ' * indent, text | |
140 | indent += 4 | |
141 | item, cookie = self.tree.GetFirstChild(parent) | |
142 | while item: | |
143 | if self.tree.ItemHasChildren(item): | |
144 | self.PrintAllItems(item, indent) | |
145 | else: | |
146 | text = self.tree.GetItemText(item) | |
147 | print ' ' * indent, text | |
148 | item, cookie = self.tree.GetNextChild(parent, cookie) | |
149 | ||
150 | ||
151 | app = wx.PySimpleApp(redirect=True) | |
152 | frame = TestFrame() | |
153 | frame.Show() | |
154 | app.MainLoop() | |
155 |