summaryrefslogtreecommitdiff
path: root/plugins/Notify.py
blob: 04ad045819a5e44e4bd9c33407d64d394a858668 (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
from PyQt4 import QtGui,QtCore
from thread import start_new_thread
from traceback import print_exc

import format
from misc import *
from clPlugin import *
import plugins

NOTIFY_SONGFORMAT_DEFAULT='$if($artist,$artist)$if($album, - [$album #$track])\n$title ($timems)';
NOTIFY_TIMER_DEFAULT=3

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

    # data used for showing off
    timer=None
    msg=None
    song=None
    xtra_tags=None

    def __init__(self, p, winMain, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.p=p
        self.winMain=winMain
        self.monty = winMain.monty
        
        self.setWindowFlags(QtCore.Qt.ToolTip)
        self.setWindowOpacity(0.7)
        
        font=QtGui.QFont()
        font.setPixelSize(30)
        font.setFamily('Comic Sans Ms')
        self.setFont(font)
        
    def mousePressEvent(self, event):
        self.hide()

    def show(self, msg, song=None, xtra_tags={}, time=3):
        if not self.isVisible():
            self.setVisible(True)
        self.resizeWindow=True
        self.msg=format.compile(msg)
        self.song=song
        self.xtra_tags=xtra_tags
        if self._timerID:
            self.killTimer(self._timerID)
        self._timerID=self.startTimer(500)
        try:
            self.timer=int(time)*2
        except:
            self.timer=3
        self.raise_()
        self.timerEvent(None)
        
    def hide(self):
        if self._timerID:
            self.killTimer(self._timerID)
            self._timerID=None
        self.setHidden(True)

    
    def centerH(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move((screen.width()-size.width())/2, 100)
    def paintEvent(self, event):
        p=QtGui.QPainter(self)
        margin=3    # margin in pixels
        spacing=10  # space between album cover and text
        
        # determine the Rect our message must fit in
        txt=self.msg(format.params(self.song, self.xtra_tags))
        rect=p.boundingRect(0,0,1,1, QtCore.Qt.AlignHCenter, txt)
        
        # check if 1/ albumcover plugin is loaded, and 2/ there is an 
        # album cover
        width=0
        try:
            cover=plugins.getPlugin('albumcover')
            img=cover.getWidget().getIMG()
            if img:
                width=128
            else:
                spacing=0
        except:
            img=None
            pass
        
        # do we have to resize?
        if self.resizeWindow:
            self.resizeWindow=False
            self.resize(rect.width()+width+margin*3+spacing, max(width,rect.height())+margin*2)
            self.centerH()
        
        # fill up with a nice color :)
        p.fillRect(QtCore.QRect(0,0,self.width(),self.height()), self.palette().brush(QtGui.QPalette.Base))
        
        # draw album cover if necessary
        if img:
            rImg=QtCore.QRectF(margin,margin,width,width)
            p.drawImage(rImg, img)
        
        p.setPen(self.palette().color(QtGui.QPalette.Text))
        rect=p.boundingRect(width+margin+spacing,margin,
                rect.width(),self.height(), QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter, txt)
        p.drawText(rect, QtCore.Qt.AlignHCenter, txt)
    
    def timerEvent(self, event):
        self.timer-=1
        if self.timer<=0:
            self.hide()
        self.update()
        


class pluginNotify(Plugin):
    o=None
    monty = None
    def __init__(self, winMain):
        Plugin.__init__(self, winMain, 'Notify')
        self.addMontyListener('onSongChange', self.onSongChange)
        self.addMontyListener('onReady', self.onReady)
        self.addMontyListener('onDisconnect', self.onDisconnect)
        self.addMontyListener('onStateChange', self.onStateChange)
        self.addMontyListener('onVolumeChange', self.onVolumeChange)
        self.monty = winMain.monty
    
    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):
        self.o.show(self.getSetting('songformat').replace("\n", "\\n"), self.monty.getCurrentSong()
                , time=self.getSetting('timer'))

    def onReady(self, params):
        self.o.show('self.montypc loaded!', monty.getCurrentSong(), time=self.getSetting('timer'))
    
    def onDisconnect(self, params):
        self.o.show('Disconnected!', time=self.getSetting('timer'))
    
    def onStateChange(self, params):
        self.o.show(params['newState'], self.monty.getCurrentSong(), time=self.getSetting('timer'))
    
    def onVolumeChange(self, params):
        self.o.show('Volume: %i%%'%(params['newVolume']), self.monty.getCurrentSong(), time=self.getSetting('timer'))
    
    def _getSettings(self):
        txt=QtGui.QTextEdit()
        txt.insertPlainText(self.getSetting('songformat'))
        return [
            ['songformat', 'Song format', 'How to format the current playing song.', txt],
             ['timer', 'Show seconds', 'How many seconds does the notification have to be shown.', QtGui.QLineEdit(str(self.getSetting('timer')))],
            ]
    def afterSaveSettings(self):
        try:
            int(self.getSetting('timer'))
        except:
            self.getSetting('timer')
            self.getSettingWidget('notify.timer').setText(str(NOTIFY_DEFAULT_TIMER))
        self.onSongChange(None)