From 2f8be2c77bf36657d8d73c167465adb1343a1893 Mon Sep 17 00:00:00 2001
From: Abdullah Tahiri
Date: Tue, 21 Mar 2023 08:31:05 +0100
Subject: [PATCH] NotificationBox: Enable options to not show if reference
widget is not active and refactor restricttoarea
---
src/Gui/NotificationArea.cpp | 10 +++-------
src/Gui/NotificationBox.cpp | 23 +++++++++++++++++++++--
src/Gui/NotificationBox.h | 25 ++++++++++++++++++++-----
3 files changed, 44 insertions(+), 14 deletions(-)
diff --git a/src/Gui/NotificationArea.cpp b/src/Gui/NotificationArea.cpp
index 8e81a502cc..6e4d6fd8c8 100644
--- a/src/Gui/NotificationArea.cpp
+++ b/src/Gui/NotificationArea.cpp
@@ -991,17 +991,13 @@ void NotificationArea::showInNotificationArea()
msgw += QString::fromLatin1("
");
- // Calculate the main window QRect in global screen coordinates.
- auto mainwindow = getMainWindow();
- auto mainwindowrect = mainwindow->rect();
- auto globalmainwindowrect =
- QRect(mainwindow->mapToGlobal(mainwindowrect.topLeft()), mainwindowrect.size());
-
NotificationBox::showText(this->mapToGlobal(QPoint()),
msgw,
+ getMainWindow(),
pImp->notificationExpirationTime,
pImp->minimumOnScreenTime,
- globalmainwindowrect,
+ NotificationBox::OnlyIfReferenceActive |
+ NotificationBox::RestrictAreaToReference,
pImp->notificationWidth);
}
}
diff --git a/src/Gui/NotificationBox.cpp b/src/Gui/NotificationBox.cpp
index 3699a37593..ff07a9646a 100644
--- a/src/Gui/NotificationBox.cpp
+++ b/src/Gui/NotificationBox.cpp
@@ -309,9 +309,28 @@ bool NotificationLabel::notificationLabelChanged(const QString& text)
/***************************** NotificationBox **********************************/
-void NotificationBox::showText(const QPoint& pos, const QString& text, int displayTime,
- unsigned int minShowTime, const QRect &restrictionarea, int width)
+void NotificationBox::showText(const QPoint& pos, const QString& text, QWidget * referenceWidget, int displayTime,
+ unsigned int minShowTime, Options options,
+ int width)
{
+ QRect restrictionarea = {};
+
+ if(referenceWidget) {
+ if(options & Options::OnlyIfReferenceActive) {
+ if (!referenceWidget->isActiveWindow()) {
+ return;
+ }
+ }
+
+ if(options & Options::RestrictAreaToReference) {
+ // Calculate the main window QRect in global screen coordinates.
+ auto mainwindowrect = referenceWidget->rect();
+
+ restrictionarea =
+ QRect(referenceWidget->mapToGlobal(mainwindowrect.topLeft()), mainwindowrect.size());
+ }
+ }
+
// a label does already exist
if (NotificationLabel::instance && NotificationLabel::instance->isVisible()) {
if (text.isEmpty()) {// empty text means hide current label
diff --git a/src/Gui/NotificationBox.h b/src/Gui/NotificationBox.h
index 4ddcd93083..dca3cd9bf6 100644
--- a/src/Gui/NotificationBox.h
+++ b/src/Gui/NotificationBox.h
@@ -23,6 +23,7 @@
#ifndef GUI_NOTIFICATIONBOX_H
#define GUI_NOTIFICATIONBOX_H
+#include
namespace Gui
{
@@ -48,21 +49,30 @@ class NotificationBox
NotificationBox() = delete;
public:
+ enum Options {
+ None = 0x0,
+ RestrictAreaToReference = 0x1,
+ OnlyIfReferenceActive = 0x2,
+ };
+
/** Shows a non-intrusive notification.
* @param pos Position at which the notification will be shown
+ * @param text Message to be shown
+ * @param referenceWidget If provided, will set the reference to calculate a restrictionarea (see below restrictAreaToReference) and
+ * to prevent notifications for being shown if not active.
* @param displayTime Time after which the notification will auto-close (unless it is closed by
* an event, see class documentation above)
* @param minShowTime Time during which the notification can only be made disappear by popping
* it out (clicking inside it).
- * @param restrictionarea Try to keep the NotificationBox within this area. If this area is not
- * provided, the whole screen is used as restriction area. This are must be provided in global
- * screen coordinates.
+ * @param restrictAreaToReference Try to keep the NotificationBox within the QRect of the referenceWidget. if false or referenceWidget is
+ * nullptr, the whole screen is used as restriction area.
+ * @param onlyIfReferenceActive Show only if the reference window is active.
* @param width Fixes the width of the notification. Default value makes the width to be system
* determined (dependent on the text). If a fixed width is provided it is enforced over the
* restrictionarea.
*/
- static void showText(const QPoint& pos, const QString& text, int displayTime = -1,
- unsigned int minShowTime = 0, const QRect& restrictionarea = {},
+ static void showText(const QPoint& pos, const QString& text, QWidget * referenceWidget = nullptr, int displayTime = -1,
+ unsigned int minShowTime = 0, Options options = Options::None,
int width = 0);
/// Hides a notification.
static inline void hideText()
@@ -83,6 +93,11 @@ public:
static void setFont(const QFont&);
};
+inline NotificationBox::Options operator|(NotificationBox::Options lhs, NotificationBox::Options rhs) {
+ return static_cast(
+ static_cast::type>(lhs) |
+ static_cast::type>(rhs));
+}
}// namespace Gui