Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/atoidejouer/ui/notebook.py
blob: 396e444927a0349539f8d586156ca213419a92d6 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

# python import
import logging, os
# ..
from functools import partial
from gettext import gettext as _

# gtk import
import gobject, gtk, glib

# sugar import
from sugar.activity import activity

# atoidejouer import
from atoidejouer.tools import image, storage, ui

# get application logger
logger = logging.getLogger('atoidejouer')


COLOR_GREY_DARK   = ui.get_color(0.5, 0.5, 0.5)
COLOR_GREY_LIGHT  = ui.get_color(0.7, 0.7, 0.7)
COLOR_GREY_WHITE  = ui.get_color(0.85, 0.85, 0.85)
COLOR_WHITE       = ui.get_color(1.0, 1.0, 1.0)


def _cb_swicth_page(widget, page, page_num, notebook):
    if page_num == 1:
        notebook.remove_button.show()
    else:
        notebook.remove_button.hide()
        notebook.remove_label.hide()
    # get current child
    _child = widget.get_nth_page(page_num)
    # get current label
    _label = widget.get_tab_label(_child)
    # force white
    glib.idle_add(partial(_label.modify_fg, gtk.STATE_NORMAL, COLOR_WHITE))


def _get_seq_filenames(sequence_path):
    _f = open(sequence_path)
    _seq_names = _f.readlines()
    _f.close()
    # ..
    return _seq_names


def _cb_cursor_changed(treeview, notebook, type_):
    # get the current cursor and path
    _path, _column = treeview.get_cursor()
    # get model
    _model = treeview.get_model()
    # get resource info
    _info = _model
    # get iter
    _iter = _model.get_iter(_path)
    # update current
    if type_ in ['graphics', 'sounds']:
        # get value
        _filename = _model.get_value(_iter, 1)
        # udpate preview
        notebook.screen.item_preview.set_item(filename=_filename)
        # update current
        notebook.current_item = _filename
    # update current
    elif type_ == 'journal':
        # get value
        _pixbuf = _model.get_value(_iter, 0)
        _filename = _model.get_value(_iter, 1)
        _metadata = _model.get_value(_iter, 2)
        # ..
        storage.png_from_pixbuf(_filename, _metadata['activity_id'],
                _metadata['timestamp'])
        # udpate preview
        notebook.screen.item_preview.set_item(filename=_filename)
        # update current
        notebook.current_item = _filename
        # ..
        notebook._get_store_graphic()
    elif type_ == 'sequence':
        # clear sequence preview
        notebook.screen.sequence_preview.clear()
        # get value
        _sequence_path = _model.get_value(_iter, 2)
        # update sequence ..
        for _g in _get_seq_filenames(_sequence_path):
            notebook.screen.sequence_preview.add_item(_g.strip())
        # get sequence name
        _sequence_name = _model.get_value(_iter, 1)
        # get toolbar sequence entry
        _entry = notebook.screen.toolbar._sequence_entry
        # update toolbar entry
        _entry.set_text(_sequence_name)
        # update current
        notebook.current_sequence = _sequence_name
        # ..
        notebook.update_store_sequence()
    else:
        pass


def _get_seq_filenames(sequence_path):
    _f = open(sequence_path)
    _seq_names = _f.readlines()
    _f.close()
    # ..
    return [_n.strip() for _n in _seq_names]


def __remove_filename_from_sequence(type_, sequence, filename):
    # little check
    if filename.strip() == '':
        # do nothing
        return True
    else:
        # get sequence path
        _seq_path = storage.get_sequence_path(type_, sequence)
        # get current names
        _current_filenames = _get_seq_filenames(_seq_path)
        # has remaining files
        _remain = False
        if filename in _current_filenames:
            _current_filenames.remove(filename)
        else:
            return True
        # ..
        if len(_current_filenames) == 0:
            # ..
            os.remove(_seq_path)
            # ..
            return False
        else:
            # open file
            _file = open(_seq_path, 'wb')
            # update
            for _fname in _current_filenames:
                _file.write('%s\n' % _fname)
            # ..
            _file.close()
            # ..
            return True


def _on_button_click(button, notebook):
    # get file path
    if notebook._type == 'graphics':
        _path = storage.get_image_path(notebook.current_item)
    elif notebook._type == 'sounds':
        _path = storage.get_sound_path(notebook.current_item)
    # do nothing
    else:
        return
    # remove file
    os.remove(_path)
    # shortcuts
    _screen = notebook.screen
    _toolbar = _screen.toolbar
    _activity = _toolbar.activity
    # ..
    _keys = _activity.graphic_keys\
            if _toolbar.name == 'graphics_add'\
            else _activity.sound_keys
    # ..
    _type = 'graphics' if _toolbar.name == 'graphics_add' else 'sounds'
    # get sequence names
    for _seq_name in _keys._names:
        # remove from file system
        if __remove_filename_from_sequence(_type, _seq_name,
                notebook.current_item) is True:
            # remove from keys
            _keys.remove_filename_from_all(_seq_name, notebook.current_item)
        # remove the entire sequence
        else:
            # from keys
            _keys.remove_sequence(_seq_name)
    # ..
    notebook._get_store_sequence()
    # remove item in treview
    notebook.remove_current_row()
    # remove current sequence
    _screen.sequence_preview.remove_item(notebook.current_item)
    # avoid current
    notebook.current_item = None


