-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoto_viewer.py
166 lines (143 loc) · 6.48 KB
/
photo_viewer.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 math
from PyQt6 import QtWidgets, QtCore, QtGui
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPixmap
class PhotoViewer(QtWidgets.QGraphicsView):
photoClicked = QtCore.pyqtSignal(QtCore.QPoint)
def __init__(self, parent):
super(PhotoViewer, self).__init__(parent)
self.parent = parent
self.isMouseOver = False
self.ZOOMFACT = 1.25
self._zoom = 0
self._zoomfactor = 1
self._empty = True
self._scene = QtWidgets.QGraphicsScene(self)
self._photo = QtWidgets.QGraphicsPixmapItem()
self._scene.addItem(self._photo)
self._mirror_view: QtWidgets.QGraphicsView | None = None
self.setScene(self._scene)
self.setTransformationAnchor(QtWidgets.QGraphicsView.ViewportAnchor.AnchorUnderMouse)
self.setResizeAnchor(QtWidgets.QGraphicsView.ViewportAnchor.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(30, 30, 30)))
self.setFrameShape(QtWidgets.QFrame.Shape.NoFrame)
def getScene(self):
return self._scene
def setMirrorView(self, view: QtWidgets.QGraphicsView):
self._mirror_view = view
if self._mirror_view:
self._mirror_view.setScene(self._scene)
if self.hasPhoto():
self.fitMirrorView()
def fitMirrorView(self):
photo_rect = QtCore.QRectF(self._photo.pixmap().rect())
self._mirror_view.setSceneRect(photo_rect)
self._mirror_view.fitInView(photo_rect, Qt.AspectRatioMode.KeepAspectRatio)
def hasPhoto(self):
return not self._empty
def printUnityFactor(self):
rect = QtCore.QRectF(self._photo.pixmap().rect())
unity = self.transform().mapRect(QtCore.QRectF(0, 0, 1, 1))
viewrect = self.viewport().rect()
scenerect = self.transform().mapRect(rect)
factor = min(viewrect.width() / scenerect.width(),
viewrect.height() / scenerect.height())
print("puf factor {} vr_w {} sr_w {} u_w {} vr_h {} sr_h {} u_h {} ".format(factor, viewrect.width(), scenerect.width(), unity.width(), viewrect.height(), scenerect.height(), unity.height() ))
def fitInView(self, scale=True):
rect = QtCore.QRectF(self._photo.pixmap().rect())
if not rect.isNull():
self.setSceneRect(rect)
if self.hasPhoto():
unity = self.transform().mapRect(QtCore.QRectF(0, 0, 1, 1))
self.scale(1 / unity.width(), 1 / unity.height())
viewrect = self.viewport().rect()
scenerect = self.transform().mapRect(rect)
factor = min(viewrect.width() / scenerect.width(),
viewrect.height() / scenerect.height())
# here, view scaled to fit:
self._zoomfactor = factor
self._zoom = math.log( self._zoomfactor, self.ZOOMFACT )
self.scale(factor, factor)
# self.parent.updateStatusBar()
if (self.isMouseOver): # should be true on wheel, regardless
self.setDragState()
def setPhoto(self, pixmap: QPixmap | None):
if pixmap and not pixmap.isNull():
self._empty = False
previous_pixmap = self._photo.pixmap()
self._photo.setPixmap(pixmap)
if not previous_pixmap or previous_pixmap.isNull() or not previous_pixmap.size() == pixmap.size():
self.fitInView()
else:
self._empty = True
self._photo.setPixmap(QtGui.QPixmap())
self.resetZoom()
if self._mirror_view:
self.fitMirrorView()
def resetZoom(self):
if self.hasPhoto():
self._zoom = 0
self._zoomfactor = 1.0
unity = self.transform().mapRect(QtCore.QRectF(0, 0, 1, 1))
self.scale(1 / unity.width(), 1 / unity.height())
# self.parent.updateStatusBar()
if (self.isMouseOver):
self.setDragState()
def zoomPlus(self):
if self.hasPhoto():
if self._zoomfactor >= 1.0:
return
factor = self.ZOOMFACT # 1.25
self._zoomfactor = self._zoomfactor * self.ZOOMFACT
self._zoom += 1
self.scale(factor, factor)
# self.parent.updateStatusBar()
self.setDragState()
def zoomMinus(self):
if self.hasPhoto():
rect = QtCore.QRectF(self._photo.pixmap().rect())
# self.scale(1 / unity.width(), 1 / unity.height())
viewrect = self.viewport().rect()
scenerect = self.transform().mapRect(rect)
max_factor = min(viewrect.width() / scenerect.width(),
viewrect.height() / scenerect.height())
print(max_factor)
factor = 1.0/self.ZOOMFACT #0.8
print(factor)
if factor <= max_factor * 0.8:
return
self._zoomfactor = self._zoomfactor / self.ZOOMFACT
print(self._zoomfactor)
self._zoom -= 1
self.scale(factor, factor)
# self.parent.updateStatusBar()
self.setDragState()
def wheelEvent(self, event):
if self.hasPhoto():
if event.angleDelta().y() > 0:
self.zoomPlus()
else:
self.zoomMinus()
def mousePressEvent(self, event):
if self._photo.isUnderMouse():
self.photoClicked.emit(QtCore.QPoint(event.pos()))
super(PhotoViewer, self).mousePressEvent(event)
def getCanDrag(self):
return ((self.horizontalScrollBar().maximum() > 0) or (self.verticalScrollBar().maximum() > 0))
def setDragState(self):
# here we mostly want to take case of the mouse cursor/pointer - and show the hand only when dragging is possible
canDrag = self.getCanDrag()
if (canDrag):
self.setDragMode(QtWidgets.QGraphicsView.DragMode.ScrollHandDrag)
else:
self.setDragMode(QtWidgets.QGraphicsView.DragMode.NoDrag)
def enterEvent(self, event):
self.isMouseOver = True
self.setDragState()
return super(PhotoViewer, self).enterEvent(event)
def leaveEvent(self, event):
self.isMouseOver = False
# no need for setDragState - is autohandled, as we leave
return super(PhotoViewer, self).leaveEvent(event)