eric6/UI/NotificationWidget.py

changeset 7959
44e15eda6506
parent 7956
7db67b70e6a8
child 7961
9a11cf4ca0d6
equal deleted inserted replaced
7958:a70ebfad3d0e 7959:44e15eda6506
5 5
6 """ 6 """
7 Module implementing a Notification widget. 7 Module implementing a Notification widget.
8 """ 8 """
9 9
10 from enum import Enum
11
10 from PyQt5.QtCore import Qt, QTimer, QPoint 12 from PyQt5.QtCore import Qt, QTimer, QPoint
11 from PyQt5.QtWidgets import QFrame, QWidget, QVBoxLayout 13 from PyQt5.QtWidgets import QFrame, QWidget, QVBoxLayout
12 14
13 from .Ui_NotificationFrame import Ui_NotificationFrame 15 from .Ui_NotificationFrame import Ui_NotificationFrame
14 16
15 import Globals 17 import Globals
16 import Preferences 18 import Preferences
17 19 import UI.PixmapCache
18 20
19 # TODO: add background colors to indicate notification severity
20 # - all messages with timeout of 0
21 # - standard: information messages
22 # - yellow: warning messages
23 # - red: critical messages
24 21
25 # TODO: add a notification log 22 # TODO: add a notification log
26 # - date/time and message 23 # - date/time and message
27 # - display widget with capability to save the messages 24 # - display widget with capability to save the messages
28 # - optional: display widget with capability to load saved messages 25 # - optional: display widget with capability to load saved messages
29 26
27
28 class NotificationTypes(Enum):
29 """
30 Class implementing the notification types.
31 """
32 Information = 0
33 Warning = 1 # __IGNORE_WARNING_M131__
34 Critical = 2
35 Other = 99
36
37
30 class NotificationFrame(QFrame, Ui_NotificationFrame): 38 class NotificationFrame(QFrame, Ui_NotificationFrame):
31 """ 39 """
32 Class implementing a Notification widget. 40 Class implementing a Notification widget.
33 """ 41 """
34 def __init__(self, icon, heading, text, parent=None): 42 NotificationStyleSheetTemplate = "color:{0};background-color:{1};"
43
44 def __init__(self, icon, heading, text,
45 kind=NotificationTypes.Information, parent=None):
35 """ 46 """
36 Constructor 47 Constructor
37 48
38 @param icon icon to be used 49 @param icon icon to be used
39 @type QPixmap 50 @type QPixmap
40 @param heading heading to be used 51 @param heading heading to be used
41 @type str 52 @type str
42 @param text text to be used 53 @param text text to be used
43 @type str 54 @type str
55 @param kind kind of notification to be shown
56 @type NotificationTypes
44 @param parent reference to the parent widget 57 @param parent reference to the parent widget
45 @type QWidget 58 @type QWidget
46 """ 59 """
47 super(NotificationFrame, self).__init__(parent) 60 super(NotificationFrame, self).__init__(parent)
48 self.setupUi(self) 61 self.setupUi(self)
49 62
50 self.layout().setAlignment( 63 self.layout().setAlignment(
51 self.verticalLayout, Qt.AlignLeft | Qt.AlignVCenter) 64 self.verticalLayout, Qt.AlignLeft | Qt.AlignVCenter)
52 65
66 self.setStyleSheet(NotificationFrame.getStyleSheet(kind))
67
68 if icon is None:
69 icon = NotificationFrame.getIcon(kind)
53 self.icon.setPixmap(icon) 70 self.icon.setPixmap(icon)
71
54 self.heading.setText(heading) 72 self.heading.setText(heading)
55 self.text.setText(text) 73 self.text.setText(text)
56 74
57 self.show() 75 self.show()
58 self.adjustSize() 76 self.adjustSize()
77
78 @classmethod
79 def getIcon(cls, kind):
80 """
81 Class method to get the icon for a specific notification kind.
82
83 @param kind notification kind
84 @type NotificationTypes
85 @return icon for the notification kind
86 @rtype QPixmap
87 """
88 if kind == NotificationTypes.Critical:
89 return UI.PixmapCache.getPixmap("notificationCritical48")
90 elif kind == NotificationTypes.Warning:
91 return UI.PixmapCache.getPixmap("notificationWarning48")
92 elif kind == NotificationTypes.Information:
93 return UI.PixmapCache.getPixmap("notificationInformation48")
94 else:
95 return UI.PixmapCache.getPixmap("notification48")
96
97 @classmethod
98 def getStyleSheet(cls, kind):
99 """
100 Class method to get a style sheet for specific notification kind.
101
102 @param kind notification kind
103 @type NotificationTypes
104 @return string containing the style sheet for the notification kind
105 @rtype str
106 """
107 if kind == NotificationTypes.Critical:
108 return NotificationFrame.NotificationStyleSheetTemplate.format(
109 Preferences.getUI("NotificationCriticalForeground"),
110 Preferences.getUI("NotificationCriticalBackground")
111 )
112 elif kind == NotificationTypes.Warning:
113 return NotificationFrame.NotificationStyleSheetTemplate.format(
114 Preferences.getUI("NotificationWarningForeground"),
115 Preferences.getUI("NotificationWarningBackground")
116 )
117 else:
118 return ""
59 119
60 120
61 class NotificationWidget(QWidget): 121 class NotificationWidget(QWidget):
62 """ 122 """
63 Class implementing a Notification list widget. 123 Class implementing a Notification list widget.
95 self.setWindowFlags(flags) 155 self.setWindowFlags(flags)
96 156
97 if self.__settingPosition: 157 if self.__settingPosition:
98 self.setCursor(Qt.OpenHandCursor) 158 self.setCursor(Qt.OpenHandCursor)
99 159
100 def showNotification(self, icon, heading, text, timeout=0): 160 def showNotification(self, icon, heading, text,
161 kind=NotificationTypes.Information, timeout=0):
101 """ 162 """
102 Public method to show a notification. 163 Public method to show a notification.
103 164
104 @param icon icon to be used 165 @param icon icon to be used
105 @type QPixmap 166 @type QPixmap
106 @param heading heading to be used 167 @param heading heading to be used
107 @type str 168 @type str
108 @param text text to be used 169 @param text text to be used
109 @type str 170 @type str
171 @param kind kind of notification to be shown
172 @type NotificationTypes
110 @param timeout timeout in seconds after which the notification is 173 @param timeout timeout in seconds after which the notification is
111 to be removed (0 = do not remove until it is clicked on) 174 to be removed (0 = do not remove until it is clicked on)
112 """ 175 @type int
113 notificationFrame = NotificationFrame(icon, heading, text, self) 176 """
177 notificationFrame = NotificationFrame(
178 icon, heading, text, kind=kind, parent=self)
114 self.__layout.addWidget(notificationFrame) 179 self.__layout.addWidget(notificationFrame)
115 180
116 self.show() 181 self.show()
117 182
118 self.__adjustSizeAndPosition() 183 self.__adjustSizeAndPosition()
177 notification.deleteLater() 242 notification.deleteLater()
178 243
179 if self.__layout.count(): 244 if self.__layout.count():
180 self.__adjustSizeAndPosition() 245 self.__adjustSizeAndPosition()
181 else: 246 else:
182 self.close() 247 self.hide()
183 248
184 def mousePressEvent(self, evt): 249 def mousePressEvent(self, evt):
185 """ 250 """
186 Protected method to handle presses of a mouse button. 251 Protected method to handle presses of a mouse button.
187 252

eric ide

mercurial