Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/atoidejouer/ui/panel/notebook.py
blob: 63e1be30b22825e43598980b9a9f7454c8fca53f (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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512

# python import
import logging, os, shutil
# ..
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
from atoidejouer.ui.panel import PanelEdit

# 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 _set_bg(widget):
    # bg
    widget.modify_bg(gtk.STATE_NORMAL, COLOR_GREY_WHITE)
    widget.modify_bg(gtk.STATE_PRELIGHT, COLOR_GREY_WHITE)
    widget.modify_bg(gtk.STATE_ACTIVE, COLOR_WHITE)
    # fg
    widget.modify_fg(gtk.STATE_NORMAL, COLOR_WHITE)


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))
    # _label.modify_fg(gtk.STATE_NORMAL, COLOR_WHITE)
    _set_bg(_label)


def _cb_cursor_changed(treeview, notebook, type_):
    pass
    """
    # get the current cursor and path
    _path, _column = treeview.get_cursor()
    # DEBUG
    if _column is None:
        return
    else:
        pass
    # 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, 3)
        # copy journal item to library to work on it
        if notebook._type == 'graphics':
            storage.png_from_pixbuf(_filename, _metadata['timestamp'])
            # ..
            notebook._get_store_graphic()
        # for sound
        else:
            # get source path
            _src_path = storage.get_path_from_journal(_metadata['timestamp'],
                    'audio/ogg')
            # get dst path
            _dst_path = storage.get_sound_path(_filename)
            # copy
            shutil.copy(_src_path, _dst_path)
            # ..
            notebook._get_store_sound()
        # udpate preview
        notebook.screen.item_preview.set_item(filename=_filename)
        # update current
        notebook.current_item = _filename
    elif type_ == 'sequence':
        # clear sequence preview
        notebook.screen.sequence_preview.clear()
        # get value
        _sequence_path = _model.get_value(_iter, 2)
        # update sequence ..
        _fnames = storage.get_sequence_items(_sequence_path)
        for _f in _fnames:
            notebook.screen.sequence_preview.add_item(_f.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()
    """
    """
    # .. add action - add column as no title
    if _column.get_title() is None:
        # get graphic name
        _item_name = notebook.current_item
        _sequence_name = notebook.current_sequence
        if _sequence_name is None\
        or _sequence_name.strip() == ''\
        or _item_name is None:
            pass
        else:
            # udpate sequence preview
            notebook.screen.sequence_preview.add_item(_item_name)
            # update sequence file
            storage.sequence_save(notebook.screen.toolbar)
    else:
        pass
    """


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


