-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_tree.py
243 lines (192 loc) · 6.68 KB
/
module_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from collections import defaultdict
import wx
from backend.bibleinterface import biblemgr
from swlib.pysw import SW
from gui.filterable_tree import TreeItem, FilterableTree
from util.observerlist import ObserverList
from util.unicode import to_unicode
from util.debug import dprint, ERROR
from moduleinfo import ModuleInfo
from util import languages
import wx.lib.agw.customtreectrl
class ModuleTree(FilterableTree):
def __init__(self, parent):
super(ModuleTree, self).__init__(parent, None)
self.on_module_choice = ObserverList()
self.on_category_choice = ObserverList()
self.module_types = (
(_("Bibles"), biblemgr.bible),
(_("Commentaries"), biblemgr.commentary),
(_("Dictionaries"), biblemgr.dictionary),
(_("Other books"), biblemgr.genbook),
(_("Daily Devotional"), biblemgr.daily_devotional),
(_("Harmonies"), biblemgr.harmony),
)
self.extra_categories = (
_("Daily Devotional"),
_("Maps"),
_("Images"),
_("Harmonies"),
)
self.all_categories = (
_("Bibles"),
_("Commentaries"),
_("Dictionaries"),
_("Harmonies"),
_("Maps"),
_("Images"),
_("Daily Devotional"),
_("Other books"),
)
self.on_selection += self.on_version_tree
self.recreate()
@property
def blank_text(self):
return _("Find Book...")
def bind_events(self):
super(ModuleTree, self).bind_events()
# ### Nasty hack ###
# It seems that whenever all items are deleted, we get called by
# sel_changed in a bad state, from which calling GetPyData crashes it.
# So whenever an item is deleted, unbind all events.
self.tree.Bind(wx.EVT_TREE_DELETE_ITEM, lambda evt:(evt.Skip(),
self.unbind_events()))
self.tree.Bind(wx.EVT_TREE_ITEM_MENU, self.version_tree_menu)
def unbind_events(self):
if not super(ModuleTree, self).unbind_events():
return
self.tree.Unbind(wx.EVT_TREE_ITEM_MENU)
self.tree.Unbind(wx.EVT_TREE_DELETE_ITEM)
def on_version_tree(self, item):
if not item:
return
item_data = self.tree.GetPyData(item).data
if isinstance(item_data, SW.Module):
self.on_module_choice(item_data)
else:
self.on_category_choice(item_data)
def recreate(self):
self.model = TreeItem("Hidden root")
self.add_first_level_groups()
for tree_item in self.model.children:
self.add_children(tree_item)
if self.search.Value:
self.filter(self.search.Value)
else:
self.create()
def version_tree_tooltip(self, event):
item = event.GetItem()
data = self.tree.GetPyData(item)
if isinstance(data.data, SW.Module):
event.SetToolTip(to_unicode(data.data.Description(), data.data))
def version_tree_menu(self, event):
item = event.GetItem()
if not item:
return
data = self.tree.GetPyData(item).data
if not isinstance(data, SW.Module):
return
menu = wx.Menu()
self.add_menu_items(data, menu)
self.tree.PopupMenu(menu, event.GetPoint())
menu.Destroy()
def add_menu_items(self, data, menu):
def make_event(module):
def show_information(event):
ModuleInfo(self, module).ShowModal()
return show_information
item = menu.Append(
wx.ID_ANY,
_("Show information for %s") % data.Name()
)
menu.Bind(wx.EVT_MENU, make_event(data), item)
def add_first_level_groups(self):
modules = defaultdict(lambda: [])
for book_category, book in self.module_types:
for module in book.GetModules():
category = module.getConfigEntry("Category")
if category not in self.extra_categories:
category = book_category
modules[category].append(module)
for category in self.all_categories:
if modules[category]:
# XXX: Will this do horrible things and hold onto reference to all the modules for ever and a day?
self.model.add_child(category, data=modules[category], filterable=False)
def add_children(self, tree_item):
for module in tree_item.data:
self.add_module(tree_item, module)
def add_module(self, tree_item, module, inactive_description=""):
if self.hide_module(module):
return
text = "%s - %s" % (
module.Name(), to_unicode(module.Description(), module))
if biblemgr.all_modules[module.Name()] != module:
text += inactive_description
tree_item.add_child(text, data=module)
def hide_module(self, module):
return (module.Name() in biblemgr.headwords_modules)
class PathModuleTree(ModuleTree):
def CreateTreeCtrl(self, parent, style):
tree = wx.lib.agw.customtreectrl.CustomTreeCtrl(parent,
style=style ^ wx.SUNKEN_BORDER,
agwStyle=wx.TR_HAS_BUTTONS |
wx.TR_LINES_AT_ROOT |
wx.TR_HIDE_ROOT |
wx.TR_HAS_VARIABLE_ROW_HEIGHT |
wx.lib.agw.customtreectrl.TR_AUTO_CHECK_CHILD |
wx.lib.agw.customtreectrl.TR_AUTO_CHECK_PARENT
)
#tree.EnableSelectionGradient(False)
#tree.EnableSelectionVista(True)
return tree
def AppendItemToTree(self, parent, text):
return self.tree.AppendItem(parent, text, ct_type=1)
def add_first_level_groups(self):
for path, mgr, modules in reversed(biblemgr.mgrs):
# If all the modules in a path are hidden, then showing that path
# looks wrong.
if not all(self.hide_module(module) for name, module in modules):
self.model.add_child(path, data=mgr)
def add_children(self, tree_item):
for path, mgr, modules in reversed(biblemgr.mgrs):
if mgr == tree_item.data:
for modname, mod in sorted(modules, key=lambda x:x[0].lower()):
self.add_module(tree_item, mod,
"\nThis book is not active as it "
"is shadowed by a book in a different path")
break
else:
dprint(ERROR, "Did not find mgr in list", mgr)
class LanguageModuleTree(ModuleTree):
def add_first_level_groups(self):
def module_lang(x):
if x == "Greek":
return "grc"
if x == "Hebrew":
return "he"
return module.Lang()
language_mappings = {}
self.data = {}
for module in biblemgr.modules.values() + ["Greek", "Hebrew"]:
lang = module_lang(module)
if lang not in language_mappings:
language_mappings[lang] = \
languages.get_language_description(lang)
d=self.data.setdefault(lang, [])
if isinstance(module, SW.Module):
d.append(module)
for lang, mapping in sorted(language_mappings.items(),
key=lambda (lang, mapping): mapping):
self.model.add_child(mapping, data=lang)
def add_children(self, tree_item):
for mod in sorted(self.data[tree_item.data],
key=lambda mod:mod.Name().lower()):
self.add_module(tree_item, mod)
if __name__ == '__main__':
from util import i18n
i18n.initialize()
a = wx.App(0)
f = wx.Frame(None)
tree = PathModuleTree(f)
f.Show()
a.MainLoop()