summaryrefslogtreecommitdiff
path: root/nephilim/plugins/PlayControl.py
blob: 4bba711f0b2525402d6a92782388bbb79df8c7f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#
#    Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
#
#    Nephilim is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Nephilim is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Nephilim.  If not, see <http://www.gnu.org/licenses/>.
#

from PyQt4 import QtGui, QtCore

from ..common import Button
from ..plugin import Plugin
from ..       import icons

class wgPlayControl(QtGui.QToolBar):
    """Displays controls for interacting with playing, like play, volume ..."""
    " control buttons"
    play_icon    = None
    pause_icon   = None
    play_action  = None
    stop_action  = None
    prev_action  = None
    next_action  = None
    vol_slider   = None
    repeat       = None
    random       = None
    single       = None
    consume      = None
    outputs_menu = None
    p            = None
    logger       = None
    xfade        = None

    class VolumeSlider(QtGui.QSlider):

        def __init__(self, parent):
            QtGui.QSlider.__init__(self, parent)
            self.setOrientation(parent.orientation())
            self.setMaximum(100)
            self.setToolTip('Volume control')

        def paintEvent(self, event):
            painter = QtGui.QPainter(self)
            painter.eraseRect(self.rect())

            grad = QtGui.QLinearGradient(0, 0, self.width(), self.height())
            grad.setColorAt(0, self.palette().color(QtGui.QPalette.Window))
            grad.setColorAt(1, self.palette().color(QtGui.QPalette.Highlight))
            if self.orientation() == QtCore.Qt.Horizontal:
                rect = QtCore.QRect(0, 0, self.width() * self.value() / self.maximum(), self.height())
            else:
                rect = QtCore.QRect(0, self.height() * (1 - float(self.value()) / self.maximum()), self.width(), self.height())
            painter.fillRect(rect, QtGui.QBrush(grad))

    def __init__(self, p, parent = None):
        QtGui.QToolBar.__init__(self, p.name, parent)
        self.setMovable(True)
        self.p      = p
        self.logger = p.logger
        self.setObjectName(p.name)

        status = self.p.mpclient.status

        self.play_icon   = QtGui.QIcon(':icons/media-playback-start.svg')
        self.pause_icon  = QtGui.QIcon(':icons/media-playback-pause.svg')

        self.play_action = self.addAction(self.play_icon, 'play', self.on_play_click)
        self.stop_action = self.addAction(QtGui.QIcon(':icons/media-playback-stop.svg'),     'stop', self.on_stop_click)
        self.prev_action = self.addAction(QtGui.QIcon(':icons/media-skip-backward.svg'), 'previous', self.on_prev_click)
        self.next_action = self.addAction(QtGui.QIcon(':icons/media-skip-forward.svg'),      'next', self.on_next_click)
        self.addSeparator()

        self.vol_slider = self.VolumeSlider(self)
        self.vol_slider.setSliderPosition(status['volume'])
        self.vol_slider.valueChanged.connect(self.onVolumeSliderChange)
        self.addWidget(self.vol_slider)
        self.addSeparator()

        self.random = self.addAction(QtGui.QIcon(':icons/media-playlist-shuffle.svgz'), 'random')
        self.random.setCheckable(True)
        self.random.setChecked(status['random'])
        self.random.toggled.connect(self.p.mpclient.random)
        self.p.mpclient.random_changed.connect(self.random.setChecked)

        self.repeat = self.addAction(QtGui.QIcon(':icons/media-playlist-repeat.svg'), 'repeat')
        self.repeat.setCheckable(True)
        self.repeat.setChecked(status['repeat'])
        self.p.mpclient.repeat_changed.connect(self.repeat.setChecked)
        self.repeat.toggled.connect(self.p.mpclient.repeat)

        self.single = self.addAction(QtGui.QIcon(':icons/single.png'), 'single mode')
        self.single.setCheckable(True)
        self.single.setChecked(status['single'])
        self.p.mpclient.single_changed.connect(self.single.setChecked)
        self.single.toggled.connect(self.p.mpclient.single)

        self.consume = self.addAction(QtGui.QIcon(':icons/consume.png'), 'consume mode')
        self.consume.setCheckable(True)
        self.consume.setChecked(status['consume'])
        self.p.mpclient.consume_changed.connect(self.consume.setChecked)
        self.consume.toggled.connect(self.p.mpclient.consume)

        self.xfade = QtGui.QSpinBox(self)
        self.xfade.setValue(self.p.mpclient.status['xfade'])
        self.p.mpclient.crossfade_changed.connect(self.xfade.setValue)
        self.xfade.valueChanged.connect(self.p.mpclient.crossfade)
        self.xfade.setToolTip('Set crossfade between songs in seconds.')
        self.addWidget(self.xfade)

        self.outputs_menu = QtGui.QMenu('Audio outputs')
        outputs           = self.addAction(QtGui.QIcon(':icons/outputs.png'), 'Audio outputs')
        outputs.triggered.connect(lambda : self.outputs_menu.popup(QtGui.QCursor.pos()))
        self.__update_outputs()
        self.p.mpclient.connect_changed.connect(self.__update_outputs)


        self.orientationChanged.connect(self.vol_slider.setOrientation)

        self.p.mpclient.state_changed.connect(self.onStateChange)
        self.p.mpclient.volume_changed.connect(self.onVolumeChange)

    def __update_outputs(self):
        self.outputs_menu.clear()
        for output in self.p.mpclient.outputs:
            act = self.outputs_menu.addAction(output.name)
            act.setCheckable(True)
            act.setChecked(output.state)
            act.toggled.connect(output.set_state)
            output.state_changed.connect(act.setChecked)

    def onStateChange(self, new_state):
        status = self.p.mpclient.status

        if new_state == 'play':
            self.play_action.setIcon(self.pause_icon)
            self.play_action.setToolTip('pause')
        elif new_state == 'pause' or new_state == 'stop':
            self.play_action.setIcon(self.play_icon)
            self.play_action.setToolTip('play')

    def onVolumeChange(self, new_vol):
        self.vol_slider.setValue(new_vol)

    def on_play_click(self):
        status=self.p.mpclient.status
        if status['state']=='play':
            self.logger.info('Toggling playback')
            self.p.mpclient.pause()
        elif status['state']=='stop':
            self.logger.info('Pausing playback')
            self.p.mpclient.play(None)
        else:
            self.p.mpclient.resume()
    def on_stop_click(self):
        self.logger.info('Stopping playback')
        self.p.mpclient.stop()
    def on_prev_click(self):
        self.logger.info('Playing previous')
        self.p.mpclient.previous()
    def on_next_click(self):
        self.logger.info('Playing next')
        self.p.mpclient.next()
    def onVolumeSliderChange(self, val):
        self.p.mpclient.set_volume(val)

class PlayControl(Plugin):
    # public, const
    info = 'Controls playback.'

    # public, read-only
    o = None

    def _load(self):
        self.o = wgPlayControl(self, None)
        QtGui.QApplication.instance().main_win.addToolBar(QtCore.Qt.TopToolBarArea, self.o)
    def _unload(self):
        QtGui.QApplication.instance().main_win.removeToolBar(self.o)
        self.o = None