-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.py
274 lines (221 loc) · 6.8 KB
/
history.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import wx
from util.observerlist import ObserverList
from gui.guiutil import FreezeUI
import events
import guiconfig
from swlib import pysw
from util import osutils
from backend.bibleinterface import biblemgr
use_history_as_tree = False
class HistoryItem(object):
def __init__(self, parent, ref):
self.parent = parent
self.children = []
self.ref = ref
self.ref_to_scroll_to = None
def trim(self, child):
assert child in self.children, "Can't trim item when not in children"
self.remove(child)
@property
def user_ref(self):
return pysw.internal_to_user(self.ref)
class History(object):
"""Manages history
>>> from history import *
>>> h = History()
>>> h.can_forward()
False
>>> h.can_back()
False
>>> h.new_location("first location")
>>> h.can_back()
True
>>> h.can_forward()
False
>>> h.new_location("second location")
>>> h.pprint()
first location
> second location
>>> h.can_back()
True
>>> h.back()
>>> h.pprint()
> first location
second location
>>> h.can_forward()
True
>>> h.new_location("third location")
>>> h.can_forward()
False
>>> h.can_back()
True
>>> h.pprint()
first location
second location
> third location
>>> h.back()
>>> h.pprint()
> first location
second location
third location
>>> h.forward()
>>> h.pprint()
first location
second location
> third location
"""
def __init__(self):
self.history = HistoryItem(None, None)
self.current_item = self.history
self.on_history_changed = ObserverList()
def back(self):
self.current_item = self.current_item.parent
self.on_history_changed(self.current_item)
return self.current_item
def forward(self):
self.current_item = self.current_item.children[-1]
self.on_history_changed(self.current_item)
return self.current_item
def can_back(self):
return self.current_item.parent is not self.history
def can_forward(self):
return bool(self.current_item.children)
def new_location(self, new_location):
if new_location == self.current_item.ref:
return
history_item = HistoryItem(self.current_item, new_location)
self.current_item.children.append(history_item)
self.current_item = history_item
self.on_history_changed(self.current_item)
def before_navigate(self):
"""Saves the current position and settings so that the curent
position can be restored when the history is used.
The settings must be saved since it makes no sense to return to the
current position if the settings have changed.
"""
current_reference, ref_to_scroll_to = guiconfig.mainfrm.bibletext.GetCurrentReferenceAndPosition()
if current_reference:
self.current_item.ref = current_reference
self.current_item.ref_to_scroll_to = ref_to_scroll_to
def clear(self):
self.history = HistoryItem(None, None)
self.current_item = self.history
def go(self, direction):
self.before_navigate()
if direction < 0:
return self.back()
if direction > 0:
return self.forward()
def pprint(self, item=None, level=0):
if item is None:
item = self.history
for child in item.children:
print "\t"*level,
if child == self.current_item:
print ">",
print child.ref
self.pprint(child, level+1)
class HistoryTree(wx.TreeCtrl):
def __init__(self, parent, history):
self.history = history
style = wx.TR_HIDE_ROOT|wx.TR_DEFAULT_STYLE
if not use_history_as_tree:
style |= wx.TR_NO_LINES
self.current_tree_item = None
super(HistoryTree, self).__init__(parent, style=style)
self.history.on_history_changed += \
lambda item:wx.CallAfter(self.rebuild_tree, item)
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.on_tree_selected)
self.rebuild_tree()
def on_tree_selected(self, event):
item = event.GetItem()
history_item = self.GetPyData(item)
parent = self.history.current_item
self.history.before_navigate()
# if it is in our back list, go back to it
while parent:
if history_item == parent:
self.history.current_item = history_item
break
parent = parent.parent
else:
child = self.history.current_item
# if it is in our forward list, go forward to it
while child.children:
child = child.children[-1]
if history_item == child:
self.history.current_item = history_item
break
else:
self.history.new_location(history_item.ref)
guiconfig.mainfrm.set_bible_ref(history_item.ref, source=events.HISTORY,
ref_to_scroll_to=history_item.ref_to_scroll_to
)
wx.CallAfter(self.rebuild_tree)
def create_item(self, parent, item, build_recursively=True):
new_tree_item = self.AppendItem(parent, text=item.user_ref)
if item == self.history.current_item:
self.SetItemBold(new_tree_item)
self.current_tree_item = new_tree_item
self.SetPyData(new_tree_item, item)
if build_recursively:
self.build_tree(item, new_tree_item)
return new_tree_item
def build_tree(self, history_item=None, tree_item=None):
if tree_item is None:
tree_item = self.AddRoot("History")
if history_item is None:
history_item = self.history.history
while history_item.children:
# don't display extra ones for now...
if use_history_as_tree:
for item in history_item.children[:-1]:
self.create_item(tree_item, item)
# Put the last item as a sibling, not a child.
# this is good way to do it
history_item = history_item.children[-1]
# if no parent, it is a root, so just do as sibling
p = self.GetItemParent(tree_item) or tree_item
tree_item = self.create_item(p, history_item,
build_recursively=False)
def rebuild_tree(self, item=None):
self.Unbind(wx.EVT_TREE_SEL_CHANGED)
if osutils.is_gtk():
freeze = None
else:
freeze = FreezeUI(self)
self.DeleteAllItems()
self.build_tree()
root = self.GetRootItem()
id, cookie = self.GetFirstChild(root)
while id:
self.ExpandAllChildren(id)
id, cookie = self.GetNextChild(root, cookie)
# Stop freezing the UI so that we can scroll to the current item.
del freeze
if self.current_tree_item:
self.ScrollTo(self.current_tree_item)
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.on_tree_selected)
def _test_HistoryTree():
a = wx.App(0)
f = wx.Frame(None)
h = History()
h.new_location("Gen 3:16")
h.new_location("Gen 3:17")
h.new_location("Gen 3:18")
h.new_location("Gen 3:19")
h.new_location("Gen 3:20")
h.back()
h.back()
h.back()
h.new_location("Gen 3:20")
for x in range(1, 30):
h.new_location("Gen 3:%s" % x)
ht = HistoryTree(f, h)
f.Show()
a.MainLoop()
def _test():
import doctest
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)
if __name__ == '__main__':
_test_HistoryTree()