/
MainWindow.py
775 lines (687 loc) · 31.2 KB
/
MainWindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# -*- coding: utf-8 -*
#-------------------------------------------------------------------------------
# Copyright 2009 E. A. Graham Jr. <txcrackers@gmail.com>.
# Copyright 2010 B. Kroon <bart@tarmack.eu>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
from PyQt4.QtCore import SIGNAL, SLOT, QTimer, Qt, QObject, QEvent, QPoint, QPointF, QSize
from PyQt4.QtGui import QMainWindow, QLabel, QMenu, QIcon, QWidget, QAction, QWidgetAction, QToolButton, \
QBrush, QFontMetrics, QPainter, QLinearGradient, QPalette, QPen, QApplication, QPixmap, QVBoxLayout
from time import time
import os
import cPickle as pickle
try:
import dbus
dbus # Use it once to silence PyFlakes
except ImportError:
dbus = None
import mpdlibrary
import CurrentPlaylistForm
import plugins
import auxilia
from ui import KDE, MainWindow, PlayerForm
DATA_DIR = ''
if KDE:
from PyKDE4.kdeui import KWindowSystem
from PyKDE4.kdeui import KStatusNotifierItem
from PyKDE4.kdeui import KStandardAction
else:
from PyQt4.QtGui import QSystemTrayIcon
class View(QMainWindow, auxilia.Actions, MainWindow):
partyMode = None
hide_window = True
def __init__(self, modelManager, configuration, mpdclient, library, app):
QMainWindow.__init__(self)
self.modelManager = modelManager
self.app = app
self.app.commitData = self.commitData
self.focus = time()
self.shuttingDown = False
self.config = configuration
self.mpdclient = mpdclient
self.library = library
self.appIcon = os.path.abspath(DATA_DIR+'icons/Pythagora.png')
self.setupUi(self)
self.setWindowTitle('Pythagora')
self.setWindowIcon(QIcon(self.appIcon))
# Create standard views.
self.playerForm = PlayerForm(self.modelManager, self.config)
self.topLayout.addWidget(self.playerForm)
self.connect(self.modelManager.playerState, SIGNAL('currentSongChanged'), self.playerForm.updateSong)
self.connect(self.modelManager.playerState, SIGNAL('volumeChanged'), self.playerForm.setVolume)
self.connect(self.modelManager.playerState, SIGNAL('progressChanged'), self.playerForm.setProgress)
self.connect(self.modelManager.playerState, SIGNAL('playStateChanged'), self.playerForm.setPlayState)
self.connect(self.modelManager.playerState, SIGNAL('bitrateChanged'), self.playerForm.songLabel.setBitrate)
self.dbusNotifications = DBusNotifications(self.config)
self.connect(self.modelManager.playerState, SIGNAL('currentSongChanged'), self.dbusNotifications.showNotification)
self.toolBarLayout = self.playerForm.toolBarLayout
self.currentList = CurrentPlaylistForm.CurrentPlaylistForm(
self.modelManager, self, self.app, self.config)
self.currentListLayout.addWidget(self.currentList)
self.connect(self.playerForm.play, SIGNAL('clicked(bool)'), self.modelManager.playerState.playPause)
self.connect(self.playerForm.back, SIGNAL('clicked(bool)'), self.modelManager.playerState.previousSong)
self.connect(self.playerForm.forward, SIGNAL('clicked(bool)'), self.modelManager.playerState.nextSong)
self.connect(self.playerForm.stop, SIGNAL('clicked(bool)'), self.modelManager.playerState.stop)
self.connect(self.playerForm.volume, SIGNAL('valueChanged(int)'), self.modelManager.playerState.setVolume)
# Standard toolbar buttons.
self.exitAction = self.actionExit(self, self.app.quit)
self.exitButton = QToolButton()
self.exitButton.setAutoRaise(True)
self.exitButton.setIconSize(QSize(22, 22))
self.exitButton.setDefaultAction(self.exitAction)
self.settingsAction = self.actionSettings(self, self.showConfig)
self.settingsButton = QToolButton()
self.settingsButton.setAutoRaise(True)
self.settingsButton.setIconSize(QSize(22, 22))
self.settingsButton.setDefaultAction(self.settingsAction)
# Create 'Connect to' menu.
self.menuConnect = QMenu('Connect To')
self.menuConnect.menuAction().setIcon(auxilia.PIcon('network-disconnect'))
self.connectButton = QToolButton()
self.connectButton.setAutoRaise(True)
self.connectButton.setIconSize(QSize(22, 22))
self.connectButton.setPopupMode(QToolButton.InstantPopup)
self.connectButton.setIcon(auxilia.PIcon('network-disconnect'))
self.connectButton.setText('Connect To')
self.connectButton.setMenu(self.menuConnect)
# Create 'MDP' menu.
self.menuMPD = QMenu('MPD')
self.menuMPD.menuAction().setIcon(auxilia.PIcon('network-workgroup'))
self.connect(self.menuConnect, SIGNAL('aboutToShow()'), self._buildConnectTo)
self.mpdButton = QToolButton()
self.mpdButton.setAutoRaise(True)
self.mpdButton.setIconSize(QSize(22, 22))
self.mpdButton.setPopupMode(QToolButton.InstantPopup)
self.mpdButton.setIcon(auxilia.PIcon('network-workgroup'))
self.mpdButton.setText('MPD')
self.mpdButton.setMenu(self.menuMPD)
self.reloadLibrary = self.actionLibReload(self.menuMPD, lambda: self.modelManager.reloadLibrary(True))
self.updateLibrary = self.actionLibUpdate(self.menuMPD, lambda: self.mpdclient.send('update'))
self.rescanLibrary = self.actionLibRescan(self.menuMPD, lambda: self.mpdclient.send('rescan'))
# Create 'Outputs' menu.
self.menuOutputs = QMenu('Outputs')
self.menuOutputs.menuAction().setIcon(auxilia.PIcon('audio-card'))
self.connect(self.menuOutputs, SIGNAL('aboutToShow()'), self._buildOutputs)
self.outputsButton = QToolButton()
self.outputsButton.setAutoRaise(True)
self.outputsButton.setIconSize(QSize(22, 22))
self.outputsButton.setPopupMode(QToolButton.InstantPopup)
self.outputsButton.setIcon(auxilia.PIcon('audio-card'))
self.outputsButton.setText('Outputs')
self.outputsButton.setMenu(self.menuOutputs)
# Fill Toolbar.
self.toolBarLayout.addWidget(self.exitButton)
self.toolBarLayout.addWidget(self.settingsButton)
self.toolBarLayout.addWidget(self.connectButton)
self.toolBarLayout.addWidget(self.outputsButton)
self.toolBarLayout.addWidget(self.mpdButton)
self.toolBarLayout.addStretch(1)
# Fill Statusbar.
self.serverLabel = QLabel('Not connected')
self.numSongsLabel = QLabel('Songs')
self.playTimeLabel = QLabel('playTime')
self.statusbar.addWidget(self.serverLabel)
self.statusbar.addPermanentWidget(self.numSongsLabel)
self.statusbar.addPermanentWidget(self.playTimeLabel)
# Set up trayicon and menu.
if KDE:
self.trayIcon = KTrayIcon(self.appIcon, self)
else:
self.trayIcon = QTrayIcon(self.appIcon, self)
outputsMenuAction = self.menuOutputs.menuAction()
connectMenuAction = self.menuConnect.menuAction()
self.trayIcon.addMenuItem(outputsMenuAction)
self.trayIcon.addMenuItem(connectMenuAction)
self.trayIcon.addMenuItem(self.settingsAction)
self.connect(self.trayIcon, SIGNAL('activate()'), self.toggleHideRestore)
self.connect(self.trayIcon, SIGNAL('secondaryActivateRequested(QPoint)'), self._playPause)
self.connect(self.trayIcon, SIGNAL('scrollRequested(int, Qt::Orientation)'),
lambda amount: self.modelManager.playerState.volumeUp((amount+30)/60))
self.connect(self.modelManager.playerState, SIGNAL('playStateChanged'), self.trayIcon.setState)
self.connect(self.playerForm, SIGNAL('tooltipChanged'), self.trayIcon.setToolTip)
self.connect(self.tabs, SIGNAL('currentChanged(int)'), self._tabsIndexChanged)
self.connect(self.tabs.tabBar(), SIGNAL('tabMoved(int,int)'), self._tabMoved)
self.connect(self.splitter, SIGNAL('splitterMoved(int, int)'), self._storeSplitter)
# Apply configuration.
self.resize(configuration.mgrSize)
self.splitter.setSizes(configuration.mgrSplit)
self.tabs.setCurrentIndex(configuration.tabsIndex)
self.closeEvent = self.closeEvent
self.connect(self.app,SIGNAL('aboutToQuit()'),self.shutdown)
#==============================================================================
# Code for switching tabs on drag & drop. (__init__() continues)
#==============================================================================
# Instantiate timer
self.tabTimer = QTimer()
self.connect(self.tabTimer, SIGNAL('timeout()'), self._selectTab)
# Overload the default dragEvents.
self.tabs.dragLeaveEvent = self.dragLeaveEvent
self.tabs.dragEnterEvent = self.dragEnterEvent
self.tabs.dragMoveEvent = self.dragMoveEvent
def dragEnterEvent(self, event):
'''Starts timer on enter and sets first position.'''
self.tabPos = event.pos()
event.accept()
self.tabTimer.start(500)
def dragLeaveEvent(self, event):
'''If the mouse leaves the tabWidget stop the timer.'''
self.tabTimer.stop()
def dragMoveEvent(self, event):
'''Keep track of the mouse and change the position, restarts the timer when moved.'''
tabPos = event.pos()
moved = tabPos.manhattanLength() - self.tabPos.manhattanLength()
if moved > 7 or moved < -7:
self.tabTimer.start(500)
self.tabPos = tabPos
def _selectTab(self):
'''Changes the view to the tab where the mouse was hovering above.'''
index = self.tabs.tabBar().tabAt(self.tabPos)
self.tabs.setCurrentIndex(index)
self.tabTimer.stop()
#==============================================================================
def keyPressEvent(self, event):
if event.key() == Qt.Key_F11:
self._togglePartymode()
else:
super(View, self).keyPressEvent(event)
def createPluginViews(self):
'''Set all plugin tabs up.'''
loadedPlugins = {}
for plugin in plugins.allPlugins:
plugin = plugin.getWidget(self.modelManager, self.mpdclient, self.config, self.library)
if plugin:
loadedPlugins[plugin.moduleName] = plugin
for name in self.config.tabOrder:
if name in loadedPlugins:
plugin = loadedPlugins.pop(name)
self.tabs.addTab(plugin, auxilia.PIcon(plugin.moduleIcon), plugin.moduleName)
for plugin in loadedPlugins.values():
self.tabs.addTab(plugin, auxilia.PIcon(plugin.moduleIcon), plugin.moduleName)
order = self.config.tabOrder
order.append(plugin.moduleName)
self.config.tabOrder = order
def shutdown(self):
self.shuttingDown = True
self.app.processEvents()
self.mpdclient.disconnect()
self.config.mgrSize = self.size()
print 'debug: shutdown finished'
def commitData(self, sessionManager):
self.hide_window = False
QApplication.commitData(self.app, sessionManager)
def showConfig(self):
self.config.showConfiguration(self)
def closeEvent(self, event):
'''Catch MainWindow's close event so we can hide it instead.'''
if self.hide_window:
self.hide()
event.ignore()
else:
QMainWindow.closeEvent(self, event)
def _storeSplitter(self):
self.config.mgrSplit = self.splitter.sizes()
def _tabsIndexChanged(self, value):
self.config.tabsIndex = self.tabs.currentIndex()
def _tabMoved(self, old, new):
print "DEBUG: Tab from", old, "moved to", new
order = self.config.tabOrder
order.insert(new, order.pop(old))
self.config.tabOrder = order
def toggleHideRestore(self):
'''Show or hide the window based on some parameters. We can detect
when we are obscured and come to the top. In other cases we hide if
mapped and show if not.
'''
if KDE:
if KWindowSystem.activeWindow() == self.winId() and self.isVisible():
self.hide()
else:
self.show()
KWindowSystem.forceActiveWindow(self.winId())
else:
if self.isVisible():
self.hide()
else: self.show()
def _playPause(self):
self.playerForm.play.emit(SIGNAL('clicked(bool)'), True)
def _buildConnectTo(self):
self.menuConnect.clear()
self.menuConnect.addAction(auxilia.PIcon('dialog-cancel'), 'None (disconnect)')
connected = self.mpdclient.connected()
for server in self.config.knownHosts:
if connected and self.config.server and self.config.server[0] == server:
icon = auxilia.PIcon('network-connect')
else: icon = auxilia.PIcon('network-disconnect')
self.menuConnect.addAction(icon, server)
def _buildOutputs(self):
self.menuOutputs.clear()
if self.mpdclient.connected():
print 'debug: Building output menu.'
for output in self.mpdclient.outputs():
action = QAction(output.get('outputname', 'No name'), self.menuOutputs)
action.setCheckable(True)
action.setChecked(output.get('outputenabled', '0') == '1')
action.outputid = output.get('outputid')
self.menuOutputs.addAction(action)
def _togglePartymode(self):
if self.partyMode is None:
self.partyMode = self.isMaximized()
self.showFullScreen()
else:
if self.partyMode == True:
self.showNormal()
self.showMaximized()
else:
self.showNormal()
self.partyMode = None
class PlayerForm(QWidget, PlayerForm):
def __init__(self, modelManager, config):
QWidget.__init__(self)
self.modelManager = modelManager
self.config = config
self.iconPath = ''
self._currentSong = None
self.setupUi(self)
# Set attributes not set trough xml file.
self.back.setIcon(auxilia.PIcon("media-skip-backward"))
self.stop.setIcon(auxilia.PIcon("media-playback-stop"))
self.forward.setIcon(auxilia.PIcon("media-skip-forward"))
self.pauseIcon = auxilia.PIcon("media-playback-pause")
self.startIcon = auxilia.PIcon("media-playback-start")
self.songLabel = SongLabel(config)
self.titleLayout.addWidget(self.songLabel)
self.setAcceptDrops(True)
self.progress.mouseReleaseEvent = self._progressSeekEvent
self.progress.mouseMoveEvent = self._progressShowTimeEvent
self.progress.setMouseTracking(True)
self.songIcon.mousePressEvent = self._iconOverlayEvent
def updateSong(self, song):
self.songLabel.updateSong(song)
if not song is None:
self.progress.setMaximum(song.time or 1)
if song.iconPath:
self.setSongIcon(song.iconPath)
else:
self.setSongIcon(None)
if not self._currentSong is None:
self.disconnect(self._currentSong, SIGNAL('iconChanged'), self._iconChanged)
self.connect(song, SIGNAL('iconChanged'), self._iconChanged)
self.emit(SIGNAL('tooltipChanged'), song.iconPath, richTextLabel(song))
else:
self.setSongIcon(None)
if self.config.server is None:
label = 'Disconnected'
else:
label = 'Connected to %s.' % self.config.server[0]
self.emit(SIGNAL('tooltipChanged'), None, label)
self._currentSong = song
def _iconChanged(self, songID, iconPath):
self.emit(SIGNAL('tooltipChanged'), iconPath, richTextLabel(self._currentSong))
self.setSongIcon(iconPath)
def setSongIcon(self, iconPath):
self.iconPath = iconPath
if iconPath is not None:
height = self.songIcon.geometry().height()
self.songIcon.setPixmap(QPixmap(iconPath).scaled(1000, height, Qt.KeepAspectRatio, Qt.SmoothTransformation))
else:
self.songIcon.clear()
def setProgress(self, progress):
'''
Moves the progress indicator to the correct position, calculates the
timestamps and puts them in place.
'''
songTime = mpdlibrary.Time(self.progress.maximum())
if songTime <= 1:
self.progress.setValue(0)
self.progress.setFormat(progress.human)
else:
self.progress.setValue(progress)
self.progress.setFormat('/'.join((progress.human, songTime.human)))
def setVolume(self, volume):
self.volume.setValue(volume)
def setPlayState(self, state):
if state == 'play':
self.play.setIcon(self.pauseIcon)
else:
self.play.setIcon(self.startIcon)
# Put in trayicon class.
#self.view.trayIcon.setState(state)
self.state = state
def dragEnterEvent(self, event):
if event.provides('mpd/uri'):
event.accept()
def dropEvent(self, event):
if event.provides('mpd/uri'):
event.accept()
data = event.mimeData()
uri_list = pickle.loads(str(data.data('mpd/uri')))
self.modelManager.playQueue.clear()
self.modelManager.playQueue.extend(uri_list)
self.modelManager.playerState.currentSong = 0
self.modelManager.playerState.play()
def _progressSeekEvent(self, event):
if event.button() == Qt.LeftButton:
position = float(event.x()) / int(self.progress.geometry().width())
time = self.modelManager.playerState.currentSong.time
if time > 0:
self.modelManager.playerState.progress = int(time * position)
def _progressShowTimeEvent(self, event):
position = float(event.x()) / int(self.progress.geometry().width())
seconds = position * self.progress.maximum()
self.progress.setToolTip(mpdlibrary.Time(seconds).human)
def _iconOverlayEvent(self, event):
popup = QMenu(self)
layout = QVBoxLayout()
layout.setContentsMargins(4, 4, 4, 4)
popup.setLayout(layout)
iconLabel = QLabel(popup)
layout.addWidget(iconLabel)
closeEvent = lambda event: popup.setParent(None)
geometry = self.songIcon.geometry()
songIcon = QPixmap(self.iconPath)
iconLabel.setGeometry(geometry)
iconLabel.setPixmap(songIcon)
iconLabel.mousePressEvent = closeEvent
popup.popup(event.globalPos() - event.pos())
class SongLabel(QLabel):
title = 'title'
artist = 'artist'
album = 'album'
station = 'station'
bitrate = ''
parts = ('artist', 'album', 'station')
prepends = {
'artist': 'by',
'album': 'from',
'station': 'on',
}
def __init__(self, config):
QLabel.__init__(self)
self.config = config
self.songInToolTip = False
self.setAlignment(Qt.AlignBottom)
self.titleFont = self.font()
self.titleFont.setPointSize(self.font().pointSize()+6)
self.artistFont = self.font()
self.artistFont.setPointSize(self.font().pointSize()+2)
self.albumFont = self.font()
self.albumFont.setItalic(True)
self.stationFont = self.font()
self.stationFont.setItalic(True)
def updateSong(self, song):
if song is None:
if self.config.server is None:
label = 'Disconnected'
else:
label = 'Connected to %s.' % self.config.server[0]
self.setText(label)
else:
title = song.title
artist = song.artist
album = song.album
station = song.station
if album == 'None':
album = ''
self.setText(title, artist, album, station)
def setText(self, title='', artist='', album='', station=''):
self.title = title
self.artist = artist
self.album = album
if station != title:
self.station = station
else:
self.station = ''
self.repaint()
def setBitrate(self, bitrate):
self.bitrate = bitrate if bitrate and bitrate != '0' else ''
self.setToolTip(self.getToolTip())
def getToolTip(self):
title = ''
artist = ''
album = ''
station = ''
bitrate = ''
if self.songInToolTip:
if self.title:
title = '<b><big>%s</big></b>' % self.title
if self.artist:
artist = 'by <big>%s</big>' % self.artist
if self.album:
album = 'from <i>%s</i>' % self.album
if self.station:
station = 'on <i>%s</i>' % self.station
if self.bitrate:
bitrate = 'bitrate: %skbps' % self.bitrate
return '<br>'.join((item for item in (title, artist, album, station, bitrate) if item))
def paintEvent(self, event):
self.songInToolTip = False
gradient = self._gradient()
self.spaceLeft = self.contentsRect()
for part in self.parts:
font = getattr(self, '%sFont' % part)
text = getattr(self, part)
if text:
self._write(self.prepends.get(part, ''), self.font(), gradient)
self._write(text, font, gradient)
if self.spaceLeft.width() <= 3:
self.songInToolTip = True
self.spaceLeft = self.contentsRect()
self.spaceLeft.setBottom(self.spaceLeft.bottom() - QFontMetrics(self.artistFont).height())
self._write(self.title, self.titleFont, gradient)
if self.spaceLeft.width() <= 3:
self.songInToolTip = True
self.setToolTip(self.getToolTip())
def _write(self, text, font, pen):
width = QFontMetrics(font).width(text+' ')
painter = QPainter(self)
painter.setFont(font)
painter.setPen(pen)
painter.drawText(self.spaceLeft, Qt.AlignBottom, text)
self.spaceLeft.setLeft(self.spaceLeft.left() + width ) # move the left edge to the end of what we just painted.
def _gradient(self):
left = QPointF(self.contentsRect().topLeft())
right = QPointF(self.contentsRect().topRight())
gradient = QLinearGradient(left, right)
gradient.setColorAt(0.9, self.palette().color(QPalette.WindowText))
gradient.setColorAt(1.0, self.palette().color(QPalette.Window))
pen = QPen()
pen.setBrush(QBrush(gradient))
return pen
class DBusNotifications(QObject):
def __init__(self, config):
QObject.__init__(self)
self.config = config
self._currentSong = None
if dbus is None:
self.config.showNotification = False
return
self.notificationId = dbus.UInt32(0)
self.appName = unicode(QApplication.applicationName().toUtf8())
self.appIcon = os.path.abspath(DATA_DIR+'icons/Pythagora.png')
self.notificationSummary = '%s, now playing:' % self.appName
self.notificationHints = dbus.Dictionary(
{'suppress-sound': True,
'desktop-entry': 'pythagora',
'image_path': ''},
'sv')
self._connect()
def _connect(self):
try:
self.dbusNotification = dbus.SessionBus().get_object(
'org.freedesktop.Notifications',
'/org/freedesktop/Notifications',
'org.freedesktop.Notifications')
except dbus.DBusException, e:
self.config.showNotification = False
print 'debug: error using dbus - %s' % e
def showNotification(self, song):
if (not song is None) and self.config.showNotification:
if song.iconPath:
self._showNotification(song)
else:
if not self._currentSong is None:
self.disconnect(self._currentSong, SIGNAL('iconChanged'), self._iconChanged)
self._currentSong = song
self.connect(song, SIGNAL('iconChanged'), self._iconChanged)
def _iconChanged(self, songID, iconPath):
self._showNotification(self._currentSong)
def _showNotification(self, song):
label = richTextLabel(song)
self.notificationHints.update({'image_path': song.iconPath})
# Show notification
try:
self.notificationId = self.dbusNotification.Notify(
self.appName, #app_name
self.notificationId, #replaces_id
self.appIcon, #app_icon
self.notificationSummary, #summary
label, #body
[''], #actions
self.notificationHints, #hints
self.config.notificationTimeout * 1000) #timeout
except dbus.DBusException:
print 'debug: Error while showing notification. Will try again for the next song.'
class TrayIconCommon(object):
def updateSong(self, song):
label = richTextLabel(song)
self.setToolTip(song.iconPath, label)
def addMenuItem(self, action):
self.actionList.append(action)
if KDE:
class KTrayIcon(KStatusNotifierItem, TrayIconCommon):
actionList = []
def __init__(self, icon, parent):
KStatusNotifierItem.__init__(self, parent)
self.setStandardActionsEnabled(False)
self.connect(self.contextMenu(), SIGNAL('aboutToShow()'), self._buildMenu)
self.actionQuit = KStandardAction.quit(parent.app, SLOT("quit()"), self)
self.hideResoreAction = QAction('Minimize', self.contextMenu())
self.connect(self.hideResoreAction, SIGNAL('triggered()'), SIGNAL("activate()"))
self.icon = icon
self.parent = parent
self.setIconByName(icon)
self.setCategory(1)
self.setStatus(2)
def setState(self, state):
if state == 'play':
self.setIconByName("media-playback-start")
elif state == 'pause':
self.setIconByName("media-playback-pause")
else:
self.setIconByName("media-playback-stop")
def setToolTip(self, iconPath, text):
if not iconPath:
iconPath = self.icon
super(KTrayIcon, self).setToolTip(iconPath, 'Pythagora, Now Playing:', text)
def activate(self, pos):
self.emit(SIGNAL('activate()'))
def _buildMenu(self):
if self.parent.isVisible():
self.hideResoreAction.setText('Minimize')
else:
self.hideResoreAction.setText('Restore')
for action in self.actionList:
self.contextMenu().addAction(action)
self.contextMenu().addSeparator()
self.contextMenu().addAction(self.hideResoreAction)
self.contextMenu().addAction(self.actionQuit)
else:
class QTrayIcon(QSystemTrayIcon, TrayIconCommon, auxilia.Actions):
def __init__(self, icon, parent):
QSystemTrayIcon.__init__(self, QIcon(icon), parent)
self.parent = parent
self.pauseIcon = auxilia.PIcon("media-playback-pause")
self.startIcon = auxilia.PIcon("media-playback-start")
self.stopIcon = auxilia.PIcon("media-playback-stop")
self.actionList = []
self.menu = QMenu('Pythagora MPD client', parent)
self.menu.addAction(menuTitle(QIcon(icon), 'Pythagora', parent))
self.setContextMenu(self.menu)
self.hideResoreAction = QAction('Minimize', self.menu)
self.connect(self.hideResoreAction, SIGNAL('triggered()'), self._activated)
self.connect(self, SIGNAL('activated(QSystemTrayIcon::ActivationReason)'), self._activated)
self.connect(self.menu, SIGNAL('aboutToShow()'), self._buildMenu)
self.show()
def setState(self, state):
if state == 'play':
self.setIcon(self.startIcon)
elif state == 'pause':
self.setIcon(self.pauseIcon)
else:
self.setIcon(self.stopIcon)
def setToolTip(self, iconPath, text):
super(QTrayIcon, self).setToolTip('Pythagora, Now Playing:<br>%s' % text)
def _activated(self, reason=None):
if reason == QSystemTrayIcon.MiddleClick:
self.emit(SIGNAL('secondaryActivateRequested(QPoint)'), QPoint())
if reason == None or reason == QSystemTrayIcon.Trigger:
self.emit(SIGNAL('activate()'))
def _buildMenu(self):
if self.parent.isVisible():
self.hideResoreAction.setText('Minimize')
else:
self.hideResoreAction.setText('Restore')
for action in self.actionList:
self.menu.addAction(action)
self.menu.addSeparator()
self.menu.addAction(self.hideResoreAction)
self.menu.addAction(self.parent.exitAction)
def event(self, event):
if event.type() == 31: # enum: QEvent.wheel
event.accept()
self.emit(SIGNAL('scrollRequested(int, Qt::Orientation)'), event.delta(), Qt.Horizontal)
return True
else:
super(QTrayIcon, self).event(event)
return False
class EventEater(QObject):
def eventFilter(self, reciever, event):
if event.type() == QEvent.MouseButtonPress or event.type() == QEvent.MouseButtonRelease:
return True
return False
def menuTitle(icon, text, parent):
eventEater = EventEater()
buttonaction = QAction(parent)
font = buttonaction.font()
font.setBold(True)
buttonaction.setFont(font)
buttonaction.setText(text)
buttonaction.setIcon(icon)
action = QWidgetAction(parent)
action.setObjectName('trayMenuTitle')
titleButton = QToolButton(parent)
titleButton.installEventFilter(eventEater)
titleButton.setDefaultAction(buttonaction)
titleButton.setDown(True)
titleButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
action.setDefaultWidget(titleButton)
return action
def richTextLabel(song):
title = song.title
artist = song.artist
album = song.album
station = song.station
if album == 'None':
album = ''
if station and station != title:
station = 'on <i>%s</i>' % station
else:
station = ''
if title:
title = '<b><big>%s</big></b>' % title
if artist:
artist = 'by <big>%s</big>' % artist
if album:
album = 'from <i>%s</i>' % album
return '<br>'.join((item for item in (title, artist, album, station) if item))