1414class MessageLabel (QtWidgets .QLabel ):
1515 """ Subclass of QLabel, which reimplements the resizeEvent() function. This
1616 is necessary because otherwise the notifications take up too much vertical
17- space when texts they display become longer. This is because normally the height
18- of a notification is calculated as the minimum height necessary for the text
17+ space when texts they display become longer. This is because normally the height
18+ of a notification is calculated as the minimum height necessary for the text
1919 when the widget is horizontally resized to its minimum. """
2020
2121 def resizeEvent (self , event ):
@@ -30,15 +30,15 @@ class QNotification(QtWidgets.QWidget):
3030 closeClicked = QtCore .pyqtSignal ()
3131 """ PyQt signal for click on the notification's close button. """
3232
33- def __init__ (self , message , category , * args , ** kwargs ):
33+ def __init__ (self , message , category , buttontext = None , * args , ** kwargs ):
3434 """Constructor
3535
3636 Parameters
3737 ----------
3838 message : str
3939 The message to show
4040 category : {'primary', 'success', 'info', 'warning', 'danger'}
41- The type of notification. Adheres to bootstrap standard
41+ The type of notification. Adheres to bootstrap standard
4242 classes which are {primary, success, info, warning, danger}
4343 """
4444 super (QNotification , self ).__init__ (* args , ** kwargs )
@@ -50,7 +50,7 @@ def __init__(self, message, category, *args, **kwargs):
5050 self .setObjectName (category )
5151 self .setLayout (QtWidgets .QHBoxLayout ())
5252 self .setContentsMargins (0 ,0 ,0 ,0 )
53- # self.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
53+ # self.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
5454 # QtWidgets.QSizePolicy.Fixed)
5555
5656 # Create a message area
@@ -61,15 +61,20 @@ def __init__(self, message, category, *args, **kwargs):
6161 # Create the layout
6262 self .message_display = MessageLabel ()
6363 self .message_display .setObjectName ("message" )
64- self .message_display .setSizePolicy (QtWidgets .QSizePolicy .Minimum ,
64+ self .message_display .setSizePolicy (QtWidgets .QSizePolicy .Minimum ,
6565 QtWidgets .QSizePolicy .Minimum )
6666 self .message_display .setWordWrap (True )
6767
6868 # Create a button that can close notifications
69- close_button = QtWidgets .QPushButton (u"\u2715 " )
70- close_button .setObjectName ("closeButton" )
71- close_button .setFixedWidth (20 )
69+ if buttontext in (None , u'' ):
70+ close_button = QtWidgets .QPushButton (u"\u2715 " )
71+ else :
72+ close_button = QtWidgets .QPushButton (buttontext )
73+ close_button .setStyleSheet (u'text-decoration: underline;' )
74+ close_button .setSizePolicy (QtWidgets .QSizePolicy .Fixed ,
75+ QtWidgets .QSizePolicy .Fixed )
7276 close_button .setFlat (True )
77+ close_button .setObjectName ("closeButton" )
7378 close_button .clicked .connect (self .closeClicked )
7479
7580 # Add everything together
@@ -84,7 +89,7 @@ def __init__(self, message, category, *args, **kwargs):
8489
8590 # Flag that is set if notification is being removed. This can be used to
8691 # make sure that even though the notification has not been really removed
87- # yet (because it is for example in an fade out animation), it is in the
92+ # yet (because it is for example in an fade out animation), it is in the
8893 # process of being removed
8994 self .isBeingRemoved = False
9095
@@ -96,13 +101,13 @@ def __init_graphic_effects(self):
96101 self .opacityEffect = QtWidgets .QGraphicsOpacityEffect (self )
97102
98103 # Fade in animation
99- self .fadeInAnimation = QtCore .QPropertyAnimation (self .opacityEffect ,
104+ self .fadeInAnimation = QtCore .QPropertyAnimation (self .opacityEffect ,
100105 safe_encode ("opacity" ))
101106 self .fadeInAnimation .setStartValue (0.0 )
102107 self .fadeInAnimation .setEndValue (1.0 )
103108
104109 # Fade out animation
105- self .fadeOutAnimation = QtCore .QPropertyAnimation (self .opacityEffect ,
110+ self .fadeOutAnimation = QtCore .QPropertyAnimation (self .opacityEffect ,
106111 safe_encode ("opacity" ))
107112 self .fadeOutAnimation .setStartValue (1.0 )
108113 self .fadeOutAnimation .setEndValue (0.0 )
@@ -119,7 +124,7 @@ def close(self):
119124
120125 def fadeIn (self , duration ):
121126 """ Fades in the notification.
122-
127+
123128 Parameters
124129 ----------
125130 duration : int
@@ -139,8 +144,8 @@ def fadeIn(self, duration):
139144 self .fadeInAnimation .start ()
140145
141146 def fadeOut (self , finishedCallback , duration ):
142- """ Fades out the notification.
143-
147+ """ Fades out the notification.
148+
144149 Parameters
145150 ----------
146151 finishedCallback : callable
@@ -167,8 +172,8 @@ def fadeOut(self, finishedCallback, duration):
167172 self .fadeOutAnimation .start ()
168173
169174 def paintEvent (self , pe ):
170- """ redefinition of paintEvent, do not call directly.
171- Makes class QNotification available in style sheets. Interal Qt function.
175+ """ redefinition of paintEvent, do not call directly.
176+ Makes class QNotification available in style sheets. Interal Qt function.
172177 Should not be called directly. """
173178 o = QtWidgets .QStyleOption ()
174179 o .initFrom (self )
@@ -193,7 +198,7 @@ def category(self):
193198
194199 @category .setter
195200 def category (self , value ):
196- """ Sets the category of this notification.
201+ """ Sets the category of this notification.
197202
198203 Parameters
199204 ----------
@@ -211,4 +216,3 @@ def category(self, value):
211216 raise ValueError (u'{} not a valid value. '
212217 'Should be one of' ).format (value , allowed_values )
213218 self ._category = value
214-
0 commit comments