From b7abcf2989ba9a2f73a41692cd84a0f3a47c5d83 Mon Sep 17 00:00:00 2001 From: glubsy Date: Wed, 10 Jun 2020 22:02:08 +0200 Subject: [PATCH] Use native QPixmap swap() method instead of manual setPixmap() When swapping images, use getters to hopefully get a reference to each pixmap and swap them within a single slot. --- qt/pe/details_dialog.py | 24 +++++++----------------- qt/pe/image_viewer.py | 33 +++++++++++++++++---------------- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/qt/pe/details_dialog.py b/qt/pe/details_dialog.py index 348d9e27..1bc0645e 100644 --- a/qt/pe/details_dialog.py +++ b/qt/pe/details_dialog.py @@ -176,7 +176,12 @@ class DetailsDialog(DetailsDialogBase): self.tableView.setShowGrid(False) self.verticalLayout.addWidget(self.tableView) - self.disable_buttons() + self.buttonImgSwap.setEnabled(False) + self.buttonZoomIn.setEnabled(False) + self.buttonZoomOut.setEnabled(False) + self.buttonNormalSize.setEnabled(False) + self.buttonBestFit.setEnabled(False) + # We use different types of controller depending on the # underlying widgets we use to display images # because their interface methods might differ @@ -213,14 +218,6 @@ class DetailsDialog(DetailsDialogBase): """No item from the model, disable and clear everything.""" self.vController.clear_all() - def disable_buttons(self): - # FIXME Only called once at startup - self.buttonImgSwap.setEnabled(False) - self.buttonZoomIn.setEnabled(False) - self.buttonZoomOut.setEnabled(False) - self.buttonNormalSize.setEnabled(False) - self.buttonBestFit.setEnabled(False) - # --- Override def resizeEvent(self, event): if self.vController is None: @@ -244,17 +241,10 @@ class DetailsDialog(DetailsDialogBase): @pyqtSlot() def swapImages(self): """Swap pixmaps between ImageViewers.""" - self.vController.swapImages() + self.vController.swapPixmaps() # swap the columns in the details table as well self.tableView.horizontalHeader().swapSections(1, 2) - @pyqtSlot() - def deswapImages(self): - """Restore swapped pixmaps between ImageViewers.""" - self.vController.deswapImages() - # deswap the columns in the details table as well - self.tableView.horizontalHeader().swapSections(1, 2) - @pyqtSlot() def zoomIn(self): self.vController.scaleImages(1.25) diff --git a/qt/pe/image_viewer.py b/qt/pe/image_viewer.py index c802dde4..a0c787ea 100644 --- a/qt/pe/image_viewer.py +++ b/qt/pe/image_viewer.py @@ -19,10 +19,13 @@ class BaseController(QObject): super().__init__() self.selectedViewer = selectedViewer self.referenceViewer = referenceViewer + + # cached pixmaps self.selectedPixmap = QPixmap() self.referencePixmap = QPixmap() self.scaledSelectedPixmap = QPixmap() self.scaledReferencePixmap = QPixmap() + self.scaleFactor = 1.0 self.bestFit = True self.parent = parent #needed to change buttons' states @@ -121,22 +124,13 @@ class BaseController(QObject): self.parent.buttonImgSwap.setDisabled(True) self.parent.buttonNormalSize.setDisabled(True) - def swapImages(self): - if self.bestFit: - self.selectedViewer.setPixmap(self.scaledReferencePixmap) - self.referenceViewer.setPixmap(self.scaledSelectedPixmap) - else: - self.selectedViewer.setPixmap(self.referencePixmap) - self.referenceViewer.setPixmap(self.selectedPixmap) - - def deswapImages(self): - if self.bestFit: - self.selectedViewer.setPixmap(self.scaledSelectedPixmap) - self.referenceViewer.setPixmap(self.scaledReferencePixmap) - else: - self.selectedViewer.setPixmap(self.selectedPixmap) - self.referenceViewer.setPixmap(self.referencePixmap) + @pyqtSlot() + def swapPixmaps(self): + self.selectedViewer.getPixmap().swap(self.referenceViewer.getPixmap()) + self.selectedViewer.center_and_update() + self.referenceViewer.center_and_update() + @pyqtSlot() def zoomBestFit(self): self.setBestFit(True) self.scaleFactor = 1.0 @@ -145,6 +139,7 @@ class BaseController(QObject): self.parent.buttonZoomIn.setEnabled(False) self.parent.buttonNormalSize.setEnabled(True) + @pyqtSlot() def zoomNormalSize(self): self.setBestFit(False) self.scaleFactor = 1.0 @@ -302,6 +297,9 @@ class QWidgetImageViewer(QWidget): def __repr__(self): return f'{self._instance_name}' + def getPixmap(self): + return self._pixmap + def paintEvent(self, event): painter = QPainter(self) painter.translate(self.rect().center()) @@ -378,6 +376,9 @@ class QWidgetImageViewer(QWidget): self.connect_signals() self.setEnabled(True) self._pixmap = pixmap + self.center_and_update() + + def center_and_update(self): self._rect = self._pixmap.rect() self._rect.translate(-self._rect.center()) self.update() @@ -538,7 +539,7 @@ class ScrollAreaImageViewer(QScrollArea): self.update() def scale(self, factor): - self._scaleFactor = factor + self._scaleFactor *= factor self.label.resize(self._scaleFactor * self.label.pixmap().size()) self.adjustScrollBar(self.scrollarea.horizontalScrollBar(), factor) self.adjustScrollBar(self.scrollarea.verticalScrollBar(), factor)