eric6/Snapshot/SnapshotPreview.py

Tue, 02 Mar 2021 17:17:09 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 02 Mar 2021 17:17:09 +0100
changeset 8143
2c730d5fd177
parent 7923
91e843545d9a
child 8218
7c09585bd960
permissions
-rw-r--r--

Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.

# -*- coding: utf-8 -*-

# Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the snapshot preview label.
"""

from PyQt5.QtCore import pyqtSignal, QPoint, Qt
from PyQt5.QtWidgets import QLabel, QApplication


class SnapshotPreview(QLabel):
    """
    Class implementing the snapshot preview label.
    
    @signal startDrag() emitted to indicate the start of a drag operation
    """
    startDrag = pyqtSignal()
    
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget (QWidget)
        """
        super(SnapshotPreview, self).__init__(parent)
        
        self.setAlignment(Qt.AlignmentFlag.AlignHCenter |
                          Qt.AlignmentFlag.AlignCenter)
        self.setCursor(Qt.CursorShape.OpenHandCursor)
        
        self.__mouseClickPoint = QPoint(0, 0)
    
    def setPreview(self, preview):
        """
        Public slot to set the preview picture.
        
        @param preview preview picture to be shown (QPixmap)
        """
        if not preview.isNull():
            pixmap = preview.scaled(
                self.width(), self.height(),
                Qt.AspectRatioMode.KeepAspectRatio,
                Qt.TransformationMode.SmoothTransformation)
        else:
            pixmap = preview
        self.setPixmap(pixmap)
    
    def mousePressEvent(self, evt):
        """
        Protected method to handle mouse button presses.
        
        @param evt mouse button press event (QMouseEvent)
        """
        if evt.button() == Qt.MouseButton.LeftButton:
            self.__mouseClickPoint = evt.pos()
    
    def mouseReleaseEvent(self, evt):
        """
        Protected method to handle mouse button releases.
        
        @param evt mouse button release event (QMouseEvent)
        """
        self.__mouseClickPoint = QPoint(0, 0)
    
    def mouseMoveEvent(self, evt):
        """
        Protected method to handle mouse moves.
        
        @param evt mouse move event (QMouseEvent)
        """
        if (
            self.__mouseClickPoint != QPoint(0, 0) and
            (evt.pos() - self.__mouseClickPoint).manhattanLength() >
            QApplication.startDragDistance()
        ):
            self.__mouseClickPoint = QPoint(0, 0)
            self.startDrag.emit()

eric ide

mercurial