Repository navigation
Expand file tree
/
Copy pathdisplay.py
More file actions
85 lines (64 loc) · 2.79 KB
/
Copy pathdisplay.py
File metadata and controls
85 lines (64 loc) · 2.79 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
import math
try:
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QWidget, QPushButton, QVBoxLayout
from PyQt5.QtGui import QImage, QPainter, QPixmap, QTransform
from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtCore import pyqtSlot as Slot
except ImportError:
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QWidget
from PyQt5.QtGui import QImage, QPainter
from PyQt5.QtCore import QRectF
from PyQt5.QtCore import pyqtSlot as Slot
class Display(QGraphicsView):
def __init__(self, parent: QWidget = None):
super().__init__(parent)
self.__scene = CustomGraphicsScene(self)
self.setScene(self.__scene)
self.setRenderHint(QPainter.Antialiasing)
self.setRenderHint(QPainter.SmoothPixmapTransform)
self.setDragMode(QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
def set_zoom(self, zoom_factor):
"""Scales the display while keeping aspect ratio."""
self.setTransform(QTransform().scale(zoom_factor, zoom_factor))
@Slot(QImage)
def on_image_received(self, image: QImage):
self.__scene.set_image(image)
self.update()
class CustomGraphicsScene(QGraphicsScene):
def __init__(self, parent: Display = None):
super().__init__(parent)
self.__parent = parent
self.__image = QImage()
def set_image(self, image: QImage):
self.__image = image
self.update()
def drawBackground(self, painter: QPainter, rect: QRectF):
# Display size
display_width = self.__parent.width()
display_height = self.__parent.height()
# Image size
image_width = self.__image.width()
image_height = self.__image.height()
# Return if we don't have an image yet
if image_width == 0 or image_height == 0:
return
# Calculate aspect ratio of display
ratio1 = display_width / display_height
# Calculate aspect ratio of image
ratio2 = image_width / image_height
if ratio1 > ratio2:
# The height must fit to the display height. So h remains and w must be scaled down
image_width = display_height * ratio2
image_height = display_height
else:
# The image width must fit to the display width. So w remains and h must be scaled down
image_width = display_width
image_height = display_width / ratio2
image_pos_x = -1.0 * (image_width / 2.0)
image_pos_y = -1.0 * (image_height / 2.0)
# Remove digits after point
image_pos_x = math.trunc(image_pos_x)
image_pos_y = math.trunc(image_pos_y)
rect = QRectF(image_pos_x, image_pos_y, image_width, image_height)
painter.drawImage(rect, self.__image)