-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathactivitychooser.py
330 lines (263 loc) · 11.6 KB
/
activitychooser.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
329
330
# Copyright (C) 2016, Abhijit Patel
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import logging
from gi.repository import GLib
from gettext import gettext as _
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from sugar3.activity import activityfactory
from sugar3.graphics import iconentry
from sugar3.graphics import style
from sugar3.graphics.toolbutton import ToolButton
from jarabe.model import shell
from jarabe.desktop.activitieslist import ActivitiesList
from jarabe.util.normalize import normalize_string
class TitleBox(Gtk.Toolbar):
def __init__(self):
Gtk.Toolbar.__init__(self)
self.close_button = ToolButton(icon_name='dialog-cancel')
self.close_button.set_tooltip(_('Close'))
self.insert(self.close_button, -1)
self.close_button.show()
self._label = Gtk.Label()
self._label.set_alignment(0, 0.5)
tool_item = Gtk.ToolItem()
tool_item.set_expand(True)
tool_item.add(self._label)
self._label.show()
self.insert(tool_item, 0)
tool_item.show()
def set_title(self, title):
self._label.set_markup('<b>%s</b>' % title)
self._label.show()
_AUTOSEARCH_TIMEOUT = 1000
class ActivityChooser(Gtk.Window):
__gtype_name__ = 'ActivityChooser'
__gsignals__ = {
'response': (GObject.SignalFlags.RUN_FIRST, None, ([int])),
'activity-selected': (GObject.SignalFlags.RUN_FIRST, None,
([object, object])),
}
def __init__(self):
Gtk.Window.__init__(self)
self.set_decorated(False)
self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
self.set_border_width(style.LINE_WIDTH)
self.set_resizable(False)
self.set_modal(True)
self.set_can_focus(True)
self._vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(self._vbox)
self._vbox.show()
self._title_box = TitleBox()
self._title_box.close_button.connect(
'clicked',
self.__close_button_clicked_cb)
self._title_box.set_size_request(-1, style.GRID_CELL_SIZE)
self._vbox.pack_start(self._title_box, False, True, 0)
self._title_box.show()
self.set_size_request(
(Gdk.Screen.height() - style.GRID_CELL_SIZE * 3) * 3 / 4,
(Gdk.Screen.height() - style.GRID_CELL_SIZE * 2) * 2 / 3)
self.connect('key-press-event', self.__key_press_event_cb)
self.connect('realize', self.__realize_cb)
self._list_view = ActivitiesList()
self.search_bar = SearchBar()
self._vbox.pack_start(self.search_bar, False, False, 0)
self.search_bar.connect('query-changed',
self.__toolbar_query_changed_cb)
self.search_bar.search_entry.connect('key-press-event',
self.__key_press_event_cb)
self.search_bar.search_entry.grab_focus()
self._scrolled_window = Gtk.ScrolledWindow()
self._scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC,
Gtk.PolicyType.AUTOMATIC)
self._scrolled_window.add(self._list_view)
self._vbox.pack_start(self._scrolled_window, True, True, 0)
self._list_view.show()
self._list_view.connect('clear-clicked',
self.__activitylist_clear_clicked_cb,
self.search_bar)
self.tree_view = self._list_view._tree_view
self.tree_view.date_column.set_visible(False)
self.tree_view.fav_column.set_visible(False)
self.tree_view.version_column.set_visible(False)
if self.tree_view.row_activated_handler:
self.tree_view.disconnect(self.tree_view.row_activated_handler)
if self.tree_view.button_press_handler:
self.tree_view.disconnect(self.tree_view.button_press_handler)
if self.tree_view.button_reslease_handler:
self.tree_view.disconnect(self.tree_view.button_reslease_handler)
if self.tree_view.icon_clicked_handler:
self.tree_view.disconnect(self.tree_view.icon_clicked_handler)
if hasattr(self.tree_view.props, 'activate_on_single_click'):
# Gtk+ 3.8 and later
self.tree_view.props.activate_on_single_click = True
self.tree_view.connect('row-activated', self.__row_activated_cb)
else:
self.tree_view.cell_icon.connect('clicked',
self.__icon_clicked_cb)
self.tree_view.connect('button-press-event',
self.__button_press_cb)
self.tree_view.connect('button-release-event',
self.__button_release_cb)
self._row_activated_armed_path = None
self.show()
def __close_button_clicked_cb(self, button):
self.destroy()
def __realize_cb(self, widget):
shell.get_model().push_modal()
self.set_type_hint(Gdk.WindowTypeHint.DIALOG)
window = self.get_window()
window.set_accept_focus(True)
shell.get_model().push_modal()
def __toolbar_query_changed_cb(self, toolbar, query):
self._query = normalize_string(query.decode('utf-8'))
self._list_view.set_filter(self._query)
toolbar.search_entry._icon_selected = \
self._list_view.get_activities_selected()
# verify if one off the selected names is a perfect match
# this is needed by th case of activities with names contained
# in other activities like 'Paint' and 'MusicPainter'
for activity in self._list_view.get_activities_selected():
if activity['name'].upper() == query.upper():
toolbar.search_entry._icon_selected = [activity]
break
# Don't change the selection if the entry has been autocompleted
if len(toolbar.search_entry._icon_selected) == 1 \
and not toolbar.search_entry.get_text() == activity['name']:
pos = toolbar.search_entry.get_position()
toolbar.search_entry.set_text(
toolbar.search_entry._icon_selected[0]['name'])
toolbar.search_entry.select_region(pos, -1)
def __key_press_event_cb(self, widget, event):
keyname = Gdk.keyval_name(event.keyval)
if keyname == 'Escape':
self.destroy()
if not self.search_bar.search_entry.has_focus():
self.search_bar.search_entry.grab_focus()
if widget == self.search_bar.search_entry:
if event.keyval == Gdk.KEY_Return:
model = self.tree_view.get_model()
if len(model) > 1:
return True
row = model[0]
bundle_id = row[self.tree_view._model.column_bundle_id]
activity_id = activityfactory.create_activity_id()
self.emit('activity-selected', bundle_id, activity_id)
self.destroy()
return True
def __activitylist_clear_clicked_cb(self, list_view, toolbar):
toolbar.clear_query()
def set_title(self, text):
self._title_box.set_title(text)
def _got_row_tree_view(self, row):
bundle_id = row[self.tree_view._model.column_bundle_id]
activity_id = activityfactory.create_activity_id()
self.emit('activity-selected', bundle_id, activity_id)
self.destroy()
def __button_press_cb(self, widget, event):
path = self.tree_view.__button_to_path(event,
Gdk.EventType.BUTTON_PRESS)
if path is None:
return
self._row_activated_armed_path = path
def __button_release_cb(self, widget, event):
path = self.tree_view.__button_to_path(event,
Gdk.EventType.BUTTON_PRESS)
if path is None:
return
if self._row_activated_armed_path != path:
return
model = self.tree_view.get_model()
row = model[path]
self._got_row_tree_view(row)
self._row_activated_armed_path = None
def __icon_clicked_cb(self, tree_view, path):
model = tree_view.get_model()
row = model[path]
self._got_row_tree_view(row)
return True
def __row_activated_cb(self, treeview, path, col):
if col is not treeview.get_column(0):
model = treeview.get_model()
row = model[path]
self._got_row_tree_view(row)
return True
class SearchBar(Gtk.Toolbar):
'''
New Toolbar below the Titlebox of sugar3.graphics PopWindow.
This toolbar contains textentry for search.
'''
__gtype_name__ = 'ActivityChooserSearchBar'
__gsignals__ = {
'query-changed': (GObject.SignalFlags.RUN_FIRST, None,
([str])),
}
def __init__(self):
Gtk.Toolbar.__init__(self)
self._query = None
self._autosearch_timer = None
# self.set_border_width(10)
tool_item = Gtk.ToolItem()
self.insert(tool_item, -1)
tool_item.set_expand(True)
tool_item.show()
self.search_entry = iconentry.IconEntry()
self.search_entry.set_icon_from_name(iconentry.ICON_ENTRY_PRIMARY,
'entry-search')
self.search_entry.set_can_focus(True)
self.search_entry.add_clear_button()
self.search_entry.set_width_chars(20)
self.search_entry.connect('activate', self._entry_activated_cb)
self.search_entry.connect('changed', self._entry_changed_cb)
tool_item.add(self.search_entry)
self.search_entry.show()
self._add_separator()
def clear_query(self):
self.search_entry.props.text = ''
def _add_separator(self, expand=False):
separator = Gtk.SeparatorToolItem()
separator.props.draw = False
if expand:
separator.set_expand(True)
else:
separator.set_size_request(style.GRID_CELL_SIZE,
style.GRID_CELL_SIZE)
self.insert(separator, -1)
separator.show()
def _entry_activated_cb(self, entry):
if self._autosearch_timer:
GLib.source_remove(self._autosearch_timer)
self._autosearch_timer = None
new_query = entry.props.text
if self._query != new_query:
self._query = new_query
self.emit('query-changed', self._query)
def _entry_changed_cb(self, entry):
if not entry.props.text:
entry.activate()
return
if self._autosearch_timer:
GLib.source_remove(self._autosearch_timer)
self._autosearch_timer = GLib.timeout_add(_AUTOSEARCH_TIMEOUT,
self._autosearch_timer_cb)
def _autosearch_timer_cb(self):
logging.debug('_autosearch_timer_cb')
self._autosearch_timer = None
self.search_entry.activate()
return False