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

Revision 1707, 8.6 kB (checked in by arnonym, 11 years ago)

Use QSettings to store the state of the VU-switch.

And do some playing with deleting panels when the devices are removed from the bus. Otherwise the timer checking the VU-values will run forever and complain about missing dbus-objects.

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 import logging
25 log = logging.getLogger("crossbarrouter")
26
27 class VuMeter(QtGui.QFrame):
28     def __init__(self, interface, output, input=None, parent=None):
29         QtGui.QFrame.__init__(self, parent)
30         self.setLineWidth(1)
31         self.setFrameStyle(QtGui.QFrame.Panel|QtGui.QFrame.Sunken)
32         self.setMinimumSize(20, 20)
33
34         self.level = 0
35
36         self.interface = interface
37
38         self.output = output
39         if input is None:
40             input = int(self.interface.getSourceForDestination(output))
41         self.setInput(input)
42
43     def setInput(self, input):
44         #log.debug("VuMeter.setInput() %i->%i" % (self.output, input))
45         self.input = input
46
47     def updateLevel(self, value):
48         self.level = value
49         self.update()
50
51     def paintEvent(self, event):
52         p = QtGui.QPainter(self)
53         value = self.level/4096
54         r = self.rect()
55         r.setHeight(r.height() * value)
56         r.moveBottom(self.rect().height())
57         p.fillRect(r, self.palette().highlight())
58
59 class OutputSwitcher(QtGui.QFrame):
60     """
61 The name is a bit misleading. This widget selectes sources for a specified
62 destination.
63
64 In mixer-usage this widget is at the top of the input-channel. Because the input
65 of the mixer is an available output from the routers point.
66 """
67     def __init__(self, interface, outname, parent):
68         QtGui.QFrame.__init__(self, parent)
69         self.interface = interface
70         self.outname = outname
71
72         self.setLineWidth(1)
73         self.setFrameStyle(QtGui.QFrame.Sunken|QtGui.QFrame.Panel)
74
75         self.layout = QtGui.QGridLayout(self)
76         self.setLayout(self.layout)
77
78         self.lbl = QtGui.QLabel(self.outname, self)
79         self.layout.addWidget(self.lbl, 0, 0)
80
81         self.vu = VuMeter(self.interface, self.interface.getDestinationIndex(outname), parent=self)
82         self.layout.addWidget(self.vu, 0, 1, 2, 1)
83
84         self.btn = QtGui.QPushButton("Sel.", self)
85         self.layout.addWidget(self.btn, 1, 0)
86
87         self.exclusiveGroup = QtGui.QActionGroup(self)
88
89         sources = self.interface.getSourceNames()
90         self.ingroups = {}
91         for ch in sources:
92             tmp = str(ch).split(":")[0]
93             if tmp not in self.ingroups:
94                 self.ingroups[tmp] = 0
95             self.ingroups[tmp] += 1
96         #log.debug("Detected ingroups: %s" % str(self.ingroups))
97
98         self.menu = QtGui.QMenu(self)
99         self.btn.setMenu(self.menu)
100
101         action = QtGui.QAction("Disconnect", self)
102         action.setCheckable(True)
103         self.connect(action, QtCore.SIGNAL("toggled(bool)"), self.disconnectRoute)
104         self.exclusiveGroup.addAction(action)
105         self.menu.addAction(action)
106
107         for group in self.ingroups:
108             submenu = InGroupMenu(self.interface, self.outname, group, self.ingroups[group], self, self.exclusiveGroup)
109             self.menu.addMenu(submenu)
110
111     def peakValue(self, value):
112         self.vu.updateLevel(value)
113
114     def disconnectRoute(self, checked):
115         log.debug("disconnectRoute( %s )" % str(checked))
116         dest = self.interface.getDestinationIndex(self.outname)
117         src = self.interface.getSourceName( self.interface.getSourceForDestination( dest ) )
118         #log.debug(" source=%s destination=%s  possible? %s" % (src, self.outname, self.interface.canConnectNamed(src, self.outname)))
119         if self.interface.setConnectionStateNamed(src, self.outname, False):
120             log.warning(" Failed to change the connection table!")
121         self.peakValue(0)
122
123 class InGroupMenu(QtGui.QMenu):
124     def __init__(self, interface, outname, inname, insize, parent, exclusiveGroup = None):
125         QtGui.QMenu.__init__(self, inname, parent)
126         self.interface = interface
127         self.outname = str(outname)
128         self.inname = str(inname)
129         self.insize = insize
130         self.connect(self, QtCore.SIGNAL("aboutToShow()"), self.showMenu)
131
132         self.lock = False
133         if exclusiveGroup:
134             self.exclusiveGroup = exclusiveGroup
135         else:
136             self.exclusiveGroup = QtGui.QActionGroup(self)
137
138     def showMenu(self):
139         #print "About to show the menu"
140         if len(self.actions()) < self.insize+1:
141             # Do a lazy init of the sub-items.
142             for i in range(self.insize):
143                 action = QtGui.QAction("%s:%02i" % (self.inname,i), self)
144                 action.setCheckable(True)
145                 self.connect(action, QtCore.SIGNAL("toggled(bool)"), self.connectionSwitched)
146                 self.exclusiveGroup.addAction(action)
147                 self.addAction(action)
148                 idx = self.interface.getDestinationIndex(self.outname)
149                 sourceidx = self.interface.getSourceForDestination(idx)
150                 source = self.interface.getSourceName(sourceidx)
151                 self.lock = True
152                 for action in self.actions():
153                     action.setChecked(action.text() == source)
154                 self.lock = False
155
156     def connectionSwitched(self, checked):
157         if self.lock: return
158         #log.debug("connectionSwitched( %s ) sender: %s" % (str(checked),str(self.sender())))
159         inname = str(self.sender().text())
160         #log.debug(" source=%s destination=%s  possible? %s" % (inname, self.outname, self.interface.canConnectNamed(inname, self.outname)))
161         if not self.interface.setConnectionStateNamed(inname, self.outname, checked):
162             log.warning(" Failed to change the routing.")
163
164
165 class CrossbarRouter(QtGui.QWidget):
166     def __init__(self, servername, basepath, parent=None):
167         QtGui.QWidget.__init__(self, parent);
168         self.bus = dbus.SessionBus()
169         self.dev = self.bus.get_object(servername, basepath)
170         self.interface = dbus.Interface(self.dev, dbus_interface="org.ffado.Control.Element.CrossbarRouter")
171
172         self.settings = QtCore.QSettings(self)
173
174         log.info(self.interface.getDestinations())
175
176         destinations = self.interface.getDestinationNames()
177         self.outgroups = []
178         for ch in destinations:
179             tmp = str(ch).split(":")[0]
180             if not tmp in self.outgroups:
181                 self.outgroups.append(tmp)
182
183         self.biglayout = QtGui.QVBoxLayout(self)
184         self.setLayout(self.biglayout)
185
186         self.toplayout = QtGui.QHBoxLayout()
187         self.biglayout.addLayout(self.toplayout)
188
189         self.vubtn = QtGui.QPushButton("Switch VU", self)
190         self.vubtn.setCheckable(True)
191         self.connect(self.vubtn, QtCore.SIGNAL("toggled(bool)"), self.runVu)
192         self.toplayout.addWidget(self.vubtn)
193
194         self.layout = QtGui.QGridLayout()
195         self.biglayout.addLayout(self.layout)
196
197         self.switchers = {}
198         for out in destinations:
199             btn = OutputSwitcher(self.interface, out, self)
200             self.layout.addWidget(btn, int(out.split(":")[-1]) + 1, self.outgroups.index(out.split(":")[0]))
201             self.switchers[self.interface.getDestinationIndex(out)] = btn
202
203         self.timer = QtCore.QTimer(self)
204         self.timer.setInterval(200)
205         self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.updateLevels)
206
207         self.vubtn.setChecked(self.settings.value("crossbarrouter/runvu", False).toBool())
208
209     def __del__(self):
210         print "CrossbarRouter.__del__()"
211         self.settings.setValue("crossbarrouter/runvu", self.vubtn.isChecked())
212
213     def runVu(self, run=True):
214         #log.debug("CrossbarRouter.runVu( %i )" % run)
215         if run:
216             self.timer.start()
217         else:
218             self.timer.stop()
219             for sw in self.switchers:
220                 self.switchers[sw].peakValue(0)
221
222     def updateLevels(self):
223         #log.debug("CrossbarRouter.updateLevels()")
224         peakvalues = self.interface.getPeakValues()
225         #log.debug("Got %i peaks" % len(peakvalues))
226         for peak in peakvalues:
227             #log.debug("peak = [%s,%s]" % (str(peak[0]),str(peak[1])))
228             if peak[0] >= 0:
229                 self.switchers[peak[0]].peakValue(peak[1])
230
231 #
232 # vim: sw=4 ts=4 et
Note: See TracBrowser for help on using the browser.