Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/editlessonscreen.py
blob: 3ddd8cf7fe636578573881f8cae07dad59cb5ee0 (plain)
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
#!/usr/bin/env python
# vi: sw=4 et
# Copyright 2008 by Kate Scheppke and Wade Brainerd.  
# This file is part of Typing Turtle.
#
# Typing Turtle 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 3 of the License, or
# (at your option) any later version.
# 
# Typing Turtle 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 Typing Turtle.  If not, see <http://www.gnu.org/licenses/>.

# Import standard Python modules.
import logging, os, math, time, copy, locale, datetime, random, re
from gettext import gettext as _

# Import PyGTK.
import gobject, pygtk, gtk, pango

# Import Sugar UI modules.
import sugar.activity.activity
import sugar.graphics.style
import sugar.graphics.icon

class EditLessonScreen(gtk.VBox):
    def __init__(self, activity, lesson):
        gtk.VBox.__init__(self)
        self.set_border_width(10)

        self.activity = activity
        self.lesson = lesson

        self.in_build = False
        
        # Add the header.
        title = gtk.Label()
        title.set_markup("<span size='20000'><b>" + _("Edit a Lesson") + "</b></span>")
        title.set_alignment(1.0, 0.0)
        
        stoplabel = gtk.Label(_('Go Back'))
        stopbtn = gtk.Button()
        stopbtn.add(stoplabel)
        stopbtn.connect('clicked', self.stop_clicked_cb)
       
        titlebox = gtk.HBox()
        titlebox.pack_start(stopbtn, False, False, 10)
        titlebox.pack_end(title, False, False, 10)

        self.vp = gtk.Viewport()

        self.scroll = gtk.ScrolledWindow()
        self.scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.scroll.add(self.vp)

        self.pack_start(titlebox, False, False, 10)
        self.pack_start(gtk.HSeparator(), False, False, 0)
        self.pack_start(self.scroll, True, True, 0)

        self.build()
        
        self.show_all()

    def build_step(self, step, idx):
        stepbox = gtk.VBox()

        steplabel = gtk.Label()
        steplabel.set_markup("<span size='x-large' weight='bold'>" + (_('Step #%d') % (idx+1)) + "</span>")
        steplabel.set_alignment(0.0, 0.5)
        steplabel.set_padding(10, 0)

        generatelabel = gtk.Label()
        generatelabel.set_markup(_('Generate'))
        generatebtn = gtk.Button()
        generatebtn.add(generatelabel)
        delstepbtn = gtk.Button()
        delstepbtn.add(sugar.graphics.icon.Icon(icon_name='list-remove'))
        delstepbtn.connect('clicked', self.del_step_clicked_cb, idx)
        addstepbtn = gtk.Button()
        addstepbtn.add(sugar.graphics.icon.Icon(icon_name='list-add'))
        addstepbtn.connect('clicked', self.add_step_clicked_cb, idx)
        moveupbtn = gtk.Button()
        moveupbtn.add(sugar.graphics.icon.Icon(icon_name='go-up'))
        moveupbtn.connect('clicked', self.move_step_up_clicked_cb, idx)
        movedownbtn = gtk.Button()
        movedownbtn.add(sugar.graphics.icon.Icon(icon_name='go-down'))
        movedownbtn.connect('clicked', self.move_step_down_clicked_cb, idx)

        btnbox = gtk.HBox()
        btnbox.pack_start(steplabel, False, False)
        btnbox.pack_end(addstepbtn, False, False)
        btnbox.pack_end(delstepbtn, False, False)
        btnbox.pack_end(moveupbtn, False, False)
        btnbox.pack_end(movedownbtn, False, False)
        btnbox.pack_end(generatebtn, False, False)

        instlabel = gtk.Label()
        instlabel.set_markup("<span size='large' weight='bold'>" + _('Instructions') + "</span>")
        instlabel.set_alignment(0.0, 0.5)
        instlabel.set_padding(20, 0)

        stepbox.insttext = gtk.TextView(gtk.TextBuffer())
        stepbox.insttext.props.wrap_mode = gtk.WRAP_WORD
        instscroll = gtk.ScrolledWindow()
        instscroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        instscroll.add(stepbox.insttext)
        instscroll.set_size_request(-1, 75)
        stepbox.insttext.get_buffer().set_text(step['instructions'])

        instbox = gtk.HBox()
        instbox.pack_start(instlabel, False, False)
        instbox.pack_start(instscroll, True, True)

        textlabel = gtk.Label()
        textlabel.set_markup("<span size='large' weight='bold'>" + _('Text') + "</span>")
        textlabel.set_alignment(0.0, 0.5)
        textlabel.set_padding(20, 0)

        stepbox.texttext = gtk.TextView(gtk.TextBuffer())
        stepbox.texttext.props.wrap_mode = gtk.WRAP_WORD
        textscroll = gtk.ScrolledWindow()
        textscroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        textscroll.add(stepbox.texttext)
        textscroll.set_size_request(-1, 100)
        stepbox.texttext.get_buffer().set_text(step['text'])

        textbox = gtk.HBox()
        textbox.pack_start(textlabel, False, False)
        textbox.pack_start(textscroll, True, True)

        sizegroup = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)   
        sizegroup.add_widget(instlabel)
        sizegroup.add_widget(textlabel)    

        stepbox.pack_start(btnbox, False, False, 10)
        stepbox.pack_start(instbox, False, False, 10)
        stepbox.pack_start(textbox, False, False, 10)
        
        return stepbox

    def build(self):
        self.in_build = True
        
        # Add the editing controls.
        detailslabel = gtk.Label()
        detailslabel.set_markup("<span size='x-large'><b>" + _('Lesson Details') + "</b></span>")
        detailslabel.set_alignment(0.0, 0.5)
        detailslabel.set_padding(10, 0)

        namelabel = gtk.Label()
        namelabel.set_markup("<span size='large' weight='bold'>" + _('Name') + "</span>")
        namelabel.set_alignment(0.0, 0.5)
        namelabel.set_padding(20, 0)

        self.nameent = gtk.Entry()
        self.nameent.set_text(self.lesson['name'])

        namebox = gtk.HBox()
        namebox.pack_start(namelabel, False, False)
        namebox.pack_start(self.nameent, True, True)
        
        typelabel = gtk.Label()
        typelabel.set_markup("<span size='large' weight='bold'>" + _('Type') + "</span>")
        typelabel.set_alignment(0.0, 0.5)
        typelabel.set_padding(20, 0)

        self.textradio = gtk.RadioButton(None, _('Normal Lesson'))
        self.textradio.connect('toggled', self.type_toggled_cb)
        
        self.balloonradio = gtk.RadioButton(self.textradio, _('Balloon Game'))
        self.balloonradio.connect('toggled', self.type_toggled_cb)
        
        self.textradio.set_active(self.lesson['type'] == 'normal')
        self.balloonradio.set_active(self.lesson['type'] == 'balloon')        

        typebox = gtk.HBox()
        typebox.pack_start(typelabel, False, False)
        typebox.pack_start(self.textradio, False, False)
        typebox.pack_start(self.balloonradio, False, False)
        
        desclabel = gtk.Label()
        desclabel.set_markup("<span size='large' weight='bold'>" + _('Description') + "</span>")
        desclabel.set_alignment(0.0, 0.5)
        desclabel.set_padding(20, 0)

        self.desctext = gtk.TextView(gtk.TextBuffer())
        self.desctext.props.wrap_mode = gtk.WRAP_WORD
        descscroll = gtk.ScrolledWindow()
        descscroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        descscroll.add(self.desctext)
        descscroll.set_size_request(-1, 75)
        self.desctext.get_buffer().set_text(self.lesson['description'])

        descbox = gtk.HBox()
        descbox.pack_start(desclabel, False, False)
        descbox.pack_start(descscroll, True, True)

        sizegroup = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)   
        sizegroup.add_widget(namelabel)
        sizegroup.add_widget(typelabel)    
        sizegroup.add_widget(desclabel)    

        self.vbox = gtk.VBox()
        self.vbox.set_border_width(20)
        self.vbox.pack_start(detailslabel, False, False, 10)        
        self.vbox.pack_start(namebox, False, False, 10)
        self.vbox.pack_start(typebox, False, False, 10)
        self.vbox.pack_start(descbox, False, False, 10)
        self.vbox.pack_start(gtk.HSeparator(), False, False, 0)
        
        if self.lesson['type'] == 'normal':
            if not self.lesson.has_key('steps') or len(self.lesson['steps']) == 0:
                step = { 'instructions': '', 'text': '' }
                self.lesson['steps'] = [ step ]
            
            self.stepboxes = []
            
            for step in self.lesson['steps']:
                stepbox = self.build_step(step, len(self.stepboxes))
                self.stepboxes.append(stepbox)
                
                self.vbox.pack_start(stepbox, False, False, 0)
                
        if self.lesson['type'] == 'balloon':
            if not self.lesson.has_key('words') or len(self.lesson['words']) == 0:
                self.lesson['words'] = []
            
            textlabel = gtk.Label()
            textlabel.set_markup("<span size='large' weight='bold'>" + _('Words') + "</span>")
            textlabel.set_alignment(0.0, 0.5)
            textlabel.set_padding(20, 0)

            self.wordstext = gtk.TextView(gtk.TextBuffer())
            self.wordstext.props.wrap_mode = gtk.WRAP_WORD
            textscroll = gtk.ScrolledWindow()
            textscroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
            textscroll.add(self.wordstext)
            textscroll.set_size_request(-1, 200)
            self.wordstext.get_buffer().set_text(' '.join(self.lesson['words']))
    
            textbox = gtk.HBox()
            textbox.pack_start(textlabel, False, False)
            textbox.pack_start(textscroll, True, True)
            
            self.vbox.pack_start(textbox, False, False, 10)

        self.vbox.show_all()
        
        self.in_build = False
        
        # Remove any existing controls.
        if self.vp.get_child():
            self.vp.remove(self.vp.get_child())
        
        self.vp.add(self.vbox)
    
    def stop_clicked_cb(self, btn):
        self.save()
        
        self.activity.pop_screen()
    
    def save(self):
        self.lesson['name'] = self.nameent.get_text()
        
        buf = self.desctext.get_buffer()
        self.lesson['description'] = buf.get_text(buf.get_start_iter(), buf.get_end_iter())
        
        if self.textradio.get_active():
            self.lesson['type'] = 'normal'
       
            steps = []
            for sb in self.stepboxes:
                step = {}
                
                buf = sb.insttext.get_buffer()
                step['instructions'] = buf.get_text(buf.get_start_iter(), buf.get_end_iter())
                
                buf = sb.texttext.get_buffer()
                step['text'] = buf.get_text(buf.get_start_iter(), buf.get_end_iter())
                
                steps.append(step)
                
            self.lesson['steps'] = steps

        if self.balloonradio.get_active():
            self.lesson['type'] = 'balloon'
            
            buf = self.wordstext.get_buffer()
            text = buf.get_text(buf.get_start_iter(), buf.get_end_iter())
            self.lesson['words'] = text.split(' ')

    def add_step_clicked_cb(self, btn, index):
        step = { 'instructions': '', 'text': '' }
        self.lesson['steps'].insert(index, step)
        self.build()
    
    def del_step_clicked_cb(self, btn, index):
        self.lesson['steps'].pop(index)
        self.build()
    
    def move_step_up_clicked_cb(self, btn, index):
        if index > 0:
            step = self.lesson['steps'].pop(index)
            self.lesson['steps'].insert(index-1, step)
            self.build()
    
    def move_step_down_clicked_cb(self, btn, index):
        if index < len(self.lesson['steps']) - 1:
            step = self.lesson['steps'].pop(index)
            self.lesson['steps'].insert(index+1, step)
            self.build()

    def type_toggled_cb(self, btn):
        # Prevent infinite recursion
        if self.in_build:
            return
        
        if self.textradio.get_active():
            self.lesson['type'] = 'normal'
        if self.balloonradio.get_active():
            self.lesson['type'] = 'balloon'
        self.build()