eric6/UI/NotificationWidget.py

changeset 7959
44e15eda6506
parent 7956
7db67b70e6a8
child 7961
9a11cf4ca0d6
--- a/eric6/UI/NotificationWidget.py	Tue Jan 05 15:14:40 2021 +0100
+++ b/eric6/UI/NotificationWidget.py	Tue Jan 05 18:28:31 2021 +0100
@@ -7,6 +7,8 @@
 Module implementing a Notification widget.
 """
 
+from enum import Enum
+
 from PyQt5.QtCore import Qt, QTimer, QPoint
 from PyQt5.QtWidgets import QFrame, QWidget, QVBoxLayout
 
@@ -14,24 +16,33 @@
 
 import Globals
 import Preferences
-
+import UI.PixmapCache
 
-# TODO: add background colors to indicate notification severity
-#       - all messages with timeout of 0
-#       - standard: information messages
-#       - yellow: warning messages
-#       - red: critical messages
 
 # TODO: add a notification log
 #       - date/time and message
 #       - display widget with capability to save the messages
 #       - optional: display widget with capability to load saved messages
 
+
+class NotificationTypes(Enum):
+    """
+    Class implementing the notification types.
+    """
+    Information = 0
+    Warning = 1             # __IGNORE_WARNING_M131__
+    Critical = 2
+    Other = 99
+
+
 class NotificationFrame(QFrame, Ui_NotificationFrame):
     """
     Class implementing a Notification widget.
     """
-    def __init__(self, icon, heading, text, parent=None):
+    NotificationStyleSheetTemplate = "color:{0};background-color:{1};"
+    
+    def __init__(self, icon, heading, text,
+                 kind=NotificationTypes.Information, parent=None):
         """
         Constructor
         
@@ -41,6 +52,8 @@
         @type str
         @param text text to be used
         @type str
+        @param kind kind of notification to be shown
+        @type NotificationTypes
         @param parent reference to the parent widget
         @type QWidget
         """
@@ -50,12 +63,59 @@
         self.layout().setAlignment(
             self.verticalLayout, Qt.AlignLeft | Qt.AlignVCenter)
         
+        self.setStyleSheet(NotificationFrame.getStyleSheet(kind))
+        
+        if icon is None:
+            icon = NotificationFrame.getIcon(kind)
         self.icon.setPixmap(icon)
+        
         self.heading.setText(heading)
         self.text.setText(text)
         
         self.show()
         self.adjustSize()
+    
+    @classmethod
+    def getIcon(cls, kind):
+        """
+        Class method to get the icon for a specific notification kind.
+        
+        @param kind notification kind
+        @type NotificationTypes
+        @return icon for the notification kind
+        @rtype QPixmap
+        """
+        if kind == NotificationTypes.Critical:
+            return UI.PixmapCache.getPixmap("notificationCritical48")
+        elif kind == NotificationTypes.Warning:
+            return UI.PixmapCache.getPixmap("notificationWarning48")
+        elif kind == NotificationTypes.Information:
+            return UI.PixmapCache.getPixmap("notificationInformation48")
+        else:
+            return UI.PixmapCache.getPixmap("notification48")
+    
+    @classmethod
+    def getStyleSheet(cls, kind):
+        """
+        Class method to get a style sheet for specific notification kind.
+        
+        @param kind notification kind
+        @type NotificationTypes
+        @return string containing the style sheet for the notification kind
+        @rtype str
+        """
+        if kind == NotificationTypes.Critical:
+            return NotificationFrame.NotificationStyleSheetTemplate.format(
+                Preferences.getUI("NotificationCriticalForeground"),
+                Preferences.getUI("NotificationCriticalBackground")
+            )
+        elif kind == NotificationTypes.Warning:
+            return NotificationFrame.NotificationStyleSheetTemplate.format(
+                Preferences.getUI("NotificationWarningForeground"),
+                Preferences.getUI("NotificationWarningBackground")
+            )
+        else:
+            return ""
 
 
 class NotificationWidget(QWidget):
@@ -97,7 +157,8 @@
         if self.__settingPosition:
             self.setCursor(Qt.OpenHandCursor)
     
-    def showNotification(self, icon, heading, text, timeout=0):
+    def showNotification(self, icon, heading, text,
+                         kind=NotificationTypes.Information, timeout=0):
         """
         Public method to show a notification.
         
@@ -107,10 +168,14 @@
         @type str
         @param text text to be used
         @type str
+        @param kind kind of notification to be shown
+        @type NotificationTypes
         @param timeout timeout in seconds after which the notification is
             to be removed (0 = do not remove until it is clicked on)
+        @type int
         """
-        notificationFrame = NotificationFrame(icon, heading, text, self)
+        notificationFrame = NotificationFrame(
+            icon, heading, text, kind=kind, parent=self)
         self.__layout.addWidget(notificationFrame)
         
         self.show()
@@ -179,7 +244,7 @@
         if self.__layout.count():
             self.__adjustSizeAndPosition()
         else:
-            self.close()
+            self.hide()
     
     def mousePressEvent(self, evt):
         """

eric ide

mercurial