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
|
# 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 os
import dbus
import time
from gettext import gettext as _
import logging
from gi.repository import Abi
from gi.repository import GLib
from sugar3.graphics.radiotoolbutton import RadioToolButton
from sugar3.graphics.combobox import ComboBox
from sugar3.graphics.palette import Palette
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.menuitem import MenuItem
from sugar3.datastore import datastore
from sugar3.activity.activity import SCOPE_PRIVATE
logger = logging.getLogger('write-activity')
"""
# The FontCombo is not used anymore, keep the code here for reference
# for a few versions
class FontCombo(ComboBox):
def __init__(self, abi):
ComboBox.__init__(self)
self._has_custom_fonts = False
self._fonts = sorted(abi.get_font_names())
self._fonts_changed_id = self.connect('changed', self._font_changed_cb,
abi)
for i, f in enumerate(self._fonts):
self.append_item(i, f, None)
if f == 'Times New Roman':
self.set_active(i)
self._abi_handler = abi.connect('font-family', self._font_family_cb)
def _font_changed_cb(self, combobox, abi):
if self.get_active() != -1:
logger.debug('Setting font: %s', self._fonts[self.get_active()])
try:
abi.handler_block(self._abi_handler)
abi.set_font_name(self._fonts[self.get_active()])
finally:
abi.handler_unblock(self._abi_handler)
def _font_family_cb(self, abi, font_family):
font_index = -1
# search for the font name in our font list
for i, f in enumerate(self._fonts):
if f == font_family:
font_index = i
break
# if we don't know this font yet, then add it (temporary) to the list
if font_index == -1:
logger.debug('Font not found in font list: %s', font_family)
if not self._has_custom_fonts:
# add a separator to seperate the non-available fonts from
# the available ones
self._fonts.append('') # ugly
self.append_separator()
self._has_custom_fonts = True
# add the new font
self._fonts.append(font_family)
self.append_item(0, font_family, None)
# see how many fonts we have now, so we can select the last one
model = self.get_model()
num_children = model.iter_n_children(None)
logger.debug('Number of fonts in the list: %d', num_children)
font_index = num_children - 1
# activate the found font
if (font_index > -1):
self.handler_block(self._fonts_changed_id)
self.set_active(font_index)
self.handler_unblock(self._fonts_changed_id)
"""
class FontSizeCombo(ComboBox):
def __init__(self, abi):
ComboBox.__init__(self)
self._abi_handler = abi.connect('font-size', self._font_size_cb)
self._font_sizes = ['8', '9', '10', '11', '12', '14', '16', '20', \
'22', '24', '26', '28', '36', '48', '72']
self._changed_id = self.connect('changed', self._font_size_changed_cb,
abi)
for i, s in enumerate(self._font_sizes):
self.append_item(i, s, None)
if s == '12':
self.set_active(i)
def _font_size_changed_cb(self, combobox, abi):
if self.get_active() != -1:
logger.debug('Setting font size: %d',
int(self._font_sizes[self.get_active()]))
abi.handler_block(self._abi_handler)
try:
abi.set_font_size(self._font_sizes[self.get_active()])
finally:
abi.handler_unblock(self._abi_handler)
def _font_size_cb(self, abi, size):
for i, s in enumerate(self._font_sizes):
if int(s) == int(size):
self.handler_block(self._changed_id)
self.set_active(i)
self.handler_unblock(self._changed_id)
break
class AbiButton(RadioToolButton):
def __init__(self, abi, abi_signal, do_abi_cb, on_abi_cb=None, **kwargs):
RadioToolButton.__init__(self, **kwargs)
self._abi_handler = abi.connect(abi_signal, self.__abi_cb,
abi_signal, on_abi_cb)
self._toggled_handler = self.connect('toggled', self.__toggled_cb,
abi, do_abi_cb)
def __toggled_cb(self, button, abi, do_abi_cb):
if not button.props.active:
return
abi.handler_block(self._abi_handler)
try:
logging.debug('Do abi %s' % do_abi_cb)
do_abi_cb()
finally:
abi.handler_unblock(self._abi_handler)
def __abi_cb(self, abi, prop, abi_signal, on_abi_cb):
if (on_abi_cb is None and not prop) or \
(on_abi_cb is not None and not on_abi_cb(abi, prop)):
return
self.handler_block(self._toggled_handler)
try:
logging.debug('On abi %s prop=%r' % (abi_signal, prop))
self.set_active(True)
finally:
self.handler_unblock(self._toggled_handler)
class ExportButtonFactory():
_EXPORT_FORMATS = [{'mime_type': 'application/rtf',
'title': _('Rich Text (RTF)'),
'icon':'save-as-rtf',
'jpostfix': _('RTF'),
'exp_props': ''},
{'mime_type': 'text/html',
'title': _('Hypertext (HTML)'),
'icon':'save-as-html',
'jpostfix': _('HTML'),
'exp_props': 'html4:yes; declare-xml:no; ' \
'embed-css:yes; embed-images:yes;'},
{'mime_type': 'text/plain',
'title': _('Plain Text (TXT)'),
'icon':'save-as-txt',
'jpostfix': _('TXT'),
'exp_props': ''},
{'mime_type': 'application/pdf',
'title': _('Portable Document Format (PDF)'),
'icon':'save-as-pdf',
'jpostfix': _('PDF'),
'exp_props': ''}]
def __init__(self, activity, abi):
toolbar = activity.activity_button.props.page
for i in self._EXPORT_FORMATS:
button = ToolButton(i['icon'])
button.set_tooltip(i['title'])
button.connect('clicked', self.__clicked_cb, activity, abi, i)
toolbar.insert(button, -1)
button.show()
def __clicked_cb(self, menu_item, activity, abi, format):
logger.debug('exporting file: %r' % format)
exp_props = format['exp_props']
# special case HTML export to set the activity name as the HTML title
if format['mime_type'] == "text/html":
exp_props += " title:" + activity.metadata['title'] + ';'
# create a new journal item
fileObject = datastore.create()
act_meta = activity.metadata
fileObject.metadata['title'] = \
act_meta['title'] + ' (' + format['jpostfix'] + ')'
fileObject.metadata['title_set_by_user'] = \
act_meta['title_set_by_user']
fileObject.metadata['mime_type'] = format['mime_type']
fileObject.metadata['fulltext'] = \
abi.get_content('text/plain', None)[:3000]
fileObject.metadata['icon-color'] = act_meta['icon-color']
# don't set application if PDF because Write can't open PDF files
if format['mime_type'] != 'application/pdf':
fileObject.metadata['activity'] = act_meta['activity']
fileObject.metadata['keep'] = act_meta.get('keep', '0')
preview = activity.get_preview()
if preview is not None:
fileObject.metadata['preview'] = dbus.ByteArray(preview)
fileObject.metadata['share-scope'] = act_meta.get('share-scope',
SCOPE_PRIVATE)
# write out the document contents in the requested format
fileObject.file_path = os.path.join(activity.get_activity_root(),
'instance', '%i' % time.time())
abi.save('file://' + fileObject.file_path,
format['mime_type'], exp_props)
# store the journal item
datastore.write(fileObject, transfer_ownership=True)
fileObject.destroy()
del fileObject
class DocumentView(Abi.Widget):
def __init__(self):
Abi.Widget.__init__(self)
self.connect('size-allocate', self.__size_allocate_cb)
self.connect('request-clear-area', self.__request_clear_area_cb)
self.connect('unset-clear-area', self.__unset_clear_area_cb)
self.osk_changed = False
self.dy = 0
def __shallow_move_cb(self):
self.moveto_right()
return False
def __size_allocate_cb(self, widget, allocation):
self.set_allocation(allocation)
if self.get_child() is not None:
child_allocation = allocation
child_allocation.y = 0
child_allocation.x = 0
child_allocation.height -= self.dy
self.get_child().size_allocate(allocation)
if self.osk_changed is True:
self.moveto_left()
GLib.timeout_add(100, self.__shallow_move_cb)
self.osk_changed = False
def __request_clear_area_cb(self, widget, clear, cursor):
allocation = widget.get_allocation()
allocation.x = 0
allocation.y = 0
allocation.x, allocation.y = \
widget.get_window().get_root_coords(allocation.x, allocation.y)
if clear.y > allocation.y + allocation.height or \
clear.y + clear.height < allocation.y:
return False
self.dy = allocation.y + allocation.height - clear.y
# Ensure there's at least some room for the view
if self.dy > allocation.height - 80:
self.dy = 0
return False
self.osk_changed = True
self.queue_resize()
return True
def __unset_clear_area_cb(self, widget, snap_back):
self.dy = 0
self.queue_resize()
return True
|