refactor: enhance SplitView to support image rotation and handle null pixmap in ZoomableImageView
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from PySide6.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsPixmapItem, QApplication, QMainWindow, QWidget, QVBoxLayout, QSplitter, QStackedWidget, QPushButton, QLabel, QToolButton
|
||||
from PySide6.QtGui import QEnterEvent, QPixmap, QWheelEvent, QPainter, QBrush, QColor, QIcon
|
||||
from PySide6.QtGui import QEnterEvent, QPixmap, QWheelEvent, QPainter, QBrush, QColor, QIcon, QImage, QTransform
|
||||
from PySide6.QtCore import Qt, QSize, Signal, QEvent
|
||||
import sys
|
||||
from ui.widgets.placeholder_widget import PlaceholderWidget
|
||||
@@ -33,10 +33,14 @@ class ZoomableImageView(QGraphicsView):
|
||||
self._current_scale = 1.0
|
||||
|
||||
def set_image(self, pixmap: QPixmap):
|
||||
# pixmap = QPixmap(image_path)
|
||||
if pixmap.isNull():
|
||||
return
|
||||
|
||||
self._pixmap_item.setPixmap(pixmap)
|
||||
self._scene.setSceneRect(pixmap.rect())
|
||||
# self.reset_transform()
|
||||
if self._current_scale == 1.0:
|
||||
self.fitInView(self._pixmap_item, Qt.AspectRatioMode.KeepAspectRatio)
|
||||
self._first_image = False
|
||||
|
||||
def reset_transform(self):
|
||||
"""Resetuje skalowanie i ustawia 1:1"""
|
||||
@@ -281,10 +285,15 @@ class SplitView(QSplitter):
|
||||
# pixmap.fill(Qt.GlobalColor.lightGray)
|
||||
# self.widget_live.set_image(pixmap)
|
||||
|
||||
self.ref_image_rotate = 0
|
||||
self.ref_pixmap = None
|
||||
|
||||
self.widget_live.toggleOrientation.connect(self.toggle_orientation)
|
||||
self.widget_ref.toggleOrientation.connect(self.toggle_orientation)
|
||||
self.widget_live.swapViews.connect(self.swap_views)
|
||||
self.widget_ref.swapViews.connect(self.swap_views)
|
||||
self.widget_ref.rotateCCW.connect(self.rotate_left)
|
||||
self.widget_ref.rotateCW.connect(self.rotate_right)
|
||||
|
||||
def toggle_orientation(self):
|
||||
if self.orientation() == Qt.Orientation.Vertical:
|
||||
@@ -315,8 +324,10 @@ class SplitView(QSplitter):
|
||||
|
||||
def set_reference_image(self, path_image: str):
|
||||
"""Ustawienie obrazu referencyjnego"""
|
||||
pixmap = QPixmap(path_image)
|
||||
self.widget_ref.set_image(pixmap)
|
||||
self.ref_pixmap = QPixmap(path_image)
|
||||
if self.ref_image_rotate != 0:
|
||||
self.ref_pixmap = self.ref_pixmap.transformed(QTransform().rotate(self.ref_image_rotate))
|
||||
self.widget_ref.set_image(self.ref_pixmap)
|
||||
|
||||
def toggle_live_view(self):
|
||||
"""Przełączanie widoku na żywo"""
|
||||
@@ -324,3 +335,17 @@ class SplitView(QSplitter):
|
||||
self.stack.setCurrentWidget(self.widget_live)
|
||||
else:
|
||||
self.stack.setCurrentWidget(self.widget_start)
|
||||
|
||||
def rotate_left(self):
|
||||
if not self.ref_pixmap:
|
||||
return
|
||||
self.ref_image_rotate = (self.ref_image_rotate - 90) % 360
|
||||
self.ref_pixmap = self.ref_pixmap.transformed(QTransform().rotate(-90))
|
||||
self.widget_ref.set_image(self.ref_pixmap)
|
||||
|
||||
def rotate_right(self):
|
||||
if not self.ref_pixmap:
|
||||
return
|
||||
self.ref_image_rotate = (self.ref_image_rotate + 90) % 360
|
||||
self.ref_pixmap = self.ref_pixmap.transformed(QTransform().rotate(90))
|
||||
self.widget_ref.set_image(self.ref_pixmap)
|
||||
Reference in New Issue
Block a user