forked from darknessomi/musicbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosdlyrics.py
130 lines (109 loc) · 4.61 KB
/
osdlyrics.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# osdlyrics.py --- desktop lyrics for musicbox
# Copyright (c) 2015-2016 omi & Contributors
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from builtins import super
from future import standard_library
standard_library.install_aliases()
import sys
from multiprocessing import Process
from . import logger
from .config import Config
log = logger.getLogger(__name__)
config = Config()
try:
from PyQt4 import QtGui, QtCore, QtDBus
pyqt_activity = True
except ImportError:
pyqt_activity = False
log.warn("PyQt4 module not installed.")
log.warn("Osdlyrics Not Available.")
if pyqt_activity:
class Lyrics(QtGui.QWidget):
def __init__(self):
super(Lyrics, self).__init__()
self.__dbusAdaptor = LyricsAdapter(self)
self.initUI()
def initUI(self):
self.setStyleSheet("background:" + config.get_item(
"osdlyrics_background"))
if config.get_item("osdlyrics_transparent"):
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.setAttribute(QtCore.Qt.WA_ShowWithoutActivating)
self.setAttribute(QtCore.Qt.WA_X11DoNotAcceptFocus)
self.setFocusPolicy(QtCore.Qt.NoFocus)
if config.get_item("osdlyrics_on_top"):
self.setWindowFlags(QtCore.Qt.FramelessWindowHint |
QtCore.Qt.WindowStaysOnTopHint |
QtCore.Qt.X11BypassWindowManagerHint)
else:
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setMinimumSize(600, 50)
osdlyrics_size = config.get_item("osdlyrics_size")
self.resize(osdlyrics_size[0], osdlyrics_size[1])
scn = QtGui.QApplication.desktop().screenNumber(
QtGui.QApplication.desktop().cursor().pos())
bl = QtGui.QApplication.desktop().screenGeometry(scn).bottomLeft()
br = QtGui.QApplication.desktop().screenGeometry(scn).bottomRight()
bc = (bl+br)/2
frameGeo = self.frameGeometry()
frameGeo.moveCenter(bc)
frameGeo.moveBottom(bc.y())
self.move(frameGeo.topLeft())
self.text = "OSD Lyrics for Musicbox"
self.setWindowTitle("Lyrics")
self.show()
def mousePressEvent(self, event):
self.mpos = event.pos()
def mouseMoveEvent(self, event):
if (event.buttons() and QtCore.Qt.LeftButton):
diff = event.pos() - self.mpos
newpos = self.pos() + diff
self.move(newpos)
def wheelEvent(self, event):
self.resize(self.width() + event.delta(), self.height())
def paintEvent(self, event):
qp = QtGui.QPainter()
qp.begin(self)
self.drawText(event, qp)
qp.end()
def drawText(self, event, qp):
osdlyrics_color = config.get_item("osdlyrics_color")
osdlyrics_font = config.get_item("osdlyrics_font")
font = QtGui.QFont(osdlyrics_font[0], osdlyrics_font[1])
pen = QtGui.QColor(osdlyrics_color[0], osdlyrics_color[1],
osdlyrics_color[2])
qp.setFont(font)
qp.setPen(pen)
qp.drawText(event.rect(), QtCore.Qt.AlignCenter |
QtCore.Qt.TextWordWrap, self.text)
class LyricsAdapter(QtDBus.QDBusAbstractAdaptor):
QtCore.Q_CLASSINFO("D-Bus Interface", "local.musicbox.Lyrics")
QtCore.Q_CLASSINFO(
"D-Bus Introspection",
' <interface name="local.musicbox.Lyrics">\n'
' <method name="refresh_lyrics">\n'
' <arg direction="in" type="s" name="lyric"/>\n'
' </method>\n'
' </interface>\n')
def __init__(self, parent):
super(LyricsAdapter, self).__init__(parent)
@QtCore.pyqtSlot(str)
def refresh_lyrics(self, text):
self.parent().text = text.replace('||', '\n')
self.parent().repaint()
def show_lyrics():
app = QtGui.QApplication(sys.argv)
lyrics = Lyrics()
QtDBus.QDBusConnection.sessionBus().registerService('org.musicbox.Bus')
QtDBus.QDBusConnection.sessionBus().registerObject('/', lyrics)
sys.exit(app.exec_())
def show_lyrics_new_process():
if pyqt_activity and config.get_item("osdlyrics"):
p = Process(target=show_lyrics)
p.daemon = True
p.start()