summaryrefslogtreecommitdiff
path: root/plugins/Notify.py
blob: 40767fa8fd71bbac8d759509d1c36ebbc72afb13 (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
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QVariant
from traceback import print_exc

from misc import *
from clPlugin import Plugin
import plugins

NOTIFY_PRIORITY_SONG = 1
NOTIFY_PRIORITY_VOLUME = 2

class winNotify(QtGui.QWidget):
    _timerID=None
    winMain=None
    p=None

    _current_priority = 0

    timer=None

    cover_label = None
    text_label  = None

    def __init__(self, p, winMain, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.p=p
        self.winMain=winMain

        layout = QtGui.QHBoxLayout()
        self.cover_label = QtGui.QLabel()
        self.text_label  = QtGui.QLabel()
        self.text_label.setWordWrap(True)
        layout.addWidget(self.cover_label)
        layout.addWidget(self.text_label)
        self.setLayout(layout)

        self.setWindowFlags(QtCore.Qt.ToolTip | QtCore.Qt.WindowStaysOnTopHint)
        self.setWindowOpacity(0.7)

        font = QtGui.QFont()
        font.setPixelSize(20)
        self.setFont(font)

    def mousePressEvent(self, event):
        self.hide()

    def show(self, text, time = 3, priority = 0):
        if not priority >= self._current_priority:
            return
        self._current_priority = priority

        cover = plugins.getPlugin('albumcover').getWidget().get_cover()
        if cover:
            self.cover_label.setPixmap(cover.scaledToHeight(self.fontInfo().pixelSize()*4))
        else:
            self.cover_label.clear()

        self.text_label.setText(text)
        if self._timerID:
            self.killTimer(self._timerID)
        self._timerID=self.startTimer(500)
        self.timer = time*2
        self.resize(self.layout().sizeHint())
        self.centerH()
        self.setVisible(True)
        self.timerEvent(None)

    def hide(self):
        if self._timerID:
            self.killTimer(self._timerID)
            self._timerID=None
        self.setHidden(True)
        self._current_priority = -1

    def centerH(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move((screen.width()-size.width())/2, 100)

    def timerEvent(self, event):
        self.timer-=1
        if self.timer<=0:
            self.hide()
        self.update()

class pluginNotify(Plugin):
    o=None
    DEFAULTS = {'songformat' : '$track - $artist - $title ($album) [$length]',
                'timer'      : 3}
    def __init__(self, winMain):
        Plugin.__init__(self, winMain, 'Notify')
        self.addListener('onSongChange', self.onSongChange)
        self.addListener('onReady', self.onReady)
        self.addListener('onDisconnect', self.onDisconnect)
        self.addListener('onStateChange', self.onStateChange)
        self.addListener('onVolumeChange', self.onVolumeChange)

    def _load(self):
        self.o = winNotify(self, self.winMain)
    def _unload(self):
        self.o=None
    def getInfo(self):
        return "Show interesting events in a popup window."

    def onSongChange(self, params):
        song = self.mpclient.getCurrentSong()
        if not song:
            return
        self.settings.beginGroup(self.name)
        self.o.show(song.expand_tags(self.settings.value('songformat').toString()), self.settings.value('timer').toInt()[0],
                    NOTIFY_PRIORITY_SONG)
        self.settings.endGroup()

    def onReady(self, params):
        self.o.show('mpclientpc loaded!', self.settings.value(self.name + '/timer').toInt()[0])

    def onDisconnect(self, params):
        self.o.show('Disconnected!', self.settings.value(self.name + '/timer').toInt()[0])

    def onStateChange(self, params):
        self.o.show(params['newState'], self.settings.value(self.name + '/timer').toInt()[0])

    def onVolumeChange(self, params):
        self.o.show('Volume: %i%%'%(params['newVolume']), self.settings.value(self.name + '/timer').toInt()[0], priority = NOTIFY_PRIORITY_VOLUME)

    class SettingsWidgetNotify(Plugin.SettingsWidget):
        format = None
        timer  = None

        def __init__(self, plugin):
            Plugin.SettingsWidget.__init__(self, plugin)
            self.settings.beginGroup(self.plugin.getName())

            self.format = QtGui.QLineEdit(self.settings.value('songformat').toString())

            self.timer = QtGui.QLineEdit(self.settings.value('timer').toString())
            self.timer.setValidator(QtGui.QIntValidator(self.timer))

            self.setLayout(QtGui.QVBoxLayout())
            self.layout().addWidget(self.format)
            self.layout().addWidget(self.timer)
            self.settings.endGroup()

        def save_settings(self):
            self.settings.beginGroup(self.plugin.getName())
            self.settings.setValue('songformat', QVariant(self.format.text()))
            self.settings.setValue('timer', QVariant(self.timer.text().toInt()[0]))
            self.settings.endGroup()
            self.plugin.onSongChange(None)

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