Gui: Adapt Notifications Framework

This commit is contained in:
Abdullah Tahiri
2023-05-21 16:29:37 +02:00
committed by abdullahtahiriyo
parent 3097892d08
commit 3b04601ade

View File

@@ -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 <Base::LogStyle type, typename TNotifier, typename TCaption, typename TMessage>
template <Base::LogStyle, Base::IntendedRecipient = Base::IntendedRecipient::User,
Base::ContentType = Base::ContentType::Translated,
typename TNotifier, typename TCaption, typename TMessage>
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 <typename TNotifier, typename TCaption, typename TMessage>
inline void NotifyWarning(TNotifier && notifier, TCaption && caption, TMessage && message);
template <typename TNotifier, typename TCaption, typename TMessage>
inline void NotifyUserWarning(TNotifier && notifier, TCaption && caption, TMessage && message);
template <typename TNotifier, typename TCaption, typename TMessage>
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 <typename TNotifier, typename TCaption, typename TMessage>
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 <typename TNotifier, typename TCaption, typename TMessage>
inline void NotifyMessage(TNotifier && notifier, TCaption && caption, TMessage && message);
inline void NotifyUserError(TNotifier && notifier, TCaption && caption, TMessage && message);
template <typename TNotifier, typename TCaption, typename TMessage>
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 <Base::LogStyle type, typename TNotifier, typename TCaption, typename TMessage>
template <Base::LogStyle type, Base::IntendedRecipient recipient, Base::ContentType content, typename TNotifier, typename TCaption, typename TMessage>
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<ParameterGrp> 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<App::DocumentObject, std::remove_pointer_t<typename std::decay<TNotifier>::type>> ) {
Base::Console().Send<type,
Base::IntendedRecipient::Developer,
Base::ContentType::Untranslated>(notifier->getFullLabel(), msg.c_str());
}
else {
Base::Console().Send<type,
Base::IntendedRecipient::Developer,
Base::ContentType::Untranslated>(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<App::DocumentObject, std::remove_pointer_t<typename std::decay<TNotifier>::type>> ) {
Base::Console().Send<type>(notifier->getFullLabel(), msg.toUtf8());
Base::Console().Send<type, recipient, content>(notifier->getFullLabel(), msg.toUtf8());
}
else {
Base::Console().Send<type>(notifier, msg.toUtf8());
Base::Console().Send<type, recipient, content>(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<App::DocumentObject, std::remove_pointer_t<typename std::decay<TNotifier>::type>> ) {
Base::Console().Send<type>(notifier->getFullLabel(), msg.c_str());
Base::Console().Send<type, recipient, content>(notifier->getFullLabel(), msg.c_str());
}
else {
Base::Console().Send<type>(notifier, msg.c_str());
Base::Console().Send<type, recipient, content>(notifier, msg.c_str());
}
}
}
@@ -185,35 +246,75 @@ inline void Gui::NotifyWarning(TNotifier && notifier, TCaption && caption, TMess
}
template <typename TNotifier, typename TCaption, typename TMessage>
inline void Gui::NotifyError(TNotifier && notifier, TCaption && caption, TMessage && message)
inline void Gui::NotifyUserWarning(TNotifier && notifier, TCaption && caption, TMessage && message)
{
Notify<Base::LogStyle::Error>( std::forward<TNotifier>(notifier),
std::forward<TCaption>(caption),
std::forward<TMessage>(message));
Notify<Base::LogStyle::Warning,
Base::IntendedRecipient::User,
Base::ContentType::Untranslated>(std::forward<TNotifier>(notifier),
std::forward<TCaption>(caption),
std::forward<TMessage>(message));
}
template <typename TNotifier, typename TCaption, typename TMessage>
inline void Gui::NotifyMessage(TNotifier && notifier, TCaption && caption, TMessage && message)
inline void Gui::TranslatedUserWarning(TNotifier && notifier, TCaption && caption, TMessage && message)
{
Notify<Base::LogStyle::Message>(std::forward<TNotifier>(notifier),
std::forward<TCaption>(caption),
std::forward<TMessage>(message));
Notify<Base::LogStyle::Warning,
Base::IntendedRecipient::User,
Base::ContentType::Translated>(std::forward<TNotifier>(notifier),
std::forward<TCaption>(caption),
std::forward<TMessage>(message));
}
template <typename TNotifier, typename TCaption, typename TMessage>
inline void Gui::NotifyError(TNotifier && notifier, TCaption && caption, TMessage && message)
{
Notify<Base::LogStyle::Error,
Base::IntendedRecipient::All,
Base::ContentType::Untranslated>(std::forward<TNotifier>(notifier),
std::forward<TCaption>(caption),
std::forward<TMessage>(message));
}
template <typename TNotifier, typename TCaption, typename TMessage>
inline void Gui::NotifyUserError(TNotifier && notifier, TCaption && caption, TMessage && message)
{
Notify<Base::LogStyle::Error,
Base::IntendedRecipient::User,
Base::ContentType::Untranslated>(std::forward<TNotifier>(notifier),
std::forward<TCaption>(caption),
std::forward<TMessage>(message));
}
template <typename TNotifier, typename TCaption, typename TMessage>
inline void Gui::TranslatedUserError(TNotifier && notifier, TCaption && caption, TMessage && message)
{
Notify<Base::LogStyle::Error,
Base::IntendedRecipient::User,
Base::ContentType::Translated>(std::forward<TNotifier>(notifier),
std::forward<TCaption>(caption),
std::forward<TMessage>(message));
}
template <typename TNotifier, typename TCaption, typename TMessage>
inline void Gui::TranslatedNotification(TNotifier && notifier, TCaption && caption, TMessage && message)
{
Notify<Base::LogStyle::TranslatedNotification>(std::forward<TNotifier>(notifier),
std::forward<TCaption>(caption),
std::forward<TMessage>(message));
Notify<Base::LogStyle::Notification,
Base::IntendedRecipient::User,
Base::ContentType::Translated>(std::forward<TNotifier>(notifier),
std::forward<TCaption>(caption),
std::forward<TMessage>(message));
}
template <typename TNotifier, typename TCaption, typename TMessage>
inline void Gui::Notification(TNotifier && notifier, TCaption && caption, TMessage && message)
{
Notify<Base::LogStyle::Notification>(std::forward<TNotifier>(notifier),
std::forward<TCaption>(caption),
std::forward<TMessage>(message));
Notify<Base::LogStyle::Notification,
Base::IntendedRecipient::User,
Base::ContentType::Untranslated>(std::forward<TNotifier>(notifier),
std::forward<TCaption>(caption),
std::forward<TMessage>(message));
}
#endif // GUI_NOTIFICATIONS_H