mpclient: change info from function to var.
[nephilim.git] / nephilim / plugins / Notify.py
blob4a86cfbfcbac9189b9da9dcfb452ed5a93a590ef
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
22 from ..common import sec2min, APPNAME, expand_tags
23 from ..plugin import Plugin
24 from .. import plugins
26 NOTIFY_PRIORITY_SONG = 1
27 NOTIFY_PRIORITY_VOLUME = 2
29 class winNotify(QtGui.QWidget):
30 parent = None
31 p = None
33 _current_priority = 0
35 timer = None
36 cover_label = None
37 text_label = None
39 def __init__(self, p):
40 QtGui.QWidget.__init__(self, p.parent())
41 self.p = p
42 self.parent = p.parent()
44 self.timer = QtCore.QTimer(self)
45 self.timer.setSingleShot(True)
46 self.timer.timeout.connect(self._hide)
48 layout = QtGui.QHBoxLayout()
49 self.cover_label = QtGui.QLabel()
50 self.text_label = QtGui.QLabel()
51 self.text_label.setWordWrap(True)
53 self.setLayout(QtGui.QHBoxLayout())
54 self.layout().addWidget(self.cover_label)
55 self.layout().addWidget(self.text_label)
57 self.setWindowFlags(QtCore.Qt.ToolTip | QtCore.Qt.WindowStaysOnTopHint)
58 self.setWindowOpacity(0.7)
60 font = QtGui.QFont()
61 font.setPixelSize(20)
62 self.setFont(font)
64 ac = QtGui.QApplication.instance().plugins.plugin('AlbumCover')
65 if ac:
66 ac.cover_changed.connect(self.on_cover_changed)
68 def on_cover_changed(self, cover):
69 if cover.isNull():
70 self.cover_label.clear()
71 return
73 self.cover_label.setPixmap(cover.scaledToHeight(self.fontInfo().pixelSize()*4))
74 self.resize(self.layout().sizeHint())
76 def mousePressEvent(self, event):
77 self.hide()
79 def show(self, text, time = 3000, priority = 0):
80 if not priority >= self._current_priority:
81 return
82 self._current_priority = priority
84 self.text_label.setText(text)
85 self.resize(self.layout().sizeHint())
86 self.centerH()
87 self.setVisible(True)
88 self.timer.start(time)
90 def _hide(self):
91 self.setHidden(True)
92 self._current_priority = -1
94 def centerH(self):
95 screen = QtGui.QDesktopWidget().screenGeometry()
96 size = self.geometry()
97 self.move((screen.width()-size.width())/2, 100)
99 class Notify(Plugin):
100 # public, const
101 info = 'Show notifications in a popup window.'
103 # public, read-only
104 o = None
106 DEFAULTS = {'songformat' : '$track - $artist - $title ($album) [$length]',
107 'timer' : 3000}
109 def _load(self):
110 self.o = winNotify(self)
111 self.mpclient.song_changed.connect(self.onSongChange)
112 self.mpclient.connect_changed.connect(self.on_connect_changed)
113 self.mpclient.state_changed.connect(self.onStateChange)
114 self.mpclient.volume_changed.connect (self.onVolumeChange)
115 def _unload(self):
116 self.o=None
117 self.mpclient.song_changed.disconnect(self.onSongChange)
118 self.mpclient.connect_changed.disconnect(self.on_connect_changed)
119 self.mpclient.state_changed.disconnect(self.onStateChange)
120 self.mpclient.volume_changed.disconnect(self.onVolumeChange)
121 def onSongChange(self):
122 song = self.mpclient.current_song()
123 if not song:
124 return
125 self.settings.beginGroup(self.name)
126 self.o.show(expand_tags(self.settings.value('songformat').toString(), (song,)), self.settings.value('timer').toInt()[0],
127 NOTIFY_PRIORITY_SONG)
128 self.settings.endGroup()
130 def on_connect_changed(self, val):
131 self.o.show('%s.'%('Connected' if val else 'Disconnected'), self.settings.value(self.name + '/timer').toInt()[0])
133 def onStateChange(self, new_state):
134 self.o.show(new_state, self.settings.value(self.name + '/timer').toInt()[0])
136 def onVolumeChange(self, new_vol):
137 self.o.show('Volume: %i%%'%(new_vol), self.settings.value(self.name + '/timer').toInt()[0], priority = NOTIFY_PRIORITY_VOLUME)
139 class SettingsWidgetNotify(Plugin.SettingsWidget):
140 format = None
141 timer = None
143 def __init__(self, plugin):
144 Plugin.SettingsWidget.__init__(self, plugin)
145 self.settings.beginGroup(self.plugin.name)
147 self.format = QtGui.QLineEdit(self.settings.value('songformat').toString())
149 self.timer = QtGui.QLineEdit(self.settings.value('timer').toString())
150 self.timer.setValidator(QtGui.QIntValidator(self.timer))
152 self.setLayout(QtGui.QVBoxLayout())
153 self._add_widget(self.format, 'Format', 'Format of notifications. All tags supported by MPD\n'
154 'will be expanded to their values for current song,\n'
155 'e.g. $track, $title, $artist, $album, $length, $date, etc.')
156 self._add_widget(self.timer, 'Duration', 'How long should notifications be displayed in ms.')
157 self.settings.endGroup()
159 def save_settings(self):
160 self.settings.beginGroup(self.plugin.name)
161 self.settings.setValue('songformat', QVariant(self.format.text()))
162 self.settings.setValue('timer', QVariant(self.timer.text().toInt()[0]))
163 self.settings.endGroup()
164 self.plugin.onSongChange()
166 def get_settings_widget(self):
167 return self.SettingsWidgetNotify(self)