from PyQt4 import QtGui, QtCore from PyQt4.QtCore import QVariant from traceback import print_exc from ..misc import sec2min, APPNAME, expand_tags from ..plugin import Plugin from .. import plugins NOTIFY_PRIORITY_SONG = 1 NOTIFY_PRIORITY_VOLUME = 2 class winNotify(QtGui.QWidget): _timerID = None parent = None p = None _current_priority = 0 timer = None cover_label = None text_label = None def __init__(self, p): QtGui.QWidget.__init__(self, p.parent()) self.p = p self.parent = p.parent() self.timer = QtCore.QTimer(self) self.timer.setSingleShot(True) self.connect(self.timer, QtCore.SIGNAL('timeout()'), self.hide) layout = QtGui.QHBoxLayout() self.cover_label = QtGui.QLabel() self.text_label = QtGui.QLabel() self.text_label.setWordWrap(True) self.setLayout(QtGui.QHBoxLayout()) self.layout().addWidget(self.cover_label) self.layout().addWidget(self.text_label) self.setWindowFlags(QtCore.Qt.ToolTip | QtCore.Qt.WindowStaysOnTopHint) self.setWindowOpacity(0.7) font = QtGui.QFont() font.setPixelSize(20) self.setFont(font) ac = self.parent.plugins.plugin('AlbumCover') if ac: self.connect(ac, QtCore.SIGNAL('cover_changed'), self.on_cover_changed) def on_cover_changed(self, cover): if not cover: self.cover_label.clear() return self.cover_label.setPixmap(cover.scaledToHeight(self.fontInfo().pixelSize()*4)) def mousePressEvent(self, event): self.hide() def show(self, text, time = 3000, priority = 0): if not priority >= self._current_priority: return self._current_priority = priority self.text_label.setText(text) self.resize(self.layout().sizeHint()) self.centerH() self.setVisible(True) self.timer.start(time) 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) class Notify(Plugin): o=None DEFAULTS = {'songformat' : '$track - $artist - $title ($album) [$length]', 'timer' : 3000} def _load(self): self.o = winNotify(self) self.connect(self.mpclient(), QtCore.SIGNAL('song_changed'), self.onSongChange) self.connect(self.mpclient(), QtCore.SIGNAL('connected'), self.onConnected) self.connect(self.mpclient(), QtCore.SIGNAL('disconnected'), self.onDisconnect) self.connect(self.mpclient(), QtCore.SIGNAL('state_changed'), self.onStateChange) self.connect(self.mpclient(), QtCore.SIGNAL('volume_changed'),self.onVolumeChange) def _unload(self): self.o=None self.disconnect(self.mpclient(), QtCore.SIGNAL('song_changed'), self.onSongChange) self.disconnect(self.mpclient(), QtCore.SIGNAL('connected'), self.onConnected) self.disconnect(self.mpclient(), QtCore.SIGNAL('disconnected'), self.onDisconnect) self.disconnect(self.mpclient(), QtCore.SIGNAL('state_changed'), self.onStateChange) self.disconnect(self.mpclient(), QtCore.SIGNAL('volume_changed'),self.onVolumeChange) def getInfo(self): return "Show interesting events in a popup window." def onSongChange(self): song = self.mpclient().current_song() if not song: return self.settings().beginGroup(self.name()) self.o.show(expand_tags(self.settings().value('songformat').toString(), (song,)), self.settings().value('timer').toInt()[0], NOTIFY_PRIORITY_SONG) self.settings().endGroup() def onConnected(self): self.o.show('%s loaded'%APPNAME, self.settings().value(self.name() + '/timer').toInt()[0]) def onDisconnect(self): self.o.show('Disconnected!', self.settings().value(self.name() + '/timer').toInt()[0]) def onStateChange(self, new_state): self.o.show(new_state, self.settings().value(self.name() + '/timer').toInt()[0]) def onVolumeChange(self, new_vol): self.o.show('Volume: %i%%'%(new_vol), 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.name()) 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._add_widget(self.format, 'Format', 'Format of notifications. All tags supported by MPD\n' 'will be expanded to their values for current song,\n' 'e.g. $track, $title, $artist, $album, $length, $date, etc.') self._add_widget(self.timer, 'Duration', 'How long should notifications be displayed in ms.') self.settings().endGroup() def save_settings(self): self.settings().beginGroup(self.plugin.name()) self.settings().setValue('songformat', QVariant(self.format.text())) self.settings().setValue('timer', QVariant(self.timer.text().toInt()[0])) self.settings().endGroup() self.plugin.onSongChange() def get_settings_widget(self): return self.SettingsWidgetNotify(self)