| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/settings/settingsnotifications.h" |
| 4 | |
| 5 | #include "gui/notifications/notificationseditor.h" |
| 6 | #include "miscellaneous/application.h" |
| 7 | #include "miscellaneous/iconfactory.h" |
| 8 | #include "miscellaneous/notificationfactory.h" |
| 9 | #include "miscellaneous/settings.h" |
| 10 | |
| 11 | #include <QDir> |
| 12 | #include <QMetaEnum> |
| 13 | #include <QScreen> |
| 14 | |
| 15 | SettingsNotifications::SettingsNotifications(Settings* settings, QWidget* parent) : SettingsPanel(settings, parent) { |
| 16 | m_ui.setupUi(this); |
| 17 | |
| 18 | m_ui.m_lblInfo |
| 19 | ->setHelpText(tr("There are some built-in notification sounds. Just start typing \":\" and they will show up."), |
| 20 | true); |
| 21 | |
| 22 | m_ui.m_lblNativeNotif->setHelpText(tr("Note that native notifications might have some OS-dependent limitations. For " |
| 23 | "example Windows OS is known to limit the amount of notification originating " |
| 24 | "from each app during short span of time."), |
| 25 | true); |
| 26 | |
| 27 | connect(m_ui.m_checkEnableNotifications, &QCheckBox::toggled, this, &SettingsNotifications::dirtifySettings); |
| 28 | connect(m_ui.m_editor, &NotificationsEditor::someNotificationChanged, this, &SettingsNotifications::dirtifySettings); |
| 29 | |
| 30 | connect(m_ui.m_rbCustomNotifications, &QRadioButton::toggled, this, &SettingsNotifications::dirtifySettings); |
| 31 | connect(m_ui.m_rbCustomNotifications, &QRadioButton::toggled, this, &SettingsNotifications::requireRestart); |
| 32 | |
| 33 | connect(m_ui.m_rbNativeNotifications, &QRadioButton::toggled, this, &SettingsNotifications::dirtifySettings); |
| 34 | connect(m_ui.m_rbNativeNotifications, &QRadioButton::toggled, this, &SettingsNotifications::requireRestart); |
| 35 | |
| 36 | connect(m_ui.m_sbScreen, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsNotifications::dirtifySettings); |
| 37 | connect(m_ui.m_sbMargin, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsNotifications::dirtifySettings); |
| 38 | connect(m_ui.m_sbWidth, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsNotifications::dirtifySettings); |
| 39 | connect(m_ui.m_sbOpacity, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsNotifications::dirtifySettings); |
| 40 | |
| 41 | connect(m_ui.m_sbScreen, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsNotifications::showScreenInfo); |
| 42 | |
| 43 | connect(m_ui.m_cbCustomNotificationsPosition, |
| 44 | QOverload<int>::of(&QComboBox::currentIndexChanged), |
| 45 | this, |
| 46 | &SettingsNotifications::dirtifySettings); |
| 47 | } |
| 48 | |
| 49 | QIcon SettingsNotifications::icon() const { |
| 50 | return qApp->icons()->fromTheme(QSL("notifications"), QSL("dialog-information")); |
| 51 | } |
| 52 | |
| 53 | void SettingsNotifications::loadSettings() { |
| 54 | onBeginLoadSettings(); |
| 55 | |
| 56 | m_ui.m_sbScreen->setMinimum(-1); |
| 57 | m_ui.m_sbScreen->setMaximum(QGuiApplication::screens().size() - 1); |
| 58 | |
| 59 | QMetaEnum enm = QMetaEnum::fromType<ToastNotificationsManager::NotificationPosition>(); |
| 60 | |
| 61 | for (int i = 0; i < enm.keyCount(); i++) { |
| 62 | m_ui.m_cbCustomNotificationsPosition |
| 63 | ->addItem(ToastNotificationsManager:: |
| 64 | textForPosition(ToastNotificationsManager::NotificationPosition(enm.value(i))), |
| 65 | enm.value(i)); |
| 66 | } |
| 67 | |
| 68 | // Load fancy notification settings. |
| 69 | m_ui.m_checkEnableNotifications |
| 70 | ->setChecked(settings()->value(GROUP(GUI), SETTING(GUI::EnableNotifications)).toBool()); |
| 71 | m_ui.m_editor->loadNotifications(qApp->notifications()->allNotifications()); |
| 72 | |
| 73 | if (qApp->isWayland()) { |
| 74 | // Wayland does not support fancy notifications, only system ones. |
| 75 | m_ui.m_rbNativeNotifications->setChecked(true); |
| 76 | m_ui.m_rbCustomNotifications->setEnabled(false); |
| 77 | m_ui.m_rbCustomNotifications |
| 78 | ->setText(tr("%1 (not supported on Wayland)").arg(m_ui.m_rbCustomNotifications->text())); |
| 79 | } |
| 80 | else { |
| 81 | m_ui.m_rbNativeNotifications |
| 82 | ->setChecked(!settings()->value(GROUP(GUI), SETTING(GUI::UseToastNotifications)).toBool()); |
| 83 | } |
| 84 | |
| 85 | m_ui.m_sbScreen->setValue(settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsScreen)).toInt()); |
| 86 | m_ui.m_sbWidth->setValue(settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsWidth)).toInt()); |
| 87 | m_ui.m_sbMargin->setValue(settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsMargin)).toInt()); |
| 88 | m_ui.m_sbOpacity->setValue(settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsOpacity)).toDouble() * 100); |
| 89 | |
| 90 | m_ui.m_cbCustomNotificationsPosition |
| 91 | ->setCurrentIndex(m_ui.m_cbCustomNotificationsPosition |
| 92 | ->findData(settings() |
| 93 | ->value(GROUP(GUI), SETTING(GUI::ToastNotificationsPosition)) |
| 94 | .value<ToastNotificationsManager::NotificationPosition>())); |
| 95 | |
| 96 | onEndLoadSettings(); |
| 97 | } |
| 98 | |
| 99 | void SettingsNotifications::saveSettings() { |
| 100 | onBeginSaveSettings(); |
| 101 | |
| 102 | // Save notifications. |
| 103 | settings()->setValue(GROUP(GUI), GUI::EnableNotifications, m_ui.m_checkEnableNotifications->isChecked()); |
| 104 | qApp->notifications()->save(m_ui.m_editor->allNotifications(), settings()); |
| 105 | |
| 106 | settings()->setValue(GROUP(GUI), GUI::UseToastNotifications, m_ui.m_rbCustomNotifications->isChecked()); |
| 107 | settings()->setValue(GROUP(GUI), GUI::ToastNotificationsScreen, m_ui.m_sbScreen->value()); |
| 108 | settings()->setValue(GROUP(GUI), GUI::ToastNotificationsWidth, m_ui.m_sbWidth->value()); |
| 109 | settings()->setValue(GROUP(GUI), GUI::ToastNotificationsMargin, m_ui.m_sbMargin->value()); |
| 110 | settings()->setValue(GROUP(GUI), GUI::ToastNotificationsOpacity, m_ui.m_sbOpacity->value() / 100.0); |
| 111 | |
| 112 | settings()->setValue(GROUP(GUI), |
| 113 | GUI::ToastNotificationsPosition, |
| 114 | m_ui.m_cbCustomNotificationsPosition->currentData() |
| 115 | .value<ToastNotificationsManager::NotificationPosition>()); |
| 116 | |
| 117 | auto* toasts = qApp->toastNotifications(); |
| 118 | |
| 119 | if (toasts != nullptr) { |
| 120 | toasts->resetNotifications(true); |
| 121 | toasts->showNotification(Notification::Event::GeneralEvent, |
| 122 | GuiMessage(tr("How do I look?"), |
| 123 | tr("Just testing new notifications settings. " |
| 124 | "That's all."), |
| 125 | QSystemTrayIcon::MessageIcon::Warning)); |
| 126 | } |
| 127 | |
| 128 | onEndSaveSettings(); |
| 129 | } |
| 130 | |
| 131 | void SettingsNotifications::showScreenInfo(int index) { |
| 132 | QScreen* scr; |
| 133 | |
| 134 | if (index < 0 || index >= QGuiApplication::screens().size()) { |
| 135 | scr = QGuiApplication::primaryScreen(); |
| 136 | } |
| 137 | else { |
| 138 | scr = QGuiApplication::screens().at(index); |
| 139 | } |
| 140 | |
| 141 | m_ui.m_lblScreenInfo->setText(QSL("%1 (%2x%3)") |
| 142 | .arg(scr->name(), |
| 143 | QString::number(scr->virtualSize().width()), |
| 144 | QString::number(scr->virtualSize().height()))); |
| 145 | } |