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
|
#
# 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
from gettext import gettext as _
import os
import random
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf
from gi.repository import WebKit
from sugar3.graphics import style
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.icon import Icon
import questions
class _DialogWindow(Gtk.Window):
# A base class for a modal dialog window.
def __init__(self, icon_name, title):
super(_DialogWindow, self).__init__()
self.set_border_width(style.LINE_WIDTH)
width = Gdk.Screen.width() - style.GRID_CELL_SIZE * 2
height = Gdk.Screen.height() - style.GRID_CELL_SIZE * 2
self.set_size_request(width, height)
self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
self.set_decorated(False)
self.set_resizable(False)
self.set_modal(True)
vbox = Gtk.VBox()
self.add(vbox)
toolbar = _DialogToolbar(icon_name, title)
toolbar.connect('stop-clicked', self._stop_clicked_cb)
vbox.pack_start(toolbar, False)
self.content_vbox = Gtk.VBox()
self.content_vbox.set_border_width(style.DEFAULT_SPACING)
vbox.add(self.content_vbox)
self.connect('realize', self._realize_cb)
def _stop_clicked_cb(self, source):
self.destroy()
def _realize_cb(self, source):
self.set_type_hint(Gdk.WindowTypeHint.DIALOG)
self.get_window().set_accept_focus(True)
class _DialogToolbar(Gtk.Toolbar):
# Displays a dialog window's toolbar, with title, icon, and close box.
__gsignals__ = {
'stop-clicked': (GObject.SignalFlags.RUN_LAST, None, ()),
}
def __init__(self, icon_name, title):
super(_DialogToolbar, self).__init__()
if icon_name is not None:
icon = Icon()
icon.set_from_icon_name(icon_name, Gtk.IconSize.LARGE_TOOLBAR)
self._add_widget(icon)
self._add_separator()
label = Gtk.Label(label=title)
self._add_widget(label)
self._add_separator(expand=True)
stop = ToolButton(icon_name='dialog-cancel')
stop.set_tooltip(_('Done'))
stop.connect('clicked', self._stop_clicked_cb)
self.add(stop)
def _add_separator(self, expand=False):
separator = Gtk.SeparatorToolItem()
separator.set_expand(expand)
separator.set_draw(False)
self.add(separator)
def _add_widget(self, widget):
tool_item = Gtk.ToolItem()
tool_item.add(widget)
self.add(tool_item)
def _stop_clicked_cb(self, button):
self.emit('stop-clicked')
class ResourceDialog(_DialogWindow):
__gtype_name__ = 'ResourceDialog'
def __init__(self, model, id_resource):
resource = model.get_resource(id_resource)
super(ResourceDialog, self).__init__(None, resource['title'])
scrollwin = Gtk.ScrolledWindow()
scrollwin.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
self.content_vbox.pack_start(scrollwin, True, True, 0)
vbox = Gtk.VBox()
scrollwin.add_with_viewport(vbox)
image = Gtk.Image()
vbox.pack_start(image, False, padding=5)
image.set_from_file(resource['file_image'])
editor = WebKit.WebView()
editor.set_editable(False)
height = int(Gdk.Screen.height() / 3)
editor.set_size_request(-1, height)
vbox.pack_start(editor, False, padding=5)
if resource['file_text'] != '':
if os.path.exists(resource['file_text']):
with open(resource['file_text'], 'r') as html_file:
editor.load_html_string(html_file.read(), 'file:///')
class QuestionDialog(_DialogWindow):
__gtype_name__ = 'QuestionDialog'
__gsignals__ = {
'reply-selected': (GObject.SignalFlags.RUN_LAST, None,
([GObject.TYPE_STRING, GObject.TYPE_BOOLEAN])),
}
def __init__(self, model, id_question):
self._id_question = id_question
question = model.get_question(id_question)
super(QuestionDialog, self).__init__(None, question['question'])
scrollwin = Gtk.ScrolledWindow()
scrollwin.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
vbox = Gtk.VBox()
scrollwin.add_with_viewport(vbox)
self.notebook = Gtk.Notebook()
self.notebook.set_show_tabs(False)
# add a page with the controls to reply the question
self.notebook.append_page(scrollwin, None)
# ... and another to show the smiley according to the result
self.image_result = Gtk.Image()
self.notebook.append_page(self.image_result, None)
self.content_vbox.pack_start(self.notebook, True, True)
self.content_vbox.set_border_width(0)
question_type = question['type']
if question_type == model.QUESTION_TYPE_TEXT:
replies = []
replies.extend(question['replies'])
random.shuffle(replies)
for reply in replies:
hbox_row = Gtk.HBox()
reply_label = Gtk.Label(label=reply['text'])
hbox_row.pack_start(reply_label, True, padding=5)
vbox.pack_start(hbox_row, False, padding=5)
reply_button = Gtk.Button(_('Select'))
reply_button.connect('clicked', self.__button_reply_click_cb,
reply['valid'])
hbox_row.pack_start(reply_button, False, padding=5)
hbox_row.show_all()
if question_type == model.QUESTION_TYPE_GRAPHIC:
image_path = question['file_image']
reply_image_path = question['file_image_reply']
self.draw_reply_area = questions.DrawReplyArea(image_path,
reply_file_name=reply_image_path)
vbox.pack_start(self.draw_reply_area, False, padding=5)
self.draw_reply_area.connect('reply-selected',
self.__draw_reply_click_cb)
def __button_reply_click_cb(self, widget, valid_reply):
self._show_reply_feedback(valid_reply)
def __draw_reply_click_cb(self, widget, valid_reply):
self._show_reply_feedback(valid_reply)
def _change_page(self):
self.notebook.set_current_page(1)
self.get_window().set_cursor(None)
# wait 3 seconds
#report and close
GObject.timeout_add_seconds(3, self._close_all)
def _close_all(self):
self.destroy()
def _show_reply_feedback(self, valid_reply):
# load smiley image
self.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.WATCH))
if valid_reply:
smiley = random.choice(questions.SMILIES_OK)
else:
smiley = random.choice(questions.SMILIES_WRONG)
image_file_name = './images/smilies/%s.svg' % smiley
size = Gdk.Screen.height() / 2
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(image_file_name, size,
size)
self.image_result.set_from_pixbuf(pixbuf)
# report result
self.emit('reply-selected', self._id_question, valid_reply)
# wait one second and change the page
GObject.timeout_add_seconds(1, self._change_page)
|