Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/toolbar_side.py
blob: 387cc8b7e3f8cfffa4bc5a2d00ac2cd988751f4b (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
# -*- coding: utf-8 -*-
#! /usr/bin/python
#
# Author:  Arjun Sarwal   arjun@laptop.org
# Copyright (C) 2007, Arjun Sarwal
# Copyright (C) 2009-11 Walter Bender
#
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this library; if not, write to the Free Software
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA


import gtk
from gettext import gettext as _

from sugar.graphics.toolbutton import ToolButton

from config import LOWER, UPPER


class SideToolbar(gtk.Toolbar):
    ''' A toolbar on the side of the canvas for adjusting gain/bias '''

    def __init__(self, activity, channel=0):
        ''' Set up initial toolbars '''
        gtk.Toolbar.__init__(self)

        self.activity = activity
        self._channel = channel
        if self._channel == 0:
            self.show_toolbar = True
        else:  # Don't show second channel until we need it
            self.show_toolbar = False
        self.mode = 'sound'
        self.mode_values = {'sound': 3, 'sensor': 2}

        self._toggle = gtk.CheckButton()
        self._toggle.set_active(True)
        self._toggle.connect('clicked', self.toggle_cb)
        self._toggle.show()
        self._toggle_box = gtk.HBox()
        self._toggle_box.pack_start(self._toggle, False, True, 18)

        self._invert = ToolButton('invert')
        self._invert.set_tooltip(_('Invert'))
        self._invert.connect('clicked', self.invert_control_cb)
        self._invert.show()
        self.activity.wave.set_invert_state(False, channel=self._channel)

        self.button_up = ToolButton('amp-high')
        self.button_up.set_tooltip(_('Increase amplitude'))
        self.button_up.connect('clicked', self._button_up_cb)
        self.button_up.show()

        self.adjustmenty = gtk.Adjustment(self.mode_values[self.mode],
                                          LOWER, UPPER,
                                          0.1, 0.1, 0.0)
        self.adjustmenty.connect('value_changed', self._yscrollbar_cb,
                                 self.adjustmenty)
        self.yscrollbar = gtk.VScale(self.adjustmenty)
        self.yscrollbar.set_draw_value(False)
        self.yscrollbar.set_inverted(True)
        self.yscrollbar.set_update_policy(gtk.UPDATE_CONTINUOUS)

        self.button_down = ToolButton('amp-low')
        self.button_down.set_tooltip(_('Decrease amplitude'))
        self.button_down.connect('clicked', self._button_down_cb)
        self.button_down.show()

        self.box1 = gtk.VBox(False, 0)

        if self._channel == 0:
            self.box1.pack_start(self._color_wave(self.activity.stroke_color),
                                 False, True, 0)
        elif self._channel == 1:
            self.box1.pack_start(self._color_wave(self.activity.fill_color),
                                 False, True, 0)
        else:
            self.box1.pack_start(self._color_wave('#FFFFFF'), False, True, 0)
        self.box1.pack_start(self._toggle_box, False, True, 0)
        self.box1.pack_start(self._invert, False, True, 0)
        self.box1.pack_start(self.button_up, False, True, 0)
        self.box1.pack_start(self.yscrollbar, True, True, 0)
        self.box1.pack_start(self.button_down, False, True, 0)

        self.set_show_hide(False)

    def _yscrollbar_cb(self, adjy, data=None):
        ''' Callback for scrollbar '''
        if self.mode == 'sound':
            self.activity.wave.set_mag_params(1.0, adjy.value,
                                              channel=self._channel)
            self.activity.wave.set_bias_param(0,
                                              channel=self._channel)
        elif self.mode == 'sensor':
            self.activity.wave.set_bias_param(int(
                    300 * (adjy.value - (UPPER - LOWER) / 2.)),
                                              channel=self._channel)
        self.mode_values[self.mode] = adjy.value
        return True

    def _button_up_cb(self, data=None):
        '''Moves slider up'''
        new_value = self.yscrollbar.get_value() + (UPPER - LOWER) \
            / 100.
        if new_value <= UPPER:
            self.yscrollbar.set_value(new_value)
        else:
            self.yscrollbar.set_value(UPPER)
        return True

    def _button_down_cb(self, data=None):
        '''Moves slider down'''
        new_value = self.yscrollbar.get_value() - (UPPER - LOWER) \
            / 100.
        if new_value >= LOWER:
            self.yscrollbar.set_value(new_value)
        else:
            self.yscrollbar.set_value(LOWER)
        return True

    def set_show_hide(self, show=True, mode='sound'):
        ''' Show or hide the toolbar '''
        self.show_toolbar = show
        self.set_side_mode(mode)

    def set_side_mode(self, mode='sound'):
        ''' Set the toolbar to either 'sound' or 'sensor' '''
        self.mode = mode
        if self.mode == 'sound':
            self.button_up.set_icon('amp-high')
            self.button_up.set_tooltip(_('Increase amplitude'))
            self.button_down.set_icon('amp-low')
            self.button_down.set_tooltip(_('Decrease amplitude'))
        elif self.mode == 'sensor':
            self.button_up.set_icon('bias-high')
            self.button_up.set_tooltip(_('Increase bias'))
            self.button_down.set_icon('bias-low')
            self.button_down.set_tooltip(_('Decrease bias'))
            self._invert.show()
        self.yscrollbar.set_value(self.mode_values[self.mode])
        return

    def toggle_cb(self, data=None):
        self.activity.wave.set_visibility(self._toggle.get_active(),
                                          channel=self._channel)
        return True

    def invert_control_cb(self, data=None):
        ''' Callback for Invert Button '''
        if self.activity.wave.get_invert_state(channel=self._channel):
            self.activity.wave.set_invert_state(False, self._channel)
            self._invert.set_icon('invert')
            self._invert.show()
        elif not self.activity.wave.get_fft_mode():
            self.activity.wave.set_invert_state(True, self._channel)
            self._invert.set_icon('invert2')
            self._invert.show()
        self.activity.sensor_toolbar.update_string_for_textbox()
        return False

    def _color_wave(self, color):
        ''' Generate a wave graphic in color to label each side toolbar '''
        svg = '<?xml version="1.0" ?>\n\
<!DOCTYPE svg  PUBLIC "-//W3C//DTD SVG 1.1//EN"\n\
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n\
<svg enable-background="new 0 0 55.125 55" height="55px" version="1.1"\n\
viewBox="0 0 55.125 55" width="55.125px" x="0px" xml:space="preserve"\n\
xmlns="http://www.w3.org/2000/svg"\n\
xmlns:xlink="http://www.w3.org/1999/xlink" y="0px">\n\
<path d="M9.066,27.5 c2.32-6.917,4.666-13.834,9.255-13.834\n\
c9.179,0,9.179,27.668,18.358,27.668c4.59,0,6.986-6.917,9.332-13.834"\n\
fill="none" stroke="%s" stroke-linecap="round" stroke-width="3.5"/>\n\
</svg>' % (color)
        pixbuf = svg_str_to_pixbuf(svg)
        img = gtk.Image()
        img.set_from_pixbuf(pixbuf)
        img_tool = gtk.ToolItem()
        img_tool.add(img)
        return img_tool


def svg_str_to_pixbuf(svg_string):
    ''' Load pixbuf from SVG string '''
    pl = gtk.gdk.PixbufLoader('svg')
    pl.write(svg_string)
    pl.close()
    pixbuf = pl.get_pixbuf()
    return pixbuf