-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaption_window.py
More file actions
121 lines (105 loc) · 4.12 KB
/
caption_window.py
File metadata and controls
121 lines (105 loc) · 4.12 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
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QSizeGrip
from PyQt5.QtCore import Qt, QPoint, QSize
from PyQt5.QtGui import QPalette, QColor, QFont
class CaptionWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowFlags(
Qt.Window |
Qt.FramelessWindowHint |
Qt.WindowStaysOnTopHint |
Qt.Tool # This prevents the window from showing in taskbar
)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setMinimumSize(200, 50) # Set minimum size
self.setup_ui()
# For window dragging
self.dragging = False
self.offset = QPoint()
self.resizing = False
self.resize_offset = QPoint()
def setup_ui(self):
# Main layout
self.main_layout = QVBoxLayout()
self.main_layout.setContentsMargins(10, 5, 10, 5)
# Text label
self.text_label = QLabel()
self.text_label.setWordWrap(True)
self.text_label.setAlignment(Qt.AlignCenter)
# Style the label
font = QFont()
font.setPointSize(14)
font.setBold(True)
self.text_label.setFont(font)
self.text_label.setStyleSheet("""
QLabel {
color: white;
background-color: rgba(0, 0, 0, 0.7);
border-radius: 10px;
padding: 10px;
}
""")
# Size grip (resize handle)
self.size_grip = QSizeGrip(self)
self.size_grip.setStyleSheet("""
QSizeGrip {
background-color: rgba(255, 255, 255, 0.5);
border-radius: 5px;
width: 10px;
height: 10px;
}
QSizeGrip:hover {
background-color: rgba(255, 255, 255, 0.8);
}
""")
# Add widgets to layout
self.main_layout.addWidget(self.text_label)
self.setLayout(self.main_layout)
# Position the size grip
self.size_grip.setFixedSize(15, 15)
# Set a default size
self.resize(400, 100)
def update_text(self, text):
"""Update the caption text"""
self.text_label.setText(text)
def mousePressEvent(self, event):
"""Handle mouse press for window dragging"""
if event.button() == Qt.LeftButton:
# Check if click is in the resize area (bottom-right corner)
resize_area = 15 # Size of the resize area
if (self.width() - event.pos().x() <= resize_area and
self.height() - event.pos().y() <= resize_area):
self.resizing = True
self.resize_offset = event.globalPos() - self.geometry().bottomRight()
else:
self.dragging = True
self.offset = event.pos()
def mouseMoveEvent(self, event):
"""Handle window dragging and resizing"""
if self.resizing:
new_size = event.globalPos() - self.pos() - self.resize_offset
new_width = max(self.minimumWidth(), new_size.x())
new_height = max(self.minimumHeight(), new_size.y())
self.resize(new_width, new_height)
elif self.dragging:
self.move(event.globalPos() - self.offset)
def mouseReleaseEvent(self, event):
"""Handle mouse release for window dragging and resizing"""
if event.button() == Qt.LeftButton:
self.dragging = False
self.resizing = False
def resizeEvent(self, event):
"""Handle resize events"""
super().resizeEvent(event)
# Update size grip position
self.size_grip.move(
self.width() - self.size_grip.width() - 5,
self.height() - self.size_grip.height() - 5
)
def enterEvent(self, event):
"""Show resize handle when mouse enters window"""
self.size_grip.show()
def leaveEvent(self, event):
"""Hide resize handle when mouse leaves window"""
if not self.geometry().contains(self.mapFromGlobal(self.cursor().pos())):
self.size_grip.hide()