|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the snapshot preview label. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSignal, QPoint, Qt |
|
11 from PyQt6.QtWidgets import QLabel, QApplication |
|
12 |
|
13 |
|
14 class SnapshotPreview(QLabel): |
|
15 """ |
|
16 Class implementing the snapshot preview label. |
|
17 |
|
18 @signal startDrag() emitted to indicate the start of a drag operation |
|
19 """ |
|
20 startDrag = pyqtSignal() |
|
21 |
|
22 def __init__(self, parent=None): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param parent reference to the parent widget (QWidget) |
|
27 """ |
|
28 super().__init__(parent) |
|
29 |
|
30 self.setAlignment(Qt.AlignmentFlag.AlignHCenter | |
|
31 Qt.AlignmentFlag.AlignCenter) |
|
32 self.setCursor(Qt.CursorShape.OpenHandCursor) |
|
33 |
|
34 self.__mouseClickPoint = QPoint(0, 0) |
|
35 |
|
36 def setPreview(self, preview): |
|
37 """ |
|
38 Public slot to set the preview picture. |
|
39 |
|
40 @param preview preview picture to be shown (QPixmap) |
|
41 """ |
|
42 pixmap = ( |
|
43 preview.scaled( |
|
44 self.width(), self.height(), |
|
45 Qt.AspectRatioMode.KeepAspectRatio, |
|
46 Qt.TransformationMode.SmoothTransformation) |
|
47 if not preview.isNull() else |
|
48 preview |
|
49 ) |
|
50 self.setPixmap(pixmap) |
|
51 |
|
52 def mousePressEvent(self, evt): |
|
53 """ |
|
54 Protected method to handle mouse button presses. |
|
55 |
|
56 @param evt mouse button press event (QMouseEvent) |
|
57 """ |
|
58 if evt.button() == Qt.MouseButton.LeftButton: |
|
59 self.__mouseClickPoint = evt.position().toPoint() |
|
60 |
|
61 def mouseReleaseEvent(self, evt): |
|
62 """ |
|
63 Protected method to handle mouse button releases. |
|
64 |
|
65 @param evt mouse button release event (QMouseEvent) |
|
66 """ |
|
67 self.__mouseClickPoint = QPoint(0, 0) |
|
68 |
|
69 def mouseMoveEvent(self, evt): |
|
70 """ |
|
71 Protected method to handle mouse moves. |
|
72 |
|
73 @param evt mouse move event (QMouseEvent) |
|
74 """ |
|
75 if ( |
|
76 self.__mouseClickPoint != QPoint(0, 0) and ( |
|
77 evt.position().toPoint() - self.__mouseClickPoint |
|
78 ).manhattanLength() > QApplication.startDragDistance() |
|
79 ): |
|
80 self.__mouseClickPoint = QPoint(0, 0) |
|
81 self.startDrag.emit() |