Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/atoidejouer/ui/panel/sound.py
blob: cb94d9dac694b5907a0e10bd49bd31f451d51ce9 (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

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

# gtk import
import gtk

# atoidejouer import
from atoidejouer.db import story
from atoidejouer.tools import image, sound, storage, ui

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


LABEL_MAX_LENGHT  = 12

COLOR_GREY_DARK   = ui.get_color(0.5, 0.5, 0.5)
COLOR_GREY_LIGHT  = ui.get_color(0.75, 0.75, 0.75)
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_on_click(button, panel_sound, key):
    # update edit panel
    panel_sound.screen.panel_edit.refresh(key=key)


def _cb_on_remove(panel_sound, key, button):
    # remove image on right click
    story.DB()._del(key)
    panel_sound.refresh()
    # update edit panel
    panel_sound.screen.panel_edit.refresh()


def _set_bg(widget):
    widget.modify_bg(gtk.STATE_NORMAL, COLOR_GREY_LIGHT)
    widget.modify_bg(gtk.STATE_PRELIGHT, COLOR_GREY_WHITE)
    widget.modify_bg(gtk.STATE_ACTIVE, COLOR_WHITE)


def _add_simple_button(parent, icon_name, tooltip, cb):
    # .. remove button
    _button = gtk.Button()
    _button.show()
    _button.set_tooltip_text(tooltip)
    # cb
    _button.connect('clicked', cb)
    # add
    parent.pack_start(_button, expand=False, fill=False)
    # add a remove image here
    _img = gtk.Image()
    _img.show()
    _button.add(_img)
    _button.set_size_request(30, 30)
    # ..
    _set_bg(_button)
    # get pixbuff and update image
    _path = storage.get_icon_path(icon_name)
    _img.set_from_pixbuf(image.get_pixbuf(_path, 24, 24))


class PanelSound(gtk.Frame):

    def __init__(self, screen):
        # init parent
        gtk.Frame.__init__(self, _('Sounds'))
        self.set_border_width(2)
        self.set_size_request(-1, 140)
        # do show
        self.show()
        # keep the screen
        self.screen = screen
        # keep item list for the sequence
        self.keys = list()
        self._current = None
        # prepare scrolled window
        self._scrolled_window = gtk.ScrolledWindow()
        self._scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_NEVER)
        self._scrolled_window.show()
        # a box
        _hbox = gtk.HBox(homogeneous=False, spacing=2)
        _hbox.show()
        self.add(_hbox)
        # a box
        self._scrolled_hbox = gtk.HBox(homogeneous=False, spacing=2)
        self._scrolled_hbox.show()
        # add box in viewport
        self._scrolled_window.add_with_viewport(self._scrolled_hbox)
        # add
        _hbox.add(self._scrolled_window)

    def clear(self):
        # keep item list for the sequence
        self.keys = list()
        self._current = None
        # remove graphics
        _children = self._scrolled_hbox.get_children()
        for _c in _children:
            _c.destroy()
        # add box in viewport
        self._scrolled_window.add_with_viewport(self._scrolled_hbox)

    def get_current_pos(self):
        # get children
        _children = self._scrolled_hbox.get_children()
        # get button index
        return _children.index(self._current)\
                if self._current in _children else 0

    def set_current(self, key):
        # get children
        _children = self._scrolled_hbox.get_children()
        # get new idx index
        _index = self.keys.index(key)\
                if key in self.keys else 1000
        # little check
        if _index < len(_children):
            # update current
            self._current = _children[_index]
            # update toolbar frame entry
            if self.screen.toolbar._frame_entry is None:
                pass
            else:
                self.screen.toolbar._frame_entry.set_text(str(_index))
        else:
            pass

    def number_of_items(self):
        return len(self.keys)

    def __remove(self, index):
        # update list
        if index + 1 <= len(self.items):
            self.items.pop(index)
            # clear current
            self._current.destroy()
            self._current = None
        else:
            pass

    def remove_current(self):
        # get button index
        _index = self.get_current_pos()
        # do remove
        self.__remove(_index)

    def remove_item(self, item_name):
        # get children
        _children = self._scrolled_hbox.get_children()
        # get button index
        _index = _children.index(item_name)\
                if item_name in _children else 0
        # do remove
        self.__remove(_index)

    def add_key(self, key, pos=None):
        _item_box = gtk.HBox(homogeneous=False, spacing=2)
        _item_box.show()
        self._scrolled_hbox.pack_start(_item_box, expand=False, fill=False)
        # prepare vbox item
        _vbox = gtk.VBox(homogeneous=False, spacing=2)
        _vbox.show()
        _item_box.pack_start(_vbox, expand=False, fill=False)
        # simple add
        if key in self.keys:
            pass
        elif pos is None:
            self.keys.append(key)
        # add at pos
        else:
            self.keys.insert(pos, key)
            self._scrolled_hbox.reorder_child(_vbox, pos)
        # init event box to manage color
        _item = gtk.Button()
        _vbox.pack_start(_item, expand=False, fill=True)
        _set_bg(_item)
        _item.set_size_request(96, 78)
        _item.set_relief(gtk.RELIEF_NONE)
        _item.connect('clicked', _cb_on_click, self, key)
        _item.show()
        # add a picture here
        _item_img = gtk.Image()
        _item_img.show()
        _item.add(_item_img)
        # get pixbuff and update image
        _path = storage.get_image_path('sound', dir_='data')
        _item_img.set_from_pixbuf(image.get_pixbuf(_path, 96, 62))
        # manage label max length
        _title = key.title
        _title = _title[:len(_title) - LABEL_MAX_LENGHT]\
                if len(_title) > LABEL_MAX_LENGHT else _title
        # add label
        _label = gtk.Label(_title)
        _label.show()
        _vbox.pack_start(_label, expand=False, fill=True)
        # remove button
        _add_simple_button(_item_box, 'less_small', _('Remove ') + key.title,
                partial(_cb_on_remove, self, key))

    def refresh(self):
        self.clear()
        # get the current frame
        _time = 0.0 if self.screen.toolbar.activity._thread is None\
                else self.screen.toolbar.activity._thread._time
        # get the current rate
        for _k in story.DB().get(story.Key(mime_type='audio/ogg', time=_time)):
            self.add_key(_k)