forked from bpbible/bpbible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic_selector.py
203 lines (163 loc) · 6.16 KB
/
topic_selector.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
"""\
The selector that is used to select a topic from a list of topics.
"""
import wx
import wx.lib.mixins.listctrl as listmix
from passage_list import get_primary_passage_list_manager
from util.observerlist import ObserverList
class TopicListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
def __init__(self, parent, ID=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0):
wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
listmix.ListCtrlAutoWidthMixin.__init__(self)
class TopicSelector(wx.TextCtrl):
"""This control is used to select a topic.
It uses a filterable drop down list to suggest topics, somewhat similar
to the Firefox URL suggestion list.
"""
def __init__(self, parent):
super(TopicSelector, self).__init__(parent, style=wx.TE_PROCESS_ENTER)
self._manager = get_primary_passage_list_manager()
self._dropdown = wx.PopupWindow(self)
self._setup_list()
self._selected_topic = None
self.topic_changed_observers = ObserverList()
# Called when return is pressed and the drop down is not shown.
self.return_pressed_observers = ObserverList()
self._bind_events()
self.selected_topic = self._manager
def maybe_create_topic_from_text(self):
"""If the text in the text box is not the currently selected topic,
then create a new topic with that name.
"""
text = self.GetValue()
if text.lower() == self.selected_topic.full_name.lower():
return
self.selected_topic = self._manager.find_or_create_topic(text)
def get_selected_topic(self):
return self._selected_topic
def set_selected_topic(self, topic):
selected_topic_changed = (topic is not self._selected_topic)
self._selected_topic = topic
self._update_topic_text()
if selected_topic_changed:
self.topic_changed_observers(topic)
selected_topic = property(get_selected_topic, set_selected_topic,
doc="The currently selected topic for the control.")
def _bind_events(self):
self.Bind(wx.EVT_TEXT, self._on_text_changed)
self.Bind(wx.EVT_KEY_DOWN, self._on_key_down)
self.Bind(wx.EVT_SET_FOCUS, self._on_focus_got)
self.Bind(wx.EVT_KILL_FOCUS, self._on_focus_lost)
def _setup_list(self):
self._topic_list = TopicListCtrl(self._dropdown,
pos=(0, 0),
style=wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL)
self._topic_list.Bind(wx.EVT_LEFT_DOWN, self._on_list_clicked)
def _setup_dropdown_data(self):
filter = self.GetValue()
if filter:
if ">" in filter:
filtered_words = [filter.lower()]
else:
filtered_words = [word.strip() for word in filter.lower().split()
if (word and word != ">")]
filtered_topics = [(name, index) for index, (name, _) in enumerate(self._topics)
if all(filtered_word in name.lower() for filtered_word in filtered_words)]
else:
filtered_topics = [(name, index) for index, (name, _) in enumerate(self._topics)]
if not filtered_topics:
return False
longest_topic = max(len(name) for name, _ in filtered_topics)
width = self.GetSize()[0]
width = max(self._topic_list.GetCharWidth() * (longest_topic + 10), width)
height = self._topic_list.GetCharHeight() * (min(len(filtered_topics), 7) + 2)
size = (width, height)
self._topic_list.SetSize(size)
self._dropdown.SetClientSize(size)
self._topic_list.DeleteAllColumns()
self._topic_list.DeleteAllItems()
self._topic_list.InsertColumn(0, "")
for index, (name, index2) in enumerate(filtered_topics):
self._topic_list.InsertStringItem(index, name)
self._topic_list.SetItemData(index, index2)
self._topic_list._doResize()
return True
def _update_topics(self):
"""Updates the list of topics used in the dropdown.
XXX: This list is rebuilt far too frequently. We should be more
clever about rebuilding it.
"""
self._topics = []
self._get_topics(self._manager)
def _get_topics(self, topic):
if topic.is_special_topic:
return
self._topics.append((topic.full_name, topic))
for subtopic in topic.subtopics:
self._get_topics(subtopic)
def _update_topic_text(self):
if self._selected_topic is None:
self.ChangeValue(_("None"))
else:
self.ChangeValue(self._selected_topic.full_name)
self.SetInsertionPoint(0)
def _on_text_changed(self, event):
self._show_dropdown()
def _on_key_down(self, event):
keycode = event.GetKeyCode()
selection = self._topic_list.GetFirstSelected()
if keycode == wx.WXK_RETURN and not self._dropdown.IsShown():
self.return_pressed_observers()
return
if not self._dropdown.IsShown():
event.Skip()
return
if keycode == wx.WXK_DOWN:
if selection + 1 < self._topic_list.GetItemCount():
self._topic_list.Select(selection + 1)
self._topic_list.EnsureVisible(selection + 1)
elif keycode == wx.WXK_UP:
if selection > 0 :
self._topic_list.Select(selection - 1)
self._topic_list.EnsureVisible(selection - 1)
elif keycode == wx.WXK_RETURN:
self._hide_dropdown()
self._select_currently_selected()
elif keycode == wx.WXK_ESCAPE:
self._hide_dropdown()
self._update_topic_text()
else:
event.Skip()
def _on_list_clicked(self, event):
selection, flag = self._topic_list.HitTest(event.GetPosition())
if selection == -1:
return
self._topic_list.Select(selection)
self._select_currently_selected()
self._hide_dropdown()
def _select_currently_selected(self):
selection = self._topic_list.GetFirstSelected()
if selection == -1:
selection = 0
if self._topic_list.GetItemCount() == 0:
return
topic_index = self._topic_list.GetItemData(selection)
self.selected_topic = self._topics[topic_index][1]
def _on_focus_got(self, event):
self.SetInsertionPoint(0)
self.SetSelection(-1, -1)
def _on_focus_lost(self, event):
self._hide_dropdown()
def _show_dropdown(self):
if not self._dropdown.IsShown():
self._update_topics()
if not self._setup_dropdown_data():
self._hide_dropdown()
return
position = self.ClientToScreen((0, 0))
width, height = self.GetSize()
self._dropdown.Position(position, (0, height))
self._dropdown.Show()
def _hide_dropdown(self):
self._dropdown.Hide()