From 3b04601ade86e3c5e224f577899cbb26f8d91b35 Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Sun, 21 May 2023 16:29:37 +0200 Subject: [PATCH] Gui: Adapt Notifications Framework --- src/Gui/Notifications.h | 197 ++++++++++++++++++++++++++++++---------- 1 file changed, 149 insertions(+), 48 deletions(-) diff --git a/src/Gui/Notifications.h b/src/Gui/Notifications.h index 8c55d514bf..b5fed13ead 100644 --- a/src/Gui/Notifications.h +++ b/src/Gui/Notifications.h @@ -49,11 +49,12 @@ namespace Gui { * Translations: * * An attempt is made by NotificationArea to translate the message using the "Notifications" context, - * except for TranslatedNotification. + * except for messages which are already translated. Those that are untranslatable can only be send + * as messages intended for a developer directly using Console.. * * For the former, this may be marked using QT_TRANSLATE_NOOP("Notifications","My message") * - * For TranslatedNotification, many modules using blocking notifications have their translations stored + * For translated Notification, many modules using blocking notifications have their translations stored * in other contexts, and the translations available at the callee function. This kind of notification * provides a very low entry point to move existing blocking notifications into non-intrusive respecting * the user choice (given by NotificationArea/NonIntrusiveNotificationsEnabled). @@ -69,6 +70,20 @@ namespace Gui { * QObject::tr("Wrong selection"), * QObject::tr("Cannot add a constraint between two external geometries.")); * + * or + * + * Gui::TranslatedUserWarning(obj, + * QObject::tr("Wrong selection"), + * QObject::tr("Cannot add a constraint between two external geometries.")); + * + * or + * + * Gui::TranslatedUserError(obj, + * QObject::tr("Wrong selection"), + * QObject::tr("Cannot add a constraint between two external geometries.")); * + * + * depending on the severity. + * * Here obj is a DocumentObject and serves to set the Notifier field for the notification. * If the user preference is to see non-intrusive notifications, no pop-up will be shown and the * notification will be shown in the notifications area. If the user preference is to see intrusive @@ -77,7 +92,9 @@ namespace Gui { */ ///generic function to send any message provided by Base::LogStyle -template +template inline void Notify(TNotifier && notifier, TCaption && caption, TMessage && message); /** Convenience function to notify warnings @@ -87,6 +104,12 @@ inline void Notify(TNotifier && notifier, TCaption && caption, TMessage && messa template inline void NotifyWarning(TNotifier && notifier, TCaption && caption, TMessage && message); +template +inline void NotifyUserWarning(TNotifier && notifier, TCaption && caption, TMessage && message); + +template +inline void TranslatedUserWarning(TNotifier && notifier, TCaption && caption, TMessage && message); + /** Convenience function to notify errors * The NotificationArea will attempt to find a translation in the "Notifications" context. * This may be marked using QT_TRANSLATE_NOOP("Notifications","My message") @@ -94,12 +117,11 @@ inline void NotifyWarning(TNotifier && notifier, TCaption && caption, TMessage & template inline void NotifyError(TNotifier && notifier, TCaption && caption, TMessage && message); -/** Convenience function to notify messages - * The NotificationArea will attempt to find a translation in the "Notifications" context. - * This may be marked using QT_TRANSLATE_NOOP("Notifications","My message") - */ template -inline void NotifyMessage(TNotifier && notifier, TCaption && caption, TMessage && message); +inline void NotifyUserError(TNotifier && notifier, TCaption && caption, TMessage && message); + +template +inline void TranslatedUserError(TNotifier && notifier, TCaption && caption, TMessage && message); /** Convenience function to send already translated user notifications. * No attempt will be made by the NotificationArea to translate them. @@ -116,9 +138,16 @@ inline void Notification(TNotifier && notifier, TCaption && caption, TMessage && } //namespace Gui -template +template inline void Gui::Notify(TNotifier && notifier, TCaption && caption, TMessage && message) { + static_assert(content != Base::ContentType::Untranslatable, + "A Gui notification cannot be provided with an untranslatable message."); + static_assert(recipient != Base::IntendedRecipient::Developer, + "A Gui notification cannot be intended only for the developer, use Console instead."); + static_assert(!(recipient == Base::IntendedRecipient::All && content == Base::ContentType::Translated), + "Information intended for Developers must not be translated. Provide an untranslated message instead."); + Base::Reference hGrp = App::GetApplication().GetUserParameter(). GetGroup("BaseApp")->GetGroup("Preferences")-> GetGroup("NotificationArea"); @@ -126,40 +155,72 @@ inline void Gui::Notify(TNotifier && notifier, TCaption && caption, TMessage && bool nonIntrusive = hGrp->GetBool("NonIntrusiveNotificationsEnabled", true); if(!nonIntrusive) { - if constexpr( type == Base::LogStyle::Warning) { - QMessageBox::warning(Gui::getMainWindow(), - QCoreApplication::translate("Notifications", caption), - QCoreApplication::translate("Notifications", message)); + + // If Developers are also an intended recipient, notify before creating the blocking pop-up + if constexpr(recipient == Base::IntendedRecipient::All) { + // Send also to log for developer only + auto msg = std::string(message).append("\n"); // use untranslated message + + if constexpr( std::is_base_of_v::type>> ) { + Base::Console().Send(notifier->getFullLabel(), msg.c_str()); + } + else { + Base::Console().Send(notifier, msg.c_str()); + } } - else - if constexpr( type == Base::LogStyle::Error) { - QMessageBox::critical(Gui::getMainWindow(), - QCoreApplication::translate("Notifications", caption), - QCoreApplication::translate("Notifications", message)); - } - else - if constexpr( type == Base::LogStyle::TranslatedNotification) { - QMessageBox::information(Gui::getMainWindow(), - caption, - message); + + if constexpr(content == Base::ContentType::Untranslated) { + if constexpr( type == Base::LogStyle::Warning) { + QMessageBox::warning(Gui::getMainWindow(), + QCoreApplication::translate("Notifications", caption), + QCoreApplication::translate("Notifications", message)); + } + else + if constexpr( type == Base::LogStyle::Error) { + QMessageBox::critical(Gui::getMainWindow(), + QCoreApplication::translate("Notifications", caption), + QCoreApplication::translate("Notifications", message)); + } + else { + QMessageBox::information(Gui::getMainWindow(), + QCoreApplication::translate("Notifications", caption), + QCoreApplication::translate("Notifications", message)); + } } else { - QMessageBox::information(Gui::getMainWindow(), - QCoreApplication::translate("Notifications", caption), - QCoreApplication::translate("Notifications", message)); + if constexpr( type == Base::LogStyle::Warning) { + QMessageBox::warning(Gui::getMainWindow(), + caption, + message); + } + else + if constexpr( type == Base::LogStyle::Error) { + QMessageBox::critical(Gui::getMainWindow(), + caption, + message); + } + else { + QMessageBox::information(Gui::getMainWindow(), + caption, + message); + } } } else { - if constexpr( type == Base::LogStyle::TranslatedNotification) { - // trailing newline is necessary as this may be shown too in a console requiring them (depending on the configuration). - auto msg = QStringLiteral("%1. %2\n").arg(caption).arg(message); // QString + if constexpr( content == Base::ContentType::Translated) { + // trailing newline is not necessary as translated messages are not shown in logs + auto msg = QStringLiteral("%1. %2").arg(caption).arg(message); // QString if constexpr( std::is_base_of_v::type>> ) { - Base::Console().Send(notifier->getFullLabel(), msg.toUtf8()); + Base::Console().Send(notifier->getFullLabel(), msg.toUtf8()); } else { - Base::Console().Send(notifier, msg.toUtf8()); + Base::Console().Send(notifier, msg.toUtf8()); } } else { @@ -167,10 +228,10 @@ inline void Gui::Notify(TNotifier && notifier, TCaption && caption, TMessage && auto msg = std::string(message).append("\n"); if constexpr( std::is_base_of_v::type>> ) { - Base::Console().Send(notifier->getFullLabel(), msg.c_str()); + Base::Console().Send(notifier->getFullLabel(), msg.c_str()); } else { - Base::Console().Send(notifier, msg.c_str()); + Base::Console().Send(notifier, msg.c_str()); } } } @@ -185,35 +246,75 @@ inline void Gui::NotifyWarning(TNotifier && notifier, TCaption && caption, TMess } template -inline void Gui::NotifyError(TNotifier && notifier, TCaption && caption, TMessage && message) +inline void Gui::NotifyUserWarning(TNotifier && notifier, TCaption && caption, TMessage && message) { - Notify( std::forward(notifier), - std::forward(caption), - std::forward(message)); + Notify(std::forward(notifier), + std::forward(caption), + std::forward(message)); } template -inline void Gui::NotifyMessage(TNotifier && notifier, TCaption && caption, TMessage && message) +inline void Gui::TranslatedUserWarning(TNotifier && notifier, TCaption && caption, TMessage && message) { - Notify(std::forward(notifier), - std::forward(caption), - std::forward(message)); + Notify(std::forward(notifier), + std::forward(caption), + std::forward(message)); } + +template +inline void Gui::NotifyError(TNotifier && notifier, TCaption && caption, TMessage && message) +{ + Notify(std::forward(notifier), + std::forward(caption), + std::forward(message)); +} + +template +inline void Gui::NotifyUserError(TNotifier && notifier, TCaption && caption, TMessage && message) +{ + Notify(std::forward(notifier), + std::forward(caption), + std::forward(message)); +} + +template +inline void Gui::TranslatedUserError(TNotifier && notifier, TCaption && caption, TMessage && message) +{ + Notify(std::forward(notifier), + std::forward(caption), + std::forward(message)); +} + + template inline void Gui::TranslatedNotification(TNotifier && notifier, TCaption && caption, TMessage && message) { - Notify(std::forward(notifier), - std::forward(caption), - std::forward(message)); + Notify(std::forward(notifier), + std::forward(caption), + std::forward(message)); } template inline void Gui::Notification(TNotifier && notifier, TCaption && caption, TMessage && message) { - Notify(std::forward(notifier), - std::forward(caption), - std::forward(message)); + Notify(std::forward(notifier), + std::forward(caption), + std::forward(message)); } #endif // GUI_NOTIFICATIONS_H