forked from bpbible/bpbible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionarylist.py
328 lines (249 loc) · 8.92 KB
/
dictionarylist.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import wx
import wx.calendar
from backend.bibleinterface import biblemgr
from util.observerlist import ObserverList
from gui.virtuallist import VirtualListBox
from gui.guiutil import bmp
from util import osutils
from util.unicode import to_str
from gui import fonts
import guiconfig
class Upper(object):
def __init__(self, object):
self.object = object
def __len__(self):
return len(self.object)
def __getitem__(self, item):
return self.object[item].upper()
def is_date_conversion_supported():
# vietnamese under windows doesn't complete the loop
return wx.DateTime.Now().ParseFormat(wx.DateTime.Now().Format("%B %d"), "%B %d") != -1
class DictionaryList(VirtualListBox):
def __init__(self, parent, book):
super(DictionaryList, self).__init__(parent)
self.book = None
def set_book(self, book):
self.book = book
b = wx.BusyInfo("Getting dictionary topic list...")
self.topics = book.GetTopics(user_output=True)
self._upper_topics = Upper(self.topics)
self.set_data(self.topics)
def choose_item(self, text, update_text_entry_value=False):
if not self.book.mod:
return
idx = self.topics.mod.getEntryForKey(
to_str(text, self.topics.mod)
)
idx = min(len(self.topics)-1, idx)
if idx >= 0:
self.EnsureVisible(idx)
self.Select(idx)
return idx
# we want users to be able to view the 29 february even when not in a leap
# year
leap_year_default_date = wx.DateTime()
leap_year_default_date.ParseDate("1 Jan 2008")
def date_to_mmdd(date, return_formatted=True):
dt = wx.DateTime()
if not return_formatted and not is_date_conversion_supported():
try:
month, day = map(int, date.split(".", 1))
except ValueError:
pass
else:
dt.Set(day, month-1, 2008)
return dt
# tack the following bits on the end to see if they help give us dates
# the second is February -> February 1
# the third is 29 February -> 29 February 2008
additions = ["", " 1", " 2008"]
# turn off logging to avoid debug messages
ol = wx.Log.GetLogLevel()
wx.Log.SetLogLevel(0)
try:
for addition in additions:
ansa = dt.ParseDate(date + addition)
if ansa != -1:
if return_formatted:
return dt.Format("%m.%d")
return dt
finally:
# now turn it on again
wx.Log.SetLogLevel(ol)
return None
#def mmdd_to_date(date):
# if not is_date_conversion_supported():
# return None
#
# dt = wx.DateTime()
# ansa = dt.ParseFormat(date, "%m.%d", leap_year_default_date)
# if ansa == -1:
# return None
#
# return dt.Format("%B ") + str(dt.Day)
class TextEntry(wx.Panel):
def __init__(self, parent):
super(TextEntry, self).__init__(parent)
self.text = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER)
self.calendar_pic = bmp("calendar_view_day.png")
self.calendar = wx.BitmapButton(
self,
bitmap=self.calendar_pic
)
#self.calendar.SetBezelWidth(1)
w, h = self.text.Size[1], self.text.Size[1]
self.calendar.SetSize((w, h))
self.calendar.MinSize = self.calendar.Size
self.calendar.Bind(wx.EVT_BUTTON, self.show_popup)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.text, 1, wx.GROW)
self.calendar_sizer_item = sizer.Add(self.calendar, 0,
flag=
wx.FIXED_MINSIZE |
wx.ALIGN_CENTER |
wx.SHAPED
)
self.SetSizer(sizer)
self.show_calendar(False)
def show_popup(self, event):
def on_cal_changed(event):
dt = event.GetDate()
if is_date_conversion_supported():
self.Parent.choose_item(dt.Format("%B ") + str(dt.Day))
else:
self.Parent.choose_item(dt.Format("%m.%d"))
def on_cal(event):
win.Destroy()
win = wx.PopupTransientWindow(self,
wx.NO_BORDER)
now_date = date_to_mmdd(self.text.Value, return_formatted=False)
if now_date is None:
now_date = wx.DateTime_Now()
style = 0
if osutils.is_msw():
style = wx.calendar.CAL_SEQUENTIAL_MONTH_SELECTION
panel = wx.Panel(win)
cal = wx.calendar.CalendarCtrl(panel, -1, now_date, pos=(1,1),
style=wx.RAISED_BORDER|style
)
panel.ClientSize = cal.Size + (1,1)
cal.Bind(wx.calendar.EVT_CALENDAR_SEL_CHANGED, on_cal_changed)
cal.Bind(wx.calendar.EVT_CALENDAR, on_cal)
size_combo = 0
if not style & wx.calendar.CAL_SEQUENTIAL_MONTH_SELECTION:
# hide the spin control
for child in panel.Children:
if isinstance(child, wx.SpinCtrl):
child.Hide()
# we will shorten ourselves by this amount
size_combo = child.Size[1] + 6
# make combo fill up rest of space
for child in panel.Children:
if isinstance(child, wx.ComboBox):
child.Size = cal.Size[0], -1
win.Size = panel.GetSize() - (0, size_combo)
# Show the popup right below or above the button
# depending on available screen space...
btn = event.GetEventObject()
pos = btn.ClientToScreen((btn.Size[0], 0))
win.Position(pos, (-btn.Size[0], btn.Size[1]))
win.Popup()
def show_calendar(self, visible=True):
self.is_calendar = visible
self.Sizer.Show(self.calendar, visible)
self.Sizer.Layout()
def get_value(self):
if not self.is_calendar:
return self.text.GetValue()
else:
value = self.text.GetValue()
mm_dd = date_to_mmdd(value)
if not mm_dd:
return value
return mm_dd
def set_value(self, text):
self.text.ChangeValue(text)
class DictionarySelector(wx.Panel):
def __init__(self, parent, book):
super(DictionarySelector, self).__init__(parent)
self.text_entry = TextEntry(self)
self.list = DictionaryList(self, book)
self.set_book(book)
self.timer = wx.Timer(self)
self.item_to_focus_on = None
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.text_entry, 0, wx.GROW)
sizer.Add(self.list, 1, wx.GROW)
self.text_entry.text.Bind(wx.EVT_TEXT, self.on_text)
self.list.Bind(wx.EVT_LIST_ITEM_SELECTED, self.on_list)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
width = 200
self.SetSizerAndFit(sizer)
self.item_changed_observers = ObserverList()
fonts.fonts_changed += self.set_font
guiconfig.mainfrm.on_close += lambda:\
fonts.fonts_changed.remove(self.set_font)
def set_font(self):
if self.list.book.mod is None:
return
font = fonts.get_module_gui_font(self.list.book.mod)
self.list.Font = font
self.text_entry.text.Font = font
self.Layout()
def on_text(self, event):
self.item_to_focus_on = self.text_entry
self.change_selected_text(is_user_typing=True)
def change_selected_text(self, is_user_typing=False, update_text_entry_value=False, suppress_reference_change=False):
# unbind the selected event so that we don't go into an infinite loop
# TODO: check whether this is really necessary
self.list.Unbind(wx.EVT_LIST_ITEM_SELECTED)
idx = self.list.choose_item(self.GetValue().upper(), update_text_entry_value=update_text_entry_value)
if idx >= 0 and update_text_entry_value:
self.text_entry.set_value(self.list.GetItemText(idx))
self.list.Bind(wx.EVT_LIST_ITEM_SELECTED, self.on_list)
if is_user_typing:
self.timer.Start(200, oneShot=True)
else:
self.item_changed(suppress_reference_change=suppress_reference_change)
def on_timer(self, event):
self.item_changed()
def on_list(self, event):
self.item_to_focus_on = self.list
text = self.list.GetItemText(event.m_itemIndex)
self.choose_item(text)
def choose_item(self, text, update_text_entry_value=False, suppress_reference_change=False):
# change the value (doesn't fire an event)
self.text_entry.set_value(text)
# scroll to the correct entry, and fire off an item_changed
wx.CallAfter(lambda: self.change_selected_text(update_text_entry_value=update_text_entry_value, suppress_reference_change=suppress_reference_change))
def item_changed(self, suppress_reference_change=False):
self.item_changed_observers(suppress_reference_change=suppress_reference_change)
if self.item_to_focus_on is self.text_entry:
self.item_to_focus_on.SetFocus()
self.item_to_focus_on = None
def GetValue(self):
return self.text_entry.get_value()
def set_book(self, book):
was_devotion = self.text_entry.is_calendar
self.text_entry.show_calendar(book.has_feature("DailyDevotion"))
self.list.set_book(book)
self.set_font()
# if we are changing to a devotion, and weren't a devotion,
# set it to today
if book.has_feature("DailyDevotion") and not was_devotion:
dt = wx.DateTime.Today()
if is_date_conversion_supported():
self.choose_item(dt.Format("%B ") + str(dt.Day))
else:
self.choose_item(dt.Format("%m.%d"))
else:
self.choose_item(self.text_entry.text.Value)
if __name__ == '__main__':
a=wx.App(0)
d=wx.Dialog(None, style=wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE)
biblemgr.dictionary.SetModule("BibleCompanion")
l=DictionarySelector(d, biblemgr.dictionary)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(l, 1, wx.GROW)
d.SetSizer(sizer)
d.ShowModal()