]> git.0d.be Git - jack_mixer.git/blob - abspeak.py
Set version to 14 in preparation for next release
[jack_mixer.git] / abspeak.py
1 # This file is part of jack_mixer
2 #
3 # Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 import math
19
20 from gi.repository import Gtk
21 from gi.repository import Gdk
22 from gi.repository import GObject
23
24
25 CSS = b"""
26 .over_zero {
27     background-color: #cc4c00;
28 }
29
30 .is_nan {
31     background-color: #b20000;
32 }
33 """
34 css_provider = Gtk.CssProvider()
35 css_provider.load_from_data(CSS)
36 context = Gtk.StyleContext()
37 screen = Gdk.Screen.get_default()
38 context.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
39
40
41 class AbspeakWidget(Gtk.EventBox):
42     def __init__(self):
43         super().__init__()
44         self.label = Gtk.Label()
45         self.add(self.label)
46         self.connect("button-press-event", self.on_mouse)
47         self.peak = -math.inf
48
49     def get_style_context(self):
50         return self.label.get_style_context()
51
52     def on_mouse(self, widget, event):
53         if event.type == Gdk.EventType.BUTTON_PRESS:
54             if event.button == 1 or event.button == 2 or event.button == 3:
55                 context = self.get_style_context()
56                 context.remove_class('over_zero')
57                 context.remove_class('is_nan')
58
59             if event.button == 1 or event.button == 3:
60                 self.emit("reset")
61             elif event.button == 2:
62                 adjust = -self.peak
63
64                 if abs(adjust) < 30:    # we better don't adjust more than +- 30 dB
65                     self.emit("volume-adjust", adjust)
66
67     def set_peak(self, peak):
68         self.peak = peak
69         context = self.get_style_context()
70
71         if math.isnan(peak):
72             context.remove_class('over_zero')
73             context.add_class('is_nan')
74             self.label.set_text("NaN")
75         else:
76             text = "%+.1f" % peak
77             context.remove_class('is_nan')
78
79             if peak > 0:
80                 context.add_class('over_zero')
81
82             self.label.set_text(text)
83
84
85 GObject.signal_new("reset", AbspeakWidget,
86                    GObject.SignalFlags.RUN_FIRST | GObject.SignalFlags.ACTION, None, [])
87 GObject.signal_new("volume-adjust", AbspeakWidget,
88                    GObject.SignalFlags.RUN_FIRST | GObject.SignalFlags.ACTION, None,
89                    [GObject.TYPE_FLOAT])