Rename Sidebar to BookmarkView
Is not a sidebar anymore, and we will add a sidebar to show
the index, the to avoid problems in the future, renamed the object
and variables to have more consistent nomenclature.
Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
Gonzalo Odiard
11 years ago
0 | # Copyright 2009 One Laptop Per Child | |
1 | # Author: Sayamindu Dasgupta <sayamindu@laptop.org> | |
2 | # | |
3 | # This program is free software; you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
5 | # the Free Software Foundation; either version 2 of the License, or | |
6 | # (at your option) any later version. | |
7 | # | |
8 | # This program is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with this program; if not, write to the Free Software | |
15 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | ||
17 | import logging | |
18 | ||
19 | from gi.repository import GObject | |
20 | from gi.repository import Gtk | |
21 | from gi.repository import Gdk | |
22 | ||
23 | from sugar3.graphics.icon import Icon | |
24 | from sugar3.graphics.xocolor import XoColor | |
25 | from sugar3 import profile | |
26 | from sugar3.util import timestamp_to_elapsed_string | |
27 | from sugar3.graphics import style | |
28 | ||
29 | from readdialog import BookmarkAddDialog, BookmarkEditDialog | |
30 | ||
31 | from gettext import gettext as _ | |
32 | ||
33 | ||
34 | _logger = logging.getLogger('read-activity') | |
35 | ||
36 | # TODO: Add support for multiple bookmarks in a single page | |
37 | # (required when sharing) | |
38 | ||
39 | ||
40 | class BookmarkView(Gtk.EventBox): | |
41 | ||
42 | __gsignals__ = { | |
43 | 'bookmark-changed': (GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE, | |
44 | ([])), | |
45 | } | |
46 | ||
47 | def __init__(self): | |
48 | Gtk.EventBox.__init__(self) | |
49 | self.set_size_request(style.GRID_CELL_SIZE, | |
50 | style.GRID_CELL_SIZE * 2) | |
51 | self._xo_color = profile.get_color() | |
52 | self._fill_color = style.Color(self._xo_color.get_fill_color()) | |
53 | self._stroke_color = style.Color(self._xo_color.get_stroke_color()) | |
54 | ||
55 | self._box = Gtk.VButtonBox() | |
56 | self._box.set_layout(Gtk.ButtonBoxStyle.CENTER) | |
57 | self.add(self._box) | |
58 | self._box.show() | |
59 | self.show() | |
60 | ||
61 | self._bookmark_icon = None | |
62 | self._bookmark_manager = None | |
63 | self._is_showing_local_bookmark = False | |
64 | self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK) | |
65 | self._box.connect('draw', self.__draw_cb) | |
66 | ||
67 | def __draw_cb(self, widget, ctx): | |
68 | width = style.GRID_CELL_SIZE | |
69 | height = style.GRID_CELL_SIZE * 2 | |
70 | ||
71 | ctx.rectangle(0, 0, width, height) | |
72 | ctx.set_source_rgba(*self._fill_color.get_rgba()) | |
73 | ctx.paint() | |
74 | ||
75 | ctx.new_path() | |
76 | ctx.move_to(0, 0) | |
77 | ctx.line_to(width, 0) | |
78 | ctx.line_to(width, height) | |
79 | ctx.line_to(width / 2, height - width / 2) | |
80 | ctx.line_to(0, height) | |
81 | ctx.close_path() | |
82 | ctx.set_source_rgba(*self._stroke_color.get_rgba()) | |
83 | ctx.fill() | |
84 | ||
85 | def _add_bookmark_icon(self, bookmark): | |
86 | xocolor = XoColor(str(bookmark.color)) | |
87 | self._bookmark_icon = Icon(icon_name='emblem-favorite', pixel_size=18, | |
88 | xo_color=xocolor) | |
89 | ||
90 | self._box.props.has_tooltip = True | |
91 | self.__box_query_tooltip_cb_id = self._box.connect('query_tooltip', | |
92 | self.__bookmark_icon_query_tooltip_cb, bookmark) | |
93 | ||
94 | self.__event_cb_id = \ | |
95 | self.connect('event', self.__event_cb, bookmark) | |
96 | ||
97 | self._box.pack_start(self._bookmark_icon, False, False, 0) | |
98 | self._bookmark_icon.show_all() | |
99 | ||
100 | if bookmark.is_local(): | |
101 | self._is_showing_local_bookmark = True | |
102 | ||
103 | def __bookmark_icon_query_tooltip_cb(self, widget, x, y, keyboard_mode, | |
104 | tip, bookmark): | |
105 | tooltip_header = bookmark.get_note_title() | |
106 | tooltip_body = bookmark.get_note_body() | |
107 | time = timestamp_to_elapsed_string(bookmark.timestamp) | |
108 | #TRANS: This goes like Bookmark added by User 5 days ago | |
109 | #TRANS: (the elapsed string gets translated automatically) | |
110 | tooltip_footer = (_('Bookmark added by %(user)s %(time)s') \ | |
111 | % {'user': bookmark.nick.decode('utf-8'), | |
112 | 'time': time.decode('utf-8')}) | |
113 | ||
114 | vbox = Gtk.VBox() | |
115 | ||
116 | l = Gtk.Label('<big>%s</big>' % tooltip_header) | |
117 | l.set_use_markup(True) | |
118 | l.set_width_chars(40) | |
119 | l.set_line_wrap(True) | |
120 | vbox.pack_start(l, False, False, 0) | |
121 | l.show() | |
122 | ||
123 | l = Gtk.Label('%s' % tooltip_body) | |
124 | l.set_use_markup(True) | |
125 | l.set_alignment(0, 0) | |
126 | l.set_padding(2, 6) | |
127 | l.set_width_chars(40) | |
128 | l.set_line_wrap(True) | |
129 | l.set_justify(Gtk.Justification.FILL) | |
130 | vbox.pack_start(l, True, True, 0) | |
131 | l.show() | |
132 | ||
133 | l = Gtk.Label('<small><i>%s</i></small>' % tooltip_footer) | |
134 | l.set_use_markup(True) | |
135 | l.set_width_chars(40) | |
136 | l.set_line_wrap(True) | |
137 | vbox.pack_start(l, False, False, 0) | |
138 | l.show() | |
139 | ||
140 | tip.set_custom(vbox) | |
141 | ||
142 | return True | |
143 | ||
144 | def __event_cb(self, widget, event, bookmark): | |
145 | if event.type == Gdk.EventType.BUTTON_PRESS and \ | |
146 | self._bookmark_icon is not None: | |
147 | ||
148 | bookmark_title = bookmark.get_note_title() | |
149 | bookmark_content = bookmark.get_note_body() | |
150 | ||
151 | dialog = BookmarkEditDialog( | |
152 | parent_xid=self.get_toplevel().get_window(), | |
153 | dialog_title=_("Add notes for bookmark: "), | |
154 | bookmark_title=bookmark_title, | |
155 | bookmark_content=bookmark_content, page=bookmark.page_no, | |
156 | sidebarinstance=self) | |
157 | dialog.show_all() | |
158 | ||
159 | return False | |
160 | ||
161 | def _clear_bookmarks(self): | |
162 | for bookmark_icon in self._box.get_children(): | |
163 | self._box.disconnect(self.__box_query_tooltip_cb_id) | |
164 | self.disconnect(self.__event_cb_id) | |
165 | ||
166 | bookmark_icon.hide() # XXX: Is this needed?? | |
167 | bookmark_icon.destroy() | |
168 | ||
169 | self._bookmark_icon = None | |
170 | ||
171 | self._is_showing_local_bookmark = False | |
172 | ||
173 | def set_bookmarkmanager(self, bookmark_manager): | |
174 | self._bookmark_manager = bookmark_manager | |
175 | ||
176 | def get_bookmarkmanager(self): | |
177 | return (self._bookmark_manager) | |
178 | ||
179 | def update_for_page(self, page): | |
180 | ||
181 | self._clear_bookmarks() | |
182 | if self._bookmark_manager is None: | |
183 | return | |
184 | ||
185 | bookmarks = self._bookmark_manager.get_bookmarks_for_page(page) | |
186 | ||
187 | if bookmarks: | |
188 | self.show() | |
189 | else: | |
190 | self.hide() | |
191 | ||
192 | for bookmark in bookmarks: | |
193 | self._add_bookmark_icon(bookmark) | |
194 | self.notify_bookmark_change() | |
195 | ||
196 | def notify_bookmark_change(self): | |
197 | self.emit('bookmark-changed') | |
198 | ||
199 | def add_bookmark(self, page): | |
200 | bookmark_title = (_("%s's bookmark") % profile.get_nick_name()) | |
201 | bookmark_content = (_("Bookmark for page %d") % (int(page) + 1)) | |
202 | dialog = BookmarkAddDialog( | |
203 | parent_xid=self.get_toplevel().get_window(), | |
204 | dialog_title=_("Add notes for bookmark: "), | |
205 | bookmark_title=bookmark_title, | |
206 | bookmark_content=bookmark_content, page=page, | |
207 | sidebarinstance=self) | |
208 | dialog.show_all() | |
209 | ||
210 | def _real_add_bookmark(self, page, content): | |
211 | self._bookmark_manager.add_bookmark(page, unicode(content)) | |
212 | self.update_for_page(page) | |
213 | ||
214 | def del_bookmark(self, page): | |
215 | self._bookmark_manager.del_bookmark(page) | |
216 | self.update_for_page(page) | |
217 | ||
218 | def is_showing_local_bookmark(self): | |
219 | return self._is_showing_local_bookmark |
50 | 50 | |
51 | 51 | from readtoolbar import EditToolbar |
52 | 52 | from readtoolbar import ViewToolbar |
53 | from readsidebar import Sidebar | |
53 | from bookmarkview import BookmarkView | |
54 | 54 | from readtopbar import TopBar |
55 | 55 | from readdb import BookmarkManager |
56 | 56 | from sugarmenuitem import SugarMenuItem |
147 | 147 | |
148 | 148 | self._view = None |
149 | 149 | self.dpi = _get_screen_dpi() |
150 | self._sidebar = Sidebar() | |
151 | self._sidebar.show() | |
152 | self._sidebar.connect('bookmark-changed', self._update_bookmark_cb) | |
150 | self._bookmark_view = BookmarkView() | |
151 | self._bookmark_view.show() | |
152 | self._bookmark_view.connect('bookmark-changed', | |
153 | self._update_bookmark_cb) | |
153 | 154 | |
154 | 155 | toolbar_box = ToolbarBox() |
155 | 156 | |
263 | 264 | self._hbox.show() |
264 | 265 | overlay.add(self._hbox) |
265 | 266 | |
266 | self._sidebar.props.halign = Gtk.Align.END | |
267 | self._sidebar.props.valign = Gtk.Align.START | |
267 | self._bookmark_view.props.halign = Gtk.Align.END | |
268 | self._bookmark_view.props.valign = Gtk.Align.START | |
268 | 269 | # HACK: This is to calculate the scrollbar width |
269 | 270 | # defined in sugar-artwork gtk-widgets.css.em |
270 | 271 | if style.zoom(1): |
272 | 273 | else: |
273 | 274 | scrollbar_width = 11 |
274 | 275 | |
275 | self._sidebar.props.margin_right = scrollbar_width | |
276 | overlay.add_overlay(self._sidebar) | |
276 | self._bookmark_view.props.margin_right = scrollbar_width | |
277 | overlay.add_overlay(self._bookmark_view) | |
277 | 278 | overlay.show() |
278 | 279 | self._vbox.pack_start(overlay, True, True, 0) |
279 | 280 | self.set_canvas(self._vbox) |
486 | 487 | def __bookmarker_toggled_cb(self, button): |
487 | 488 | page = self._view.get_current_page() |
488 | 489 | if self._bookmarker.props.active: |
489 | self._sidebar.add_bookmark(page) | |
490 | self._bookmark_view.add_bookmark(page) | |
490 | 491 | else: |
491 | 492 | alert = ConfirmationAlert() |
492 | 493 | alert.props.title = _('Delete bookmark') |
500 | 501 | self.remove_alert(alert) |
501 | 502 | |
502 | 503 | if response_id is Gtk.ResponseType.OK: |
503 | self._sidebar.del_bookmark(page) | |
504 | self._bookmark_view.del_bookmark(page) | |
504 | 505 | elif response_id is Gtk.ResponseType.CANCEL: |
505 | 506 | self._bookmarker.handler_block(self._bookmarker_toggle_handler_id) |
506 | 507 | self._bookmarker.props.active = True |
512 | 513 | if self._toc_model != None: |
513 | 514 | self._toc_select_active_page() |
514 | 515 | |
515 | self._sidebar.update_for_page(page_to) | |
516 | self._bookmark_view.update_for_page(page_to) | |
516 | 517 | self.update_bookmark_button() |
517 | 518 | |
518 | 519 | tuples_list = self._bookmarkmanager.get_highlights(page_to) |
526 | 527 | def update_bookmark_button(self): |
527 | 528 | self._bookmarker.handler_block(self._bookmarker_toggle_handler_id) |
528 | 529 | self._bookmarker.props.active = \ |
529 | self._sidebar.is_showing_local_bookmark() | |
530 | self._bookmark_view.is_showing_local_bookmark() | |
530 | 531 | self._bookmarker.handler_unblock(self._bookmarker_toggle_handler_id) |
531 | 532 | |
532 | 533 | def _update_nav_buttons(self, current_page): |
830 | 831 | |
831 | 832 | filehash = get_md5(filepath) |
832 | 833 | self._bookmarkmanager = BookmarkManager(filehash) |
833 | self._sidebar.set_bookmarkmanager(self._bookmarkmanager) | |
834 | self._bookmark_view.set_bookmarkmanager(self._bookmarkmanager) | |
834 | 835 | self._update_toc() |
835 | 836 | self._view.connect_page_changed_handler(self.__page_changed_cb) |
836 | 837 | self._view.load_metadata(self) |
847 | 848 | # README: bookmark sidebar is not showing the bookmark in the |
848 | 849 | # first page because this is updated just if the page number changes |
849 | 850 | if current_page == 0: |
850 | self._sidebar.update_for_page(current_page) | |
851 | self._bookmark_view.update_for_page(current_page) | |
851 | 852 | |
852 | 853 | # We've got the document, so if we're a shared activity, offer it |
853 | 854 | try: |
0 | # Copyright 2009 One Laptop Per Child | |
1 | # Author: Sayamindu Dasgupta <sayamindu@laptop.org> | |
2 | # | |
3 | # This program is free software; you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
5 | # the Free Software Foundation; either version 2 of the License, or | |
6 | # (at your option) any later version. | |
7 | # | |
8 | # This program is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with this program; if not, write to the Free Software | |
15 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | ||
17 | import logging | |
18 | ||
19 | from gi.repository import GObject | |
20 | from gi.repository import Gtk | |
21 | from gi.repository import Gdk | |
22 | ||
23 | from sugar3.graphics.icon import Icon | |
24 | from sugar3.graphics.xocolor import XoColor | |
25 | from sugar3 import profile | |
26 | from sugar3.util import timestamp_to_elapsed_string | |
27 | from sugar3.graphics import style | |
28 | ||
29 | from readdialog import BookmarkAddDialog, BookmarkEditDialog | |
30 | ||
31 | from gettext import gettext as _ | |
32 | ||
33 | ||
34 | _logger = logging.getLogger('read-activity') | |
35 | ||
36 | # TODO: Add support for multiple bookmarks in a single page | |
37 | # (required when sharing) | |
38 | ||
39 | ||
40 | class Sidebar(Gtk.EventBox): | |
41 | ||
42 | __gsignals__ = { | |
43 | 'bookmark-changed': (GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE, | |
44 | ([])), | |
45 | } | |
46 | ||
47 | def __init__(self): | |
48 | Gtk.EventBox.__init__(self) | |
49 | self.set_size_request(style.GRID_CELL_SIZE, | |
50 | style.GRID_CELL_SIZE * 2) | |
51 | self._xo_color = profile.get_color() | |
52 | self._fill_color = style.Color(self._xo_color.get_fill_color()) | |
53 | self._stroke_color = style.Color(self._xo_color.get_stroke_color()) | |
54 | ||
55 | self._box = Gtk.VButtonBox() | |
56 | self._box.set_layout(Gtk.ButtonBoxStyle.CENTER) | |
57 | self.add(self._box) | |
58 | self._box.show() | |
59 | self.show() | |
60 | ||
61 | self._bookmark_icon = None | |
62 | self._bookmark_manager = None | |
63 | self._is_showing_local_bookmark = False | |
64 | self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK) | |
65 | self._box.connect('draw', self.__draw_cb) | |
66 | ||
67 | def __draw_cb(self, widget, ctx): | |
68 | width = style.GRID_CELL_SIZE | |
69 | height = style.GRID_CELL_SIZE * 2 | |
70 | ||
71 | ctx.rectangle(0, 0, width, height) | |
72 | ctx.set_source_rgba(*self._fill_color.get_rgba()) | |
73 | ctx.paint() | |
74 | ||
75 | ctx.new_path() | |
76 | ctx.move_to(0, 0) | |
77 | ctx.line_to(width, 0) | |
78 | ctx.line_to(width, height) | |
79 | ctx.line_to(width / 2, height - width / 2) | |
80 | ctx.line_to(0, height) | |
81 | ctx.close_path() | |
82 | ctx.set_source_rgba(*self._stroke_color.get_rgba()) | |
83 | ctx.fill() | |
84 | ||
85 | def _add_bookmark_icon(self, bookmark): | |
86 | xocolor = XoColor(str(bookmark.color)) | |
87 | self._bookmark_icon = Icon(icon_name='emblem-favorite', pixel_size=18, | |
88 | xo_color=xocolor) | |
89 | ||
90 | self._box.props.has_tooltip = True | |
91 | self.__box_query_tooltip_cb_id = self._box.connect('query_tooltip', | |
92 | self.__bookmark_icon_query_tooltip_cb, bookmark) | |
93 | ||
94 | self.__event_cb_id = \ | |
95 | self.connect('event', self.__event_cb, bookmark) | |
96 | ||
97 | self._box.pack_start(self._bookmark_icon, False, False, 0) | |
98 | self._bookmark_icon.show_all() | |
99 | ||
100 | if bookmark.is_local(): | |
101 | self._is_showing_local_bookmark = True | |
102 | ||
103 | def __bookmark_icon_query_tooltip_cb(self, widget, x, y, keyboard_mode, | |
104 | tip, bookmark): | |
105 | tooltip_header = bookmark.get_note_title() | |
106 | tooltip_body = bookmark.get_note_body() | |
107 | time = timestamp_to_elapsed_string(bookmark.timestamp) | |
108 | #TRANS: This goes like Bookmark added by User 5 days ago | |
109 | #TRANS: (the elapsed string gets translated automatically) | |
110 | tooltip_footer = (_('Bookmark added by %(user)s %(time)s') \ | |
111 | % {'user': bookmark.nick.decode('utf-8'), | |
112 | 'time': time.decode('utf-8')}) | |
113 | ||
114 | vbox = Gtk.VBox() | |
115 | ||
116 | l = Gtk.Label('<big>%s</big>' % tooltip_header) | |
117 | l.set_use_markup(True) | |
118 | l.set_width_chars(40) | |
119 | l.set_line_wrap(True) | |
120 | vbox.pack_start(l, False, False, 0) | |
121 | l.show() | |
122 | ||
123 | l = Gtk.Label('%s' % tooltip_body) | |
124 | l.set_use_markup(True) | |
125 | l.set_alignment(0, 0) | |
126 | l.set_padding(2, 6) | |
127 | l.set_width_chars(40) | |
128 | l.set_line_wrap(True) | |
129 | l.set_justify(Gtk.Justification.FILL) | |
130 | vbox.pack_start(l, True, True, 0) | |
131 | l.show() | |
132 | ||
133 | l = Gtk.Label('<small><i>%s</i></small>' % tooltip_footer) | |
134 | l.set_use_markup(True) | |
135 | l.set_width_chars(40) | |
136 | l.set_line_wrap(True) | |
137 | vbox.pack_start(l, False, False, 0) | |
138 | l.show() | |
139 | ||
140 | tip.set_custom(vbox) | |
141 | ||
142 | return True | |
143 | ||
144 | def __event_cb(self, widget, event, bookmark): | |
145 | if event.type == Gdk.EventType.BUTTON_PRESS and \ | |
146 | self._bookmark_icon is not None: | |
147 | ||
148 | bookmark_title = bookmark.get_note_title() | |
149 | bookmark_content = bookmark.get_note_body() | |
150 | ||
151 | dialog = BookmarkEditDialog( | |
152 | parent_xid=self.get_toplevel().get_window(), | |
153 | dialog_title=_("Add notes for bookmark: "), | |
154 | bookmark_title=bookmark_title, | |
155 | bookmark_content=bookmark_content, page=bookmark.page_no, | |
156 | sidebarinstance=self) | |
157 | dialog.show_all() | |
158 | ||
159 | return False | |
160 | ||
161 | def _clear_bookmarks(self): | |
162 | for bookmark_icon in self._box.get_children(): | |
163 | self._box.disconnect(self.__box_query_tooltip_cb_id) | |
164 | self.disconnect(self.__event_cb_id) | |
165 | ||
166 | bookmark_icon.hide() # XXX: Is this needed?? | |
167 | bookmark_icon.destroy() | |
168 | ||
169 | self._bookmark_icon = None | |
170 | ||
171 | self._is_showing_local_bookmark = False | |
172 | ||
173 | def set_bookmarkmanager(self, bookmark_manager): | |
174 | self._bookmark_manager = bookmark_manager | |
175 | ||
176 | def get_bookmarkmanager(self): | |
177 | return (self._bookmark_manager) | |
178 | ||
179 | def update_for_page(self, page): | |
180 | ||
181 | self._clear_bookmarks() | |
182 | if self._bookmark_manager is None: | |
183 | return | |
184 | ||
185 | bookmarks = self._bookmark_manager.get_bookmarks_for_page(page) | |
186 | ||
187 | if bookmarks: | |
188 | self.show() | |
189 | else: | |
190 | self.hide() | |
191 | ||
192 | for bookmark in bookmarks: | |
193 | self._add_bookmark_icon(bookmark) | |
194 | self.notify_bookmark_change() | |
195 | ||
196 | def notify_bookmark_change(self): | |
197 | self.emit('bookmark-changed') | |
198 | ||
199 | def add_bookmark(self, page): | |
200 | bookmark_title = (_("%s's bookmark") % profile.get_nick_name()) | |
201 | bookmark_content = (_("Bookmark for page %d") % (int(page) + 1)) | |
202 | dialog = BookmarkAddDialog( | |
203 | parent_xid=self.get_toplevel().get_window(), | |
204 | dialog_title=_("Add notes for bookmark: "), | |
205 | bookmark_title=bookmark_title, | |
206 | bookmark_content=bookmark_content, page=page, | |
207 | sidebarinstance=self) | |
208 | dialog.show_all() | |
209 | ||
210 | def _real_add_bookmark(self, page, content): | |
211 | self._bookmark_manager.add_bookmark(page, unicode(content)) | |
212 | self.update_for_page(page) | |
213 | ||
214 | def del_bookmark(self, page): | |
215 | self._bookmark_manager.del_bookmark(page) | |
216 | self.update_for_page(page) | |
217 | ||
218 | def is_showing_local_bookmark(self): | |
219 | return self._is_showing_local_bookmark |