Debian Codegraph @

src/Gui/NotificationBox.h

raw
1 /***************************************************************************
2 * Copyright (c) 2023 Abdullah Tahiri <[email protected] > *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #ifndef GUI_NOTIFICATIONBOX_H
24 #define GUI_NOTIFICATIONBOX_H
25
26 #include <type_traits>
27
28 #include <QFont>
29 #include <QPoint>
30 #include <QString>
31
32 namespace Gui
33 {
34
35 /** This class provides a non-intrusive tip alike notification
36 * dialog, which unlike QToolTip, is kept shown during a time.
37 *
38 * The notification is shown during minShowTime, unless pop out
39 * (i.e. clicked inside the notification).
40 *
41 * The notification will show up to a maximum of displayTime. The
42 * only event that closes the notification between minShowTime and
43 * displayTime is a mouse button click (anywhere of the screen).
44 *
45 * When displayTime is not provided, it is calculated based on the length
46 * of the text.
47 *
48 * This class interface and its implementation are based on QT's
49 * QToolTip.
50 */
51 class NotificationBox
52 {
53 public:
54 NotificationBox() = delete;
55
56 enum class Options
57 {
58 None = 0x0,
59 RestrictAreaToReference = 0x1,
60 OnlyIfReferenceActive = 0x2,
61 HideIfReferenceWidgetDeactivated = 0x4,
62 };
63
64 /** Shows a non-intrusive notification.
65 * @param pos Position at which the notification will be shown
66 * @param text Message to be shown
67 * @param referenceWidget If provided, will set the reference to calculate a restrictionarea
68 * (see below options) and to prevent notifications for being shown if not active.
69 * @param displayTime Time after which the notification will auto-close (unless it is closed by
70 * an event, see class documentation above)
71 * @param minShowTime Time during which the notification can only be made disappear by popping
72 * it out (clicking inside it).
73 * @param options Different flag options:
74 * - HideIfReferenceWidgetDeactivated - Hides a notification if the main window becomes
75 * inactive.
76 * - RestrictAreaToReference - Try to keep the NotificationBox within the QRect of the
77 * referenceWidget. if false or referenceWidget is nullptr, the whole screen is used as
78 * restriction area.
79 * - OnlyIfReferenceActive - Show only if the reference window is active.
80 *
81 * @param width Fixes the width of the notification. Default value makes the width to be system
82 * determined (dependent on the text). If a fixed width is provided it is enforced over the
83 * restrictionarea.
84 *
85 * @return returns whether the notification was shown or not
86 */
87 static bool showText(
88 const QPoint& pos,
89 const QString& text,
90 QWidget* referenceWidget = nullptr,
91 int displayTime = -1,
92 unsigned int minShowTime = 0,
93 Options options = Options::None,
94 int width = 0
95 );
96 /// Hides a notification.
97 static inline void hideText()
98 {
99 showText(QPoint(), QString());
100 }
101 /// Returns whether a notification is being shown or not.
102 static bool isVisible();
103 /// Returns the text of the notification.
104 static QString text();
105 /// Returns the palette.
106 static QPalette palette();
107 /// Sets the palette.
108 static void setPalette(const QPalette&);
109 /// Returns the font of the notification.
110 static QFont font();
111 /// Sets the font to be used in the notification.
112 static void setFont(const QFont&);
113 };
114
115 inline NotificationBox::Options operator|(NotificationBox::Options lhs, NotificationBox::Options rhs)
116 {
117 return static_cast<NotificationBox::Options>(
118 static_cast<std::underlying_type<NotificationBox::Options>::type>(lhs)
119 | static_cast<std::underlying_type<NotificationBox::Options>::type>(rhs)
120 );
121 }
122
123 inline bool operator&(NotificationBox::Options lhs, NotificationBox::Options rhs)
124 {
125 return (
126 static_cast<std::underlying_type<NotificationBox::Options>::type>(lhs)
127 & static_cast<std::underlying_type<NotificationBox::Options>::type>(rhs)
128 );
129 }
130
131 } // namespace Gui
132
133 #endif // GUI_NOTIFICATIONBOX_H