2020-06-05 20:39:16 +00:00
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
from PyQt5.QtCore import QObject, Qt, QSize, QRectF, QPointF, QPoint, pyqtSlot, pyqtSignal, QEvent
|
|
|
|
from PyQt5.QtGui import QPixmap, QPainter, QPalette, QCursor
|
2020-06-09 23:24:51 +00:00
|
|
|
from PyQt5.QtWidgets import ( QLabel, QSizePolicy, QWidget, QScrollArea,
|
|
|
|
QScrollBar, QApplication, QAbstractScrollArea )
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
#TODO QWidget version: fix panning while zoomed-in
|
2020-06-09 23:24:51 +00:00
|
|
|
#TODO: add keyboard shortcuts
|
|
|
|
|
|
|
|
class BaseController(QObject):
|
2020-06-10 16:23:48 +00:00
|
|
|
"""Base proxy interface to keep image viewers synchronized.
|
2020-06-09 23:24:51 +00:00
|
|
|
Relays function calls. Singleton. """
|
|
|
|
|
|
|
|
def __init__(self, selectedViewer, referenceViewer, parent):
|
|
|
|
super().__init__()
|
|
|
|
self.selectedViewer = selectedViewer
|
|
|
|
self.referenceViewer = referenceViewer
|
2020-06-10 20:02:08 +00:00
|
|
|
|
|
|
|
# cached pixmaps
|
2020-06-09 23:24:51 +00:00
|
|
|
self.selectedPixmap = QPixmap()
|
|
|
|
self.referencePixmap = QPixmap()
|
|
|
|
self.scaledSelectedPixmap = QPixmap()
|
|
|
|
self.scaledReferencePixmap = QPixmap()
|
2020-06-10 20:02:08 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
self.current_scale = 1.0
|
|
|
|
self._scaleFactor = 1.3 # how fast we zoom
|
2020-06-09 23:24:51 +00:00
|
|
|
self.bestFit = True
|
2020-06-16 19:14:28 +00:00
|
|
|
self.wantScrollBars = True
|
2020-06-09 23:24:51 +00:00
|
|
|
self.parent = parent #needed to change buttons' states
|
2020-06-10 16:23:48 +00:00
|
|
|
self.selectedViewer.controller = self
|
|
|
|
self.referenceViewer.controller = self
|
2020-06-09 23:24:51 +00:00
|
|
|
self._setupConnections()
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def _setupConnections(self):
|
|
|
|
self.selectedViewer.connect_signals()
|
|
|
|
self.referenceViewer.connect_signals()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
def update(self, ref, dupe):
|
|
|
|
self.resetState()
|
|
|
|
self.selectedPixmap = QPixmap(str(dupe.path))
|
2020-06-10 16:23:48 +00:00
|
|
|
if ref is dupe: # currently selected file is the actual reference file
|
2020-06-09 23:24:51 +00:00
|
|
|
self.referencePixmap = QPixmap()
|
|
|
|
self.scaledReferencePixmap = QPixmap()
|
|
|
|
self.parent.buttonImgSwap.setEnabled(False)
|
|
|
|
# disable the blank widget.
|
2020-06-16 19:14:28 +00:00
|
|
|
self.referenceViewer.setImage(self.referencePixmap)
|
2020-06-09 23:24:51 +00:00
|
|
|
else:
|
|
|
|
self.referencePixmap = QPixmap(str(ref.path))
|
|
|
|
self.parent.buttonImgSwap.setEnabled(True)
|
|
|
|
|
|
|
|
self._updateImages()
|
|
|
|
|
|
|
|
def _updateImages(self):
|
|
|
|
target_size = None
|
2020-06-10 16:23:48 +00:00
|
|
|
if not self.selectedPixmap.isNull():
|
2020-06-09 23:24:51 +00:00
|
|
|
target_size = self.selectedViewer.size()
|
|
|
|
if not self.bestFit:
|
|
|
|
# zoomed in state, expand
|
|
|
|
self.scaledSelectedPixmap = self.selectedPixmap.scaled(
|
|
|
|
target_size, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)
|
|
|
|
else:
|
|
|
|
# best fit, keep ratio always
|
|
|
|
self.scaledSelectedPixmap = self.selectedPixmap.scaled(
|
|
|
|
target_size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.selectedViewer.setImage(self.scaledSelectedPixmap)
|
|
|
|
self.selectedViewer.center_and_update()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-10 16:23:48 +00:00
|
|
|
if not self.referencePixmap.isNull():
|
2020-06-09 23:24:51 +00:00
|
|
|
# the selectedImage viewer widget sometimes ends up being bigger
|
|
|
|
# than the referenceImage viewer, which distorts by one pixel the
|
|
|
|
# scaled down pixmap for the reference, hence we'll reuse its size here.
|
|
|
|
# target_size = self.selectedViewer.size()
|
|
|
|
if not self.bestFit:
|
|
|
|
self.scaledReferencePixmap = self.referencePixmap.scaled(
|
|
|
|
target_size, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)
|
|
|
|
else:
|
|
|
|
self.scaledReferencePixmap = self.referencePixmap.scaled(
|
|
|
|
target_size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.referenceViewer.setImage(self.scaledReferencePixmap)
|
|
|
|
self.referenceViewer.center_and_update()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def zoom_in(self):
|
|
|
|
self.scaleImages(True)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def zoom_out(self):
|
|
|
|
self.scaleImages(False)
|
|
|
|
|
|
|
|
@pyqtSlot(bool) # True = zoom-in
|
|
|
|
def scaleImages(self, zoom_type):
|
|
|
|
|
|
|
|
if zoom_type: # zoom_in
|
|
|
|
self.current_scale *= self._scaleFactor
|
|
|
|
self.selectedViewer.zoom_in()
|
|
|
|
self.referenceViewer.zoom_in()
|
|
|
|
else:
|
|
|
|
self.current_scale /= self._scaleFactor
|
|
|
|
self.selectedViewer.zoom_out()
|
|
|
|
self.referenceViewer.zoom_out()
|
|
|
|
|
|
|
|
# self.selectedViewer.scaleBy(self.scaleFactor)
|
|
|
|
# self.referenceViewer.scaleBy(self.scaleFactor)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
self.parent.buttonZoomIn.setEnabled(self.current_scale < 16.0)
|
|
|
|
self.parent.buttonZoomOut.setEnabled(self.current_scale > 1.0)
|
|
|
|
self.parent.buttonBestFit.setEnabled(self.bestFit is False)
|
|
|
|
self.parent.buttonNormalSize.setEnabled(self.current_scale != 1.0)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
def resetState(self):
|
|
|
|
self.selectedPixmap = QPixmap()
|
|
|
|
self.scaledSelectedPixmap = QPixmap()
|
|
|
|
self.referencePixmap = QPixmap()
|
|
|
|
self.scaledReferencePixmap = QPixmap()
|
|
|
|
|
|
|
|
self.setBestFit(True)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.current_scale = 1.0
|
|
|
|
|
|
|
|
self.selectedViewer.resetCenter()
|
|
|
|
self.referenceViewer.resetCenter()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
self.parent.buttonZoomIn.setEnabled(False)
|
|
|
|
self.parent.buttonZoomOut.setEnabled(False)
|
|
|
|
self.parent.buttonBestFit.setEnabled(False) # active mode by default
|
|
|
|
self.parent.buttonNormalSize.setEnabled(True)
|
|
|
|
|
|
|
|
def clear_all(self):
|
|
|
|
"""No item from the model, disable and clear everything."""
|
|
|
|
self.resetState()
|
2020-06-16 19:14:28 +00:00
|
|
|
self.selectedViewer.setImage(self.selectedPixmap) # null
|
2020-06-09 23:24:51 +00:00
|
|
|
self.selectedViewer.setDisabled(True)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.referenceViewer.setImage(self.referencePixmap) # null
|
2020-06-09 23:24:51 +00:00
|
|
|
self.referenceViewer.setDisabled(True)
|
|
|
|
self.parent.buttonImgSwap.setDisabled(True)
|
|
|
|
self.parent.buttonNormalSize.setDisabled(True)
|
|
|
|
|
2020-06-10 20:02:08 +00:00
|
|
|
@pyqtSlot()
|
2020-06-16 19:14:28 +00:00
|
|
|
def scale_to_bestfit(self):
|
|
|
|
"""Setup before scaling to bestfit"""
|
2020-06-09 23:24:51 +00:00
|
|
|
self.setBestFit(True)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.current_scale = 1.0
|
|
|
|
|
|
|
|
self.selectedViewer.scaleBy(1.0)
|
|
|
|
self.referenceViewer.scaleBy(1.0)
|
|
|
|
|
|
|
|
self.selectedViewer.resetCenter()
|
|
|
|
self.referenceViewer.resetCenter()
|
|
|
|
self._updateImages()
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
self.parent.buttonBestFit.setEnabled(False)
|
|
|
|
self.parent.buttonZoomOut.setEnabled(False)
|
|
|
|
self.parent.buttonZoomIn.setEnabled(False)
|
|
|
|
self.parent.buttonNormalSize.setEnabled(True)
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def setBestFit(self, value):
|
|
|
|
self.bestFit = value
|
|
|
|
self.selectedViewer.bestFit = value
|
|
|
|
self.referenceViewer.bestFit = value
|
|
|
|
|
2020-06-10 20:02:08 +00:00
|
|
|
@pyqtSlot()
|
2020-06-09 23:24:51 +00:00
|
|
|
def zoomNormalSize(self):
|
|
|
|
self.setBestFit(False)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.current_scale = 1.0
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
self.selectedViewer.setImage(self.selectedPixmap)
|
|
|
|
self.referenceViewer.setImage(self.referencePixmap)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
self.selectedViewer.center_and_update()
|
|
|
|
self.referenceViewer.center_and_update()
|
2020-06-10 16:23:48 +00:00
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
self.selectedViewer.pixmapReset()
|
|
|
|
self.referenceViewer.pixmapReset()
|
|
|
|
|
|
|
|
self.parent.buttonNormalSize.setEnabled(False)
|
|
|
|
self.parent.buttonZoomIn.setEnabled(True)
|
|
|
|
self.parent.buttonZoomOut.setEnabled(True)
|
|
|
|
self.parent.buttonBestFit.setEnabled(True)
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def syncCenters(self): # virtual
|
|
|
|
pass
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def swapPixmaps(self): #virtual
|
|
|
|
pass
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
class QWidgetController(BaseController):
|
|
|
|
"""Specialized version for QWidget-based viewers"""
|
|
|
|
def __init__(self, selectedViewer, referenceViewer, parent):
|
|
|
|
super().__init__(selectedViewer, referenceViewer, parent)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-10 16:23:48 +00:00
|
|
|
@pyqtSlot(QPointF)
|
2020-06-16 19:14:28 +00:00
|
|
|
def onDraggedMouse(self, delta):
|
2020-06-10 16:23:48 +00:00
|
|
|
if self.sender() is self.referenceViewer:
|
2020-06-16 19:14:28 +00:00
|
|
|
self.selectedViewer.onDraggedMouse(delta)
|
2020-06-10 16:23:48 +00:00
|
|
|
else:
|
2020-06-16 19:14:28 +00:00
|
|
|
self.referenceViewer.onDraggedMouse(delta)
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def swapPixmaps(self):
|
|
|
|
self.selectedViewer.getPixmap().swap(self.referenceViewer.getPixmap())
|
|
|
|
self.selectedViewer.center_and_update()
|
|
|
|
self.referenceViewer.center_and_update()
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
class ScrollAreaController(BaseController):
|
2020-06-10 16:23:48 +00:00
|
|
|
"""Specialized version fro QLabel-based viewers"""
|
2020-06-09 23:24:51 +00:00
|
|
|
def __init__(self, selectedViewer, referenceViewer, parent):
|
|
|
|
super().__init__(selectedViewer, referenceViewer, parent)
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
@pyqtSlot(QPointF)
|
|
|
|
def onDraggedMouse(self, delta):
|
|
|
|
if self.sender() is self.referenceViewer:
|
|
|
|
self.selectedViewer.onDraggedMouse(delta)
|
|
|
|
else:
|
|
|
|
self.referenceViewer.onDraggedMouse(delta)
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def swapPixmaps(self):
|
|
|
|
self.referenceViewer._pixmap.swap(self.selectedViewer._pixmap)
|
|
|
|
self.referenceViewer.setCachedPixmap()
|
|
|
|
self.selectedViewer.setCachedPixmap()
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def syncCenters(self):
|
|
|
|
self.selectedViewer.setCenter(self.referenceViewer.getCenter())
|
|
|
|
self.referenceViewer.setCenter(self.selectedViewer.getCenter())
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GraphicsViewController(BaseController):
|
2020-06-10 16:23:48 +00:00
|
|
|
"""Specialized version fro QGraphicsView-based viewers"""
|
2020-06-16 19:14:28 +00:00
|
|
|
def __init__(self, selectedViewer, referenceViewer, parent):
|
|
|
|
super().__init__(selectedViewer, referenceViewer, parent)
|
|
|
|
|
|
|
|
@pyqtSlot(QPointF)
|
|
|
|
def onDraggedMouse(self, delta):
|
|
|
|
if self.sender() is self.referenceViewer:
|
|
|
|
self.selectedViewer.onDraggedMouse(delta)
|
|
|
|
else:
|
|
|
|
self.referenceViewer.onDraggedMouse(delta)
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def syncCenters(self):
|
|
|
|
self.selectedViewer.setCenter(self.referenceViewer.getCenter())
|
|
|
|
self.referenceViewer.setCenter(self.selectedViewer.getCenter())
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class QWidgetImageViewer(QWidget):
|
2020-06-16 19:14:28 +00:00
|
|
|
"""Uses a QPixmap as the center piece."""
|
2020-06-09 23:24:51 +00:00
|
|
|
mouseDragged = pyqtSignal(QPointF)
|
2020-06-16 19:14:28 +00:00
|
|
|
mouseWheeled = pyqtSignal(bool)
|
2020-06-05 20:39:16 +00:00
|
|
|
|
|
|
|
def __init__(self, parent, name=""):
|
|
|
|
super().__init__(parent)
|
2020-06-09 23:24:51 +00:00
|
|
|
self._app = QApplication
|
|
|
|
self._pixmap = QPixmap()
|
|
|
|
self._rect = QRectF()
|
|
|
|
self._reference = QPointF()
|
|
|
|
self._delta = QPointF()
|
2020-06-16 19:14:28 +00:00
|
|
|
self._scaleFactor = 1.3
|
|
|
|
self._current_scale = 1.0
|
2020-06-09 23:24:51 +00:00
|
|
|
self._drag = False
|
2020-06-10 16:23:48 +00:00
|
|
|
self._dragConnection = None
|
|
|
|
self._wheelConnection = None
|
2020-06-09 23:24:51 +00:00
|
|
|
self._instance_name = name
|
|
|
|
self.bestFit = True
|
2020-06-10 16:23:48 +00:00
|
|
|
self.controller = None
|
2020-06-16 19:14:28 +00:00
|
|
|
self.setMouseTracking(False)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'{self._instance_name}'
|
|
|
|
|
2020-06-10 20:02:08 +00:00
|
|
|
def getPixmap(self):
|
|
|
|
return self._pixmap
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
def paintEvent(self, event):
|
|
|
|
painter = QPainter(self)
|
|
|
|
painter.translate(self.rect().center())
|
2020-06-16 19:14:28 +00:00
|
|
|
painter.scale(self._current_scale, self._current_scale)
|
2020-06-09 23:24:51 +00:00
|
|
|
painter.translate(self._delta)
|
|
|
|
painter.drawPixmap(self._rect.topLeft(), self._pixmap)
|
2020-06-16 19:14:28 +00:00
|
|
|
# print(f"{self} paintEvent delta={self._delta} current scale={self._current_scale}")
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def resetCenter(self):
|
2020-06-09 23:24:51 +00:00
|
|
|
""" Resets origin """
|
2020-06-16 19:14:28 +00:00
|
|
|
self._delta = QPointF() # FIXME does this even work?
|
|
|
|
self.scaleBy(1.0)
|
2020-06-09 23:24:51 +00:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def changeEvent(self, event):
|
|
|
|
if event.type() == QEvent.EnabledChange:
|
|
|
|
print(f"{self} is now {'enabled' if self.isEnabled() else 'disabled'}")
|
2020-06-10 16:23:48 +00:00
|
|
|
if self.isEnabled():
|
|
|
|
self.connect_signals()
|
|
|
|
return
|
|
|
|
self.disconnect_signals()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
def mousePressEvent(self, event):
|
|
|
|
if self.bestFit:
|
|
|
|
event.ignore()
|
|
|
|
return
|
2020-06-16 19:14:28 +00:00
|
|
|
if event.button() == Qt.LeftButton:
|
2020-06-09 23:24:51 +00:00
|
|
|
self._drag = True
|
2020-06-16 19:14:28 +00:00
|
|
|
else:
|
|
|
|
self._drag = False
|
|
|
|
event.ignore()
|
|
|
|
return
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
self._reference = event.pos()
|
|
|
|
self._app.setOverrideCursor(Qt.ClosedHandCursor)
|
|
|
|
self.setMouseTracking(True)
|
2020-06-16 19:14:28 +00:00
|
|
|
event.accept()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
def mouseMoveEvent(self, event):
|
|
|
|
if self.bestFit:
|
|
|
|
event.ignore()
|
|
|
|
return
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
self._delta += (event.pos() - self._reference) * 1.0 / self._current_scale
|
2020-06-09 23:24:51 +00:00
|
|
|
self._reference = event.pos()
|
|
|
|
if self._drag:
|
|
|
|
self.mouseDragged.emit(self._delta)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.update()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
def mouseReleaseEvent(self, event):
|
|
|
|
if self.bestFit:
|
|
|
|
event.ignore()
|
|
|
|
return
|
2020-06-16 19:14:28 +00:00
|
|
|
if event.button() == Qt.LeftButton:
|
|
|
|
self._drag = False
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
self._app.restoreOverrideCursor()
|
|
|
|
self.setMouseTracking(False)
|
|
|
|
|
|
|
|
def wheelEvent(self, event):
|
|
|
|
if self.bestFit:
|
|
|
|
event.ignore()
|
|
|
|
return
|
|
|
|
|
|
|
|
if event.angleDelta().y() > 0:
|
2020-06-16 19:14:28 +00:00
|
|
|
self.mouseWheeled.emit(True) # zoom-in
|
2020-06-09 23:24:51 +00:00
|
|
|
else:
|
2020-06-16 19:14:28 +00:00
|
|
|
self.mouseWheeled.emit(False) # zoom-out
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def setImage(self, pixmap):
|
2020-06-09 23:24:51 +00:00
|
|
|
if pixmap.isNull():
|
|
|
|
if not self._pixmap.isNull():
|
|
|
|
self._pixmap = pixmap
|
2020-06-10 16:23:48 +00:00
|
|
|
self.disconnect_signals()
|
2020-06-09 23:24:51 +00:00
|
|
|
self.update()
|
|
|
|
return
|
|
|
|
elif not self.isEnabled():
|
|
|
|
self.setEnabled(True)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.connect_signals()
|
2020-06-09 23:24:51 +00:00
|
|
|
self._pixmap = pixmap
|
2020-06-10 20:02:08 +00:00
|
|
|
|
|
|
|
def center_and_update(self):
|
2020-06-09 23:24:51 +00:00
|
|
|
self._rect = self._pixmap.rect()
|
|
|
|
self._rect.translate(-self._rect.center())
|
|
|
|
self.update()
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def shouldBeActive(self):
|
2020-06-10 16:23:48 +00:00
|
|
|
return True if not self.pixmap.isNull() else False
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def connect_signals(self):
|
|
|
|
if not self._dragConnection:
|
|
|
|
self._dragConnection = self.mouseDragged.connect(
|
|
|
|
self.controller.onDraggedMouse)
|
|
|
|
if not self._wheelConnection:
|
|
|
|
self._wheelConnection = self.mouseWheeled.connect(
|
|
|
|
self.controller.scaleImages)
|
|
|
|
|
2020-06-10 16:23:48 +00:00
|
|
|
def disconnect_signals(self):
|
|
|
|
if self._dragConnection:
|
|
|
|
self.mouseDragged.disconnect()
|
|
|
|
self._dragConnection = None
|
|
|
|
if self._wheelConnection:
|
|
|
|
self.mouseWheeled.disconnect()
|
|
|
|
self._wheelConnection = None
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def zoom_in(self):
|
|
|
|
self._current_scale *= 1.25
|
|
|
|
self.update()
|
2020-06-10 16:23:48 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def zoom_out(self):
|
|
|
|
self._current_scale *= 0.8
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def scaleBy(self, factor):
|
|
|
|
self._current_scale = factor
|
2020-06-09 23:24:51 +00:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def sizeHint(self):
|
|
|
|
return QSize(400, 400)
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def pixmapReset(self):
|
|
|
|
"""Called when the pixmap is set back to original size."""
|
2020-06-16 19:14:28 +00:00
|
|
|
self._current_scale = 1.0
|
2020-06-09 23:24:51 +00:00
|
|
|
self.update()
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
@pyqtSlot(QPointF)
|
2020-06-16 19:14:28 +00:00
|
|
|
def onDraggedMouse(self, delta):
|
2020-06-09 23:24:51 +00:00
|
|
|
self._delta = delta
|
|
|
|
self.update()
|
2020-06-16 19:14:28 +00:00
|
|
|
print(f"{self} received drag signal from {self.sender()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QLabelNoAA(QLabel):
|
|
|
|
def __init__(self, parent):
|
|
|
|
super().__init__(parent)
|
|
|
|
self._pixmap = QPixmap()
|
|
|
|
self._current_scale = 1.0
|
|
|
|
self._scaleFactor = 1.3
|
|
|
|
self._delta = QPointF()
|
|
|
|
self._rect = QRectF()
|
|
|
|
|
|
|
|
def paintEvent(self, event):
|
|
|
|
painter = QPainter(self)
|
|
|
|
painter.translate(self.rect().center())
|
|
|
|
# painter.setRenderHint(QPainter.Antialiasing, False)
|
|
|
|
# scale the coordinate system:
|
|
|
|
painter.scale(self._current_scale, self._current_scale)
|
|
|
|
painter.translate(self._delta)
|
|
|
|
painter.drawPixmap(self.rect().topLeft(), self._pixmap)
|
|
|
|
print(f"LabelnoAA paintEvent scale {self._current_scale}")
|
|
|
|
|
|
|
|
def setPixmap(self, pixmap):
|
|
|
|
self._pixmap = pixmap
|
|
|
|
# self.center_and_update()
|
|
|
|
super().setPixmap(pixmap)
|
|
|
|
|
|
|
|
# def center_and_update(self):
|
|
|
|
# self._rect = self.rect()
|
|
|
|
# self._rect.translate(-self._rect.center())
|
|
|
|
# self._update(self._current_scale)
|
|
|
|
|
|
|
|
def sizeHint(self):
|
|
|
|
return self._pixmap.size() * self._current_scale
|
|
|
|
|
|
|
|
|
|
|
|
class ScalableWidget(QWidget):
|
|
|
|
def __init__(self, parent):
|
|
|
|
super().__init__()
|
|
|
|
self._pixmap = QPixmap()
|
|
|
|
self._current_scale = 1.0
|
|
|
|
self._scaleFactor = 1.3
|
|
|
|
self._delta = QPointF()
|
|
|
|
self._rect = QRectF()
|
|
|
|
|
|
|
|
def paintEvent(self, event):
|
|
|
|
painter = QPainter(self)
|
|
|
|
# painter.translate(self.rect().center())
|
|
|
|
# painter.setRenderHint(QPainter.Antialiasing, False)
|
|
|
|
# scale the coordinate system:
|
|
|
|
painter.scale(self._current_scale, self._current_scale)
|
|
|
|
painter.translate(self._delta)
|
|
|
|
painter.drawPixmap(self.rect().topLeft(), self._pixmap)
|
|
|
|
print(f"ScalableWidget paintEvent scale {self._current_scale}")
|
|
|
|
|
|
|
|
def setPixmap(self, pixmap):
|
|
|
|
self._pixmap = pixmap
|
|
|
|
# self.center_and_update()
|
|
|
|
# super().setPixmap(pixmap)
|
|
|
|
|
|
|
|
# def center_and_update(self):
|
|
|
|
# self._rect = self.rect()
|
|
|
|
# self._rect.translate(-self._rect.center())
|
|
|
|
# self._update(self._current_scale)
|
|
|
|
|
|
|
|
def sizeHint(self):
|
|
|
|
# if self._current_scale <= 1.0:
|
|
|
|
# return self._pixmap.size()
|
|
|
|
return self._pixmap.size() * self._current_scale
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ScrollAreaImageViewer(QScrollArea):
|
|
|
|
"""Version with Qlabel for testing"""
|
|
|
|
mouseDragged = pyqtSignal(QPointF)
|
2020-06-16 19:14:28 +00:00
|
|
|
mouseWheeled = pyqtSignal(bool)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
def __init__(self, parent, name=""):
|
|
|
|
super().__init__(parent)
|
|
|
|
self._parent = parent
|
|
|
|
self._app = QApplication
|
|
|
|
self._pixmap = QPixmap()
|
2020-06-16 19:14:28 +00:00
|
|
|
self._scaledpixmap = None
|
2020-06-09 23:24:51 +00:00
|
|
|
self._rect = QRectF()
|
|
|
|
self._reference = QPointF()
|
|
|
|
self._delta = QPointF()
|
2020-06-16 19:14:28 +00:00
|
|
|
self._scaleFactor = 1.3
|
|
|
|
self._current_scale = 1.0
|
2020-06-09 23:24:51 +00:00
|
|
|
self._drag = False
|
2020-06-16 19:14:28 +00:00
|
|
|
self._dragConnection = None
|
|
|
|
self._wheelConnection = None
|
2020-06-09 23:24:51 +00:00
|
|
|
self._instance_name = name
|
2020-06-16 19:14:28 +00:00
|
|
|
self.wantScrollBars = True
|
|
|
|
self.bestFit = True
|
2020-06-10 16:23:48 +00:00
|
|
|
self.controller = None
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
self.label = ScalableWidget(self)
|
|
|
|
|
|
|
|
if isinstance(self.label, QLabelNoAA):
|
|
|
|
sizePolicy = QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
|
|
|
|
sizePolicy.setHorizontalStretch(0)
|
|
|
|
sizePolicy.setVerticalStretch(0)
|
|
|
|
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
|
|
|
|
self.label.setBackgroundRole(QPalette.Base)
|
|
|
|
self.label.setSizePolicy(sizePolicy)
|
|
|
|
# self.label.setAlignment(Qt.AlignCenter) # useless?
|
|
|
|
self.label.setScaledContents(True) # Available in QLabel only, not used
|
|
|
|
# self.label.adjustSize()
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
self.setBackgroundRole(QPalette.Dark)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.setWidgetResizable(False)
|
2020-06-09 23:24:51 +00:00
|
|
|
self.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)
|
2020-06-16 19:14:28 +00:00
|
|
|
# self.viewport().setAttribute(Qt.WA_StaticContents)
|
|
|
|
self.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
|
|
|
|
|
|
|
|
if self.wantScrollBars:
|
|
|
|
self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
|
|
|
|
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
|
|
|
|
else:
|
|
|
|
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
|
|
|
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
self.setWidget(self.label)
|
|
|
|
self.setVisible(True)
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
if self.wantScrollBars:
|
|
|
|
self._verticalScrollBar = self.verticalScrollBar()
|
|
|
|
self._horizontalScrollBar = self.horizontalScrollBar()
|
|
|
|
|
|
|
|
self._verticalScrollBar.rangeChanged.connect(
|
|
|
|
self.printvalue)
|
|
|
|
self._horizontalScrollBar.rangeChanged.connect(
|
|
|
|
self.printvalue)
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def printvalue(self):
|
|
|
|
print(f"verticalscrollbar.maximum: {self._verticalScrollBar.maximum()}")
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return f'{self._instance_name}'
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def getPixmap(self):
|
|
|
|
return self._pixmap
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def connect_signals(self):
|
|
|
|
if not self._dragConnection:
|
|
|
|
self._dragConnection = self.mouseDragged.connect(
|
|
|
|
self.controller.onDraggedMouse)
|
|
|
|
if not self._wheelConnection:
|
|
|
|
self._wheelConnection = self.mouseWheeled.connect(
|
|
|
|
self.controller.scaleImages)
|
|
|
|
|
|
|
|
def disconnect_signals(self):
|
|
|
|
if self._dragConnection:
|
|
|
|
self.mouseDragged.disconnect()
|
|
|
|
self._dragConnection = None
|
|
|
|
if self._wheelConnection:
|
|
|
|
self.mouseWheeled.disconnect()
|
|
|
|
self._wheelConnection = None
|
2020-06-05 20:39:16 +00:00
|
|
|
|
|
|
|
def changeEvent(self, event):
|
|
|
|
if event.type() == QEvent.EnabledChange:
|
|
|
|
print(f"{self} is now {'enabled' if self.isEnabled() else 'disabled'}")
|
|
|
|
|
|
|
|
def mousePressEvent(self, event):
|
2020-06-16 19:14:28 +00:00
|
|
|
if self.bestFit:
|
2020-06-05 20:39:16 +00:00
|
|
|
event.ignore()
|
|
|
|
return
|
2020-06-16 19:14:28 +00:00
|
|
|
if event.button() == Qt.LeftButton:
|
2020-06-09 23:24:51 +00:00
|
|
|
self._drag = True
|
2020-06-16 19:14:28 +00:00
|
|
|
else:
|
|
|
|
self._drag = False
|
|
|
|
event.ignore()
|
|
|
|
return
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
self._reference = event.pos()
|
|
|
|
self._app.setOverrideCursor(Qt.ClosedHandCursor)
|
2020-06-05 20:39:16 +00:00
|
|
|
self.setMouseTracking(True)
|
2020-06-16 19:14:28 +00:00
|
|
|
event.accept()
|
2020-06-05 20:39:16 +00:00
|
|
|
|
|
|
|
def mouseMoveEvent(self, event):
|
2020-06-16 19:14:28 +00:00
|
|
|
if self.bestFit:
|
2020-06-05 20:39:16 +00:00
|
|
|
event.ignore()
|
|
|
|
return
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
self._delta += (event.pos() - self._reference) * 1.0/self._current_scale
|
2020-06-09 23:24:51 +00:00
|
|
|
self._reference = event.pos()
|
|
|
|
if self._drag:
|
|
|
|
self.mouseDragged.emit(self._delta)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.label._delta = self._delta
|
|
|
|
self.label.update()
|
2020-06-05 20:39:16 +00:00
|
|
|
|
|
|
|
def mouseReleaseEvent(self, event):
|
2020-06-16 19:14:28 +00:00
|
|
|
if self.bestFit:
|
2020-06-05 20:39:16 +00:00
|
|
|
event.ignore()
|
|
|
|
return
|
2020-06-16 19:14:28 +00:00
|
|
|
if event.button() == Qt.LeftButton:
|
|
|
|
self._drag = False
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
self._app.restoreOverrideCursor()
|
2020-06-05 20:39:16 +00:00
|
|
|
self.setMouseTracking(False)
|
|
|
|
|
|
|
|
def wheelEvent(self, event):
|
2020-06-16 19:14:28 +00:00
|
|
|
if self.bestFit:
|
2020-06-05 20:39:16 +00:00
|
|
|
event.ignore()
|
|
|
|
return
|
|
|
|
|
|
|
|
if event.angleDelta().y() > 0:
|
2020-06-16 19:14:28 +00:00
|
|
|
self.mouseWheeled.emit(True) # zoom-in
|
2020-06-05 20:39:16 +00:00
|
|
|
else:
|
2020-06-16 19:14:28 +00:00
|
|
|
self.mouseWheeled.emit(False) # zoom-out
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def setImage(self, pixmap, cache=True):
|
|
|
|
if pixmap.isNull():
|
|
|
|
if not self._pixmap.isNull():
|
|
|
|
self._pixmap = pixmap
|
|
|
|
self.update()
|
|
|
|
return
|
|
|
|
elif not self.isEnabled():
|
|
|
|
self.setEnabled(True)
|
|
|
|
self.connect_signals()
|
|
|
|
|
|
|
|
self._pixmap = pixmap
|
2020-06-09 23:24:51 +00:00
|
|
|
self.label.setPixmap(pixmap)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.label.adjustSize()
|
|
|
|
|
|
|
|
def center_and_update(self):
|
|
|
|
self._rect = self.rect()
|
2020-06-09 23:24:51 +00:00
|
|
|
self._rect.translate(-self._rect.center())
|
2020-06-16 19:14:28 +00:00
|
|
|
self.label._current_scale = self._current_scale
|
|
|
|
self.label.update()
|
|
|
|
# self.viewport().update()
|
|
|
|
|
|
|
|
def setCachedPixmap(self):
|
|
|
|
"""In case we have changed the cached pixmap, reset it."""
|
|
|
|
self.label.setPixmap(self._pixmap)
|
|
|
|
self.label.update()
|
|
|
|
|
|
|
|
def shouldBeActive(self):
|
|
|
|
return True if not self.pixmap.isNull() else False
|
|
|
|
|
|
|
|
def zoom_in(self):
|
|
|
|
self._current_scale *= 1.25
|
|
|
|
self.scaleBy(self._current_scale)
|
|
|
|
|
|
|
|
def zoom_out(self):
|
|
|
|
self._current_scale *= 0.8
|
|
|
|
self.scaleBy(self._current_scale)
|
|
|
|
|
|
|
|
def scaleBy(self, factor):
|
|
|
|
print(f"{self} current_scale={self._current_scale}")
|
|
|
|
# This kills my computer when scaling up! DO NOT USE!
|
|
|
|
# self._pixmap = self._pixmap.scaled(
|
|
|
|
# self._pixmap.size().__mul__(factor),
|
|
|
|
# Qt.KeepAspectRatio, Qt.FastTransformation)
|
|
|
|
|
|
|
|
# self.label.setPixmap(self._pixmap)
|
|
|
|
|
|
|
|
# This does nothing:
|
|
|
|
# newsize = self._pixmap.size().__imul__(factor)
|
|
|
|
# self.label.resize(newsize)
|
|
|
|
if self._current_scale < 1.0:
|
|
|
|
self.label.resize(self._pixmap.size())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# we might need a QRect here to update?
|
|
|
|
self.label._current_scale = factor
|
|
|
|
self.label.update()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.label.adjustSize() # needed to center view on zoom change
|
|
|
|
|
|
|
|
if self.wantScrollBars:
|
|
|
|
self.adjustScrollBar(self.horizontalScrollBar(), factor)
|
|
|
|
self.adjustScrollBar(self.verticalScrollBar(), factor)
|
2020-06-05 20:39:16 +00:00
|
|
|
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
def adjustScrollBar(self, scrollBar, factor):
|
2020-06-16 19:14:28 +00:00
|
|
|
# scrollBar.setMaximum(
|
|
|
|
# scrollBar.maximum() - scrollBar.minimum() + scrollBar.pageStep())
|
|
|
|
# scrollBar.setValue(int(
|
|
|
|
# factor * scrollBar.value() +
|
|
|
|
# ((factor - 1) * scrollBar.pageStep()/2)))
|
|
|
|
scrollBar.setValue(int(scrollBar.maximum() / 2))
|
|
|
|
|
|
|
|
# self.viewport().update()
|
|
|
|
|
|
|
|
def resetCenter(self):
|
|
|
|
""" Resets origin """
|
|
|
|
self._delta = QPointF()
|
|
|
|
self.label._delta = self._delta
|
|
|
|
self._current_scale = 1.0
|
|
|
|
self.scaleBy(1.0)
|
|
|
|
# self.label.update() # already called in scaleBy
|
|
|
|
|
|
|
|
def setCenter(self, point):
|
|
|
|
self._reference = point
|
|
|
|
|
|
|
|
def getCenter(self):
|
|
|
|
return self._reference
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-05 20:39:16 +00:00
|
|
|
def sizeHint(self):
|
2020-06-16 19:14:28 +00:00
|
|
|
return self._pixmap.rect().size()
|
|
|
|
|
|
|
|
def viewportSizeHint(self):
|
|
|
|
return self._pixmap.rect().size()
|
2020-06-05 20:39:16 +00:00
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def pixmapReset(self):
|
|
|
|
"""Called when the pixmap is set back to original size."""
|
2020-06-16 19:14:28 +00:00
|
|
|
self._current_scale = 1.0
|
|
|
|
self.scaleBy(1.0)
|
|
|
|
# self.ensureWidgetVisible(self.label) # might not need
|
|
|
|
self.label.update()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
@pyqtSlot(QPointF)
|
2020-06-16 19:14:28 +00:00
|
|
|
def onDraggedMouse(self, delta):
|
|
|
|
# This updates position from mouse delta from other panel
|
2020-06-09 23:24:51 +00:00
|
|
|
self._delta = delta
|
2020-06-16 19:14:28 +00:00
|
|
|
self.label._delta = delta
|
|
|
|
self.label.update()
|
|
|
|
print(f"{self} received mouse drag signal from {self.sender()}")
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsPixmapItem
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
class GraphicsViewViewer(QGraphicsView):
|
|
|
|
"""Re-Implementation."""
|
|
|
|
mouseDragged = pyqtSignal(QPointF)
|
|
|
|
mouseWheeled = pyqtSignal(bool)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def __init__(self, parent, name=""):
|
2020-06-09 23:24:51 +00:00
|
|
|
super().__init__(parent)
|
2020-06-16 19:14:28 +00:00
|
|
|
self._parent = parent
|
|
|
|
self._app = QApplication
|
|
|
|
self._pixmap = QPixmap()
|
|
|
|
self._scaledpixmap = None
|
|
|
|
self._rect = QRectF()
|
|
|
|
self._reference = QPointF()
|
|
|
|
self._delta = QPointF()
|
|
|
|
self._scaleFactor = 1.3
|
|
|
|
self._current_scale = 1.0
|
|
|
|
self._drag = False
|
|
|
|
self._dragConnection = None
|
|
|
|
self._wheelConnection = None
|
|
|
|
self._instance_name = name
|
|
|
|
self.wantScrollBars = True
|
|
|
|
self.bestFit = True
|
|
|
|
self.controller = None
|
|
|
|
self._centerPoint = QPointF()
|
|
|
|
|
|
|
|
# specific to this class
|
2020-06-09 23:24:51 +00:00
|
|
|
self._scene = QGraphicsScene()
|
|
|
|
self._item = QGraphicsPixmapItem()
|
2020-06-16 19:14:28 +00:00
|
|
|
self.setScene(self._scene)
|
|
|
|
self._scene.addItem(self._item)
|
2020-06-09 23:24:51 +00:00
|
|
|
self.setDragMode(QGraphicsView.DragMode.ScrollHandDrag)
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
if self.wantScrollBars:
|
|
|
|
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
|
|
|
|
self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
|
|
|
|
else:
|
|
|
|
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
|
|
|
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
self.setResizeAnchor(QGraphicsView.AnchorViewCenter)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
|
|
|
|
# self.setViewportUpdateMode (QGraphicsView.FullViewportUpdate)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def connect_signals(self):
|
|
|
|
if not self._dragConnection:
|
|
|
|
self._dragConnection = self.mouseDragged.connect(
|
|
|
|
self.controller.onDraggedMouse)
|
|
|
|
if not self._wheelConnection:
|
|
|
|
self._wheelConnection = self.mouseWheeled.connect(
|
|
|
|
self.controller.scaleImages)
|
|
|
|
|
|
|
|
def disconnect_signals(self):
|
|
|
|
if self._dragConnection:
|
|
|
|
self.mouseDragged.disconnect()
|
|
|
|
self._dragConnection = None
|
|
|
|
if self._wheelConnection:
|
|
|
|
self.mouseWheeled.disconnect()
|
|
|
|
self._wheelConnection = None
|
|
|
|
|
|
|
|
def mousePressEvent(self, event):
|
|
|
|
if self.bestFit:
|
|
|
|
event.ignore()
|
|
|
|
return
|
|
|
|
if event.button() == Qt.LeftButton:
|
|
|
|
self._drag = True
|
|
|
|
else:
|
|
|
|
self._drag = False
|
|
|
|
event.ignore()
|
|
|
|
return
|
|
|
|
|
|
|
|
self._reference = event.pos()
|
|
|
|
self._app.setOverrideCursor(Qt.ClosedHandCursor)
|
|
|
|
self.setMouseTracking(True)
|
|
|
|
event.accept()
|
|
|
|
|
|
|
|
def mouseMoveEvent(self, event):
|
|
|
|
if self.bestFit:
|
|
|
|
event.ignore()
|
|
|
|
return
|
|
|
|
|
|
|
|
self._delta += (event.pos() - self._reference) * 1.0/self._current_scale
|
|
|
|
self._reference = event.pos()
|
|
|
|
if self._drag:
|
|
|
|
self.mouseDragged.emit(self._delta)
|
|
|
|
self.label.update()
|
|
|
|
super().mouseMoveEvent(event)
|
|
|
|
|
|
|
|
def mouseReleaseEvent(self, event):
|
|
|
|
if self.bestFit:
|
|
|
|
event.ignore()
|
|
|
|
return
|
|
|
|
if event.button() == Qt.LeftButton:
|
|
|
|
self._drag = False
|
|
|
|
|
|
|
|
self._app.restoreOverrideCursor()
|
|
|
|
self.setMouseTracking(False)
|
|
|
|
super().mouseReleaseEvent(event)
|
|
|
|
|
|
|
|
def wheelEvent(self, event):
|
|
|
|
if self.bestFit:
|
|
|
|
event.ignore()
|
|
|
|
return
|
|
|
|
|
|
|
|
if event.angleDelta().y() > 0:
|
|
|
|
self.mouseWheeled.emit(True) # zoom-in
|
|
|
|
else:
|
|
|
|
self.mouseWheeled.emit(False) # zoom-out
|
|
|
|
|
|
|
|
|
|
|
|
def setImage(self, pixmap):
|
|
|
|
self._pixmap = pixmap
|
2020-06-09 23:24:51 +00:00
|
|
|
self._item.setPixmap(pixmap)
|
2020-06-16 19:14:28 +00:00
|
|
|
# offset = -QRectF(pixmap.rect()).center()
|
|
|
|
# self._item.setOffset(offset)
|
|
|
|
# self.setSceneRect(offset.x()*4, offset.y()*4, -offset.x()*8, -offset.y()*8)
|
2020-06-09 23:24:51 +00:00
|
|
|
self.translate(1, 1)
|
2020-06-16 19:14:28 +00:00
|
|
|
self._scene.setSceneRect(self._pixmap.rect())
|
|
|
|
|
|
|
|
def scaleBy(self, factor):
|
|
|
|
# super().scale(factor, factor)
|
|
|
|
self.zoom(factor)
|
|
|
|
|
|
|
|
def resetCenter(self):
|
|
|
|
# """ Resets origin """
|
|
|
|
# self._delta = QPointF()
|
|
|
|
# self._scaleFactor = 1.0
|
|
|
|
# self.scale(self._scaleFactor)
|
|
|
|
# self.update()
|
|
|
|
pass
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def setNewCenter(self, position):
|
|
|
|
self._centerPoint = position
|
|
|
|
self.centerOn(self._centerPoint)
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def pixmapReset(self):
|
|
|
|
"""Called when the pixmap is set back to original size."""
|
|
|
|
self.scaleBy(1.0)
|
|
|
|
|
|
|
|
super().fitInView(self._scene.sceneRect(), Qt.KeepAspectRatio )
|
|
|
|
self.setNewCenter(self._scene.sceneRect().center())
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
@pyqtSlot(QPointF)
|
|
|
|
def onDraggedMouse(self, delta):
|
|
|
|
self._delta = delta
|
|
|
|
# self._item.move()
|
|
|
|
print(f"{self} received mouse drag signal from {self.sender()}")
|
|
|
|
|
|
|
|
def sizeHint(self):
|
|
|
|
return self._item.rect().size()
|
|
|
|
|
|
|
|
def viewportSizeHint(self):
|
|
|
|
return self._item.rect().size()
|
|
|
|
|
|
|
|
def zoom_in(self):
|
|
|
|
self.zoom(self._scaleFactor)
|
|
|
|
|
|
|
|
def zoom_out(self):
|
|
|
|
self.zoom(1.0 / self._scaleFactor)
|
|
|
|
|
|
|
|
def zoom(self, factor):
|
|
|
|
#Get the position of the mouse before scaling, in scene coords
|
|
|
|
pointBeforeScale = QPointF(self.mapToScene(self.mapFromGlobal(QCursor.pos())))
|
|
|
|
|
|
|
|
#Get the original screen centerpoint
|
|
|
|
screenCenter = self.mapToScene( self.rect().center() )
|
|
|
|
|
|
|
|
super().scale(factor, factor)
|
|
|
|
|
|
|
|
#Get the position after scaling, in scene coords
|
|
|
|
pointAfterScale = QPointF( self.mapToScene( self.mapFromGlobal(QCursor.pos()) ) )
|
|
|
|
|
|
|
|
#Get the offset of how the screen moved
|
|
|
|
offset = QPointF( pointBeforeScale - pointAfterScale)
|
|
|
|
|
|
|
|
#Adjust to the new center for correct zooming
|
|
|
|
newCenter = QPointF(screenCenter + offset)
|
|
|
|
self.setNewCenter(newCenter)
|
|
|
|
|
|
|
|
# self.updateSceneRect(self._item.rect()) # TEST THIS?
|
|
|
|
|
|
|
|
# mouse position has changed!!
|
|
|
|
# emit mouseMoved( QGraphicsView::mapToScene( event->pos() ) );
|
|
|
|
# emit mouseMoved( QGraphicsView::mapToScene( mapFromGlobal(QCursor::pos()) ) );
|
|
|
|
# emit somethingChanged();
|
2020-06-09 23:24:51 +00:00
|
|
|
|