Skip to content

Commit 39e603c

Browse files
authored
Merge pull request #3 from smathot/master
Add optional buttontext
2 parents a87937d + a27f058 commit 39e603c

File tree

5 files changed

+48
-47
lines changed

5 files changed

+48
-47
lines changed

QNotifications/QNotification.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
class 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-

QNotifications/QNotificationArea.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ def __init__(self, targetWidget, *args, **kwargs):
8787

8888
useGlobalCSS = kwargs.pop(u'useGlobalCSS', False)
8989
super(QNotificationArea, self).__init__(*args, **kwargs)
90-
90+
9191
if not useGlobalCSS:
9292
self.setStyleSheet(self.default_notification_styles)
93-
93+
9494
self.setParent(targetWidget)
9595
self.targetWidget = targetWidget
9696
self.setContentsMargins(0,0,0,0)
97-
97+
9898
notification_area_layout = QtWidgets.QVBoxLayout()
9999
self.setLayout(notification_area_layout)
100100

@@ -123,8 +123,8 @@ def __delete_notification(self, notification=None):
123123

124124
# Public functions
125125
def setEntryEffect(self, effect, duration=250):
126-
""" Sets the effect with which the notifications are to appear.
127-
126+
""" Sets the effect with which the notifications are to appear.
127+
128128
Parameters
129129
----------
130130
effect : {'fadeIn', None}
@@ -153,8 +153,8 @@ def setEntryEffect(self, effect, duration=250):
153153
self.entryEffectDuration = duration
154154

155155
def setExitEffect(self, effect, duration=500):
156-
""" Sets the effect with which the notifications are to disappear.
157-
156+
""" Sets the effect with which the notifications are to disappear.
157+
158158
Parameters
159159
----------
160160
effect : {'fadeOut', None}
@@ -183,10 +183,10 @@ def setExitEffect(self, effect, duration=500):
183183
self.exitEffectDuration = duration
184184

185185
# Events
186-
@QtCore.pyqtSlot('QString', 'QString', int)
187-
def display(self, message, category, timeout=5000):
188-
""" Displays a notification.
189-
186+
@QtCore.pyqtSlot('QString', 'QString', int, 'QString')
187+
def display(self, message, category, timeout=5000, buttontext=None):
188+
""" Displays a notification.
189+
190190
Parameters
191191
----------
192192
message : str
@@ -205,7 +205,7 @@ def display(self, message, category, timeout=5000):
205205
"""
206206

207207
self.show()
208-
notification = QNotification(message, category, self)
208+
notification = QNotification(message, category, buttontext, self)
209209
notification.closeClicked.connect(self.remove)
210210
self.layout().addWidget(notification)
211211
# Check for entry effects
@@ -217,13 +217,13 @@ def display(self, message, category, timeout=5000):
217217

218218
self.adjustSize()
219219
if not timeout is None and timeout > 0:
220-
QtCore.QTimer.singleShot(timeout,
220+
QtCore.QTimer.singleShot(timeout,
221221
lambda : self.remove(notification))
222222

223223
@QtCore.pyqtSlot()
224224
def remove(self, notification = None):
225225
""" Removes a notification.
226-
226+
227227
Parameters
228228
----------
229229
notification : QNotification (default: None)
@@ -266,17 +266,15 @@ def remove(self, notification = None):
266266
def resizeEvent(self, event):
267267
""" Internal QT function (do not call directly). """
268268
self.target_resize_event(event)
269-
newsize = event.size()
269+
newsize = event.size()
270270
self.setFixedWidth(newsize.width())
271271
self.adjustSize()
272272

273273
def paintEvent(self, pe):
274-
""" Redefinition of paintEvent.
275-
Makes class QNotificationArea available in style sheets.
274+
""" Redefinition of paintEvent.
275+
Makes class QNotificationArea available in style sheets.
276276
Internal QT function (do not call directly) """
277277
o = QtWidgets.QStyleOption()
278278
o.initFrom(self)
279279
p = QtGui.QPainter(self)
280280
self.style().drawPrimitive(QtWidgets.QStyle.PE_Widget, o, p, self)
281-
282-

QNotifications/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
You should have received a copy of the GPLv3 License
1818
along with this module.>.
1919
"""
20-
__version__ = "1.0.7"
20+
__version__ = "1.0.8"
2121
__author__ = "Daniel Schreij (dschreij@gmail.com)"
2222

2323
# Do some base imports
2424
from QNotifications.QNotificationArea import QNotificationArea
2525
from QNotifications.QNotification import QNotification
26-

example.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
class Example(QtCore.QObject):
3535
""" Example showing off the notifications """
36-
notify = QtCore.pyqtSignal(str,str,int)
36+
notify = QtCore.pyqtSignal(str,str,int,str)
3737

3838
def __init__(self):
3939
super(Example,self).__init__()
@@ -56,7 +56,7 @@ def __setup_widget(self):
5656
type_label = QtWidgets.QLabel("Notification type: ", display_widget)
5757
self.type_dropdown = QtWidgets.QComboBox(display_widget)
5858
self.type_dropdown.addItems(["primary", "success", "info", "warning", "danger"])
59-
59+
6060
# Notification duration
6161
duration_label = QtWidgets.QLabel("Display duration: (ms)", display_widget)
6262
self.message_duration = QtWidgets.QSpinBox(display_widget)
@@ -69,14 +69,14 @@ def __setup_widget(self):
6969
self.entry_dropdown.addItems(["None","fadeIn"])
7070
try:
7171
self.entry_dropdown.currentTextChanged.connect(self.__process_combo_change)
72-
except AttributeError:
72+
except AttributeError:
7373
self.entry_dropdown.editTextChanged.connect(self.__process_combo_change)
7474
# Entry effect duration
7575
self.entryduration_label = QtWidgets.QLabel("Effect duration: (ms)", display_widget)
7676
self.entryduration = QtWidgets.QSpinBox(display_widget)
7777
self.entryduration.setRange(100, 1000)
7878
self.entryduration.setSingleStep(50)
79-
# Exit effect
79+
# Exit effect
8080
exiteffect_label = QtWidgets.QLabel("Exit effect: ", display_widget)
8181
self.exit_dropdown = QtWidgets.QComboBox(display_widget)
8282
self.exit_dropdown.addItems(["None","fadeOut"])
@@ -144,7 +144,7 @@ def __submit_message(self):
144144
entry_effect = self.entry_dropdown.currentText()
145145
exit_effect = self.exit_dropdown.currentText()
146146
if entry_effect != "None":
147-
self.notification_area.setEntryEffect(entry_effect,
147+
self.notification_area.setEntryEffect(entry_effect,
148148
self.entryduration.value())
149149
else:
150150
self.notification_area.setEntryEffect(None)
@@ -153,7 +153,7 @@ def __submit_message(self):
153153
self.exitduration.value())
154154
else:
155155
self.notification_area.setExitEffect(None)
156-
self.notify.emit(textvalue, typevalue, duration)
156+
self.notify.emit(textvalue, typevalue, duration, None)
157157

158158
if __name__ == "__main__":
159159
app = QtWidgets.QApplication(sys.argv)

stdeb.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Source=python-qnotifications
33
Package=python-qnotifications
44
Debian-version=1
5-
Suite=wily
5+
Suite=xenial
66
Copyright-File=copyright
77
Build-Depends=python-qtpy, python-qt4
88
Depends=python-qtpy, python-qt4

0 commit comments

Comments
 (0)