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