Debian Codegraph @

src/Gui/Notifications.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_NOTIFICATIONS_H
24 #define GUI_NOTIFICATIONS_H
25
26 #include <QMessageBox>
27 #include <QCoreApplication>
28
29 #include <Base/Console.h>
30 #include <App/Application.h>
31 #include <App/DocumentObject.h>
32 #include <Gui/MainWindow.h>
33
34 namespace Gui
35 {
36
37 class ViewProviderDocumentObject;
38
39 /** Methods to seamlessly provide intrusive or non-intrusive notifications of error, warning,
40 * messages, translated notifications, or untranslated notifications originating in a given
41 * document object.
42 *
43 * They are intended for easy substitution of currently blocking modal dialogs in which the user
44 * may only click 'ok'.
45 *
46 * It produces a blocking modal notification or a non-intrusive non-modal notification depending on
47 * the preference parameter NotificationArea/NonIntrusiveNotificationsEnabled.
48 *
49 * The notifier field can be a string or an App::DocumentObject * object, then the notifier is taken
50 * from the getFullLabel() method of the DocumentObject.
51 *
52 * Translations:
53 *
54 * An attempt is made by NotificationArea to translate the message using the "Notifications"
55 * context, except for messages which are already translated. Those that are untranslatable can only
56 * be send as messages intended for a developer directly using Console..
57 *
58 * For the former, this may be marked using QT_TRANSLATE_NOOP("Notifications","My message")
59 *
60 * For translated Notification, many modules using blocking notifications have their translations
61 * stored in other contexts, and the translations available at the callee function. This kind of
62 * notification provides a very low entry point to move existing blocking notifications into
63 * non-intrusive respecting the user choice (given by
64 * NotificationArea/NonIntrusiveNotificationsEnabled).
65 *
66 * Example 1:
67 *
68 * QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
69 * QObject::tr("Cannot add a constraint between two external geometries."));
70 *
71 * Can be rewritten as:
72 *
73 * Gui::TranslatedNotification(obj,
74 * QObject::tr("Wrong selection"),
75 * QObject::tr("Cannot add a constraint between two external geometries."));
76 *
77 * or
78 *
79 * Gui::TranslatedUserWarning(obj,
80 * QObject::tr("Wrong selection"),
81 * QObject::tr("Cannot add a constraint between two external geometries."));
82 *
83 * or
84 *
85 * Gui::TranslatedUserError(obj,
86 * QObject::tr("Wrong selection"),
87 * QObject::tr("Cannot add a constraint between two external geometries.")); *
88 *
89 * depending on the severity.
90 *
91 * Here obj is a DocumentObject and serves to set the Notifier field for the notification.
92 * If the user preference is to see non-intrusive notifications, no pop-up will be shown and the
93 * notification will be shown in the notifications area. If the user preference is to see intrusive
94 * notifications, the pop-up will be shown (and it won't appear on the notification area as as a
95 * non- intrusive notification).
96 */
97
98 /// generic function to send any message provided by Base::LogStyle
99 template<
100 Base::LogStyle,
101 Base::IntendedRecipient = Base::IntendedRecipient::User,
102 Base::ContentType = Base::ContentType::Translated,
103 typename TNotifier,
104 typename TCaption,
105 typename TMessage>
106 inline void Notify(TNotifier&& notifier, TCaption&& caption, TMessage&& message);
107
108 /** Convenience function to notify warnings
109 * The NotificationArea will attempt to find a translation in the "Notifications" context.
110 * This may be marked using QT_TRANSLATE_NOOP("Notifications","My message")
111 */
112 template<typename TNotifier, typename TCaption, typename TMessage>
113 inline void NotifyWarning(TNotifier&& notifier, TCaption&& caption, TMessage&& message);
114
115 template<typename TNotifier, typename TCaption, typename TMessage>
116 inline void NotifyUserWarning(TNotifier&& notifier, TCaption&& caption, TMessage&& message);
117
118 template<typename TNotifier, typename TCaption, typename TMessage>
119 inline void TranslatedUserWarning(TNotifier&& notifier, TCaption&& caption, TMessage&& message);
120
121 /** Convenience function to notify errors
122 * The NotificationArea will attempt to find a translation in the "Notifications" context.
123 * This may be marked using QT_TRANSLATE_NOOP("Notifications","My message")
124 */
125 template<typename TNotifier, typename TCaption, typename TMessage>
126 inline void NotifyError(TNotifier&& notifier, TCaption&& caption, TMessage&& message);
127
128 template<typename TNotifier, typename TCaption, typename TMessage>
129 inline void NotifyUserError(TNotifier&& notifier, TCaption&& caption, TMessage&& message);
130
131 template<typename TNotifier, typename TCaption, typename TMessage>
132 inline void TranslatedUserError(TNotifier&& notifier, TCaption&& caption, TMessage&& message);
133
134 /** Convenience function to send already translated user notifications.
135 * No attempt will be made by the NotificationArea to translate them.
136 */
137 template<typename TNotifier, typename TCaption, typename TMessage>
138 inline void TranslatedNotification(TNotifier&& notifier, TCaption&& caption, TMessage&& message);
139
140 /** Convenience function to send untranslated user notifications.
141 * The NotificationArea will attempt to find a translation in the "Notifications" context.
142 * This may be marked using QT_TRANSLATE_NOOP("Notifications","My message")
143 */
144 template<typename TNotifier, typename TCaption, typename TMessage>
145 inline void Notification(TNotifier&& notifier, TCaption&& caption, TMessage&& message);
146 } // namespace Gui
147
148
149 template<
150 Base::LogStyle type,
151 Base::IntendedRecipient recipient,
152 Base::ContentType content,
153 typename TNotifier,
154 typename TCaption,
155 typename TMessage>
156 inline void Gui::Notify(TNotifier&& notifier, TCaption&& caption, TMessage&& message)
157 {
158 static_assert(
159 content != Base::ContentType::Untranslatable,
160 "A Gui notification cannot be provided with an untranslatable message."
161 );
162 static_assert(
163 recipient != Base::IntendedRecipient::Developer,
164 "A Gui notification cannot be intended only for the developer, use Console instead."
165 );
166 static_assert(
167 !(recipient == Base::IntendedRecipient::All && content == Base::ContentType::Translated),
168 "Information intended for Developers must not be translated. Provide an untranslated "
169 "message instead."
170 );
171
172 Base::Reference<ParameterGrp> hGrp = App::GetApplication()
173 .GetUserParameter()
174 .GetGroup("BaseApp")
175 ->GetGroup("Preferences")
176 ->GetGroup("NotificationArea");
177
178 bool nonIntrusive = hGrp->GetBool("NonIntrusiveNotificationsEnabled", true);
179
180 if (!nonIntrusive) {
181
182 // If Developers are also an intended recipient, notify before creating the blocking pop-up
183 if constexpr (recipient == Base::IntendedRecipient::All) {
184 // Send also to log for developer only
185 auto msg = std::string(message).append("\n"); // use untranslated message
186
187 if constexpr (std::is_base_of_v<
188 App::DocumentObject,
189 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
190 Base::Console()
191 .send<type, Base::IntendedRecipient::Developer, Base::ContentType::Untranslated>(
192 notifier->getFullLabel(),
193 msg.c_str()
194 );
195 }
196 else if constexpr (std::is_base_of_v<
197 Gui::ViewProviderDocumentObject,
198 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
199 Base::Console()
200 .send<type, Base::IntendedRecipient::Developer, Base::ContentType::Untranslated>(
201 notifier->getObject()->getFullLabel(),
202 msg.c_str()
203 );
204 }
205 else if constexpr (std::is_base_of_v<
206 Gui::Document,
207 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
208 Base::Console()
209 .send<type, Base::IntendedRecipient::Developer, Base::ContentType::Untranslated>(
210 notifier->getDocument()->Label.getStrValue(),
211 msg.c_str()
212 );
213 }
214 else if constexpr (std::is_base_of_v<
215 App::Document,
216 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
217 Base::Console()
218 .send<type, Base::IntendedRecipient::Developer, Base::ContentType::Untranslated>(
219 notifier->Label.getStrValue(),
220 msg.c_str()
221 );
222 }
223 else {
224 Base::Console()
225 .send<type, Base::IntendedRecipient::Developer, Base::ContentType::Untranslated>(
226 notifier,
227 msg.c_str()
228 );
229 }
230 }
231
232 if constexpr (content == Base::ContentType::Untranslated) {
233 if constexpr (type == Base::LogStyle::Warning) {
234 QMessageBox::warning(
235 Gui::getMainWindow(),
236 QCoreApplication::translate("Notifications", caption),
237 QCoreApplication::translate("Notifications", message)
238 );
239 }
240 else if constexpr (type == Base::LogStyle::Error) {
241 QMessageBox::critical(
242 Gui::getMainWindow(),
243 QCoreApplication::translate("Notifications", caption),
244 QCoreApplication::translate("Notifications", message)
245 );
246 }
247 else {
248 QMessageBox::information(
249 Gui::getMainWindow(),
250 QCoreApplication::translate("Notifications", caption),
251 QCoreApplication::translate("Notifications", message)
252 );
253 }
254 }
255 else {
256 if constexpr (type == Base::LogStyle::Warning) {
257 QMessageBox::warning(Gui::getMainWindow(), caption, message);
258 }
259 else if constexpr (type == Base::LogStyle::Error) {
260 QMessageBox::critical(Gui::getMainWindow(), caption, message);
261 }
262 else {
263 QMessageBox::information(Gui::getMainWindow(), caption, message);
264 }
265 }
266 }
267 else {
268
269 if constexpr (content == Base::ContentType::Translated) {
270 // trailing newline is not necessary as translated messages are not shown in logs
271 auto msg = QStringLiteral("%1. %2").arg(caption).arg(message); // QString
272
273 if constexpr (std::is_base_of_v<
274 App::DocumentObject,
275 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
276 Base::Console().send<type, recipient, content>(notifier->getFullLabel(), msg.toUtf8());
277 }
278 else if constexpr (std::is_base_of_v<
279 Gui::ViewProviderDocumentObject,
280 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
281 Base::Console().send<type, recipient, content>(
282 notifier->getObject()->getFullLabel(),
283 msg.toUtf8()
284 );
285 }
286 else if constexpr (std::is_base_of_v<
287 Gui::Document,
288 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
289 Base::Console().send<type, recipient, content>(
290 notifier->getDocument()->Label.getStrValue(),
291 msg.toUtf8()
292 );
293 }
294 else if constexpr (std::is_base_of_v<
295 App::Document,
296 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
297 Base::Console().send<type, recipient, content>(
298 notifier->Label.getStrValue(),
299 msg.toUtf8()
300 );
301 }
302 else {
303 Base::Console().send<type, recipient, content>(notifier, msg.toUtf8());
304 }
305 }
306 else {
307 // trailing newline is necessary as this may be shown too in a console requiring them
308 // (depending on the configuration).
309 auto msg = std::string(message).append("\n");
310
311 if constexpr (std::is_base_of_v<
312 App::DocumentObject,
313 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
314 Base::Console().send<type, recipient, content>(notifier->getFullLabel(), msg.c_str());
315 }
316 else if constexpr (std::is_base_of_v<
317 Gui::ViewProviderDocumentObject,
318 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
319 Base::Console().send<type, recipient, content>(
320 notifier->getObject()->getFullLabel(),
321 msg.c_str()
322 );
323 }
324 else if constexpr (std::is_base_of_v<
325 Gui::Document,
326 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
327 Base::Console().send<type, recipient, content>(
328 notifier->getDocument()->Label.getStrValue(),
329 msg.c_str()
330 );
331 }
332 else if constexpr (std::is_base_of_v<
333 App::Document,
334 std::remove_pointer_t<typename std::decay<TNotifier>::type>>) {
335 Base::Console().send<type, recipient, content>(
336 notifier->Label.getStrValue(),
337 msg.c_str()
338 );
339 }
340 else {
341 Base::Console().send<type, recipient, content>(notifier, msg.c_str());
342 }
343 }
344 }
345 }
346
347 template<typename TNotifier, typename TCaption, typename TMessage>
348 inline void Gui::NotifyWarning(TNotifier&& notifier, TCaption&& caption, TMessage&& message)
349 {
350 Notify<Base::LogStyle::Warning>(
351 std::forward<TNotifier>(notifier),
352 std::forward<TCaption>(caption),
353 std::forward<TMessage>(message)
354 );
355 }
356
357 template<typename TNotifier, typename TCaption, typename TMessage>
358 inline void Gui::NotifyUserWarning(TNotifier&& notifier, TCaption&& caption, TMessage&& message)
359 {
360 Notify<Base::LogStyle::Warning, Base::IntendedRecipient::User, Base::ContentType::Untranslated>(
361 std::forward<TNotifier>(notifier),
362 std::forward<TCaption>(caption),
363 std::forward<TMessage>(message)
364 );
365 }
366
367 template<typename TNotifier, typename TCaption, typename TMessage>
368 inline void Gui::TranslatedUserWarning(TNotifier&& notifier, TCaption&& caption, TMessage&& message)
369 {
370 Notify<Base::LogStyle::Warning, Base::IntendedRecipient::User, Base::ContentType::Translated>(
371 std::forward<TNotifier>(notifier),
372 std::forward<TCaption>(caption),
373 std::forward<TMessage>(message)
374 );
375 }
376
377
378 template<typename TNotifier, typename TCaption, typename TMessage>
379 inline void Gui::NotifyError(TNotifier&& notifier, TCaption&& caption, TMessage&& message)
380 {
381 Notify<Base::LogStyle::Error, Base::IntendedRecipient::All, Base::ContentType::Untranslated>(
382 std::forward<TNotifier>(notifier),
383 std::forward<TCaption>(caption),
384 std::forward<TMessage>(message)
385 );
386 }
387
388 template<typename TNotifier, typename TCaption, typename TMessage>
389 inline void Gui::NotifyUserError(TNotifier&& notifier, TCaption&& caption, TMessage&& message)
390 {
391 Notify<Base::LogStyle::Error, Base::IntendedRecipient::User, Base::ContentType::Untranslated>(
392 std::forward<TNotifier>(notifier),
393 std::forward<TCaption>(caption),
394 std::forward<TMessage>(message)
395 );
396 }
397
398 template<typename TNotifier, typename TCaption, typename TMessage>
399 inline void Gui::TranslatedUserError(TNotifier&& notifier, TCaption&& caption, TMessage&& message)
400 {
401 Notify<Base::LogStyle::Error, Base::IntendedRecipient::User, Base::ContentType::Translated>(
402 std::forward<TNotifier>(notifier),
403 std::forward<TCaption>(caption),
404 std::forward<TMessage>(message)
405 );
406 }
407
408
409 template<typename TNotifier, typename TCaption, typename TMessage>
410 inline void Gui::TranslatedNotification(TNotifier&& notifier, TCaption&& caption, TMessage&& message)
411 {
412 Notify<Base::LogStyle::Notification, Base::IntendedRecipient::User, Base::ContentType::Translated>(
413 std::forward<TNotifier>(notifier),
414 std::forward<TCaption>(caption),
415 std::forward<TMessage>(message)
416 );
417 }
418
419 template<typename TNotifier, typename TCaption, typename TMessage>
420 inline void Gui::Notification(TNotifier&& notifier, TCaption&& caption, TMessage&& message)
421 {
422 Notify<Base::LogStyle::Notification, Base::IntendedRecipient::User, Base::ContentType::Untranslated>(
423 std::forward<TNotifier>(notifier),
424 std::forward<TCaption>(caption),
425 std::forward<TMessage>(message)
426 );
427 }
428
429 #endif // GUI_NOTIFICATIONS_H