1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2024-10-31 22:05:58 +00:00

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.
This commit is contained in:
glubsy 2020-06-10 22:02:08 +02:00
parent 8103cb3664
commit b7abcf2989
2 changed files with 24 additions and 33 deletions

View File

@ -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)

View File

@ -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)