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
21 from ..common
import Button
22 from ..plugin
import Plugin
24 class wgPlayControl(QtGui
.QToolBar
):
25 """Displays controls for interacting with playing, like play, volume ..."""
41 class VolumeSlider(QtGui
.QSlider
):
43 def __init__(self
, parent
):
44 QtGui
.QSlider
.__init
__(self
, parent
)
45 self
.setOrientation(parent
.orientation())
47 self
.setToolTip('Volume control')
49 def paintEvent(self
, event
):
50 painter
= QtGui
.QPainter(self
)
51 painter
.eraseRect(self
.rect())
53 grad
= QtGui
.QLinearGradient(0, 0, self
.width(), self
.height())
54 grad
.setColorAt(0, self
.palette().color(QtGui
.QPalette
.Window
))
55 grad
.setColorAt(1, self
.palette().color(QtGui
.QPalette
.Highlight
))
56 if self
.orientation() == QtCore
.Qt
.Horizontal
:
57 rect
= QtCore
.QRect(0, 0, self
.width() * self
.value() / self
.maximum(), self
.height())
59 rect
= QtCore
.QRect(0, self
.height() * (1 - float(self
.value()) / self
.maximum()), self
.width(), self
.height())
60 painter
.fillRect(rect
, QtGui
.QBrush(grad
))
62 def __init__(self
, p
, parent
= None):
63 QtGui
.QToolBar
.__init
__(self
, p
.name
, parent
)
66 self
.logger
= p
.logger
67 self
.setObjectName(p
.name
)
69 self
.play_icon
= QtGui
.QIcon('gfx/media-playback-start.svg')
70 self
.pause_icon
= QtGui
.QIcon('gfx/media-playback-pause.svg')
72 self
.play_action
= self
.addAction(self
.play_icon
, 'play', self
.on_play_click
)
73 self
.stop_action
= self
.addAction(QtGui
.QIcon('gfx/media-playback-stop.svg'), 'stop', self
.on_stop_click
)
74 self
.prev_action
= self
.addAction(QtGui
.QIcon('gfx/media-skip-backward.svg'), 'previous', self
.on_prev_click
)
75 self
.next_action
= self
.addAction(QtGui
.QIcon('gfx/media-skip-forward.svg'), 'next', self
.on_next_click
)
78 self
.vol_slider
= self
.VolumeSlider(self
)
79 self
.vol_slider
.valueChanged
.connect(self
.onVolumeSliderChange
)
80 self
.addWidget(self
.vol_slider
)
83 self
.random
= self
.addAction(QtGui
.QIcon('gfx/media-playlist-shuffle.svgz'), 'random')
84 self
.random
.setCheckable(True)
85 self
.random
.toggled
.connect(self
.p
.mpclient
.random
)
86 self
.p
.mpclient
.random_changed
.connect(self
.random
.setChecked
)
88 self
.repeat
= self
.addAction(QtGui
.QIcon('gfx/media-playlist-repeat.svg'), 'repeat')
89 self
.repeat
.setCheckable(True)
90 self
.p
.mpclient
.repeat_changed
.connect(self
.repeat
.setChecked
)
91 self
.repeat
.toggled
.connect(self
.p
.mpclient
.repeat
)
93 self
.single
= self
.addAction(QtGui
.QIcon('gfx/single.png'), 'single mode')
94 self
.single
.setCheckable(True)
95 self
.p
.mpclient
.single_changed
.connect(self
.single
.setChecked
)
96 self
.single
.toggled
.connect(self
.p
.mpclient
.single
)
98 self
.consume
= self
.addAction(QtGui
.QIcon('gfx/consume.png'), 'consume mode')
99 self
.consume
.setCheckable(True)
100 self
.p
.mpclient
.consume_changed
.connect(self
.consume
.setChecked
)
101 self
.consume
.toggled
.connect(self
.p
.mpclient
.consume
)
103 self
.orientationChanged
.connect(self
.vol_slider
.setOrientation
)
105 self
.p
.mpclient
.state_changed
.connect(self
.onStateChange
)
106 self
.p
.mpclient
.volume_changed
.connect(self
.onVolumeChange
)
108 def onStateChange(self
, new_state
):
109 status
= self
.p
.mpclient
.status()
111 if new_state
== 'play':
112 self
.play_action
.setIcon(self
.pause_icon
)
113 self
.play_action
.setToolTip('pause')
114 elif new_state
== 'pause' or new_state
== 'stop':
115 self
.play_action
.setIcon(self
.play_icon
)
116 self
.play_action
.setToolTip('play')
118 def onVolumeChange(self
, new_vol
):
119 self
.vol_slider
.setValue(new_vol
)
121 def on_play_click(self
):
122 status
=self
.p
.mpclient
.status()
123 if status
['state']=='play':
124 self
.logger
.info('Toggling playback')
125 self
.p
.mpclient
.pause()
126 elif status
['state']=='stop':
127 self
.logger
.info('Pausing playback')
128 self
.p
.mpclient
.play(None)
130 self
.p
.mpclient
.resume()
131 def on_stop_click(self
):
132 self
.logger
.info('Stopping playback')
133 self
.p
.mpclient
.stop()
134 def on_prev_click(self
):
135 self
.logger
.info('Playing previous')
136 self
.p
.mpclient
.previous()
137 def on_next_click(self
):
138 self
.logger
.info('Playing next')
139 self
.p
.mpclient
.next()
140 def onVolumeSliderChange(self
):
141 self
.p
.mpclient
.set_volume(self
.vol_slider
.value())
143 class PlayControl(Plugin
):
147 self
.o
= wgPlayControl(self
, None)
148 QtGui
.QApplication
.instance().main_win
.addToolBar(QtCore
.Qt
.TopToolBarArea
, self
.o
)
150 QtGui
.QApplication
.instance().main_win
.removeToolBar(self
.o
)
153 return "Have total control over the playing!"