root/trunk/libffado/support/mixer-qt4/ffado/widgets/crossbarrouter.py

Revision 1656, 7.4 kB (checked in by arnonym, 12 years ago)

Allow to deactivate the peaks.

Line 
1 #
2 # Copyright (C) 2009 by Arnold Krille
3 #
4 # This file is part of FFADO
5 # FFADO = Free Firewire (pro-)audio drivers for linux
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 2 of the License, or
10 # (at your option) version 3 of the License.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 from PyQt4 import QtGui, QtCore
22 import dbus
23
24 class VuMeter(QtGui.QFrame):
25     def __init__(self, interface, output, input=None, parent=None):
26         QtGui.QFrame.__init__(self, parent)
27         self.setLineWidth(1)
28         self.setFrameStyle(QtGui.QFrame.Panel|QtGui.QFrame.Sunken)
29         self.setMinimumSize(20, 20)
30
31         self.level = 0
32
33         self.interface = interface
34
35         self.output = output
36         if input is None:
37             input = int(self.interface.getSourceForDestination(output))
38         self.setInput(input)
39
40     def setInput(self, input):
41         #print "VuMeter.setInput() %i->%i" % (self.output, input)
42         self.input = input
43
44     def updateLevel(self, value):
45         self.level = value
46         self.update()
47
48     def paintEvent(self, event):
49         p = QtGui.QPainter(self)
50         value = self.level/4096
51         r = self.rect()
52         r.setHeight(r.height() * value)
53         r.moveBottom(self.rect().height())
54         p.fillRect(r, self.palette().highlight())
55
56 class OutputSwitcher(QtGui.QFrame):
57     """
58 The name is a bit misleading. This widget selectes sources for a specified
59 destination.
60
61 In mixer-usage this widget is at the top of the input-channel. Because the input
62 of the mixer is an available output from the routers point.
63 """
64     def __init__(self, interface, outname, parent):
65         QtGui.QFrame.__init__(self, parent)
66         self.interface = interface
67         self.outname = outname
68
69         self.setLineWidth(1)
70         self.setFrameStyle(QtGui.QFrame.Sunken|QtGui.QFrame.Panel)
71
72         self.layout = QtGui.QGridLayout(self)
73         self.setLayout(self.layout)
74
75         self.lbl = QtGui.QLabel(self.outname, self)
76         self.layout.addWidget(self.lbl, 0, 0)
77
78         self.vu = VuMeter(self.interface, self.interface.getDestinationIndex(outname), parent=self)
79         self.layout.addWidget(self.vu, 0, 1, 2, 1)
80
81         self.btn = QtGui.QPushButton("Sel.", self)
82         self.layout.addWidget(self.btn, 1, 0)
83
84         self.exclusiveGroup = QtGui.QActionGroup(self)
85
86         sources = self.interface.getSourceNames()
87         self.ingroups = {}
88         for ch in sources:
89             tmp = str(ch).split(":")[0]
90             if tmp not in self.ingroups:
91                 self.ingroups[tmp] = 0
92             self.ingroups[tmp] += 1
93         #print "Detected ingroups: %s" % str(self.ingroups)
94
95         self.menu = QtGui.QMenu(self)
96         self.btn.setMenu(self.menu)
97
98         for group in self.ingroups:
99             submenu = InGroupMenu(self.interface, self.outname, group, self.ingroups[group], self, self.exclusiveGroup)
100             self.menu.addMenu(submenu)
101
102     def peakValue(self, value):
103         self.vu.updateLevel(value)
104
105
106 class InGroupMenu(QtGui.QMenu):
107     def __init__(self, interface, outname, inname, insize, parent, exclusiveGroup = None):
108         QtGui.QMenu.__init__(self, inname, parent)
109         self.interface = interface
110         self.outname = str(outname)
111         self.inname = str(inname)
112         self.insize = insize
113         self.connect(self, QtCore.SIGNAL("aboutToShow()"), self.showMenu)
114
115         self.lock = False
116         if exclusiveGroup:
117             self.exclusiveGroup = exclusiveGroup
118         else:
119             self.exclusiveGroup = QtGui.QActionGroup(self)
120
121     def showMenu(self):
122         #print "About to show the menu"
123         if len(self.actions()) < self.insize:
124             # Do a lazy init of the sub-items.
125             for i in range(self.insize):
126                 action = QtGui.QAction("%s:%02i" % (self.inname,i), self)
127                 action.setCheckable(True)
128                 self.connect(action, QtCore.SIGNAL("toggled(bool)"), self.connectionSwitched)
129                 self.exclusiveGroup.addAction(action)
130                 self.addAction(action)
131                 idx = self.interface.getDestinationIndex(self.outname)
132                 sourceidx = self.interface.getSourceForDestination(idx)
133                 #print self.interface.getConnectionState(sourceidx, idx)
134                 source = self.interface.getSourceName(sourceidx)
135                 self.lock = True
136                 for action in self.actions():
137                     action.setChecked(action.text() == source)
138                 self.lock = False
139
140     def connectionSwitched(self, checked):
141         if self.lock: return
142         print "connectionSwitched( %s ) sender: %s" % (str(checked),str(self.sender()))
143         inname = str(self.sender().text())
144         print " source=%s destination=%s  possible? %s" % (inname, self.outname, self.interface.canConnectNamed(inname, self.outname))
145         print " connectionState is now %s" % self.interface.setConnectionStateNamed(inname, self.outname, checked)
146
147
148 class CrossbarRouter(QtGui.QWidget):
149     def __init__(self, servername, basepath, parent=None):
150         QtGui.QWidget.__init__(self, parent);
151         self.bus = dbus.SessionBus()
152         self.dev = self.bus.get_object(servername, basepath)
153         self.interface = dbus.Interface(self.dev, dbus_interface="org.ffado.Control.Element.CrossbarRouter")
154
155         print self.interface.getDestinations()
156
157         destinations = self.interface.getDestinationNames()
158         self.outgroups = []
159         for ch in destinations:
160             tmp = str(ch).split(":")[0]
161             if not tmp in self.outgroups:
162                 self.outgroups.append(tmp)
163
164         self.biglayout = QtGui.QVBoxLayout(self)
165         self.setLayout(self.biglayout)
166
167         self.toplayout = QtGui.QHBoxLayout()
168         self.biglayout.addLayout(self.toplayout)
169
170         btn = QtGui.QPushButton("Switch VU", self)
171         btn.setCheckable(True)
172         btn.setChecked(True)
173         self.connect(btn, QtCore.SIGNAL("toggled(bool)"), self.runVu)
174         self.toplayout.addWidget(btn)
175
176         self.layout = QtGui.QGridLayout()
177         self.biglayout.addLayout(self.layout)
178
179         self.switchers = {}
180         for out in destinations:
181             btn = OutputSwitcher(self.interface, out, self)
182             self.layout.addWidget(btn, int(out.split(":")[-1]) + 1, self.outgroups.index(out.split(":")[0]))
183             self.switchers[self.interface.getDestinationIndex(out)] = btn
184
185         self.timer = QtCore.QTimer(self)
186         self.timer.setInterval(200)
187         self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.updateLevels)
188         self.runVu(True)
189
190     def runVu(self, run=True):
191         if run:
192             self.timer.start()
193         else:
194             self.timer.stop()
195             for sw in self.switchers:
196                 self.switchers[sw].peakValue(0)
197
198     def updateLevels(self):
199         #print "CrossbarRouter.updateLevels()"
200         peakvalues = self.interface.getPeakValues()
201         #print "Got %i peaks" % len(peakvalues)
202         for peak in peakvalues:
203             self.switchers[peak[0]].peakValue(peak[1])
204
205 #
206 # vim: sw=4 ts=4 et
Note: See TracBrowser for help on using the browser.