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
):
41 def __init__(self
, p
):
42 QtGui
.QWidget
.__init
__(self
, p
.parent())
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)
66 ac
= self
.parent
.plugins
.plugin('AlbumCover')
68 self
.connect(ac
, QtCore
.SIGNAL('cover_changed'), self
.on_cover_changed
)
70 def on_cover_changed(self
, cover
):
72 self
.cover_label
.clear()
75 self
.cover_label
.setPixmap(cover
.scaledToHeight(self
.fontInfo().pixelSize()*4))
77 def mousePressEvent(self
, event
):
80 def show(self
, text
, time
= 3000, priority
= 0):
81 if not priority
>= self
._current
_priority
:
83 self
._current
_priority
= priority
85 self
.text_label
.setText(text
)
86 self
.resize(self
.layout().sizeHint())
89 self
.timer
.start(time
)
93 self
.killTimer(self
._timerID
)
96 self
._current
_priority
= -1
99 screen
= QtGui
.QDesktopWidget().screenGeometry()
100 size
= self
.geometry()
101 self
.move((screen
.width()-size
.width())/2, 100)
103 class Notify(Plugin
):
105 DEFAULTS
= {'songformat' : '$track - $artist - $title ($album) [$length]',
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
)
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
)
123 return "Show interesting events in a popup window."
125 def onSongChange(self
):
126 song
= self
.mpclient().current_song()
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
):
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
)