Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/cardlist.py
blob: 35df613bccb0c7688a7c13c17caea97a5eb68224 (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
#! /usr/bin/env python
#
#    Copyright (C) 2006, 2007, One Laptop Per Child
#
#    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., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import gtk
import svgcard
import logging
from os.path import join
import os
import model
import zipfile
import tempfile
import random
from gobject import SIGNAL_RUN_FIRST, TYPE_PYOBJECT

from sugar import profile
from sugar.datastore import datastore

_logger = logging.getLogger('memorize-activity')

class CardList(gtk.EventBox):
        
    __gsignals__ = {
        'pair-selected': (SIGNAL_RUN_FIRST, None, 6 * [TYPE_PYOBJECT]), 
        'update-create-toolbar': (SIGNAL_RUN_FIRST, None, 3 * [TYPE_PYOBJECT]), 
        'update-create-buttons': (SIGNAL_RUN_FIRST, None, 2 * [TYPE_PYOBJECT]), 
    }
    
    def __init__(self):
        gtk.EventBox.__init__(self)
        self.model = model.Model(os.path.dirname(__file__))
        self.pairs = []
        self.current_pair = None
        
        self.set_size_request(450, 150)        
        self.vbox = gtk.VBox(False)        
        
        fill_box = gtk.Label()
        fill_box.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#000000'))
        fill_box.show()
        self.vbox.pack_end(fill_box, True, True)
                   
        scroll = gtk.ScrolledWindow()
        scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
        scroll.add_with_viewport(self.vbox)
        scroll.set_border_width(0)
        scroll.get_child().modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#000000'))
        self.add(scroll)
        self.show_all()
        
    def load_game(self, widget, game_name):
        self.model.read(game_name)
        self.current_game_key = self.model.data['game_file']
        self.emit('update-create-toolbar', self.model.data['name'], self.model.data.get('equal_pairs', 'False'), self.model.data.get('divided', '0'))
        game_pairs = self.model.pairs
        game_data = self.model.data
        self.clean_list()
        for key in game_pairs:
            if game_pairs[key].props.aimg != None:
                aimg = gtk.gdk.pixbuf_new_from_file(os.path.join(self.model.data['pathimg'], game_pairs[key].props.aimg))
            else:
                aimg = None
                
            if game_pairs[key].props.bimg != None:
                bimg = gtk.gdk.pixbuf_new_from_file(os.path.join(self.model.data['pathimg'], game_pairs[key].props.bimg))
            else:
                bimg = None
            
            if game_pairs[key].props.asnd != None:
                asnd = os.path.join(self.model.data['pathsnd'], game_pairs[key].props.asnd)
            else:
                asnd = None
            
            if game_pairs[key].props.bsnd != None:            
                bsnd = os.path.join(self.model.data['pathsnd'], game_pairs[key].props.bsnd)
            else:
                bsnd = None
                
            self.add_pair(None, game_pairs[key].props.achar, game_pairs[key].props.bchar, aimg, bimg, asnd, bsnd, False)
        
    def save_game(self, widget, game_name, equal_pairs, grouped):
        
        temp_folder = tempfile.mkdtemp()
        temp_img_folder = join(temp_folder, 'images')
        temp_snd_folder = join(temp_folder, 'sounds')

        os.makedirs(temp_img_folder)
        os.makedirs(temp_snd_folder)
                    
        zip = zipfile.ZipFile(join(temp_folder, 'game.zip'), 'w')
        
        game_model = model.Model(temp_folder)
        game_model.data['name'] = game_name
        for pair in range(len(self.pairs)):
            pair_card = model.Pair()
            
            # achar
            achar = self.pairs[pair].get_text(1)
            if achar != '':
                pair_card.set_property('achar', achar)
            
            # bchar
            bchar = self.pairs[pair].get_text(2)
            if bchar != '':
                pair_card.set_property('bchar', bchar)

            # aimg
            aimg = self.pairs[pair].get_pixbuf(1)
            if aimg != None:
                
                if equal_pairs:
                    aimgfile = 'img'+str(pair)+'.jpg'
                else:
                    aimgfile = 'aimg'+str(pair)+'.jpg'
                aimg.save(join(temp_img_folder, aimgfile), 'jpeg', {'quality':'85'})
                zip.write(join(temp_img_folder, aimgfile), join('images', aimgfile))
                pair_card.set_property('aimg', aimgfile)

            # bimg
            bimg = self.pairs[pair].get_pixbuf(2)
            if bimg != None:
                if equal_pairs:
                    bimgfile = 'img'+str(pair)+'.jpg'
                else:
                    bimgfile = 'bimg'+str(pair)+'.jpg'
                    bimg.save(join(temp_img_folder, bimgfile), 'jpeg', {'quality':'85'})
                    zip.write(join(temp_img_folder, bimgfile), join('images', bimgfile))
                pair_card.set_property('bimg', bimgfile)
            # asnd
            asnd = self.pairs[pair].get_sound(1)
            if asnd != None:
                if equal_pairs:
                    asndfile = 'snd'+str(pair)+'.wav'
                else:
                    asndfile = 'asnd'+str(pair)+'.wav'    
                _logger.error(asndfile+': '+ asnd)    
                zip.write(asnd, join('sounds', asndfile))
                pair_card.set_property('asnd', asndfile)

            # bsnd
            bsnd = self.pairs[pair].get_sound(2)
            if bsnd != None:
                if equal_pairs:
                    bsndfile = 'snd'+str(pair)+'.wav'
                else:
                    bsndfile = 'bsnd'+str(pair)+'.wav'    
                    zip.write(bsnd, join('sounds', bsndfile))
                _logger.error(bsndfile+': '+ bsnd)    
                pair_card.set_property('bsnd', bsndfile)
                
            game_model.pairs[pair] = pair_card
        game_model.write(equal_pairs, grouped)
        zip.write(join(temp_folder, 'game.xml'), 'game.xml')
        zip.close()
        
        # Saves the zip in datastore
        gameObject = datastore.create()
        gameObject.metadata['title'] = game_name
        gameObject.metadata['mime_type'] = 'application/memorizegame'
        gameObject.metadata['icon-color'] = profile.get_color().to_string()
        gameObject.file_path = join(temp_folder, 'game.zip')
        datastore.write(gameObject)        
        
    def clean_list(self, button = None):
        if button != None:
            self.current_game_key = None
        map(lambda x: self.vbox.remove(x), self.pairs)
        del self.pairs
        self.pairs = []
        
    def clean_tmp_folder(self, path):
        for root, dirs, files in os.walk(path, topdown=False):
            for name in files:
                os.remove(join(root, name))
            for name in dirs:
                os.rmdir(join(root, name))
        os.rmdir(path)
    
    def add_pair(self, widget, achar, bchar, aimg, bimg, asnd, bsnd, show = True):
        pair = Pair(achar, bchar, aimg, bimg, asnd, bsnd)
        self.vbox.pack_end(pair, False, True)
        self.pairs.append(pair)
        pair.connect('pair-selected', self.set_selected)
        pair.connect('pair-closed', self.rem_pair)
        self.emit('update-create-buttons', True, True)
        if show:
            self.show_all()
            
    def rem_pair(self, widget, event):
        self.vbox.remove(widget)        
        self.pairs.remove(widget)
        del widget
        self.emit('update-create-buttons', True, True)
            
    def set_selected(self, widget, event):
        if self.current_pair <> None:
            self.old = self.current_pair
            self.old.set_selected(False)
        self.current_pair = widget 
        widget.set_selected(True)
        self.emit('pair-selected', self.current_pair.get_text(1), self.current_pair.get_text(2), self.current_pair.get_pixbuf(1), self.current_pair.get_pixbuf(2), self.current_pair.get_sound(1), self.current_pair.get_sound(2))
        
    def update_selected(self, widget, newtext1, newtext2, aimg, bimg, asnd, bsnd):
        self.current_pair.change_text(newtext1, newtext2)
        self.current_pair.change_pixbuf(aimg, bimg)
        self.current_pair.change_sound(asnd, bsnd)
        
        self.emit('update-create-buttons', True, True)
        
class Pair(gtk.EventBox):
    
    __gsignals__ = {
        'pair-selected': (SIGNAL_RUN_FIRST, None, [TYPE_PYOBJECT]), 
        'pair-closed': (SIGNAL_RUN_FIRST, None, [TYPE_PYOBJECT]), 
    }
    
    def __init__(self, text1, text2 = None, aimg = None, bimg = None, asnd = None, bsnd = None):
        gtk.EventBox.__init__(self)
        self.bg_color = '#000000'
        if text2 == None:
            self.text2 = text1
        else: 
            self.text2 = text2
        self.text1 = text1
        
        self.asnd = asnd
        self.bsnd = bsnd
        
        self.current_game_key = None
        
        close_button = gtk.Button('X')
        close_button.connect('button-press-event', self.emit_close)
        table = gtk.Table()
        table.connect('button-press-event', self.emit_selected)
        table.set_col_spacings(0)
        table.set_border_width(10)
        self.bcard1 = svgcard.SvgCard(-1, {'front_text':{'card_text':text1, 'text_color':'#ffffff'}, 'front':{'fill_color':'#4c4d4f', 'stroke_color':'#ffffff', 'opacity':'1'}}, None, 184, 1, self.bg_color)
        self.bcard2 = svgcard.SvgCard(-1, {'front_text':{'card_text':text2, 'text_color':'#ffffff'}, 'front':{'fill_color':'#4c4d4f', 'stroke_color':'#ffffff', 'opacity':'1'}}, None, 184, 1, self.bg_color)

        self.bcard1.flip()
        self.bcard2.flip()
        self.bcard1.set_pixbuf(aimg)
        self.bcard2.set_pixbuf(bimg)
        
        table.attach(self.bcard1, 0, 1, 0, 8)
        table.attach(self.bcard2, 1, 2, 0, 8)
        table.attach(close_button, 2, 3, 0, 1, gtk.FILL, gtk.FILL)
        
        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(self.bg_color))
        self.add(table)
        self.show_all()

    def emit_selected(self, widget, event):
        self.emit('pair-selected', self)

    def emit_close(self, widget, event):
        self.emit('pair-closed', self)

    def set_selected(self, status):
        if not status:
            self.bg_color = '#000000'
        else:
            self.bg_color = '#b2b3b7'
            
        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(self.bg_color))
        self.bcard1.set_background(self.bg_color)
        self.bcard2.set_background(self.bg_color)
        
    def change_pixbuf(self, aimg, bimg):
        self.bcard1.set_pixbuf(aimg)
        self.bcard2.set_pixbuf(bimg)
    
    def change_text(self, text1, text2):
        self.bcard1.change_text(text1)
        self.bcard2.change_text(text2)

    def change_sound(self, asnd, bsnd):
        self.asnd = asnd
        self.bsnd = bsnd
    
    def get_text(self, card):
        if card == 1:
            return self.bcard1.get_text()
        else:
            return self.bcard2.get_text()        
        
    def get_pixbuf(self, card):
        if card == 1:
            return self.bcard1.get_pixbuf()
        else:
            return self.bcard2.get_pixbuf()
        
    def get_sound(self, card):
        if card == 1:
            return self.asnd
        else:
            return self.bsnd