-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscsi_search_area_overlay.py
201 lines (162 loc) · 7.48 KB
/
scsi_search_area_overlay.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
import json
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
from PyQt6.QtGui import QPainter, QPen, QBrush, QColor
from PyQt6.QtCore import Qt, QRect, QPoint
from scsi_utils import resource_path, get_user_directory
BOUNDS_OVERLAY_FILE = "data\\rectangle_bounds.json"
class OverlayRectangle(QMainWindow):
def __init__(self, x, y, width, height):
super().__init__()
self.setWindowFlags(Qt.WindowType.FramelessWindowHint | Qt.WindowType.WindowStaysOnTopHint | Qt.WindowType.Tool)
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
self.setGeometry(x, y, width, height)
# Variables for dragging and resizing
self.is_dragging = False
self.is_resizing = False
self.resize_edge_threshold = 10 # Pixels near edges for resizing
self.drag_start_position = QPoint()
self.resize_start_rect = QRect()
self.resize_direction = None
# Minimum size for the rectangle (adjust as needed)
self.min_width = 50
self.min_height = 25
# File to save the rectangle bounds
self.bounds_file = get_user_directory(BOUNDS_OVERLAY_FILE)
self.load_bounds() # Load saved bounds if available
def paintEvent(self, event):
painter = QPainter(self)
# Set pen for the rectangle's border
pen = QPen(Qt.GlobalColor.red, 3)
painter.setPen(pen)
# Set brush for the rectangle's interior (semi-transparent)
brush = QBrush(QColor(255, 0, 0, 50)) # RGBA (Red, Green, Blue, Alpha)
painter.setBrush(brush)
# Draw the rectangle
painter.drawRect(self.rect())
def mousePressEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton:
mouse_pos = event.pos()
# Check if the user clicked near the edges for resizing
self.resize_direction = self.get_resize_direction(mouse_pos)
if self.resize_direction:
self.is_resizing = True
self.resize_start_rect = self.geometry()
self.drag_start_position = event.globalPosition().toPoint()
else:
# Otherwise, start dragging
self.is_dragging = True
self.drag_start_position = event.globalPosition().toPoint() - self.pos()
def mouseMoveEvent(self, event):
if self.is_resizing:
# Calculate resizing based on the direction
self.handle_resize(event.globalPosition().toPoint())
elif self.is_dragging:
# Handle dragging
new_position = event.globalPosition().toPoint() - self.drag_start_position
self.move(new_position)
else:
# Update cursor to indicate resize edges
self.update_cursor(event.pos())
def mouseReleaseEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton:
self.is_dragging = False
self.is_resizing = False
self.resize_direction = None
self.setCursor(Qt.CursorShape.ArrowCursor)
# Save the rectangle bounds when resizing is complete
self.save_bounds()
def get_resize_direction(self, mouse_pos):
"""Determine the resize direction based on mouse position."""
rect = self.rect()
resize_direction = None
if mouse_pos.x() <= self.resize_edge_threshold:
resize_direction = 'left'
elif mouse_pos.x() >= rect.width() - self.resize_edge_threshold:
resize_direction = 'right'
if mouse_pos.y() <= self.resize_edge_threshold:
resize_direction = 'top' if resize_direction is None else resize_direction + '_top'
elif mouse_pos.y() >= rect.height() - self.resize_edge_threshold:
resize_direction = 'bottom' if resize_direction is None else resize_direction + '_bottom'
return resize_direction
def handle_resize(self, global_mouse_pos):
"""Resize the rectangle based on the drag position."""
delta = global_mouse_pos - self.drag_start_position
new_rect = QRect(self.resize_start_rect)
if 'left' in self.resize_direction:
new_rect.setLeft(new_rect.left() + delta.x())
if 'right' in self.resize_direction:
new_rect.setRight(new_rect.right() + delta.x())
if 'top' in self.resize_direction:
new_rect.setTop(new_rect.top() + delta.y())
if 'bottom' in self.resize_direction:
new_rect.setBottom(new_rect.bottom() + delta.y())
# Ensure the new width and height are not smaller than the minimum size
if new_rect.width() < self.min_width:
new_rect.setWidth(self.min_width)
if new_rect.height() < self.min_height:
new_rect.setHeight(self.min_height)
# Apply the new geometry
self.setGeometry(new_rect)
def update_cursor(self, mouse_pos):
"""Update the mouse cursor to indicate resizing edges."""
resize_direction = self.get_resize_direction(mouse_pos)
if resize_direction is None:
self.setCursor(Qt.CursorShape.ArrowCursor)
elif 'left' in resize_direction or 'right' in resize_direction:
self.setCursor(Qt.CursorShape.SizeHorCursor)
elif 'top' in resize_direction or 'bottom' in resize_direction:
self.setCursor(Qt.CursorShape.SizeVerCursor)
elif 'top_left' in resize_direction or 'bottom_right' in resize_direction:
self.setCursor(Qt.CursorShape.SizeFDiagCursor)
elif 'top_right' in resize_direction or 'bottom_left' in resize_direction:
self.setCursor(Qt.CursorShape.SizeBDiagCursor)
def save_bounds(self):
"""Save the rectangle bounds to a file."""
rect = self.geometry()
bounds_data = {
"x": rect.x(),
"y": rect.y(),
"width": rect.width(),
"height": rect.height()
}
with open(self.bounds_file, "w") as file:
json.dump(bounds_data, file, indent=4)
def load_bounds(self):
"""Load saved rectangle bounds from a file, if available."""
try:
with open(self.bounds_file, "r") as file:
bounds_data = json.load(file)
# Set the geometry based on saved bounds
self.setGeometry(bounds_data["x"], bounds_data["y"], bounds_data["width"], bounds_data["height"])
except FileNotFoundError:
# If no saved bounds exist, don't adjust
pass
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
# Main window setup
self.setWindowTitle("Draw Rectangle Example")
self.setGeometry(100, 100, 400, 200)
# Button to draw rectangle
self.draw_button = QPushButton("Draw Rectangle")
self.draw_button.clicked.connect(self.draw_rectangle)
# Layout for the main window
layout = QVBoxLayout()
layout.addWidget(self.draw_button)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def draw_rectangle(self):
# Example coordinates and size (adjust as needed)
x, y, width, height = 500, 300, 200, 100
# Create the overlay rectangle
self.overlay = OverlayRectangle(x, y, width, height)
self.overlay.show()
if __name__ == "__main__":
app = QApplication([])
# Launch the main window
window = MainWindow()
window.show()
app.exec()