-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop_clock.py
More file actions
219 lines (171 loc) · 7.02 KB
/
desktop_clock.py
File metadata and controls
219 lines (171 loc) · 7.02 KB
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
# 参考资料:
# - [Animations and Transformations with QtQuick](https://www.pythonguis.com/tutorials/qml-animations-transformations/)
# - [在线QML热更编辑器](https://patrickelectric.work/qmlonline/)
# - [FigmaQML](https://github.com/mmertama/FigmaQML)
from plugin import *
from PyQt5.QtQuick import *
from PyQt5.QtQuickWidgets import *
from PyQt5.QtQml import *
from time import localtime
sys.path.insert(0, os.path.dirname(__file__))
from resource import *
class Backend(QObject):
hms = pyqtSignal(int, int, int, arguments=['hours','minutes','seconds'])
def __init__(self):
super().__init__()
self.timer = QTimer()
self.timer.setInterval(100) # msecs 100 = 1/10th sec
self.timer.timeout.connect(self.updateTime)
self.timer.start()
def updateTime(self):
localTime = localtime()
self.hms.emit(localTime.tm_hour, localTime.tm_min, localTime.tm_sec)
class DrawingScene(QGraphicsScene):
def __init__(self, parent=None):
super().__init__(parent)
qmlUrl = "qrc:/desktop_clock/qml/main.qml"
canvasQuickItem = CanvasQuickQmlItem(QRectF(0, 0, 200, 200), qmlUrl)
canvasQuickItem.setFlags(
QGraphicsItem.ItemIsSelectable
| QGraphicsItem.ItemIsMovable
| QGraphicsItem.ItemIsFocusable
)
canvasQuickItem.setAcceptHoverEvents(True)
self.backend = Backend()
canvasQuickItem.quickWidget.rootObject().setProperty("backend", self.backend)
self.backend.updateTime()
self.addItem(canvasQuickItem)
class DrawingView(QGraphicsView):
def __init__(self, scene: QGraphicsScene, parent=None):
super().__init__(scene, parent)
self.initUI()
def initUI(self):
self.setRenderHints(
QPainter.Antialiasing
| QPainter.HighQualityAntialiasing
| QPainter.TextAntialiasing
| QPainter.SmoothPixmapTransform
)
self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setStyleSheet(
"background: transparent; border:0px; padding: 0px; margin: 0px;"
)
self.scene_width, self.scene_height = 64000, 64000
self.scene().setSceneRect(
-self.scene_width // 2,
-self.scene_height // 2,
self.scene_width,
self.scene_height,
)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setDragMode(QGraphicsView.RubberBandDrag)
class DesktopClockWindow3(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents, True)
self.setWindowFlag(Qt.WindowType.WindowTransparentForInput, True)
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True)
self.setWindowFlags(Qt.WindowType.FramelessWindowHint | Qt.WindowType.WindowStaysOnTopHint | Qt.WindowType.Tool)
self.initUI()
self.show()
screen = QApplication.primaryScreen()
availableGeometry = screen.availableGeometry()
self.move(
availableGeometry.width() - self.width(),
availableGeometry.height() - self.height(),
)
def initUI(self):
self.setStyleSheet("QWidget { background-color: #E3212121; }")
self.layout = QVBoxLayout(self)
self.layout.setContentsMargins(0, 0, 0, 0)
self.scene = DrawingScene()
view = DrawingView(self.scene)
self.layout.addWidget(view)
def paintEvent(self, a0: QPaintEvent) -> None:
backgroundPath = QPainterPath()
backgroundPath.setFillRule(Qt.WindingFill)
return super().paintEvent(a0)
class DesktopClockWindow1(QQuickWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents, True)
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True)
self.setWindowFlags(Qt.WindowType.FramelessWindowHint | Qt.WindowType.WindowStaysOnTopHint | Qt.WindowType.Tool)
self.setClearColor(QColor(Qt.GlobalColor.transparent))
self.setSource(QUrl("qrc:/desktop_clock/qml/main.qml"))
self.backend = Backend()
self.rootObject().setProperty("backend", self.backend)
self.backend.updateTime()
screen = QApplication.primaryScreen()
availableGeometry = screen.availableGeometry()
self.move(
availableGeometry.width() - self.width() - 12,
availableGeometry.height() - self.height() - 12,
)
class DesktopClockWindow2(QQuickView):
def __init__(self, parent=None):
super().__init__(parent)
self.setFlag(Qt.WindowType.FramelessWindowHint, True)
self.setFlag(Qt.WindowType.WindowStaysOnTopHint, True)
self.setFlag(Qt.WindowType.WindowTransparentForInput, True)
self.setColor(QColor(Qt.GlobalColor.transparent))
self.setSource(QUrl("qrc:/desktop_clock/qml/main.qml"))
self.backend = Backend()
self.rootObject().setProperty("backend", self.backend)
self.backend.updateTime()
screen = QApplication.primaryScreen()
availableGeometry = screen.availableGeometry()
self.setPosition(
availableGeometry.width() - self.width() - 12,
availableGeometry.height() - self.height() - 48,
)
class DesktopClock(PluginInterface):
def __init__(self):
super().__init__()
self.effectWnd = None
self._runtimePath = os.path.dirname(os.path.abspath(__file__))
pass
@property
def runtimePath(self):
return self._runtimePath
@property
def previewImages(self) -> list:
folderPath = os.path.join(self.runtimePath, "preview")
images = glob.glob(f"{folderPath}/*.*", recursive=False)
return images
@property
def name(self):
return "DesktopClock"
@property
def displayName(self):
return "桌面时钟"
@property
def desc(self):
return "给桌面右下角添加一个时钟挂件"
@property
def author(self) -> str:
return "yaoxuanzhi"
@property
def icon(self):
return QIcon(self.runtimePath + "/icons/desktop_clock.svg")
@property
def version(self) -> str:
return "v1.0.0"
@property
def url(self) -> str:
return "https://github.com/InterwovenCode/desktop_clock"
@property
def tags(self) -> list:
return ["clock"]
def onChangeEnabled(self):
if self.enable:
QQuickWindow.setSceneGraphBackend(QSGRendererInterface.GraphicsApi.Software)
self.effectWnd = DesktopClockWindow1()
# self.effectWnd = DesktopClockWindow2()
# self.effectWnd = DesktopClockWindow3()
self.effectWnd.show()
else:
self.effectWnd.close()
self.effectWnd = None