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
19 from PyQt4
.QtCore
import QVariant
22 from ..misc
import Button
23 from ..plugin
import Plugin
25 class wgPlayControl(QtGui
.QToolBar
):
26 """Displays controls for interacting with playing, like play, volume ..."""
39 class VolumeSlider(QtGui
.QSlider
):
41 def __init__(self
, parent
):
42 QtGui
.QSlider
.__init
__(self
, parent
)
43 self
.setOrientation(parent
.orientation())
45 self
.setToolTip('Volume control')
47 def paintEvent(self
, event
):
48 painter
= QtGui
.QPainter(self
)
49 painter
.eraseRect(self
.rect())
51 grad
= QtGui
.QLinearGradient(0, 0, self
.width(), self
.height())
52 grad
.setColorAt(0, self
.palette().color(QtGui
.QPalette
.Window
))
53 grad
.setColorAt(1, self
.palette().color(QtGui
.QPalette
.Highlight
))
54 if self
.orientation() == QtCore
.Qt
.Horizontal
:
55 rect
= QtCore
.QRect(0, 0, self
.width() * self
.value() / self
.maximum(), self
.height())
57 rect
= QtCore
.QRect(0, self
.height() * (1 - float(self
.value()) / self
.maximum()), self
.width(), self
.height())
58 painter
.fillRect(rect
, QtGui
.QBrush(grad
))
60 def __init__(self
, p
, parent
= None):
61 QtGui
.QToolBar
.__init
__(self
, p
.name(), parent
)
65 self
.play_icon
= QtGui
.QIcon('gfx/media-playback-start.svg')
66 self
.pause_icon
= QtGui
.QIcon('gfx/media-playback-pause.svg')
68 self
.play_action
= self
.addAction(self
.play_icon
, 'play', self
.on_play_click
)
69 self
.stop_action
= self
.addAction(QtGui
.QIcon('gfx/media-playback-stop.svg'), 'stop', self
.on_stop_click
)
70 self
.prev_action
= self
.addAction(QtGui
.QIcon('gfx/media-skip-backward.svg'), 'previous', self
.on_prev_click
)
71 self
.next_action
= self
.addAction(QtGui
.QIcon('gfx/media-skip-forward.svg'), 'next', self
.on_next_click
)
74 self
.vol_slider
= self
.VolumeSlider(self
)
75 self
.connect(self
.vol_slider
, QtCore
.SIGNAL('valueChanged(int)'),self
.onVolumeSliderChange
)
76 self
.addWidget(self
.vol_slider
)
79 self
.random
= self
.addAction(QtGui
.QIcon('gfx/media-playlist-shuffle.svgz'), 'random')
80 self
.random
.setCheckable(True)
81 self
.connect(self
.random
, QtCore
.SIGNAL('toggled(bool)'), self
.p
.mpclient().random
)
82 self
.connect(self
.p
.mpclient(), QtCore
.SIGNAL('random_changed'), self
.random
.setChecked
)
84 self
.repeat
= self
.addAction(QtGui
.QIcon('gfx/media-playlist-repeat.svg'), 'repeat')
85 self
.repeat
.setCheckable(True)
86 self
.connect(self
.p
.mpclient(), QtCore
.SIGNAL('repeat_changed'), self
.repeat
.setChecked
)
87 self
.connect(self
.repeat
, QtCore
.SIGNAL('toggled(bool)'), self
.p
.mpclient().repeat
)
89 self
.connect(self
, QtCore
.SIGNAL('orientationChanged(Qt::Orientation)'), self
.vol_slider
.setOrientation
)
91 self
.connect(self
.p
.mpclient(), QtCore
.SIGNAL('state_changed'), self
.onStateChange
)
92 self
.connect(self
.p
.mpclient(), QtCore
.SIGNAL('volume_changed'), self
.onVolumeChange
)
94 def onStateChange(self
, new_state
):
95 status
= self
.p
.mpclient().status()
97 if new_state
== 'play':
98 self
.play_action
.setIcon(self
.play_icon
)
99 self
.play_action
.setToolTip('pause')
100 elif new_state
== 'pause' or new_state
== 'stop':
101 self
.play_action
.setIcon(self
.pause_icon
)
102 self
.play_action
.setToolTip('play')
104 def onVolumeChange(self
, new_vol
):
105 self
.vol_slider
.setValue(new_vol
)
107 def on_play_click(self
):
108 status
=self
.p
.mpclient().status()
109 if status
['state']=='play':
110 logging
.info('Toggling playback')
111 self
.p
.mpclient().pause()
112 elif status
['state']=='stop':
113 logging
.info('Pausing playback')
114 self
.p
.mpclient().play(None)
116 self
.p
.mpclient().resume()
117 def on_stop_click(self
):
118 logging
.info('Stopping playback')
119 self
.p
.mpclient().stop()
120 def on_prev_click(self
):
121 logging
.info('Playing previous')
122 self
.p
.mpclient().previous()
123 def on_next_click(self
):
124 logging
.info('Playing next')
125 self
.p
.mpclient().next()
126 def onVolumeSliderChange(self
):
127 self
.p
.mpclient().set_volume(self
.vol_slider
.value())
129 class PlayControl(Plugin
):
133 self
.o
= wgPlayControl(self
, None)
134 self
.parent().addToolBar(QtCore
.Qt
.TopToolBarArea
, self
.o
)
136 self
.parent().removeToolBar(self
.o
)
139 return "Have total control over the playing!"