|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a Notification widget. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import Qt, QTimer, QPoint |
|
11 from PyQt4.QtGui import QWidget, QPixmap |
|
12 |
|
13 from .Ui_NotificationWidget import Ui_NotificationWidget |
|
14 |
|
15 import Globals |
|
16 |
|
17 |
|
18 class NotificationWidget(QWidget, Ui_NotificationWidget): |
|
19 """ |
|
20 Class implementing a Notification widget. |
|
21 """ |
|
22 def __init__(self, parent=None, setPosition=False): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param parent reference to the parent widget (QWidget) |
|
27 @param setPosition flag indicating to set the display |
|
28 position interactively (boolean) |
|
29 """ |
|
30 super().__init__(parent) |
|
31 self.setupUi(self) |
|
32 |
|
33 self.__timeout = 5000 |
|
34 self.__icon = QPixmap() |
|
35 self.__heading = "" |
|
36 self.__text = "" |
|
37 self.__dragPosition = QPoint() |
|
38 |
|
39 self.__settingPosition = setPosition |
|
40 |
|
41 flags = Qt.Tool | \ |
|
42 Qt.FramelessWindowHint | \ |
|
43 Qt.WindowStaysOnTopHint | \ |
|
44 Qt.X11BypassWindowManagerHint |
|
45 if Globals.isWindowsPlatform(): |
|
46 flags |= Qt.ToolTip |
|
47 self.setWindowFlags(flags) |
|
48 |
|
49 self.__timer = QTimer(self) |
|
50 self.__timer.setSingleShot(True) |
|
51 self.__timer.timeout.connect(self.close) |
|
52 |
|
53 if self.__settingPosition: |
|
54 self.setCursor(Qt.OpenHandCursor) |
|
55 |
|
56 def setPixmap(self, icon): |
|
57 """ |
|
58 Public method to set the icon for the notification. |
|
59 |
|
60 @param icon icon to be used (QPixmap) |
|
61 """ |
|
62 self.__icon = QPixmap(icon) |
|
63 |
|
64 def setHeading(self, heading): |
|
65 """ |
|
66 Public method to set the heading for the notification. |
|
67 |
|
68 @param heading heading to be used (string) |
|
69 """ |
|
70 self.__heading = heading |
|
71 |
|
72 def setText(self, text): |
|
73 """ |
|
74 Public method to set the text for the notification. |
|
75 |
|
76 @param text text to be used (string) |
|
77 """ |
|
78 self.__text = text |
|
79 |
|
80 def setTimeout(self, timeout): |
|
81 """ |
|
82 Public method to set the timeout for the notification. |
|
83 |
|
84 @param timeout timeout to be used in seconds (integer) |
|
85 """ |
|
86 self.__timeout = timeout * 1000 |
|
87 |
|
88 def show(self): |
|
89 """ |
|
90 Public method to show the notification. |
|
91 """ |
|
92 self.icon.setPixmap(self.__icon) |
|
93 self.heading.setText(self.__heading) |
|
94 self.text.setText(self.__text) |
|
95 |
|
96 if not self.__settingPosition: |
|
97 self.__timer.stop() |
|
98 self.__timer.setInterval(self.__timeout) |
|
99 self.__timer.start() |
|
100 |
|
101 super().show() |
|
102 |
|
103 def mousePressEvent(self, evt): |
|
104 """ |
|
105 Protected method to handle presses of a mouse button. |
|
106 |
|
107 @param evt reference to the mouse event (QMouseEvent) |
|
108 """ |
|
109 if not self.__settingPosition: |
|
110 self.close() |
|
111 return |
|
112 |
|
113 if evt.button() == Qt.LeftButton: |
|
114 self.__dragPosition = evt.globalPos() - self.frameGeometry().topLeft() |
|
115 self.setCursor(Qt.ClosedHandCursor) |
|
116 evt.accept() |
|
117 |
|
118 def mouseReleaseEvent(self, evt): |
|
119 """ |
|
120 Protected method to handle releases of a mouse button. |
|
121 |
|
122 @param evt reference to the mouse event (QMouseEvent) |
|
123 """ |
|
124 if self.__settingPosition and evt.button() == Qt.LeftButton: |
|
125 self.setCursor(Qt.OpenHandCursor) |
|
126 |
|
127 def mouseMoveEvent(self, evt): |
|
128 """ |
|
129 Protected method to handle dragging the window. |
|
130 |
|
131 @param evt reference to the mouse event (QMouseEvent) |
|
132 """ |
|
133 if evt.buttons() & Qt.LeftButton: |
|
134 self.move(evt.globalPos() - self.__dragPosition) |
|
135 evt.accept() |