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

from misc import *
from clSettings import settings,mpdSettings
from clMonty import monty
from clPlugin import *

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

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

	def __init__(self, winMain, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.winMain=winMain
		
		self.setWindowFlags(QtCore.Qt.ToolTip)
		self.setWindowOpacity(0.7)
		
		font=QtGui.QFont()
		font.setPixelSize(30)
		font.setFamily('Comic Sans Ms')
		self.setFont(font)
		
		monty.addListener('onSongChange', self.onSongChange)
		monty.addListener('onReady', self.onReady)
		monty.addListener('onDisconnect', self.onDisconnect)
		monty.addListener('onStateChange', self.onStateChange)
		monty.addListener('onVolumeChange', self.onVolumeChange)
	
	def onSongChange(self, params):
		self.show('$if($artist,$artist$if($album, [$album #$track])\n$title ($length))', monty.getCurrentSong())

	def onReady(self, params):
		self.show('montypc loaded!')
	
	def onDisconnect(self, params):
		self.show('Disconnected!')
	
	def onStateChange(self, params):
		self.show(params['newState'])
	
	def onVolumeChange(self, params):
		self.show('Volume: %i%%'%(params['newVolume']))
	
	def mousePressEvent(self, event):
		self.hide()

	def show(self, msg, song=None, xtra_tags={}):
		if not self.isVisible():
			self.setVisible(True)
		self.resizeWindow=True
		self.msg=msg
		self.song=song
		self.xtra_tags=xtra_tags
		if self._timerID:
			self.killTimer(self._timerID)
		self._timerID=self.startTimer(200)
		self.timer=11
		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="%s"%(format(self.msg, 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=self.winMain.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()), QtGui.QBrush(QtGui.QColor(230,230,255)))
		
		# draw album cover if necessary
		if img:
			rImg=QtCore.QRectF(margin,margin,width,width)
			p.drawImage(rImg, img)
		
		# Pen In Black ...
		p.setPen(QtCore.Qt.black)
		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
	def __init__(self, winMain):
		Plugin.__init__(self, winMain, 'Notify')
	def _load(self):
		self.o=winNotify(self.winMain)
	def getInfo(self):
		return "Show interesting events in a popup window."

	def _getSettings(self):
		return []
	def afterSaveSettings(self):
		self.o.onSongChange(None)