class ResourceNotebook(gtk.Frame):

    def __init__(self, screen, type_):
        # init parent
        gtk.Frame.__init__(self)
        self.set_shadow_type(gtk.SHADOW_NONE)
        # do show
        self.show()
        # keep the screen
        self.screen = screen
        # keep the type
        self._type = type_
        # keep the current item
        self.current_item = None
        self.current_sequence = None
        # ..
        self.library_treeview = None
        # keep stores
        self._store_sequence = None
        self._store_graphic = None
        self._store_sound = None
        self._store_journal = None
        # init main box
        _main_box = gtk.VBox(homogeneous=False, spacing=0)
        _main_box.show()
        # ..
        self.add(_main_box)
        # init remove button and label
        self.remove_button, self.remove_label = self._get_remove_button()
        # ..
        _main_box.pack_start(self._get_notebook(), expand=True, fill=True)
        # ..
        _main_box.pack_start(self.remove_button, expand=False, fill=True)
        _main_box.pack_start(self.remove_label, expand=False, fill=True)

    def _get_remove_button(self):
        # init button
        _button = ui.get_button(label=_('Remove Item from Disk'),
                stock_id='delete', width=48, padding=(5, 0))
        _button.set_border_width(2)
        _button.hide()
        # set cb
        _button.connect('clicked', _on_button_click, self)
        # init 
        _label = gtk.Label()
        # set markup
        _label.set_use_markup(True)
        # align
        _label.set_padding(5, 5)
        _label.set_alignment(0, 0.5)
        # return it
        return _button, _label

    def remove_current_row(self):
        # get the current cursor and path
        _path, _column = self.library_treeview.get_cursor()
        # get model
        _model = self.library_treeview.get_model()
        # get resource info
        _info = _model
        # get iter
        _iter = _model.get_iter(_path)
        # and remove
        _model.remove(_iter)

    def update_store_sequence(self):
        # .. 
        if self.current_sequence is None:
            return
        else:
            # ..
            _gfx_filename = storage.get_sequence_first_graphic_name(
                    self._type, self.current_sequence)
            # set current
            self.screen.sequence_preview.set_current(_gfx_filename)

    def _get_store_sequence(self):
        # init store
        if self._store_sequence is None:
            self._store_sequence = gtk.ListStore(gtk.gdk.Pixbuf,
                    gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
        else:
            self._store_sequence.clear()
        # prepare dir path
        _seq_dir = os.path.join(activity.get_activity_root(), 'data',
                                'sequences', self._type)
        # ..
        for _filename in os.listdir(_seq_dir):
            # ..
            _path = os.path.join(_seq_dir, _filename)
            # little check
            if os.path.isfile(_path):
                # check name
                try:
                    _filename, _ext = os.path.splitext(_filename)
                    # check ext
                    if _ext == '.seq':
                        pass
                    else:
                        continue
                except Exception, e:
                    # TODO log something
                    continue
                # ...
                _pixbuf = image.get_sequence_first_graphic(self._type,
                        _filename)
                # do update
                self._store_sequence.append([_pixbuf, _filename, _path])
        # return the store
        return self._store_sequence

    def _get_store_graphic(self):
        # init/reset store
        if self._store_graphic is None:
            self._store_graphic = gtk.ListStore(gtk.gdk.Pixbuf,
                    gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
        else:
            self._store_graphic.clear()
        # ..
        _graphic_dir = os.path.join(activity.get_activity_root(), 'data',
                                    'graphics')
        # ..
        for _filename in os.listdir(_graphic_dir):
            # ..
            _path = os.path.join(_graphic_dir, _filename)
            # little check
            if os.path.isfile(_path):
                # check name
                try:
                    _filename, _ext = os.path.splitext(_filename)
                    # check ext
                    if _ext in ['.png']:
                        pass
                    else:
                        continue
                except Exception, e:
                    # TODO log something
                    continue
                # ..
                _pixbuf = image.get_pixbuf(_path, 64, 48)
                # do update
                self._store_graphic.append([_pixbuf, _filename, _path])
            else:
                continue
        # return the store
        return self._store_graphic

    def _get_store_sound(self):
        # init/reset store
        if self._store_sound is None:
            self._store_sound = gtk.ListStore(gtk.gdk.Pixbuf,
                    gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
        else:
            self._store_sound.clear()
        # prepare icon path
        _pix_path = storage.get_image_path('sound', dir_='data')
        # ..
        _graphic_dir = os.path.join(activity.get_activity_root(), 'data',
                'sounds')
        # ..
        for _filename in os.listdir(_graphic_dir):
            # ..
            _path = os.path.join(_graphic_dir, _filename)
            # little check
            if os.path.isfile(_path):
                # check name
                try:
                    _filename, _ext = os.path.splitext(_filename)
                    # check ext
                    if _ext in ['.ogg']:
                        pass
                    else:
                        continue
                except Exception, e:
                    # TODO log something
                    continue
                # ..
                _pixbuf = image.get_pixbuf(_pix_path, 64, 48)
                # do update
                self._store_sound.append([_pixbuf, _filename, _path])
            else:
                continue
        # return the store
        return self._store_sound

    def _get_store_journal(self):
        # init/reset store
        if self._store_journal is None:
            self._store_journal = gtk.ListStore(gtk.gdk.Pixbuf,
                    gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
        else:
            self._store_journal.clear()
        # app to get the journal entries
        _app_name = 'paint' if self._type=='graphics' else 'record'
        # update store
        for _i in storage.list_info_from_journal(_app_name):
            # prepare preview
            _pixbuf = storage.get_pixbuf_from_data(_i['preview'],
                                                   size=(64, 48))
            # store info
            _store_info = {
                    'activity_id':_i['activity_id'],
                    'timestamp': _i['timestamp']
                    }
            # do update
            self._store_journal.append([_pixbuf, _i['title'], _store_info])
        # return the store
        return self._store_journal

    def _get_treeview(self, type_=None):
        # ..
        _scrolled_win = gtk.ScrolledWindow()
        _scrolled_win.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        _scrolled_win.show()
        # prepare colums
        _col_preview = gtk.TreeViewColumn(_('Preview'))
        _col_description = gtk.TreeViewColumn(_('Name'))
        # set renderers
        _cell_pix = gtk.CellRendererPixbuf()
        _cell_pix.set_property('height', 50)
        _col_preview.pack_start(_cell_pix, expand=False)
        _col_preview.add_attribute(_cell_pix, 'pixbuf', 0)
        # ..
        _cell_text = gtk.CellRendererText()
        _cell_text.set_property('height', 50)
        _col_description.pack_start(_cell_text, expand=True)
        _col_description.add_attribute(_cell_text, 'text', 1)
        # update store
        if type_ == 'sequence':
            _store = self._get_store_sequence()
        elif type_ == 'graphics':
            _store = self._get_store_graphic()
        elif type_ == 'sounds':
            _store = self._get_store_sound()
        elif type_ == 'journal':
            _store = self._get_store_journal()
        else:
            return None
        # init treeview
        _treeview = gtk.TreeView(_store)
        _treeview.set_reorderable(False)
        # cb
        _treeview.connect('cursor-changed', _cb_cursor_changed, self, type_)
        # show it
        _treeview.show()
        # add columns
        _treeview.append_column(_col_preview)
        _treeview.append_column(_col_description)
        # ..
        _scrolled_win.add(_treeview)
        # return it
        return _scrolled_win

    def _get_notebook(self):
        # get list 1
        _list1 = self._get_treeview(type_='sequence')
        # create a label for the button
        _label1 = gtk.Label(_('Sequences'))
        # ...
        _label1.modify_fg(gtk.STATE_ACTIVE, COLOR_WHITE)
        # ...
        _label1.show()
        # get list 2
        _list2 = self._get_treeview(type_=self._type)
        # keep library treeview
        self.library_treeview = _list2.get_children()[0]
        # create a label for the button
        _label2 = gtk.Label(_('Library'))
        # ...
        _label2.modify_fg(gtk.STATE_ACTIVE, COLOR_WHITE)
        # ...
        _label2.show()
        # get list 2
        _list3 = self._get_treeview(type_='journal')
        # create a label for the button
        _label3 = gtk.Label(_('Journal'))
        # ...
        _label3.modify_fg(gtk.STATE_ACTIVE, COLOR_WHITE)
        # ...
        _label3.show()
        # init notebook
        _notebook = gtk.Notebook()
        _notebook.set_show_border(True)
        _notebook.set_border_width(2)
        _notebook.set_tab_pos(gtk.POS_TOP)
        _notebook.set_size_request(360, -1)
        # ..
        _notebook.modify_bg(gtk.STATE_NORMAL, COLOR_GREY_DARK)
        _notebook.modify_bg(gtk.STATE_ACTIVE, COLOR_GREY_LIGHT)
        # ..
        _notebook.connect('switch-page', _cb_swicth_page, self)
        # ..
        _notebook.show()
        # add tabs
        _notebook.append_page(_list1, _label1)
        _notebook.append_page(_list2, _label2)
        _notebook.append_page(_list3, _label3)
        # return it
        return _notebook