add licence headers to all files.
[nephilim.git] / nephilim / plugins / Notify.py
blob2f373eb961532979408f19947f7f687bd2f2cc85
2 # Copyright (C) 2008 jerous <jerous@gmail.com>
3 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
5 # Nephilim is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # Nephilim is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
19 from PyQt4 import QtGui, QtCore
20 from PyQt4.QtCore import QVariant
21 from traceback import print_exc
23 from ..misc import sec2min, APPNAME, expand_tags
24 from ..plugin import Plugin
25 from .. import plugins
27 NOTIFY_PRIORITY_SONG = 1
28 NOTIFY_PRIORITY_VOLUME = 2
30 class winNotify(QtGui.QWidget):
31 _timerID = None
32 parent = None
33 p = None
35 _current_priority = 0
37 timer = None
38 cover_label = None
39 text_label = None
41 def __init__(self, p):
42 QtGui.QWidget.__init__(self, p.parent())
43 self.p = p
44 self.parent = p.parent()
46 self.timer = QtCore.QTimer(self)
47 self.timer.setSingleShot(True)
48 self.connect(self.timer, QtCore.SIGNAL('timeout()'), self.hide)
50 layout = QtGui.QHBoxLayout()
51 self.cover_label = QtGui.QLabel()
52 self.text_label = QtGui.QLabel()
53 self.text_label.setWordWrap(True)
55 self.setLayout(QtGui.QHBoxLayout())
56 self.layout().addWidget(self.cover_label)
57 self.layout().addWidget(self.text_label)
59 self.setWindowFlags(QtCore.Qt.ToolTip | QtCore.Qt.WindowStaysOnTopHint)
60 self.setWindowOpacity(0.7)
62 font = QtGui.QFont()
63 font.setPixelSize(20)
64 self.setFont(font)
66 ac = self.parent.plugins.plugin('AlbumCover')
67 if ac:
68 self.connect(ac, QtCore.SIGNAL('cover_changed'), self.on_cover_changed)
70 def on_cover_changed(self, cover):
71 if not cover:
72 self.cover_label.clear()
73 return
75 self.cover_label.setPixmap(cover.scaledToHeight(self.fontInfo().pixelSize()*4))
77 def mousePressEvent(self, event):
78 self.hide()
80 def show(self, text, time = 3000, priority = 0):
81 if not priority >= self._current_priority:
82 return
83 self._current_priority = priority
85 self.text_label.setText(text)
86 self.resize(self.layout().sizeHint())
87 self.centerH()
88 self.setVisible(True)
89 self.timer.start(time)
91 def hide(self):
92 if self._timerID:
93 self.killTimer(self._timerID)
94 self._timerID=None
95 self.setHidden(True)
96 self._current_priority = -1
98 def centerH(self):
99 screen = QtGui.QDesktopWidget().screenGeometry()
100 size = self.geometry()
101 self.move((screen.width()-size.width())/2, 100)
103 class Notify(Plugin):
104 o=None
105 DEFAULTS = {'songformat' : '$track - $artist - $title ($album) [$length]',
106 'timer' : 3000}
108 def _load(self):
109 self.o = winNotify(self)
110 self.connect(self.mpclient(), QtCore.SIGNAL('song_changed'), self.onSongChange)
111 self.connect(self.mpclient(), QtCore.SIGNAL('connected'), self.onConnected)
112 self.connect(self.mpclient(), QtCore.SIGNAL('disconnected'), self.onDisconnect)
113 self.connect(self.mpclient(), QtCore.SIGNAL('state_changed'), self.onStateChange)
114 self.connect(self.mpclient(), QtCore.SIGNAL('volume_changed'),self.onVolumeChange)
115 def _unload(self):
116 self.o=None
117 self.disconnect(self.mpclient(), QtCore.SIGNAL('song_changed'), self.onSongChange)
118 self.disconnect(self.mpclient(), QtCore.SIGNAL('connected'), self.onConnected)
119 self.disconnect(self.mpclient(), QtCore.SIGNAL('disconnected'), self.onDisconnect)
120 self.disconnect(self.mpclient(), QtCore.SIGNAL('state_changed'), self.onStateChange)
121 self.disconnect(self.mpclient(), QtCore.SIGNAL('volume_changed'),self.onVolumeChange)
122 def getInfo(self):
123 return "Show interesting events in a popup window."
125 def onSongChange(self):
126 song = self.mpclient().current_song()
127 if not song:
128 return
129 self.settings().beginGroup(self.name())
130 self.o.show(expand_tags(self.settings().value('songformat').toString(), (song,)), self.settings().value('timer').toInt()[0],
131 NOTIFY_PRIORITY_SONG)
132 self.settings().endGroup()
134 def onConnected(self):
135 self.o.show('%s loaded'%APPNAME, self.settings().value(self.name() + '/timer').toInt()[0])
137 def onDisconnect(self):
138 self.o.show('Disconnected!', self.settings().value(self.name() + '/timer').toInt()[0])
140 def onStateChange(self, new_state):
141 self.o.show(new_state, self.settings().value(self.name() + '/timer').toInt()[0])
143 def onVolumeChange(self, new_vol):
144 self.o.show('Volume: %i%%'%(new_vol), self.settings().value(self.name() + '/timer').toInt()[0], priority = NOTIFY_PRIORITY_VOLUME)
146 class SettingsWidgetNotify(Plugin.SettingsWidget):
147 format = None
148 timer = None
150 def __init__(self, plugin):
151 Plugin.SettingsWidget.__init__(self, plugin)
152 self.settings().beginGroup(self.plugin.name())
154 self.format = QtGui.QLineEdit(self.settings().value('songformat').toString())
156 self.timer = QtGui.QLineEdit(self.settings().value('timer').toString())
157 self.timer.setValidator(QtGui.QIntValidator(self.timer))
159 self.setLayout(QtGui.QVBoxLayout())
160 self._add_widget(self.format, 'Format', 'Format of notifications. All tags supported by MPD\n'
161 'will be expanded to their values for current song,\n'
162 'e.g. $track, $title, $artist, $album, $length, $date, etc.')
163 self._add_widget(self.timer, 'Duration', 'How long should notifications be displayed in ms.')
164 self.settings().endGroup()
166 def save_settings(self):
167 self.settings().beginGroup(self.plugin.name())
168 self.settings().setValue('songformat', QVariant(self.format.text()))
169 self.settings().setValue('timer', QVariant(self.timer.text().toInt()[0]))
170 self.settings().endGroup()
171 self.plugin.onSongChange()
173 def get_settings_widget(self):
174 return self.SettingsWidgetNotify(self)