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-23 03:03:56 +00:00
|
|
|
MAX_SCALE = 12.0
|
|
|
|
MIN_SCALE = 0.1
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
class BaseController(QObject):
|
2020-06-20 23:49:17 +00:00
|
|
|
"""Abstract Base class. Singleton.
|
|
|
|
Base proxy interface to keep image viewers synchronized.
|
|
|
|
Relays function calls, keep tracks of things."""
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
def __init__(self, selectedViewer, referenceViewer, parent):
|
|
|
|
super().__init__()
|
|
|
|
self.selectedViewer = selectedViewer
|
|
|
|
self.referenceViewer = referenceViewer
|
2020-06-20 23:49:17 +00:00
|
|
|
self.selectedViewer.controller = self
|
|
|
|
self.referenceViewer.controller = self
|
|
|
|
self._setupConnections()
|
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-16 19:14:28 +00:00
|
|
|
self.current_scale = 1.0
|
2020-06-09 23:24:51 +00:00
|
|
|
self.bestFit = True
|
2020-06-16 19:14:28 +00:00
|
|
|
self.wantScrollBars = True
|
2020-06-20 23:49:17 +00:00
|
|
|
self.parent = parent #To change buttons' states
|
2020-06-23 03:03:56 +00:00
|
|
|
self.cached_group = None
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def _setupConnections(self):
|
2020-06-20 23:49:17 +00:00
|
|
|
self.selectedViewer.connectMouseSignals()
|
|
|
|
self.referenceViewer.connectMouseSignals()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-23 03:03:56 +00:00
|
|
|
def updateView(self, ref, dupe, group):
|
|
|
|
# Keep current scale accross dupes from the same group
|
|
|
|
same_group = True
|
|
|
|
if group != self.cached_group:
|
|
|
|
same_group = False
|
|
|
|
self.resetState()
|
|
|
|
|
|
|
|
self.cached_group = group
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
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)
|
|
|
|
|
2020-06-23 03:03:56 +00:00
|
|
|
self.updateBothImages(same_group)
|
|
|
|
|
|
|
|
def updateBothImages(self, same_group=False):
|
|
|
|
selected_size = self._updateImage(
|
|
|
|
self.selectedPixmap, self.scaledSelectedPixmap, self.selectedViewer, None, same_group)
|
|
|
|
# the SelectedImageViewer widget sometimes ends up being bigger
|
|
|
|
# than the ReferenceImageViewer by one pixel, which distorts the
|
|
|
|
# scaled down pixmap for the reference, hence we'll reuse its size here.
|
|
|
|
self._updateImage(
|
|
|
|
self.referencePixmap, self.scaledReferencePixmap, self.referenceViewer, selected_size, same_group)
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-23 03:03:56 +00:00
|
|
|
def _updateImage(self, pixmap, scaledpixmap, viewer, target_size=None, same_group=False):
|
|
|
|
# If not same_group, we need full update"""
|
|
|
|
if not pixmap.isNull():
|
|
|
|
target_size = viewer.size()
|
2020-06-09 23:24:51 +00:00
|
|
|
if not self.bestFit:
|
2020-06-23 03:03:56 +00:00
|
|
|
if same_group:
|
|
|
|
viewer.setImage(pixmap)
|
|
|
|
viewer.centerViewAndUpdate()
|
|
|
|
return target_size
|
2020-06-09 23:24:51 +00:00
|
|
|
# zoomed in state, expand
|
2020-06-23 03:03:56 +00:00
|
|
|
scaledpixmap = pixmap.scaled(
|
2020-06-09 23:24:51 +00:00
|
|
|
target_size, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)
|
|
|
|
else:
|
|
|
|
# best fit, keep ratio always
|
2020-06-23 03:03:56 +00:00
|
|
|
scaledpixmap = pixmap.scaled(
|
2020-06-09 23:24:51 +00:00
|
|
|
target_size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
|
2020-06-23 03:03:56 +00:00
|
|
|
viewer.setImage(scaledpixmap)
|
|
|
|
viewer.centerViewAndUpdate()
|
|
|
|
return target_size
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
@pyqtSlot(float)
|
|
|
|
def scaleImagesBy(self, factor):
|
|
|
|
"""Compute new scale from factor and scale."""
|
|
|
|
self.current_scale *= factor
|
|
|
|
self.selectedViewer.scaleBy(factor)
|
|
|
|
self.referenceViewer.scaleBy(factor)
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-23 03:03:56 +00:00
|
|
|
self.parent.buttonZoomIn.setEnabled(self.current_scale < MAX_SCALE)
|
|
|
|
self.parent.buttonZoomOut.setEnabled(self.current_scale > MIN_SCALE)
|
2020-06-20 23:49:17 +00:00
|
|
|
self.parent.buttonBestFit.setEnabled(self.bestFit is False)
|
|
|
|
self.parent.buttonNormalSize.setEnabled(self.current_scale != 1.0)
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
@pyqtSlot(float)
|
|
|
|
def scaleImagesAt(self, scale):
|
|
|
|
"""Scale at a pre-computed scale."""
|
|
|
|
self.current_scale = scale
|
|
|
|
self.selectedViewer.scaleAt(scale)
|
|
|
|
self.referenceViewer.scaleAt(scale)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-23 03:03:56 +00:00
|
|
|
self.parent.buttonZoomIn.setEnabled(self.current_scale < MAX_SCALE)
|
|
|
|
self.parent.buttonZoomOut.setEnabled(self.current_scale > MIN_SCALE)
|
2020-06-16 19:14:28 +00:00
|
|
|
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):
|
2020-06-23 03:03:56 +00:00
|
|
|
"""Only called when the group of dupes has changed"""
|
2020-06-09 23:24:51 +00:00
|
|
|
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
|
|
|
|
2020-06-23 03:03:56 +00:00
|
|
|
self.selectedViewer.centerViewAndUpdate()
|
|
|
|
self.referenceViewer.centerViewAndUpdate()
|
|
|
|
|
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-23 03:03:56 +00:00
|
|
|
self.selectedViewer.setEnabled(False)
|
2020-06-16 19:14:28 +00:00
|
|
|
self.referenceViewer.setImage(self.referencePixmap) # null
|
2020-06-23 03:03:56 +00:00
|
|
|
self.referenceViewer.setEnabled(False)
|
|
|
|
self.parent.buttonImgSwap.setEnabled(False)
|
|
|
|
self.parent.buttonNormalSize.setEnabled(False)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-10 20:02:08 +00:00
|
|
|
@pyqtSlot()
|
2020-06-20 23:49:17 +00:00
|
|
|
def ScaleToBestFit(self):
|
2020-06-16 19:14:28 +00:00
|
|
|
"""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
|
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
self.selectedViewer.scaleAt(1.0)
|
|
|
|
self.referenceViewer.scaleAt(1.0)
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
self.selectedViewer.resetCenter()
|
|
|
|
self.referenceViewer.resetCenter()
|
2020-06-23 03:03:56 +00:00
|
|
|
|
|
|
|
target_size = self._updateImage(self.selectedPixmap, self.scaledSelectedPixmap, self.selectedViewer, None, True)
|
|
|
|
self._updateImage(self.referencePixmap, self.scaledReferencePixmap, self.referenceViewer, target_size, True)
|
2020-06-16 19:14:28 +00:00
|
|
|
|
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-20 23:49:17 +00:00
|
|
|
self.selectedViewer.centerViewAndUpdate()
|
|
|
|
self.referenceViewer.centerViewAndUpdate()
|
2020-06-10 16:23:48 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
self.selectedViewer.scaleToNormalSize()
|
|
|
|
self.referenceViewer.scaleToNormalSize()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
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
|
|
|
class QWidgetController(BaseController):
|
2020-06-20 23:49:17 +00:00
|
|
|
"""Specialized version for QWidget-based viewers."""
|
2020-06-16 19:14:28 +00:00
|
|
|
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())
|
2020-06-20 23:49:17 +00:00
|
|
|
self.selectedViewer.centerViewAndUpdate()
|
|
|
|
self.referenceViewer.centerViewAndUpdate()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-23 03:03:56 +00:00
|
|
|
def _updateImage(self, pixmap, scaledpixmap, viewer, target_size, same_group):
|
|
|
|
#FIXME might not need this at all, REMOVE?
|
|
|
|
super()._updateImage(pixmap, scaledpixmap, viewer, target_size, same_group)
|
|
|
|
viewer.update()
|
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
class ScrollAreaController(BaseController):
|
2020-06-20 23:49:17 +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-20 23:49:17 +00:00
|
|
|
def _setupConnections(self):
|
|
|
|
super()._setupConnections()
|
|
|
|
self.selectedViewer.connectScrollBars()
|
|
|
|
self.referenceViewer.connectScrollBars()
|
|
|
|
|
|
|
|
@pyqtSlot(QPoint)
|
2020-06-16 19:14:28 +00:00
|
|
|
def onDraggedMouse(self, delta):
|
2020-06-20 23:49:17 +00:00
|
|
|
self.selectedViewer.ignore_signal = True
|
|
|
|
self.referenceViewer.ignore_signal = True
|
|
|
|
|
|
|
|
self.selectedViewer.onDraggedMouse(delta)
|
|
|
|
self.referenceViewer.onDraggedMouse(delta)
|
|
|
|
|
|
|
|
self.selectedViewer.ignore_signal = False
|
|
|
|
self.referenceViewer.ignore_signal = False
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def swapPixmaps(self):
|
|
|
|
self.referenceViewer._pixmap.swap(self.selectedViewer._pixmap)
|
|
|
|
self.referenceViewer.setCachedPixmap()
|
|
|
|
self.selectedViewer.setCachedPixmap()
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def syncCenters(self):
|
2020-06-20 23:49:17 +00:00
|
|
|
if self.sender() is self.referenceViewer:
|
|
|
|
self.selectedViewer.setCenter(self.referenceViewer.getCenter())
|
|
|
|
else:
|
|
|
|
self.referenceViewer.setCenter(self.selectedViewer.getCenter())
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
@pyqtSlot(float, QPointF)
|
|
|
|
def onMouseWheel(self, scale, delta):
|
|
|
|
self.scaleImagesAt(scale)
|
|
|
|
self.selectedViewer.adjustScrollBarsScaled(delta)
|
2020-06-23 03:03:56 +00:00
|
|
|
# Signal from scrollbars will automatically change the other:
|
2020-06-20 23:49:17 +00:00
|
|
|
# self.referenceViewer.adjustScrollBarsScaled(delta)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
@pyqtSlot(int)
|
|
|
|
def onVScrollBarChanged(self, value):
|
2020-06-23 03:03:56 +00:00
|
|
|
if self.sender() is self.referenceViewer._verticalScrollBar:
|
2020-06-21 18:51:06 +00:00
|
|
|
if not self.selectedViewer.ignore_signal:
|
|
|
|
self.selectedViewer._verticalScrollBar.setValue(value)
|
2020-06-20 23:49:17 +00:00
|
|
|
else:
|
2020-06-21 18:51:06 +00:00
|
|
|
if not self.referenceViewer.ignore_signal:
|
|
|
|
self.referenceViewer._verticalScrollBar.setValue(value)
|
2020-06-20 23:49:17 +00:00
|
|
|
|
|
|
|
@pyqtSlot(int)
|
|
|
|
def onHScrollBarChanged(self, value):
|
2020-06-23 03:03:56 +00:00
|
|
|
if self.sender() is self.referenceViewer._horizontalScrollBar:
|
|
|
|
if not self.selectedViewer.ignore_signal:
|
2020-06-20 23:49:17 +00:00
|
|
|
self.selectedViewer._horizontalScrollBar.setValue(value)
|
|
|
|
else:
|
|
|
|
if not self.referenceViewer.ignore_signal:
|
|
|
|
self.referenceViewer._horizontalScrollBar.setValue(value)
|
|
|
|
|
|
|
|
@pyqtSlot(float)
|
|
|
|
def scaleImagesBy(self, factor):
|
|
|
|
super().scaleImagesBy(factor)
|
|
|
|
# The other is automatically updated via sigals
|
|
|
|
self.selectedViewer.adjustScrollBarsFactor(factor)
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def ScaleToBestFit(self):
|
|
|
|
# Disable scrollbars to avoid GridLayout size rounding "error"
|
|
|
|
super().ScaleToBestFit()
|
|
|
|
print("toggling scrollbars")
|
|
|
|
self.selectedViewer.toggleScrollBars()
|
|
|
|
self.referenceViewer.toggleScrollBars()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-23 03:03:56 +00:00
|
|
|
|
2020-06-09 23:24:51 +00:00
|
|
|
class GraphicsViewController(BaseController):
|
2020-06-20 23:49:17 +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()
|
|
|
|
def syncCenters(self):
|
2020-06-20 23:49:17 +00:00
|
|
|
if self.sender() is self.referenceViewer:
|
|
|
|
self.selectedViewer.setCenter(self.referenceViewer.getCenter())
|
|
|
|
else:
|
|
|
|
self.referenceViewer.setCenter(self.selectedViewer.getCenter())
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class QWidgetImageViewer(QWidget):
|
2020-06-20 23:49:17 +00:00
|
|
|
"""Use a QPixmap, but no scrollbars."""
|
2020-06-21 18:51:06 +00:00
|
|
|
#FIXME: panning while zoomed-in is broken (due to delta not interpolated right?)
|
|
|
|
#TODO: keyboard shortcuts for navigation
|
2020-06-09 23:24:51 +00:00
|
|
|
mouseDragged = pyqtSignal(QPointF)
|
2020-06-20 23:49:17 +00:00
|
|
|
mouseWheeled = pyqtSignal(float)
|
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()
|
2020-06-20 23:49:17 +00:00
|
|
|
self._lastMouseClickPoint = QPointF()
|
|
|
|
self._mousePanningDelta = QPointF()
|
2020-06-16 19:14:28 +00:00
|
|
|
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-20 23:49:17 +00:00
|
|
|
def connectMouseSignals(self):
|
|
|
|
if not self._dragConnection:
|
|
|
|
self._dragConnection = self.mouseDragged.connect(
|
|
|
|
self.controller.onDraggedMouse)
|
|
|
|
if not self._wheelConnection:
|
|
|
|
self._wheelConnection = self.mouseWheeled.connect(
|
|
|
|
self.controller.scaleImagesBy)
|
|
|
|
|
|
|
|
def disconnectMouseSignals(self):
|
|
|
|
if self._dragConnection:
|
|
|
|
self.mouseDragged.disconnect()
|
|
|
|
self._dragConnection = None
|
|
|
|
if self._wheelConnection:
|
|
|
|
self.mouseWheeled.disconnect()
|
|
|
|
self._wheelConnection = None
|
|
|
|
|
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-20 23:49:17 +00:00
|
|
|
painter.translate(self._mousePanningDelta)
|
2020-06-09 23:24:51 +00:00
|
|
|
painter.drawPixmap(self._rect.topLeft(), self._pixmap)
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def resetCenter(self):
|
2020-06-09 23:24:51 +00:00
|
|
|
""" Resets origin """
|
2020-06-20 23:49:17 +00:00
|
|
|
# Make sure we are not still panning around
|
|
|
|
self._mousePanningDelta = QPointF()
|
2020-06-23 03:03:56 +00:00
|
|
|
self.scaleAt(1.0)
|
2020-06-09 23:24:51 +00:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def changeEvent(self, event):
|
|
|
|
if event.type() == QEvent.EnabledChange:
|
2020-06-20 23:49:17 +00:00
|
|
|
# print(f"{self} is now {'enabled' if self.isEnabled() else 'disabled'}")
|
2020-06-10 16:23:48 +00:00
|
|
|
if self.isEnabled():
|
2020-06-20 23:49:17 +00:00
|
|
|
self.connectMouseSignals()
|
2020-06-10 16:23:48 +00:00
|
|
|
return
|
2020-06-20 23:49:17 +00:00
|
|
|
self.disconnectMouseSignals()
|
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
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
self._lastMouseClickPoint = event.pos()
|
2020-06-09 23:24:51 +00:00
|
|
|
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-20 23:49:17 +00:00
|
|
|
self._mousePanningDelta += (event.pos() - self._lastMouseClickPoint) \
|
|
|
|
* 1.0 / self._current_scale
|
|
|
|
self._lastMouseClickPoint = event.pos()
|
2020-06-09 23:24:51 +00:00
|
|
|
if self._drag:
|
2020-06-20 23:49:17 +00:00
|
|
|
self.mouseDragged.emit(self._mousePanningDelta)
|
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-23 03:03:56 +00:00
|
|
|
if self._current_scale > MAX_SCALE:
|
|
|
|
return
|
2020-06-20 23:49:17 +00:00
|
|
|
self.mouseWheeled.emit(1.25) # zoom-in
|
2020-06-09 23:24:51 +00:00
|
|
|
else:
|
2020-06-23 03:03:56 +00:00
|
|
|
if self._current_scale < MIN_SCALE:
|
|
|
|
return
|
2020-06-20 23:49:17 +00:00
|
|
|
self.mouseWheeled.emit(0.8) # 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-20 23:49:17 +00:00
|
|
|
self.disconnectMouseSignals()
|
2020-06-09 23:24:51 +00:00
|
|
|
self.update()
|
|
|
|
return
|
|
|
|
elif not self.isEnabled():
|
|
|
|
self.setEnabled(True)
|
2020-06-20 23:49:17 +00:00
|
|
|
self.connectMouseSignals()
|
2020-06-09 23:24:51 +00:00
|
|
|
self._pixmap = pixmap
|
2020-06-10 20:02:08 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
def centerViewAndUpdate(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-20 23:49:17 +00:00
|
|
|
def scaleBy(self, factor):
|
|
|
|
self._current_scale *= factor
|
2020-06-16 19:14:28 +00:00
|
|
|
self.update()
|
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
def scaleAt(self, scale):
|
|
|
|
self._current_scale = scale
|
2020-06-09 23:24:51 +00:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def sizeHint(self):
|
|
|
|
return QSize(400, 400)
|
|
|
|
|
|
|
|
@pyqtSlot()
|
2020-06-20 23:49:17 +00:00
|
|
|
def scaleToNormalSize(self):
|
2020-06-09 23:24:51 +00:00
|
|
|
"""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-20 23:49:17 +00:00
|
|
|
self._mousePanningDelta = delta
|
2020-06-09 23:24:51 +00:00
|
|
|
self.update()
|
2020-06-21 18:51:06 +00:00
|
|
|
# print(f"{self} received drag signal from {self.sender()}")
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
class ScalablePixmap(QWidget):
|
|
|
|
"""Container for a pixmap that scales up very fast"""
|
2020-06-16 19:14:28 +00:00
|
|
|
def __init__(self, parent):
|
|
|
|
super().__init__(parent)
|
|
|
|
self._pixmap = QPixmap()
|
|
|
|
self._current_scale = 1.0
|
|
|
|
|
|
|
|
def paintEvent(self, event):
|
|
|
|
painter = QPainter(self)
|
2020-06-20 23:49:17 +00:00
|
|
|
# painter.translate(self.rect().center())
|
2020-06-16 19:14:28 +00:00
|
|
|
# painter.setRenderHint(QPainter.Antialiasing, False)
|
|
|
|
# scale the coordinate system:
|
|
|
|
painter.scale(self._current_scale, self._current_scale)
|
2020-06-20 23:49:17 +00:00
|
|
|
painter.drawPixmap(self.rect().topLeft(), self._pixmap) #same as (0,0, self.pixmap)
|
|
|
|
# print(f"ScalableWidget paintEvent scale {self._current_scale}")
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
def setPixmap(self, pixmap):
|
|
|
|
self._pixmap = pixmap
|
2020-06-20 23:49:17 +00:00
|
|
|
# self.update()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
def sizeHint(self):
|
|
|
|
return self._pixmap.size() * self._current_scale
|
2020-06-20 23:49:17 +00:00
|
|
|
# return self._pixmap.size()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
def minimumSizeHint(self):
|
|
|
|
return self.sizeHint()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
# def moveEvent(self, event):
|
|
|
|
# print(f"{self} moved by {event.pos()}")
|
2020-06-09 23:24:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ScrollAreaImageViewer(QScrollArea):
|
|
|
|
"""Version with Qlabel for testing"""
|
2020-06-20 23:49:17 +00:00
|
|
|
mouseDragged = pyqtSignal(QPoint)
|
|
|
|
mouseWheeled = pyqtSignal(float, QPointF)
|
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()
|
2020-06-20 23:49:17 +00:00
|
|
|
self._lastMouseClickPoint = QPointF()
|
|
|
|
self._mousePanningDelta = QPoint()
|
2020-06-16 19:14:28 +00:00
|
|
|
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-20 23:49:17 +00:00
|
|
|
self._vBarConnection = None
|
|
|
|
self._hBarConnection = 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-20 23:49:17 +00:00
|
|
|
self.label = ScalablePixmap(self)
|
|
|
|
# This is to avoid sending signals twice on scrollbar updates
|
|
|
|
self.ignore_signal = False
|
2020-06-16 19:14:28 +00:00
|
|
|
|
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)
|
2020-06-20 23:49:17 +00:00
|
|
|
self.setAlignment(Qt.AlignCenter)
|
|
|
|
|
|
|
|
self._verticalScrollBar = self.verticalScrollBar()
|
|
|
|
self._horizontalScrollBar = self.horizontalScrollBar()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
if self.wantScrollBars:
|
2020-06-20 23:49:17 +00:00
|
|
|
self.toggleScrollBars()
|
2020-06-16 19:14:28 +00:00
|
|
|
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-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-23 03:03:56 +00:00
|
|
|
def toggleScrollBars(self, forceOn=False):
|
2020-06-20 23:49:17 +00:00
|
|
|
if not self.wantScrollBars:
|
|
|
|
return
|
2020-06-23 03:03:56 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
# Ensure that it's off on the first run
|
|
|
|
if self.horizontalScrollBarPolicy() == Qt.ScrollBarAsNeeded:
|
2020-06-23 03:03:56 +00:00
|
|
|
if forceOn:
|
|
|
|
return
|
2020-06-20 23:49:17 +00:00
|
|
|
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
|
|
|
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
|
|
|
else:
|
|
|
|
self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
|
|
|
|
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
|
|
|
|
|
|
|
|
def connectMouseSignals(self):
|
2020-06-16 19:14:28 +00:00
|
|
|
if not self._dragConnection:
|
|
|
|
self._dragConnection = self.mouseDragged.connect(
|
2020-06-20 23:49:17 +00:00
|
|
|
self.controller.onDraggedMouse)
|
2020-06-16 19:14:28 +00:00
|
|
|
if not self._wheelConnection:
|
|
|
|
self._wheelConnection = self.mouseWheeled.connect(
|
2020-06-20 23:49:17 +00:00
|
|
|
self.controller.onMouseWheel)
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
def disconnectMouseSignals(self):
|
2020-06-16 19:14:28 +00:00
|
|
|
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
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
def connectScrollBars(self):
|
|
|
|
"""Only call once controller is connected."""
|
|
|
|
# Cyclic connections are handled by Qt
|
|
|
|
self._verticalScrollBar.valueChanged.connect(
|
|
|
|
self.controller.onVScrollBarChanged, Qt.UniqueConnection)
|
|
|
|
self._horizontalScrollBar.valueChanged.connect(
|
|
|
|
self.controller.onHScrollBarChanged, Qt.UniqueConnection)
|
2020-06-05 20:39:16 +00:00
|
|
|
|
|
|
|
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-20 23:49:17 +00:00
|
|
|
self._lastMouseClickPoint = event.pos()
|
2020-06-09 23:24:51 +00:00
|
|
|
self._app.setOverrideCursor(Qt.ClosedHandCursor)
|
2020-06-05 20:39:16 +00:00
|
|
|
self.setMouseTracking(True)
|
2020-06-20 23:49:17 +00:00
|
|
|
super().mousePressEvent(event)
|
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-09 23:24:51 +00:00
|
|
|
if self._drag:
|
2020-06-20 23:49:17 +00:00
|
|
|
delta = (event.pos() - self._lastMouseClickPoint)
|
|
|
|
self._lastMouseClickPoint = event.pos()
|
|
|
|
self.mouseDragged.emit(delta)
|
|
|
|
super().mouseMoveEvent(event)
|
|
|
|
# self.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-09 23:24:51 +00:00
|
|
|
self._app.restoreOverrideCursor()
|
2020-06-05 20:39:16 +00:00
|
|
|
self.setMouseTracking(False)
|
2020-06-20 23:49:17 +00:00
|
|
|
super().mouseReleaseEvent(event)
|
2020-06-05 20:39:16 +00:00
|
|
|
|
|
|
|
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
|
2020-06-20 23:49:17 +00:00
|
|
|
oldScale = self._current_scale
|
2020-06-23 03:03:56 +00:00
|
|
|
if event.angleDelta().y() > 0: # zoom-in
|
|
|
|
if oldScale < MAX_SCALE:
|
|
|
|
self._current_scale *= 1.25
|
2020-06-05 20:39:16 +00:00
|
|
|
else:
|
2020-06-23 03:03:56 +00:00
|
|
|
if oldScale > MIN_SCALE: # zoom-out
|
|
|
|
self._current_scale *= 0.8
|
|
|
|
if oldScale == self._current_scale:
|
|
|
|
return
|
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
deltaToPos = (event.position() / oldScale) - (self.label.pos() / oldScale)
|
|
|
|
delta = (deltaToPos * self._current_scale) - (deltaToPos * oldScale)
|
|
|
|
self.mouseWheeled.emit(self._current_scale, delta)
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-23 03:03:56 +00:00
|
|
|
def setImage(self, pixmap):
|
2020-06-16 19:14:28 +00:00
|
|
|
self._pixmap = pixmap
|
2020-06-09 23:24:51 +00:00
|
|
|
self.label.setPixmap(pixmap)
|
2020-06-23 03:03:56 +00:00
|
|
|
self.label.update()
|
2020-06-16 19:14:28 +00:00
|
|
|
self.label.adjustSize()
|
2020-06-23 03:03:56 +00:00
|
|
|
if pixmap.isNull():
|
|
|
|
self.setEnabled(False)
|
|
|
|
self.disconnectMouseSignals()
|
|
|
|
else:
|
|
|
|
if not self.isEnabled():
|
|
|
|
self.setEnabled(True)
|
|
|
|
self.connectMouseSignals()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
def centerViewAndUpdate(self):
|
|
|
|
self._rect = self.label.rect()
|
|
|
|
self.label.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 scaleBy(self, factor):
|
2020-06-20 23:49:17 +00:00
|
|
|
self._current_scale *= factor
|
|
|
|
# print(f"scaleBy(factor={factor}) current_scale={self._current_scale}")
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
# This kills my computer when scaling up! DO NOT USE!
|
|
|
|
# self._pixmap = self._pixmap.scaled(
|
|
|
|
# self._pixmap.size().__mul__(factor),
|
|
|
|
# Qt.KeepAspectRatio, Qt.FastTransformation)
|
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
# pointBeforeScale = QPoint(self.viewport().width() / 2,
|
|
|
|
# self.viewport().height() / 2)
|
|
|
|
pointBeforeScale = self.label.rect().center()
|
|
|
|
screenCenter = self.rect().center()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
screenCenter.setX(screenCenter.x() + self.horizontalScrollBar().value())
|
|
|
|
screenCenter.setY(screenCenter.y() + self.verticalScrollBar().value())
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
# WARNING: factor has to be either 1.25 or 0.8 here!
|
|
|
|
self.label.resize(self.label.size().__imul__(factor))
|
|
|
|
# self.label.updateGeometry()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
self.label._current_scale = self._current_scale
|
2020-06-16 19:14:28 +00:00
|
|
|
self.label.update()
|
2020-06-20 23:49:17 +00:00
|
|
|
# Center view on zoom change(?) same as imageLabel->resize(imageLabel->pixmap()->size())
|
|
|
|
# self.label.adjustSize()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
# pointAfterScale = QPoint(self.viewport().width() / 2,
|
|
|
|
# self.viewport().height() / 2)
|
|
|
|
pointAfterScale = self.label.rect().center()
|
|
|
|
# print(f"label.newsize: {self.label.size()}\npointAfter: {pointAfterScale}")
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
offset = pointBeforeScale - pointAfterScale
|
|
|
|
newCenter = screenCenter - offset #FIXME need factor here somewhere
|
|
|
|
# print(f"offset: {offset} newCenter: {newCenter}\n-----------------")
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
# self.centerOn(newCenter)
|
2020-06-05 20:39:16 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
# self.adjustScrollBarCentered()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
def scaleAt(self, scale):
|
|
|
|
self._current_scale = scale
|
|
|
|
self.label.resize(self._pixmap.size().__imul__(scale))
|
|
|
|
self.label._current_scale = scale
|
|
|
|
self.label.update()
|
|
|
|
# self.label.adjustSize()
|
|
|
|
|
|
|
|
def centerOn(self, position):
|
|
|
|
# TODO here make widget move without the scrollbars if possible
|
|
|
|
|
|
|
|
self.ensureWidgetVisible(self.label) # moves back to center of label
|
|
|
|
# self.ensureVisible(position.x(), position.y())
|
|
|
|
# self.scrollContentsBy(position.x(), position.y())
|
|
|
|
|
|
|
|
# hvalue = self.horizontalScrollBar().value()
|
|
|
|
# vvalue = self.verticalScrollBar().value()
|
|
|
|
# topLeft = self.viewport().rect().topLeft()
|
|
|
|
# self.label.move(topLeft.x() - hvalue, topLeft.y() - vvalue)
|
|
|
|
# self.label.updateGeometry()
|
|
|
|
|
|
|
|
def adjustScrollBarsFactor(self, factor):
|
|
|
|
"""After scaling, no mouse position, default to center."""
|
|
|
|
# scrollBar.setMaximum(scrollBar.maximum() - scrollBar.minimum() + scrollBar.pageStep())
|
|
|
|
self._horizontalScrollBar.setValue(int(factor * self._horizontalScrollBar.value() + \
|
|
|
|
((factor - 1) * self._horizontalScrollBar.pageStep()/2)))
|
|
|
|
self._verticalScrollBar.setValue(int(factor * self._verticalScrollBar.value() + \
|
|
|
|
((factor - 1) * self._verticalScrollBar.pageStep()/2)))
|
|
|
|
|
|
|
|
def adjustScrollBarsScaled(self, delta):
|
|
|
|
"""After scaling with the mouse, update relative to mouse position."""
|
|
|
|
self._horizontalScrollBar.setValue(
|
|
|
|
self._horizontalScrollBar.value() + delta.x())
|
|
|
|
self._verticalScrollBar.setValue(
|
|
|
|
self._verticalScrollBar.value() + delta.y())
|
|
|
|
|
|
|
|
def adjustScrollBarsAuto(self):
|
|
|
|
"""After panning, update accordingly."""
|
|
|
|
self.horizontalScrollBar().setValue(
|
|
|
|
self.horizontalScrollBar().value() - self._mousePanningDelta.x())
|
|
|
|
self.verticalScrollBar().setValue(
|
|
|
|
self.verticalScrollBar().value() - self._mousePanningDelta.y())
|
|
|
|
|
2020-06-23 03:03:56 +00:00
|
|
|
def adjustScrollBarCentered(self):
|
2020-06-20 23:49:17 +00:00
|
|
|
"""Just center in the middle."""
|
|
|
|
self._horizontalScrollBar.setValue(
|
|
|
|
int(self._horizontalScrollBar.maximum() / 2))
|
|
|
|
self._verticalScrollBar.setValue(
|
|
|
|
int(self._verticalScrollBar.maximum() / 2))
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
def resetCenter(self):
|
|
|
|
""" Resets origin """
|
2020-06-20 23:49:17 +00:00
|
|
|
self._mousePanningDelta = QPoint()
|
|
|
|
# self.label._mousePanningDelta = self._mousePanningDelta
|
2020-06-16 19:14:28 +00:00
|
|
|
self._current_scale = 1.0
|
2020-06-20 23:49:17 +00:00
|
|
|
# self.scaleBy(1.0)
|
2020-06-23 03:03:56 +00:00
|
|
|
# self.label.update() # already called in scaleBy²
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
def setCenter(self, point):
|
2020-06-20 23:49:17 +00:00
|
|
|
self._lastMouseClickPoint = point
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
def getCenter(self):
|
2020-06-20 23:49:17 +00:00
|
|
|
return self._lastMouseClickPoint
|
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()
|
2020-06-20 23:49:17 +00:00
|
|
|
def scaleToNormalSize(self):
|
2020-06-05 20:39:16 +00:00
|
|
|
"""Called when the pixmap is set back to original size."""
|
2020-06-20 23:49:17 +00:00
|
|
|
self.scaleAt(1.0)
|
|
|
|
self.ensureWidgetVisible(self.label) # needed for centering
|
|
|
|
# self.label.update()
|
2020-06-23 03:03:56 +00:00
|
|
|
self.toggleScrollBars(True)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
@pyqtSlot(QPoint)
|
2020-06-16 19:14:28 +00:00
|
|
|
def onDraggedMouse(self, delta):
|
2020-06-20 23:49:17 +00:00
|
|
|
"""Update position from mouse delta sent by the other panel."""
|
|
|
|
self._mousePanningDelta = delta
|
|
|
|
# self.label.move(self.label.pos() + delta)
|
|
|
|
# self.label.update()
|
|
|
|
|
2020-06-23 03:03:56 +00:00
|
|
|
# Signal from scrollbars had already synced the values here
|
2020-06-20 23:49:17 +00:00
|
|
|
self.adjustScrollBarsAuto()
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
# print(f"{self} onDraggedMouse slot with delta {delta}")
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
def changeEvent(self, event):
|
|
|
|
if event.type() == QEvent.EnabledChange:
|
|
|
|
print(f"{self} is now {'enabled' if self.isEnabled() else 'disabled'}")
|
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."""
|
2020-06-20 23:49:17 +00:00
|
|
|
mouseDragged = pyqtSignal()
|
2020-06-16 19:14:28 +00:00
|
|
|
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()
|
2020-06-20 23:49:17 +00:00
|
|
|
self._lastMouseClickPoint = QPointF()
|
|
|
|
self._mousePanningDelta = QPointF()
|
2020-06-16 19:14:28 +00:00
|
|
|
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
|
2020-06-20 23:49:17 +00:00
|
|
|
self._centerPoint = QPointF(0.0, 0.0)
|
|
|
|
self.centerOn(self._centerPoint)
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
# 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)
|
2020-06-20 23:49:17 +00:00
|
|
|
self.setViewportUpdateMode (QGraphicsView.FullViewportUpdate)
|
|
|
|
self.setMouseTracking(True)
|
2020-06-09 23:24:51 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
def connectMouseSignals(self):
|
2020-06-16 19:14:28 +00:00
|
|
|
if not self._dragConnection:
|
|
|
|
self._dragConnection = self.mouseDragged.connect(
|
2020-06-20 23:49:17 +00:00
|
|
|
self.controller.syncCenters)
|
2020-06-16 19:14:28 +00:00
|
|
|
if not self._wheelConnection:
|
|
|
|
self._wheelConnection = self.mouseWheeled.connect(
|
|
|
|
self.controller.scaleImages)
|
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
def disconnectMouseSignals(self):
|
2020-06-16 19:14:28 +00:00
|
|
|
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
|
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
self._lastMouseClickPoint = event.pos()
|
2020-06-16 19:14:28 +00:00
|
|
|
self._app.setOverrideCursor(Qt.ClosedHandCursor)
|
|
|
|
self.setMouseTracking(True)
|
2020-06-20 23:49:17 +00:00
|
|
|
# We need to propagate to scrollbars, so we send back up
|
|
|
|
super().mousePressEvent(event)
|
|
|
|
# event.accept()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
def mouseMoveEvent(self, event):
|
|
|
|
if self.bestFit:
|
|
|
|
event.ignore()
|
|
|
|
return
|
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
self._centerPoint = self.mapToScene( self.rect().center() )
|
|
|
|
super().mouseMoveEvent(event)
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
if self._drag:
|
2020-06-20 23:49:17 +00:00
|
|
|
self.mouseDragged.emit()
|
|
|
|
# self._item.update()
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
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-20 23:49:17 +00:00
|
|
|
# self._scene.setSceneRect(self._pixmap.rect())
|
|
|
|
|
|
|
|
def centerViewAndUpdate(self):
|
|
|
|
pass
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
def scaleBy(self, factor):
|
|
|
|
# super().scale(factor, factor)
|
|
|
|
self.zoom(factor)
|
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
def setCenter(self, point):
|
|
|
|
self._centerPoint = point
|
|
|
|
self.centerOn(self._centerPoint)
|
|
|
|
|
|
|
|
def getCenter(self):
|
|
|
|
return self._centerPoint
|
|
|
|
|
2020-06-16 19:14:28 +00:00
|
|
|
def resetCenter(self):
|
2020-06-20 23:49:17 +00:00
|
|
|
""" Resets origin """
|
|
|
|
self._mousePanningDelta = QPointF()
|
|
|
|
self._current_scale = 1.0
|
|
|
|
self.scaleBy(1.0)
|
|
|
|
# self.update()
|
|
|
|
self.setCenter(self._scene.sceneRect().center())
|
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()
|
2020-06-20 23:49:17 +00:00
|
|
|
def scaleToNormalSize(self):
|
2020-06-16 19:14:28 +00:00
|
|
|
"""Called when the pixmap is set back to original size."""
|
2020-06-20 23:49:17 +00:00
|
|
|
self.scaleBy(1.0) # FIXME
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
super().fitInView(self._scene.sceneRect(), Qt.KeepAspectRatio )
|
|
|
|
self.setNewCenter(self._scene.sceneRect().center())
|
|
|
|
self.update()
|
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
# def sizeHint(self):
|
|
|
|
# return self._item.rect().size()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
2020-06-20 23:49:17 +00:00
|
|
|
# def viewportSizeHint(self):
|
|
|
|
# return self._item.rect().size()
|
2020-06-16 19:14:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
|