-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.py
203 lines (175 loc) · 7.2 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
"""
@author: Daniel Schreij
This file is part of QNotifications.
QNotifications is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
QNotifications is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GPLv3 License
along with this module.>.
"""
# Python3 compatibility
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from qtpy import QtWidgets, QtCore
import QNotifications
__author__ = u"Daniel Schreij"
__license__ = u"GPLv3"
import sys
class Example(QtCore.QObject):
""" Example showing off the notifications """
notify = QtCore.Signal(
['QString', 'QString', int, bool],
['QString', 'QString', int, bool, 'QString']
)
def __init__(self):
super(Example,self).__init__()
self.display_widget = self.__setup_widget()
self.notification_area = self.__setup_notification_area(self.display_widget)
self.display_widget.show()
def __setup_widget(self):
display_widget = QtWidgets.QWidget()
display_widget.setGeometry(100,100,800,600)
display_widget.setLayout(QtWidgets.QVBoxLayout())
inputLayout = QtWidgets.QFormLayout()
inputLayout.setFieldGrowthPolicy(inputLayout.ExpandingFieldsGrow)
# Notification message
message_label = QtWidgets.QLabel("Send notification: ", display_widget)
self.message_textbox = QtWidgets.QLineEdit(display_widget)
# Notification type
type_label = QtWidgets.QLabel("Notification type: ", display_widget)
self.type_dropdown = QtWidgets.QComboBox(display_widget)
self.type_dropdown.addItems(["primary", "success", "info", "warning", "danger"])
# Notification duration
duration_label = QtWidgets.QLabel("Display duration: (ms)", display_widget)
self.message_duration = QtWidgets.QSpinBox(display_widget)
self.message_duration.setRange(500, 5000)
self.message_duration.setValue(5000)
self.message_duration.setSingleStep(50)
# Notification auto-hide
autohide_label = QtWidgets.QLabel(
"Automatically hide on mouse over:",
display_widget
)
self.message_autohide = QtWidgets.QCheckBox(display_widget)
# Entry effect
entryeffect_label = QtWidgets.QLabel("Entry effect: ", display_widget)
self.entry_dropdown = QtWidgets.QComboBox(display_widget)
self.entry_dropdown.addItems(["None","fadeIn"])
try:
self.entry_dropdown.currentTextChanged.connect(self.__process_combo_change)
except AttributeError:
self.entry_dropdown.editTextChanged.connect(self.__process_combo_change)
# Entry effect duration
self.entryduration_label = QtWidgets.QLabel("Effect duration: (ms)", display_widget)
self.entryduration = QtWidgets.QSpinBox(display_widget)
self.entryduration.setRange(100, 1000)
self.entryduration.setSingleStep(50)
# Exit effect
exiteffect_label = QtWidgets.QLabel("Exit effect: ", display_widget)
self.exit_dropdown = QtWidgets.QComboBox(display_widget)
self.exit_dropdown.addItems(["None","fadeOut"])
# Qt5
try:
self.exit_dropdown.currentTextChanged.connect(self.__process_combo_change)
except AttributeError:
self.exit_dropdown.editTextChanged.connect(self.__process_combo_change)
# Exit effect duration
self.exitduration_label = QtWidgets.QLabel("Effect duration: (ms)", display_widget)
self.exitduration = QtWidgets.QSpinBox(display_widget)
self.exitduration.setRange(100, 1000)
self.exitduration.setSingleStep(50)
self.buttontext_label = QtWidgets.QLabel("Button text", display_widget)
self.buttontext_textbox = QtWidgets.QLineEdit(display_widget)
# Send button
send_button = QtWidgets.QPushButton("Send", display_widget)
inputLayout.addRow(message_label, self.message_textbox)
inputLayout.addRow(type_label, self.type_dropdown)
inputLayout.addRow(duration_label, self.message_duration)
inputLayout.addRow(autohide_label, self.message_autohide)
inputLayout.addRow(entryeffect_label, self.entry_dropdown)
inputLayout.addRow(self.entryduration_label, self.entryduration)
inputLayout.addRow(exiteffect_label, self.exit_dropdown)
inputLayout.addRow(self.exitduration_label, self.exitduration)
inputLayout.addRow(self.buttontext_label, self.buttontext_textbox)
inputLayout.addRow(QtWidgets.QWidget(), send_button)
self.entryduration_label.setDisabled(True)
self.entryduration.setDisabled(True)
self.exitduration_label.setDisabled(True)
self.exitduration.setDisabled(True)
display_widget.layout().addWidget(QtWidgets.QLabel("<h1>Example</h2>",
display_widget))
display_widget.layout().addLayout(inputLayout)
self.message_textbox.returnPressed.connect(send_button.click)
self.buttontext_textbox.returnPressed.connect(send_button.click)
send_button.clicked.connect(self.__submit_message)
return display_widget
def __setup_notification_area(self, targetWidget):
notification_area = QNotifications.QNotificationArea(targetWidget)
self.notify['QString', 'QString', int, bool].connect(
notification_area.display
)
self.notify['QString', 'QString', int, bool, 'QString'].connect(
notification_area.display
)
return notification_area
def __process_combo_change(self, val):
if self.sender() == self.entry_dropdown:
if val == "None":
self.entryduration_label.setDisabled(True)
self.entryduration.setDisabled(True)
else:
self.entryduration_label.setDisabled(False)
self.entryduration.setDisabled(False)
elif self.sender() == self.exit_dropdown:
if val == "None":
self.exitduration_label.setDisabled(True)
self.exitduration.setDisabled(True)
else:
self.exitduration_label.setDisabled(False)
self.exitduration.setDisabled(False)
def __submit_message(self):
textvalue = self.message_textbox.text().strip()
typevalue = self.type_dropdown.currentText()
if textvalue:
duration = self.message_duration.value()
autohide = self.message_autohide.isChecked()
entry_effect = self.entry_dropdown.currentText()
exit_effect = self.exit_dropdown.currentText()
if entry_effect != "None":
self.notification_area.setEntryEffect(entry_effect,
self.entryduration.value())
else:
self.notification_area.setEntryEffect(None)
if exit_effect != "None":
self.notification_area.setExitEffect(exit_effect,
self.exitduration.value())
else:
self.notification_area.setExitEffect(None)
buttontext = self.buttontext_textbox.text().strip()
if buttontext:
self.notify['QString', 'QString', int, bool, 'QString'].emit(
textvalue, typevalue, duration, autohide, buttontext
)
else:
self.notify['QString', 'QString', int, bool].emit(
textvalue, typevalue, duration, autohide
)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
print(QtCore.PYQT_VERSION_STR)
# Enable High DPI display with PyQt5
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
app.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps)
example = Example()
exitcode = app.exec_()
print("App exiting with code {}".format(exitcode))
del(example)
sys.exit(exitcode)