forked from SourceryAI/floating-clock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.py
166 lines (128 loc) · 5.48 KB
/
clock.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
import sys
from PySide6.QtCore import QPoint, QSettings, Qt, QTime, QTimer
from PySide6.QtGui import QAction, QFont, QMouseEvent
from PySide6.QtWidgets import QApplication, QLabel, QMenu, QVBoxLayout, QWidget
class Clock(QWidget):
def __init__(self, parent=None) -> None:
super().__init__(parent)
# Geometry of Main Window (x, y, width, height)
self.setGeometry(1100, 800, 180, 60)
# Hide Frame
self.setWindowFlag(Qt.WindowStaysOnTopHint, True)
self.setWindowFlag(Qt.FramelessWindowHint, True)
self.setAttribute(Qt.WA_TranslucentBackground)
# Mouse & Menu
self.pressed = False
self.oldPos = QPoint(0, 0)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.mouseRightMenu)
# Clock Object
clock = QTimer(self)
clock.timeout.connect(self.showTime)
clock.start(1000) # update the clock per second
self.show()
self.settings = QSettings(f"{sys.argv[0]}/../config.ini", QSettings.IniFormat)
font = QFont()
font.setFamily(self.settings.value("USER/FONT_FAMILY"))
font.setPointSize(int(self.settings.value("USER/FONT_SIZE")))
# Load Settings
self.settings = QSettings(f"{sys.argv[0]}/../config.ini", QSettings.IniFormat)
# # Font Object
self.font = QFont()
self.font.setFamily(self.settings.value("DEFAULT/FONT_FAMILY"))
self.font_size_step = int(self.settings.value("USER/FONT_SIZE_STEP"))
# Label Object
self.label = QLabel()
self.label.setAlignment(Qt.AlignCenter)
# Layout
layout = QVBoxLayout(self, spacing=0)
layout.addWidget(self.label)
self.setLayout(layout)
self.reloadUI()
def reloadUI(self):
self.font_size = int(self.settings.value("USER/FONT_SIZE"))
self.font.setPointSize(self.font_size)
self.label.setFont(self.font)
self.label.setStyleSheet(
f"color: {self.settings.value('USER/FONT_COLOR')}; "
f"background-color: {self.settings.value('USER/BG_COLOR')}"
)
def resetUI(self):
default_bg_color = self.settings.value("DEFAULT/BG_COLOR")
default_font_color = self.settings.value("DEFAULT/FONT_COLOR")
default_font_size = self.settings.value("DEFAULT/FONT_SIZE")
self.settings.setValue("USER/BG_COLOR", default_bg_color)
self.settings.setValue("USER/FONT_COLOR", default_font_color)
self.settings.setValue("USER/FONT_SIZE", default_font_size)
self.reloadUI()
def increaseFontSize(self):
self.font_size += self.font_size_step
self.changeFontSize()
def decreaseFontSize(self):
self.font_size -= self.font_size_step
self.changeFontSize()
def changeFontSize(self):
font = self.label.font()
font.setPointSize(self.font_size)
self.label.setFont(font)
def setFontColor(self, color: str) -> None:
self.settings.setValue("USER/FONT_COLOR", color)
self.reloadUI()
def setBackgroundColor(self, color: str) -> None:
self.settings.setValue("USER/BG_COLOR", color)
self.reloadUI()
def mousePressEvent(self, event: QMouseEvent) -> None:
self.pressed = True
self.oldPos = event.pos()
super().mousePressEvent(event)
def mouseMoveEvent(self, event: QMouseEvent) -> None:
if self.pressed:
self.move(self.pos() + (event.pos() - self.oldPos))
super().mouseMoveEvent(event)
def mouseReleaseEvent(self, event: QMouseEvent) -> None:
self.pressed = False
self.oldPos = event.pos()
super().mouseReleaseEvent(event)
def mouseRightMenu(self, pos) -> None:
menu = QMenu(self)
# font size
font_size_menu = menu.addMenu("font size")
larger_action = QAction("larger", self)
smaller_action = QAction("smaller", self)
larger_action.triggered.connect(self.increaseFontSize)
smaller_action.triggered.connect(self.decreaseFontSize)
font_size_menu.addAction(larger_action)
font_size_menu.addAction(smaller_action)
ft_colors = ["none", "blue", "orange", "green", "red", "yellow", "white"]
bg_colors = ["none", "blue", "orange", "green", "red", "yellow", "white"]
# font color
font_color_menu = menu.addMenu("font color")
font_color_actions = [
QAction(color, self, triggered=lambda c=color: self.setFontColor(c))
for color in ft_colors
]
font_color_menu.addActions(font_color_actions)
# background color
bg_color_menu = menu.addMenu("bg color")
bg_color_actions = [
QAction(color, self, triggered=lambda c=color: self.setBackgroundColor(c))
for color in bg_colors
]
bg_color_menu.addActions(bg_color_actions)
# reset
reset_action = QAction("reset", self)
reset_action.triggered.connect(self.resetUI)
menu.addAction(reset_action)
# quit
quit_action = QAction("quit", self)
quit_action.triggered.connect(self.close)
menu.addAction(quit_action)
menu.exec(self.mapToGlobal(pos))
def showTime(self):
current_time = QTime.currentTime().toString("hh:mm")
self.label.setText(current_time)
if __name__ == "__main__":
app = QApplication(sys.argv)
clock = Clock()
clock.show() # show all the widgets
app.exit(app.exec()) # start the app