-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagnifying_label.py
75 lines (55 loc) · 2.79 KB
/
magnifying_label.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
from PyQt5 import QtGui
from PyQt5.QtCore import QRect, Qt
from PyQt5.QtGui import QPainter, QPixmap
from PyQt5.QtWidgets import QLabel
from numpy import half
class MagnifyingLabel(QLabel):
def __init__(self, parent=None):
super(MagnifyingLabel, self).__init__(parent)
self.gray_pen = QtGui.QPen(QtGui.QColor(128, 128, 128))
self.setMouseTracking(True)
def mousePressEvent(self, event):
super().mousePressEvent(event)
self.parent().capture_click(event)
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
if self.pixmap() is not None:
magnify_size = 100
magnification = 2
x = event.pos().x()
y = event.pos().y()
pixmap = self.pixmap()
half_size = magnify_size // (2 * magnification)
if self.alignment() == Qt.AlignTop:
top = y - half_size
left = x - half_size
else:
raise ValueError(f"Invalid alignment {self.alignment()}, magnifying label will not work properly.")
width = magnify_size // magnification
height = magnify_size // magnification
rect = QRect(left, top, width, height)
magnified_pixmap = pixmap.copy(rect)
magnified_pixmap = magnified_pixmap.scaled(magnify_size, magnify_size, Qt.KeepAspectRatio,
Qt.SmoothTransformation)
final_pixmap = QPixmap(magnify_size, magnify_size)
final_pixmap.fill(Qt.white) # Fill the pixmap with white color
x_offset = (magnify_size - magnified_pixmap.width()) // 2
if x - half_size < 0: # Cursor near the left edge
x_offset = magnify_size - magnified_pixmap.width()
elif x + half_size > pixmap.width(): # Cursor near the right edge
x_offset = 0
y_offset = (magnify_size - magnified_pixmap.height()) // 2 # Vertical offset remains centered
if y - half_size < 0: # Cursor near the left edge
y_offset = magnify_size - magnified_pixmap.height()
elif y + half_size > pixmap.height(): # Cursor near the right edge
y_offset = 0
painter = QPainter(final_pixmap)
painter.drawPixmap(x_offset, y_offset, magnified_pixmap)
painter.setPen(self.gray_pen)
crosshair_x = magnify_size // 2
crosshair_y = magnify_size // 2
painter.drawLine(crosshair_x - 10, crosshair_y, crosshair_x + 10, crosshair_y)
painter.drawLine(crosshair_x, crosshair_y - 10, crosshair_x, crosshair_y + 10)
painter.end()
self.parent().magnifier_label.setPixmap(final_pixmap)
self.parent().magnifier_label.move(x, y)