Mantid
Loading...
Searching...
No Matches
NotificationService.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10#include "MantidKernel/Timer.h"
11#include <QApplication>
12
14
15// Key for the "Notifications.Enabled" option
16const std::string NotificationService::NOTIFICATIONS_ENABLED_KEY = "Notifications.Enabled";
17// Minimum number os seconds between identical warnings
19
20// setup static variables
24
25void NotificationService::showMessage(const QString &title, const QString &message, MessageIcon icon,
26 int millisecondsTimeoutHint) {
27 if (isEnabled() && isSupportedByOS()) {
28 if ((g_lastMessage != message) || (g_lastTitle != title) ||
30 // remeber the last message details
31 g_lastMessage = message;
32 g_lastTitle = title;
33 g_timer.reset();
34
35 QSystemTrayIcon sysTrayIcon(qApp);
36 // get the window icon for the app
37 QIcon windowIcon = qApp->windowIcon();
38 // if no icon is set then use the mantid icon
39 if (windowIcon.isNull()) {
40 try {
41 windowIcon = QIcon(":/images/MantidIcon.ico");
42 } catch (const std::exception &) {
43 // if we cannot use the embedded icon, use a blank one
44 windowIcon = QIcon(QPixmap(32, 32));
45 }
46 }
47 // set this as the window icon otherwise you get a warning on the console
48 sysTrayIcon.setIcon(windowIcon);
49
50 sysTrayIcon.show();
51 sysTrayIcon.showMessage(title, message, icon, millisecondsTimeoutHint);
52
53 sysTrayIcon.hide();
54 }
55 }
56}
57
59 bool retVal = false;
60 try {
61 retVal = Mantid::Kernel::ConfigService::Instance().getValue<bool>(NOTIFICATIONS_ENABLED_KEY).get_value_or(true);
62 } catch (const Mantid::Kernel::Exception::FileError &) {
63 // The Config Service could not find the properties file
64 // Disable notifications
65 retVal = false;
66 } catch (const Poco::ExistsException &) {
67 // The Config Service could not find the properties file
68 // Disable notifications
69 retVal = false;
70 }
71 return retVal;
72}
73
74bool NotificationService::isSupportedByOS() { return QSystemTrayIcon::supportsMessages(); }
75
76} // namespace MantidQt::MantidWidgets
static void showMessage(const QString &title, const QString &message, MessageIcon icon=MessageIcon::Information, int millisecondsTimeoutHint=5000)
Display a notification.
static bool isEnabled()
Is the notification service enabled through the config service?
static bool isSupportedByOS()
Are notifications supported by this OS?
Records the filename and the description of failure.
Definition: Exception.h:98
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
A simple class that provides a wall-clock (not processor time) timer.
Definition: Timer.h:27
float elapsed_no_reset() const
Returns the wall-clock time elapsed in seconds since the Timer object's creation, or the last call to...
Definition: Timer.cpp:40
void reset()
Explicitly reset the timer.
Definition: Timer.cpp:48