def _on_button_click(button, notebook):
    pass
    """
    # 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'
    # prepare seq path
    _seq_dir = os.path.join(activity.get_activity_root(), 'data',
                            'sequences', _type)
    # get sequence names
    for _seq_name in os.listdir(_seq_dir):
        # get sequence name from corresponding file name
        _seq_name = _seq_name.replace('.seq', '')
        # 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 PanelNotebook(gtk.Frame):

    def __init__(self, screen):
        # init parent
        gtk.Frame.__init__(self)
        self.set_shadow_type(gtk.SHADOW_NONE)
        # keep the screen
        self.screen = screen
        # keep the current item
        self.current_item = None
        # ..
        self._graphics_treeview = None
        self._sounds_treeview = None
        # keep stores
        self._store_graphic = None
        self._store_sound = None
        # init main box
        _main_box = gtk.VBox(homogeneous=False, spacing=0)
        _main_box.show()
        # ..
        self.add(_main_box)
        # and the notebook
        _main_box.pack_start(self._get_notebook(), expand=True, fill=True)
        """
        # init remove button and label
        self.remove_button, self.remove_label = self._get_remove_button()
        # ..
        _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 _get_store_graphic(self):
        # init/reset store
        if self._store_graphic is None:
            self._store_graphic = gtk.ListStore(
                    gtk.gdk.Pixbuf,
                    gobject.TYPE_STRING,
                    gtk.gdk.Pixbuf,
                    gobject.TYPE_PYOBJECT
                    )
        else:
            self._store_graphic.clear()
        # ..
        _add_path = storage.get_icon_path('more_small')
        # ..
        _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)
                _pix_a = image.get_pixbuf(_add_path, 24, 24)
                # do update
                self._store_graphic.append([_pixbuf, _filename, _pix_a, _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,
                    gtk.gdk.Pixbuf,
                    gobject.TYPE_PYOBJECT
                    )
        else:
            self._store_sound.clear()
        # prepare icon path
        _pix_path = storage.get_image_path('sound', dir_='data')
        _add_path = storage.get_icon_path('more_small')
        # ..
        _sound_dir = os.path.join(activity.get_activity_root(), 'data',
                'sounds')
        # ..
        for _filename in os.listdir(_sound_dir):
            # ..
            _path = os.path.join(_sound_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)
                _pix_a = image.get_pixbuf(_add_path, 24, 24)
                # do update
                self._store_sound.append([_pixbuf, _filename, _pix_a, _path])
            else:
                continue
        # return the store
        return self._store_sound
    """

    def update_store(self, type_):
        _store = self._store_graphic if type_ == 'graphics'\
                else self._store_sound
        # init/reset store
        if _store is None:
            _store = gtk.ListStore(
                    gtk.gdk.Pixbuf,
                    gobject.TYPE_STRING,
                    gtk.gdk.Pixbuf,
                    gobject.TYPE_PYOBJECT
                    )
        else:
            _store.clear()
        # app to get the journal entries
        _a_name = 'paint' if type_=='graphics' else 'record'
        # prepare icon path
        _default_pix_path = storage.get_image_path(_a_name, dir_='data')
        # mime type factory
        _mime_type = 'image/png' if type_=='graphics' else 'audio/ogg'
        # update store
        for _i in storage.list_info_from_journal(mime_type=_mime_type):
            # DEBUG
            logger.debug('[ui.panel.notebook] update_store - title: %s' % _i['title'])
            if _i['preview'] is None:
                _pixbuf = image.get_pixbuf(_default_pix_path, 64, 48)
            else:
                # prepare preview
                _pixbuf = storage.get_pixbuf_from_data(_i['preview'],
                        size=(64, 48))
            # ..
            _add_path = storage.get_icon_path('more_small')
            _pix_a = image.get_pixbuf(_add_path, 24, 24)
            # store info
            _store_info = {
                    'activity_id':_i['activity_id'],
                    'timestamp': _i['timestamp']
                    }
            # do update
            _row = [_pixbuf, _i['title'], _pix_a, _store_info]
            _store.append(_row)
        # ..
        return _store

    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'))
        _col_add = gtk.TreeViewColumn()
        # set renderers
        _cell_pix = gtk.CellRendererPixbuf()
        _cell_pix.set_property('height', 48)
        _col_preview.pack_start(_cell_pix, expand=False)
        _col_preview.add_attribute(_cell_pix, 'pixbuf', 0)
        _col_description.set_fixed_width(64)
        # ..
        _cell_text = gtk.CellRendererText()
        _cell_text.set_property('height', 48)
        _col_description.pack_start(_cell_text, expand=False)
        _col_description.add_attribute(_cell_text, 'text', 1)
        _col_description.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
        _col_description.set_fixed_width(220)
        # ..
        _cell_add = gtk.CellRendererPixbuf()
        _cell_add.set_property('height', 48)
        _col_add.pack_start(_cell_add, expand=False)
        _col_add.add_attribute(_cell_add, 'pixbuf', 2)
        _col_add.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
        _col_add.set_fixed_width(28)
        # update store
        _store = self.update_store(type_)
        # ensure sorting
        _store.set_sort_column_id(1, gtk.SORT_ASCENDING)
        # 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)
        _treeview.append_column(_col_add)
        # ..
        _scrolled_win.add(_treeview)
        # return it
        return _scrolled_win

    def _get_edit_panel(self):
        # ..
        _scrolled_win = gtk.ScrolledWindow()
        _scrolled_win.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        _scrolled_win.show()
        # ..
        self._edit_panel = PanelEdit(self.screen)
        _scrolled_win.add(self._edit_panel)
        # ..
        return _scrolled_win

    def _get_label(self, text):
        # create a label for the button
        _label = gtk.Label(text)
        _label.modify_fg(gtk.STATE_ACTIVE, COLOR_WHITE)
        _label.show()
        # ..
        return _label

    def _get_notebook(self):
        # get panel edit
        _list0 = self._get_edit_panel()
        _label0 = self._get_label(_('Edit'))
        # get list 1
        _list1 = self._get_treeview(type_='graphics')
        _label1 = self._get_label(_('Graphics'))
        # keep sequence treeview
        self._graphics_treeview = _list1.get_children()[0]
        # get list 2
        _list2 = self._get_treeview(type_='sounds')
        _label2 = self._get_label(_('Sounds'))
        # keep library treeview
        self._sounds_treeview = _list2.get_children()[0]
        # 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(_list0, _label0)
        _notebook.append_page(_list1, _label1)
        _notebook.append_page(_list2, _label2)
        # return it
        return _notebook

    def refresh(self, key=None):
        self._edit_panel.refresh(key=key)