summaryrefslogtreecommitdiff
path: root/nephilim/plugins/Systray.py
blob: 0997f3758918a1ccc55c0e9269b406722a356955 (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
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QVariant

from ..plugin import Plugin
from ..misc import sec2min, ORGNAME, APPNAME, appIcon, expand_tags

class Systray(Plugin):
    o         = None
    format    = None
    eventObj  = None
    time      = None   # indicator of current time [0..64]
    appIcon   = None
    pixmap    = None
    DEFAULTS  = {'format': '$track - $title by $artist on $album ($length)'}

    def _load(self):
        self.appIcon = QtGui.QIcon(appIcon)
        self.format = self.settings().value(self.name() + '/format').toString()
        class SystrayWheelEventObject(QtCore.QObject):
            """This class listens for systray-wheel events"""
            def eventFilter(self, object, event):
                if type(event)==QtGui.QWheelEvent:
                    numDegrees=event.delta() / 8
                    numSteps=5*numDegrees/15
                    self.plugin.mpclient().set_volume(self.plugin.mpclient().volume() + numSteps)
                    event.accept()
                    return True
                return False

        self.o = QtGui.QSystemTrayIcon(QtGui.QIcon(appIcon), self.parent())
        self.eventObj=SystrayWheelEventObject()
        self.eventObj.plugin = self
        self.o.installEventFilter(self.eventObj)
        self.parent().connect(self.o, QtCore.SIGNAL('activated (QSystemTrayIcon::ActivationReason)')
            , self.onSysTrayClick)

        self.connect(self.mpclient(), QtCore.SIGNAL('song_changed'), self.update)
        self.connect(self.mpclient(), QtCore.SIGNAL('disconnected'), self.update)
        self.connect(self.mpclient(), QtCore.SIGNAL('time_changed'), self.update)

        self.o.show()
    def _unload(self):
        self.disconnect(self.mpclient(), QtCore.SIGNAL('song_changed'), self.update)
        self.disconnect(self.mpclient(), QtCore.SIGNAL('disconnected'), self.update)
        self.disconnect(self.mpclient(), QtCore.SIGNAL('time_changed'), self.update)
        self.o.hide()
        self.o.setIcon(QtGui.QIcon(None))
        self.o = None
        self.parent()._wheelEvent = None
    def getInfo(self):
        return "Display the mpclientpc icon in the systray."

    def update(self):
        status = self.mpclient().status()
        if not status:
            return

        values = {'state':''}
        values['state']={'play':'playing', 'stop':'stopped', 'pause':'paused'}[status['state']]
        if 'time' in status:
            values['length'] = sec2min(status['length'])
            values['time']   = sec2min(status['time'])

        song = self.mpclient().current_song()
        if song:
            self.o.setToolTip(expand_tags(self.format, (song,)))
        else:
            self.o.setToolTip('%s not playing'%APPNAME)

        try:
            curTime = (64*status['time'])/status['length']
        except:
            curTime = -1
        if self.time != curTime:
            self.time = curTime
            # redraw the systray icon
            self.pixmap = self.appIcon.pixmap(64,64)
            painter     = QtGui.QPainter(self.pixmap)
            painter.fillRect(1, curTime, 63, 64, self.parent().palette().brush(QtGui.QPalette.Base))
            self.appIcon.paint(painter, 1, 0, 63, 64)
            self.o.setIcon(QtGui.QIcon(self.pixmap))
        elif not song:
            self.time = None
            self.o.setIcon(QtGui.QIcon(appIcon))

    def onSysTrayClick(self, reason):
        if reason == QtGui.QSystemTrayIcon.Trigger or\
           reason == QtGui.QSystemTrayIcon.Context:
            w = self.parent()
            # left mouse button
            if w.isVisible():
                w.setVisible(False)
            else:
                w.setVisible(True)
        elif reason == QtGui.QSystemTrayIcon.MiddleClick:
            if self.mpclient().is_playing():
                self.mpclient().pause()
            else:
                self.mpclient().resume()

    class SettingsWidgetSystray(Plugin.SettingsWidget):
        format = None

        def __init__(self, plugin):
            Plugin.SettingsWidget.__init__(self, plugin)

            self.format = QtGui.QLineEdit(self.settings().value(self.plugin.name() + '/format').toString())

            self.setLayout(QtGui.QVBoxLayout())
            self._add_widget(self.format, 'Tooltip format')

        def save_settings(self):
            self.settings().beginGroup(self.plugin.name())
            self.settings().setValue('format', QVariant(self.format.text()))
            self.settings().endGroup()

    def get_settings_widget(self):
        return self.SettingsWidgetSystray(self)