3030__author__ = u"Daniel Schreij"
3131__license__ = u"GPLv3"
3232
33+ class MessageLabel (QtWidgets .QLabel ):
34+ """ Subclass of QLabel, which reimplements the resizeEvent() function. This
35+ is necessary because otherwise the notifications take up too much vertical
36+ space when texts they display become longer. This is because normally the height
37+ of a notification is calculated as the minimum height necessary for the text
38+ when the widget is horizontally resized to its minimum. """
39+
40+ def resizeEvent (self , event ):
41+ super (MessageLabel , self ).resizeEvent (event )
42+ if ( self .wordWrap () and \
43+ self .sizePolicy ().verticalPolicy () == QtWidgets .QSizePolicy .Minimum ):
44+ self .setMaximumHeight ( self .heightForWidth ( self .width () ) )
45+
3346class QNotification (QtWidgets .QWidget ):
3447 """ Class representing a single notification """
3548
@@ -55,15 +68,20 @@ def __init__(self, message, category, *args, **kwargs):
5568 # Set Object name for reference
5669 self .setObjectName (category )
5770 self .setLayout (QtWidgets .QHBoxLayout ())
71+ self .setContentsMargins (0 ,0 ,0 ,0 )
72+ # self.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
73+ # QtWidgets.QSizePolicy.Fixed)
5874
5975 # Create a message area
6076 #contents = QtWidgets.QWidget(self)
6177 messageArea = QtWidgets .QHBoxLayout ()
6278 messageArea .setContentsMargins (0 ,0 ,0 ,0 )
6379
6480 # Create the layout
65- self .message_display = QtWidgets . QLabel ()
81+ self .message_display = MessageLabel ()
6682 self .message_display .setObjectName ("message" )
83+ self .message_display .setSizePolicy (QtWidgets .QSizePolicy .Minimum ,
84+ QtWidgets .QSizePolicy .Minimum )
6785 self .message_display .setWordWrap (True )
6886
6987 # Create a button that can close notifications
@@ -75,7 +93,7 @@ def __init__(self, message, category, *args, **kwargs):
7593
7694 # Add everything together
7795 messageArea .addWidget (self .message_display )
78- messageArea .addStretch (1 )
96+ # messageArea.addStretch(1)
7997 messageArea .addWidget (close_button )
8098 self .layout ().addLayout (messageArea )
8199
0 commit comments