KanoopGuiQt 1.3.0
Qt GUI utility library
Loading...
Searching...
No Matches
toastmanager.h
1#ifndef TOASTMANAGER_H
2#define TOASTMANAGER_H
3
4#include <QDateTime>
5#include <QFrame>
6
7#include <Kanoop/timespan.h>
8#include <Kanoop/gui/libkanoopgui.h>
9
10class QGraphicsOpacityEffect;
11class Label;
12class QToolButton;
13class QLabel;
14class ToastWidget;
15
16/**
17 * @brief Non-modal toast notification manager overlaid on a parent widget.
18 *
19 * ToastManager creates ToastWidget pop-ups positioned over the parent widget
20 * and fades them out after a configurable delay. Separate foreground/background
21 * colors can be set for normal messages and error messages. The toast size and
22 * position within the parent are also configurable.
23 */
24class LIBKANOOPGUI_EXPORT ToastManager : public QObject
25{
26 Q_OBJECT
27public:
28 /**
29 * @brief Construct attached to a parent widget.
30 * @param parent Widget over which toasts are displayed
31 */
32 explicit ToastManager(QWidget *parent);
33
34 /**
35 * @brief Display a normal-priority message toast.
36 * @param text Message text to display
37 */
38 void message(const QString& text) { displayToast(text, _messageBackgroundColor, _messageForegroundColor); }
39
40 /**
41 * @brief Display an error-priority message toast.
42 * @param text Error message text to display
43 */
44 void errorMessage(const QString& text) { displayToast(text, _errorBackgroundColor, _errorForegroundColor); }
45
46 /**
47 * @brief Display a toast with explicit colors.
48 * @param text Message text to display
49 * @param backgroundColor Background color for the toast
50 * @param foregroundColor Foreground (text) color for the toast
51 */
52 void displayToast(const QString& text, const QColor& backgroundColor, const QColor& foregroundColor);
53
54 /**
55 * @brief Return the time after which the toast begins fading.
56 * @return Begin-fade delay
57 */
58 TimeSpan beginFadeTime() const { return _beginFadeTime; }
59
60 /**
61 * @brief Set the time after which the toast begins fading.
62 * @param value New begin-fade delay
63 */
64 void setBeginFadeTime(const TimeSpan& value) { _beginFadeTime = value; }
65
66 /**
67 * @brief Return the duration of the fade-out animation.
68 * @return Fade duration
69 */
70 TimeSpan fadeTime() const { return _fadeTime; }
71
72 /**
73 * @brief Set the duration of the fade-out animation.
74 * @param value New fade duration
75 */
76 void setFadeTime(TimeSpan value) { _fadeTime = value; }
77
78 /**
79 * @brief Return the foreground color for normal messages.
80 * @return Normal message foreground color
81 */
82 QColor messageForegroundColor() const { return _messageForegroundColor; }
83
84 /**
85 * @brief Set the foreground color for normal messages.
86 * @param value New foreground color
87 */
88 void setMessageForegroundColor(const QColor& value) { _messageForegroundColor = value; }
89
90 /**
91 * @brief Return the background color for normal messages.
92 * @return Normal message background color
93 */
94 QColor messageBackgroundColor() const { return _messageBackgroundColor; }
95
96 /**
97 * @brief Set the background color for normal messages.
98 * @param value New background color
99 */
100 void setMessageBackgroundColor(const QColor& value) { _messageBackgroundColor = value; }
101
102 /**
103 * @brief Return the foreground color for error messages.
104 * @return Error message foreground color
105 */
106 QColor errorForegroundColor() const { return _errorForegroundColor; }
107
108 /**
109 * @brief Set the foreground color for error messages.
110 * @param value New foreground color
111 */
112 void setErrorForegroundColor(const QColor& value) { _errorForegroundColor = value; }
113
114 /**
115 * @brief Return the background color for error messages.
116 * @return Error message background color
117 */
118 QColor errorBackgroundColor() const { return _errorBackgroundColor; }
119
120 /**
121 * @brief Set the background color for error messages.
122 * @param value New background color
123 */
124 void setErrorBackgroundColor(const QColor& value) { _errorBackgroundColor = value; }
125
126 /**
127 * @brief Return the fixed size of toast widgets.
128 * @return Toast size
129 */
130 QSize size() const { return _size; }
131
132 /**
133 * @brief Set the fixed size of toast widgets.
134 * @param value New toast size
135 */
136 void resize(const QSize& value) { _size = value; }
137
138 /**
139 * @brief Return the position of toasts within the parent widget.
140 * @return Toast position in parent coordinates
141 */
142 QPoint pos() const { return _pos; }
143
144 /**
145 * @brief Set the position of toasts within the parent widget.
146 * @param value New toast position in parent coordinates
147 */
148 void move(const QPoint& value) { _pos = value; }
149
150private:
151 void performLayout();
152 virtual bool eventFilter(QObject *watched, QEvent *event) override;
153
154 QWidget* _parentWidget;
155 TimeSpan _beginFadeTime;
156 TimeSpan _fadeTime;
157 QPoint _pos;
158 QSize _size;
159
160 QColor _messageBackgroundColor;
161 QColor _messageForegroundColor;
162 QColor _errorBackgroundColor;
163 QColor _errorForegroundColor;
164
165 QList<ToastWidget*> _toasts;
166
167signals:
168
169private slots:
170 void closeToast();
171};
172
173#endif // TOASTMANAGER_H
QLabel subclass with font size and color Q_PROPERTYs.
Definition label.h:16
Non-modal toast notification manager overlaid on a parent widget.
void move(const QPoint &value)
Set the position of toasts within the parent widget.
void setFadeTime(TimeSpan value)
Set the duration of the fade-out animation.
void resize(const QSize &value)
Set the fixed size of toast widgets.
void setBeginFadeTime(const TimeSpan &value)
Set the time after which the toast begins fading.
void displayToast(const QString &text, const QColor &backgroundColor, const QColor &foregroundColor)
Display a toast with explicit colors.
ToastManager(QWidget *parent)
Construct attached to a parent widget.
QPoint pos() const
Return the position of toasts within the parent widget.
QColor errorBackgroundColor() const
Return the background color for error messages.
QColor messageForegroundColor() const
Return the foreground color for normal messages.
TimeSpan beginFadeTime() const
Return the time after which the toast begins fading.
void setMessageForegroundColor(const QColor &value)
Set the foreground color for normal messages.
TimeSpan fadeTime() const
Return the duration of the fade-out animation.
void message(const QString &text)
Display a normal-priority message toast.
QSize size() const
Return the fixed size of toast widgets.
void errorMessage(const QString &text)
Display an error-priority message toast.
void setMessageBackgroundColor(const QColor &value)
Set the background color for normal messages.
void setErrorForegroundColor(const QColor &value)
Set the foreground color for error messages.
QColor messageBackgroundColor() const
Return the background color for normal messages.
void setErrorBackgroundColor(const QColor &value)
Set the background color for error messages.
QColor errorForegroundColor() const
Return the foreground color for error messages.