|
| 1 | +from PyQt5.QtCore import Qt |
| 2 | +from PyQt5.QtGui import QImage, QPixmap, QTransform |
| 3 | +from PyQt5.QtWidgets import QGraphicsPixmapItem, QGraphicsScene, QGraphicsView, QMenu |
| 4 | + |
| 5 | + |
| 6 | +class Zoomable(QGraphicsView): |
| 7 | + """ |
| 8 | + A QGraphicsView widget that allows zooming in and out with the mouse wheel. |
| 9 | +
|
| 10 | + :param parent: The parent widget for the ZoomableViewWidget instance. |
| 11 | + :type parent: QWidget, optional |
| 12 | +
|
| 13 | + Attributes: |
| 14 | + _zoom (float): The current zoom level of the view. |
| 15 | +
|
| 16 | + Methods: |
| 17 | + wheelEvent(event): |
| 18 | + Handles mouse wheel events to zoom in or out based on scroll direction. |
| 19 | +
|
| 20 | + contextMenuEvent(event): |
| 21 | + Opens a context menu with options for zooming in and out. |
| 22 | +
|
| 23 | + zoomIn(): |
| 24 | + Increases the zoom level by a factor of zoom_speed. |
| 25 | +
|
| 26 | + zoomOut(): |
| 27 | + Decreases the zoom level by a factor of zoom_speed. |
| 28 | +
|
| 29 | + zoomReset(): |
| 30 | + Resets the zoom level to 1. |
| 31 | +
|
| 32 | + zoom() -> float: |
| 33 | + Returns the current zoom level. |
| 34 | +
|
| 35 | + updateView(): |
| 36 | + Updates the view transformation based on the current zoom level. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self, parent=None): |
| 40 | + """ |
| 41 | + Initializes the ZoomableViewWidget with an optional parent. |
| 42 | +
|
| 43 | + :param parent: The parent widget for the ZoomableViewWidget instance. |
| 44 | + :type parent: QWidget, optional |
| 45 | + """ |
| 46 | + super(Zoomable, self).__init__(parent) |
| 47 | + self._zoom: float = 1.0 |
| 48 | + self._zoom_speed: float = 1.1 |
| 49 | + self._image_pointer: QGraphicsPixmapItem = None |
| 50 | + self.setDragMode(QGraphicsView.ScrollHandDrag) |
| 51 | + self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) |
| 52 | + self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) |
| 53 | + |
| 54 | + scene = QGraphicsScene(self) |
| 55 | + self.setScene(scene) |
| 56 | + |
| 57 | + def wheelEvent(self, event) -> None: |
| 58 | + """ |
| 59 | + Handles mouse wheel events to adjust the zoom level. |
| 60 | +
|
| 61 | + :param event: The QWheelEvent containing information about the mouse wheel event. |
| 62 | + :type event: QWheelEvent |
| 63 | + """ |
| 64 | + mouse = event.angleDelta().y() / 120 |
| 65 | + |
| 66 | + if mouse > 0: |
| 67 | + self.zoomIn() |
| 68 | + else: |
| 69 | + self.zoomOut() |
| 70 | + |
| 71 | + def contextMenuEvent(self, event) -> None: |
| 72 | + """ |
| 73 | + Opens a context menu with options for zooming in and out. |
| 74 | +
|
| 75 | + :param event: The QContextMenuEvent containing information about the context menu event. |
| 76 | + :type event: QContextMenuEvent |
| 77 | + """ |
| 78 | + menu = QMenu() |
| 79 | + menu.addAction("Zoom in\tMouseWheel Up", self.zoomIn) |
| 80 | + menu.addAction("Zoom out\tMouseWheel Down", self.zoomOut) |
| 81 | + menu.addAction("Reset Zoom", self.zoomReset) |
| 82 | + menu.exec_(event.globalPos()) |
| 83 | + |
| 84 | + def zoomIn(self) -> None: |
| 85 | + """ |
| 86 | + Increases the zoom level by a factor of self._zoom_speed. |
| 87 | + """ |
| 88 | + self._zoom *= self._zoom_speed |
| 89 | + self.update_view() |
| 90 | + |
| 91 | + def zoomOut(self) -> None: |
| 92 | + """ |
| 93 | + Decreases the zoom level by a factor of self._zoom_speed. |
| 94 | + """ |
| 95 | + self._zoom /= self._zoom_speed |
| 96 | + self.update_view() |
| 97 | + |
| 98 | + def zoomReset(self) -> None: |
| 99 | + """ |
| 100 | + Resets the zoom level to 1. |
| 101 | + """ |
| 102 | + self._zoom = 1 |
| 103 | + self.update_view() |
| 104 | + self.fit_view() |
| 105 | + |
| 106 | + def zoom(self) -> float: |
| 107 | + """ |
| 108 | + Returns the current zoom level. |
| 109 | +
|
| 110 | + :return: The current zoom level. |
| 111 | + :rtype: float |
| 112 | + """ |
| 113 | + return self._zoom |
| 114 | + |
| 115 | + def zoom_speed(self) -> float: |
| 116 | + """ |
| 117 | + Returns the current zoom speed. |
| 118 | +
|
| 119 | + :return: The current zoom level. |
| 120 | + :rtype: float |
| 121 | + """ |
| 122 | + return self._zoom_speed |
| 123 | + |
| 124 | + def set_zoom_speed(self, zoom_speed: float) -> None: |
| 125 | + """ |
| 126 | + Set the zoom speed. |
| 127 | + """ |
| 128 | + self._zoom_speed = zoom_speed |
| 129 | + |
| 130 | + def update_view(self) -> None: |
| 131 | + """ |
| 132 | + Updates the view transformation based on the current zoom level. |
| 133 | + """ |
| 134 | + self.setTransform(QTransform().scale(self._zoom, self._zoom)) |
| 135 | + |
| 136 | + def set_image(self, image: QImage) -> None: |
| 137 | + """ |
| 138 | + Update the shown image. |
| 139 | + """ |
| 140 | + |
| 141 | + if self.scene().items(): |
| 142 | + self.scene().removeItem(self._image_pointer) |
| 143 | + |
| 144 | + pixmap = QPixmap(image) |
| 145 | + self._image_pointer = self.scene().addPixmap(pixmap) |
| 146 | + |
| 147 | + def fit_view(self) -> None: |
| 148 | + self.fitInView(self.scene().sceneRect(), Qt.KeepAspectRatio) |
| 149 | + self._zoom = self.transform().m11() |
0 commit comments