NotificationBox: Enable options to not show if reference widget is not active and refactor restricttoarea

This commit is contained in:
Abdullah Tahiri
2023-03-21 08:31:05 +01:00
committed by abdullahtahiriyo
parent d9de68334b
commit f0883512d5
3 changed files with 44 additions and 14 deletions

View File

@@ -991,17 +991,13 @@ void NotificationArea::showInNotificationArea()
msgw += QString::fromLatin1("</table></p>");
// 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);
}
}

View File

@@ -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

View File

@@ -23,6 +23,7 @@
#ifndef GUI_NOTIFICATIONBOX_H
#define GUI_NOTIFICATIONBOX_H
#include <type_traits>
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<NotificationBox::Options>(
static_cast<std::underlying_type<NotificationBox::Options>::type>(lhs) |
static_cast<std::underlying_type<NotificationBox::Options>::type>(rhs));
}
}// namespace Gui