switch to QVariant API 2
[nephilim.git] / nephilim / plugins / PlayControl.py
blob5fad0e84a14913428aea291365cbe8043788b65f
2 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
4 # Nephilim is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # Nephilim 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.
14 # You should have received a copy of the GNU General Public License
15 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
18 from PyQt4 import QtGui, QtCore
20 from ..common import Button
21 from ..plugin import Plugin
22 from .. import icons
24 class wgPlayControl(QtGui.QToolBar):
25 """Displays controls for interacting with playing, like play, volume ..."""
26 " control buttons"
27 play_icon = None
28 pause_icon = None
29 play_action = None
30 stop_action = None
31 prev_action = None
32 next_action = None
33 vol_slider = None
34 repeat = None
35 random = None
36 single = None
37 consume = None
38 outputs_menu = None
39 p = None
40 logger = None
42 class VolumeSlider(QtGui.QSlider):
44 def __init__(self, parent):
45 QtGui.QSlider.__init__(self, parent)
46 self.setOrientation(parent.orientation())
47 self.setMaximum(100)
48 self.setToolTip('Volume control')
50 def paintEvent(self, event):
51 painter = QtGui.QPainter(self)
52 painter.eraseRect(self.rect())
54 grad = QtGui.QLinearGradient(0, 0, self.width(), self.height())
55 grad.setColorAt(0, self.palette().color(QtGui.QPalette.Window))
56 grad.setColorAt(1, self.palette().color(QtGui.QPalette.Highlight))
57 if self.orientation() == QtCore.Qt.Horizontal:
58 rect = QtCore.QRect(0, 0, self.width() * self.value() / self.maximum(), self.height())
59 else:
60 rect = QtCore.QRect(0, self.height() * (1 - float(self.value()) / self.maximum()), self.width(), self.height())
61 painter.fillRect(rect, QtGui.QBrush(grad))
63 def __init__(self, p, parent = None):
64 QtGui.QToolBar.__init__(self, p.name, parent)
65 self.setMovable(True)
66 self.p = p
67 self.logger = p.logger
68 self.setObjectName(p.name)
70 status = self.p.mpclient.status
72 self.play_icon = QtGui.QIcon(':icons/media-playback-start.svg')
73 self.pause_icon = QtGui.QIcon(':icons/media-playback-pause.svg')
75 self.play_action = self.addAction(self.play_icon, 'play', self.on_play_click)
76 self.stop_action = self.addAction(QtGui.QIcon(':icons/media-playback-stop.svg'), 'stop', self.on_stop_click)
77 self.prev_action = self.addAction(QtGui.QIcon(':icons/media-skip-backward.svg'), 'previous', self.on_prev_click)
78 self.next_action = self.addAction(QtGui.QIcon(':icons/media-skip-forward.svg'), 'next', self.on_next_click)
79 self.addSeparator()
81 self.vol_slider = self.VolumeSlider(self)
82 self.vol_slider.setSliderPosition(status['volume'])
83 self.vol_slider.valueChanged.connect(self.onVolumeSliderChange)
84 self.addWidget(self.vol_slider)
85 self.addSeparator()
87 self.random = self.addAction(QtGui.QIcon(':icons/media-playlist-shuffle.svgz'), 'random')
88 self.random.setCheckable(True)
89 self.random.setChecked(status['random'])
90 self.random.toggled.connect(self.p.mpclient.random)
91 self.p.mpclient.random_changed.connect(self.random.setChecked)
93 self.repeat = self.addAction(QtGui.QIcon(':icons/media-playlist-repeat.svg'), 'repeat')
94 self.repeat.setCheckable(True)
95 self.repeat.setChecked(status['repeat'])
96 self.p.mpclient.repeat_changed.connect(self.repeat.setChecked)
97 self.repeat.toggled.connect(self.p.mpclient.repeat)
99 self.single = self.addAction(QtGui.QIcon(':icons/single.png'), 'single mode')
100 self.single.setCheckable(True)
101 self.single.setChecked(status['single'])
102 self.p.mpclient.single_changed.connect(self.single.setChecked)
103 self.single.toggled.connect(self.p.mpclient.single)
105 self.consume = self.addAction(QtGui.QIcon(':icons/consume.png'), 'consume mode')
106 self.consume.setCheckable(True)
107 self.consume.setChecked(status['consume'])
108 self.p.mpclient.consume_changed.connect(self.consume.setChecked)
109 self.consume.toggled.connect(self.p.mpclient.consume)
111 self.outputs_menu = QtGui.QMenu('Audio outputs')
112 outputs = self.addAction(QtGui.QIcon(':icons/outputs.png'), 'Audio outputs')
113 outputs.triggered.connect(lambda : self.outputs_menu.popup(QtGui.QCursor.pos()))
114 self.__update_outputs()
115 self.p.mpclient.connect_changed.connect(self.__update_outputs)
117 self.orientationChanged.connect(self.vol_slider.setOrientation)
119 self.p.mpclient.state_changed.connect(self.onStateChange)
120 self.p.mpclient.volume_changed.connect(self.onVolumeChange)
122 def __update_outputs(self):
123 self.outputs_menu.clear()
124 for output in self.p.mpclient.outputs:
125 act = self.outputs_menu.addAction(output.name)
126 act.setCheckable(True)
127 act.setChecked(output.state)
128 act.toggled.connect(output.set_state)
129 output.state_changed.connect(act.setChecked)
131 def onStateChange(self, new_state):
132 status = self.p.mpclient.status
134 if new_state == 'play':
135 self.play_action.setIcon(self.pause_icon)
136 self.play_action.setToolTip('pause')
137 elif new_state == 'pause' or new_state == 'stop':
138 self.play_action.setIcon(self.play_icon)
139 self.play_action.setToolTip('play')
141 def onVolumeChange(self, new_vol):
142 self.vol_slider.setValue(new_vol)
144 def on_play_click(self):
145 status=self.p.mpclient.status
146 if status['state']=='play':
147 self.logger.info('Toggling playback')
148 self.p.mpclient.pause()
149 elif status['state']=='stop':
150 self.logger.info('Pausing playback')
151 self.p.mpclient.play(None)
152 else:
153 self.p.mpclient.resume()
154 def on_stop_click(self):
155 self.logger.info('Stopping playback')
156 self.p.mpclient.stop()
157 def on_prev_click(self):
158 self.logger.info('Playing previous')
159 self.p.mpclient.previous()
160 def on_next_click(self):
161 self.logger.info('Playing next')
162 self.p.mpclient.next()
163 def onVolumeSliderChange(self, val):
164 self.p.mpclient.set_volume(val)
166 class PlayControl(Plugin):
167 # public, const
168 info = 'Controls playback.'
170 # public, read-only
171 o = None
173 def _load(self):
174 self.o = wgPlayControl(self, None)
175 QtGui.QApplication.instance().main_win.addToolBar(QtCore.Qt.TopToolBarArea, self.o)
176 def _unload(self):
177 QtGui.QApplication.instance().main_win.removeToolBar(self.o)
178 self.o = None