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
21 from ..common
import sec2min
, APPNAME
, expand_tags
22 from ..plugin
import Plugin
23 from .. import plugins
25 NOTIFY_PRIORITY_SONG
= 1
26 NOTIFY_PRIORITY_VOLUME
= 2
28 class winNotify(QtGui
.QWidget
):
38 def __init__(self
, p
):
39 QtGui
.QWidget
.__init
__(self
, p
.parent())
41 self
.parent
= p
.parent()
43 self
.timer
= QtCore
.QTimer(self
)
44 self
.timer
.setSingleShot(True)
45 self
.timer
.timeout
.connect(self
._hide
)
47 layout
= QtGui
.QHBoxLayout()
48 self
.cover_label
= QtGui
.QLabel()
49 self
.text_label
= QtGui
.QLabel()
50 self
.text_label
.setWordWrap(True)
52 self
.setLayout(QtGui
.QHBoxLayout())
53 self
.layout().addWidget(self
.cover_label
)
54 self
.layout().addWidget(self
.text_label
)
56 self
.setWindowFlags(QtCore
.Qt
.ToolTip | QtCore
.Qt
.WindowStaysOnTopHint
)
57 self
.setWindowOpacity(0.7)
63 ac
= QtGui
.QApplication
.instance().plugins
.plugin('AlbumCover')
65 ac
.cover_changed
.connect(self
.on_cover_changed
)
67 def on_cover_changed(self
, cover
):
69 self
.cover_label
.clear()
72 self
.cover_label
.setPixmap(cover
.scaledToHeight(self
.fontInfo().pixelSize()*4))
73 self
.resize(self
.layout().sizeHint())
75 def mousePressEvent(self
, event
):
78 def show(self
, text
, time
= 3000, priority
= 0):
79 if not priority
>= self
._current
_priority
:
81 self
._current
_priority
= priority
83 self
.text_label
.setText(text
)
84 self
.resize(self
.layout().sizeHint())
87 self
.timer
.start(time
)
91 self
._current
_priority
= -1
94 screen
= QtGui
.QDesktopWidget().screenGeometry()
95 size
= self
.geometry()
96 self
.move((screen
.width()-size
.width())/2, 100)
100 info
= 'Show notifications in a popup window.'
105 DEFAULTS
= {'songformat' : '${track} - ${artist} - ${title} (${album}) [${length}]',
109 self
.o
= winNotify(self
)
110 self
.mpclient
.song_changed
.connect(self
.onSongChange
)
111 self
.mpclient
.connect_changed
.connect(self
.on_connect_changed
)
112 self
.mpclient
.state_changed
.connect(self
.onStateChange
)
113 self
.mpclient
.volume_changed
.connect (self
.onVolumeChange
)
116 self
.mpclient
.song_changed
.disconnect(self
.onSongChange
)
117 self
.mpclient
.connect_changed
.disconnect(self
.on_connect_changed
)
118 self
.mpclient
.state_changed
.disconnect(self
.onStateChange
)
119 self
.mpclient
.volume_changed
.disconnect(self
.onVolumeChange
)
120 def onSongChange(self
):
121 song
= self
.mpclient
.cur_song
124 self
.settings
.beginGroup(self
.name
)
125 self
.o
.show(expand_tags(self
.settings
.value('songformat'), (song
,)), int(self
.settings
.value('timer')), NOTIFY_PRIORITY_SONG
)
126 self
.settings
.endGroup()
128 def on_connect_changed(self
, val
):
129 self
.o
.show('%s.'%('Connected' if val
else 'Disconnected'), int(self
.settings
.value(self
.name
+ '/timer')))
131 def onStateChange(self
, new_state
):
132 self
.o
.show(new_state
, int(self
.settings
.value(self
.name
+ '/timer')))
134 def onVolumeChange(self
, new_vol
):
135 self
.o
.show('Volume: %i%%'%(new_vol), int(self
.settings
.value(self
.name
+ '/timer')), priority
= NOTIFY_PRIORITY_VOLUME
)
137 class SettingsWidgetNotify(Plugin
.SettingsWidget
):
141 def __init__(self
, plugin
):
142 Plugin
.SettingsWidget
.__init
__(self
, plugin
)
143 self
.settings
.beginGroup(self
.plugin
.name
)
145 self
.format
= QtGui
.QLineEdit(self
.settings
.value('songformat'))
147 self
.timer
= QtGui
.QLineEdit(self
.settings
.value('timer'))
148 self
.timer
.setValidator(QtGui
.QIntValidator(self
.timer
))
150 self
.setLayout(QtGui
.QVBoxLayout())
151 self
._add
_widget
(self
.format
, 'Format', 'Format of notifications. All tags supported by MPD\n'
152 'will be expanded to their values for current song,\n'
153 'e.g. ${track}, ${title}, ${artist}, ${album}, ${length}, ${date}, etc.')
154 self
._add
_widget
(self
.timer
, 'Duration', 'How long should notifications be displayed in ms.')
155 self
.settings
.endGroup()
157 def save_settings(self
):
158 self
.settings
.beginGroup(self
.plugin
.name
)
159 self
.settings
.setValue('songformat', self
.format
.text())
160 self
.settings
.setValue('timer', int(self
.timer
.text()))
161 self
.settings
.endGroup()
162 self
.plugin
.onSongChange()
164 def get_settings_widget(self
):
165 return self
.SettingsWidgetNotify(self
)