|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the snapshot preview label. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import pyqtSignal, QPoint, Qt |
|
11 from PyQt4.QtGui 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.AlignHCenter | Qt.AlignCenter) |
|
31 self.setCursor(Qt.OpenHandCursor) |
|
32 |
|
33 self.__mouseClickPoint = QPoint(0, 0) |
|
34 |
|
35 def setPreview(self, preview): |
|
36 """ |
|
37 Public slot to set the preview picture. |
|
38 |
|
39 @param preview preview picture to be shown (QPixmap) |
|
40 """ |
|
41 if not preview.isNull(): |
|
42 pixmap = preview.scaled( |
|
43 self.width(), self.height(), Qt.KeepAspectRatio, Qt.SmoothTransformation) |
|
44 else: |
|
45 pixmap = preview |
|
46 self.setPixmap(pixmap) |
|
47 |
|
48 def mousePressEvent(self, evt): |
|
49 """ |
|
50 Protected method to handle mouse button presses. |
|
51 |
|
52 @param evt mouse button press event (QMouseEvent) |
|
53 """ |
|
54 if evt.button() == Qt.LeftButton: |
|
55 self.__mouseClickPoint = evt.pos() |
|
56 |
|
57 def mouseReleaseEvent(self, evt): |
|
58 """ |
|
59 Protected method to handle mouse button releases. |
|
60 |
|
61 @param evt mouse button release event (QMouseEvent) |
|
62 """ |
|
63 self.__mouseClickPoint = QPoint(0, 0) |
|
64 |
|
65 def mouseMoveEvent(self, evt): |
|
66 """ |
|
67 Protected method to handle mouse moves. |
|
68 |
|
69 @param evt mouse move event (QMouseEvent) |
|
70 """ |
|
71 if self.__mouseClickPoint != QPoint(0, 0) and \ |
|
72 (evt.pos() - self.__mouseClickPoint).manhattanLength() > \ |
|
73 QApplication.startDragDistance(): |
|
74 self.__mouseClickPoint = QPoint(0, 0) |
|
75 self.startDrag.emit() |