| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/notifications/toastnotification.h" |
| 4 | |
| 5 | #include "miscellaneous/iconfactory.h" |
| 6 | |
| 7 | #ifdef Q_OS_MAC |
| 8 | #include <Carbon/Carbon.h> |
| 9 | #endif |
| 10 | |
| 11 | ToastNotification::ToastNotification(Notification::Event event, |
| 12 | const GuiMessage& msg, |
| 13 | const GuiAction& action, |
| 14 | QWidget* parent) |
| 15 | : BaseToastNotification(parent) { |
| 16 | m_ui.setupUi(this); |
| 17 | |
| 18 | setupHeading(m_ui.m_lblTitle); |
| 19 | setupCloseButton(m_ui.m_btnClose); |
| 20 | setupTimedClosing(false); |
| 21 | |
| 22 | loadNotification(event, msg, action); |
| 23 | } |
| 24 | |
| 25 | void ToastNotification::loadNotification(Notification::Event event, const GuiMessage& msg, const GuiAction& action) { |
| 26 | m_ui.m_lblTitle->setText(msg.m_title); |
| 27 | m_ui.m_lblTitle->setToolTip(msg.m_title); |
| 28 | m_ui.m_lblBody->setText(msg.m_message); |
| 29 | m_ui.m_lblBody->setToolTip(msg.m_message); |
| 30 | |
| 31 | m_ui.m_lblIcon->setPixmap(iconForType(msg.m_type) |
| 32 | .pixmap({ |
| 33 | 32, |
| 34 | 32, |
| 35 | })); |
| 36 | |
| 37 | if (action.m_action) { |
| 38 | m_ui.m_btnAction->setText(action.m_title.isEmpty() ? tr("Do it!") : action.m_title); |
| 39 | connect(m_ui.m_btnAction, &QPushButton::clicked, this, [this, action]() { |
| 40 | action.m_action(); |
| 41 | emit closeRequested(this); |
| 42 | }); |
| 43 | } |
| 44 | else { |
| 45 | m_ui.m_mainLayout->removeItem(m_ui.m_actionLayout); |
| 46 | m_ui.m_mainLayout->update(); |
| 47 | m_ui.m_btnAction->deleteLater(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | QIcon ToastNotification::iconForType(QSystemTrayIcon::MessageIcon icon) const { |
| 52 | switch (icon) { |
| 53 | case QSystemTrayIcon::Warning: |
| 54 | return qApp->icons()->fromTheme(QSL("dialog-warning")); |
| 55 | |
| 56 | case QSystemTrayIcon::Critical: |
| 57 | return qApp->icons()->fromTheme(QSL("dialog-error")); |
| 58 | |
| 59 | case QSystemTrayIcon::Information: |
| 60 | case QSystemTrayIcon::NoIcon: |
| 61 | default: |
| 62 | return qApp->icons()->fromTheme(QSL("dialog-information")); |
| 63 | } |
| 64 | } |