From 55779221171d8a4ff4c961ab67d94fa678da6103 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sat, 6 May 2023 13:34:38 +0300 Subject: [PATCH] UI: Options: Tray: Add "Show Icon" flag --- src/ui/form/home/homewindow.cpp | 25 +++++++++++++++++++ src/ui/form/home/homewindow.h | 3 +++ src/ui/form/opt/pages/optionspage.cpp | 7 ++++++ src/ui/form/opt/pages/optionspage.h | 1 + src/ui/form/tray/trayicon.cpp | 20 +++++++-------- src/ui/form/tray/trayicon.h | 5 +++- src/ui/fortmanager.cpp | 11 +++++++-- src/ui/manager/windowmanager.cpp | 35 +++++++++++++++------------ src/ui/manager/windowmanager.h | 3 +-- src/ui/user/iniuser.h | 3 +++ 10 files changed, 83 insertions(+), 30 deletions(-) diff --git a/src/ui/form/home/homewindow.cpp b/src/ui/form/home/homewindow.cpp index ff4f5449..573756e0 100644 --- a/src/ui/form/home/homewindow.cpp +++ b/src/ui/form/home/homewindow.cpp @@ -1,9 +1,11 @@ #include "homewindow.h" +#include #include #include #include
+#include #include #include #include @@ -56,6 +58,8 @@ void HomeWindow::retranslateUi() { this->unsetLocale(); + m_btMenu->setText(tr("Menu")); + this->setWindowTitle(tr("My Fort")); } @@ -76,6 +80,14 @@ void HomeWindow::setupUi() auto layout = new QVBoxLayout(); layout->setContentsMargins(6, 6, 6, 6); + // Header + auto header = setupHeader(); + layout->addLayout(header); + + // Frame + auto frame = new QHBoxLayout(); + layout->addLayout(frame, 1); + this->setLayout(layout); // Font @@ -84,3 +96,16 @@ void HomeWindow::setupUi() // Size this->setMinimumSize(500, 400); } + +QLayout *HomeWindow::setupHeader() +{ + auto layout = new QHBoxLayout(); + + m_btMenu = ControlUtil::createButton(":/icons/sheild-96.png"); + m_btMenu->setMenu(windowManager()->trayIcon()->menu()); + + layout->addWidget(m_btMenu); + layout->addStretch(); + + return layout; +} diff --git a/src/ui/form/home/homewindow.h b/src/ui/form/home/homewindow.h index b9121c07..8bbaed60 100644 --- a/src/ui/form/home/homewindow.h +++ b/src/ui/form/home/homewindow.h @@ -33,10 +33,13 @@ private: void retranslateUi(); void setupUi(); + QLayout *setupHeader(); private: HomeController *m_ctrl = nullptr; WidgetWindowStateWatcher *m_stateWatcher = nullptr; + + QPushButton *m_btMenu = nullptr; }; #endif // HOMEWINDOW_H diff --git a/src/ui/form/opt/pages/optionspage.cpp b/src/ui/form/opt/pages/optionspage.cpp index 86c17afb..17d537d9 100644 --- a/src/ui/form/opt/pages/optionspage.cpp +++ b/src/ui/form/opt/pages/optionspage.cpp @@ -189,6 +189,7 @@ void OptionsPage::onRetranslateUi() tr("Lock the password (unlocked till \"%1\")") .arg(PasswordDialog::unlockTypeStrings().at(settings()->passwordUnlockType()))); + m_cbTrayShowIcon->setText(tr("Show Icon")); m_cbTrayAnimateAlert->setText(tr("Animate Alert Icon")); m_labelTrayEvent->setText(tr("Event:")); m_labelTrayAction->setText(tr("Action:")); @@ -611,6 +612,11 @@ void OptionsPage::setupPasswordLock() void OptionsPage::setupTrayBox() { + m_cbTrayShowIcon = ControlUtil::createCheckBox(iniUser()->trayShowIcon(), [&](bool checked) { + iniUser()->setTrayShowIcon(checked); + ctrl()->setIniUserEdited(); + }); + m_cbTrayAnimateAlert = ControlUtil::createCheckBox(iniUser()->trayAnimateAlert(), [&](bool checked) { iniUser()->setTrayAnimateAlert(checked); @@ -622,6 +628,7 @@ void OptionsPage::setupTrayBox() auto actionLayout = setupTrayActionLayout(); auto layout = new QVBoxLayout(); + layout->addWidget(m_cbTrayShowIcon); layout->addWidget(m_cbTrayAnimateAlert); layout->addWidget(ControlUtil::createSeparator()); layout->addLayout(eventLayout); diff --git a/src/ui/form/opt/pages/optionspage.h b/src/ui/form/opt/pages/optionspage.h index 438d9d65..ec618e76 100644 --- a/src/ui/form/opt/pages/optionspage.h +++ b/src/ui/form/opt/pages/optionspage.h @@ -94,6 +94,7 @@ private: QCheckBox *m_cbPassword = nullptr; QLineEdit *m_editPassword = nullptr; QToolButton *m_btPasswordLock = nullptr; + QCheckBox *m_cbTrayShowIcon = nullptr; QCheckBox *m_cbTrayAnimateAlert = nullptr; QLabel *m_labelTrayEvent = nullptr; QComboBox *m_comboTrayEvent = nullptr; diff --git a/src/ui/form/tray/trayicon.cpp b/src/ui/form/tray/trayicon.cpp index 538956c3..341089a8 100644 --- a/src/ui/form/tray/trayicon.cpp +++ b/src/ui/form/tray/trayicon.cpp @@ -265,6 +265,16 @@ void TrayIcon::updateTrayMenu(bool onlyFlags) updateClickActions(); } +void TrayIcon::quitProgram() +{ + if (iniUser()->confirmQuit()) { + windowManager()->showConfirmBox( + [&] { windowManager()->quit(); }, tr("Are you sure you want to quit the program?")); + } else { + windowManager()->quit(); + } +} + void TrayIcon::switchTrayMenu(bool /*checked*/) { showTrayMenu(QCursor::pos()); @@ -615,16 +625,6 @@ void TrayIcon::switchFilterMode(QAction *action) } } -void TrayIcon::quitProgram() -{ - if (iniUser()->confirmQuit()) { - windowManager()->showConfirmBox( - [&] { windowManager()->quit(); }, tr("Are you sure you want to quit the program?")); - } else { - windowManager()->quit(); - } -} - void TrayIcon::addHotKey(QAction *action, const QString &shortcutText) { if (shortcutText.isEmpty()) diff --git a/src/ui/form/tray/trayicon.h b/src/ui/form/tray/trayicon.h index 1f90a9ad..56fe05eb 100644 --- a/src/ui/form/tray/trayicon.h +++ b/src/ui/form/tray/trayicon.h @@ -60,6 +60,8 @@ public: DriverManager *driverManager() const; WindowManager *windowManager() const; + QMenu *menu() const { return m_menu; } + static ActionType clickEventActionType(IniUser *iniUser, ClickType clickType); static void setClickEventActionType( IniUser *iniUser, ClickType clickType, ActionType actionType); @@ -70,6 +72,8 @@ public slots: void showTrayMenu(const QPoint &pos); void updateTrayMenu(bool onlyFlags = false); + void quitProgram(); + protected slots: void switchTrayMenu(bool checked); void switchFilterModeMenu(bool checked); @@ -80,7 +84,6 @@ protected slots: void switchTrayFlag(bool checked); void switchFilterMode(QAction *action); - void quitProgram(); private: void setupController(); diff --git a/src/ui/fortmanager.cpp b/src/ui/fortmanager.cpp index fc826d10..b954ef26 100644 --- a/src/ui/fortmanager.cpp +++ b/src/ui/fortmanager.cpp @@ -318,12 +318,19 @@ void FortManager::setupTranslationManager() void FortManager::show() { auto windowManager = IoC(); + const IniUser &iniUser = IoC()->iniUser(); setupTranslationManager(); - windowManager->showTrayIcon(); + windowManager->setupTrayIcon(); - if (IoC()->iniUser().graphWindowVisible()) { + if (iniUser.trayShowIcon()) { + windowManager->showTrayIcon(); + } else { + windowManager->showHomeWindow(); + } + + if (iniUser.graphWindowVisible()) { windowManager->showGraphWindow(); } } diff --git a/src/ui/manager/windowmanager.cpp b/src/ui/manager/windowmanager.cpp index 11381413..020ea2ea 100644 --- a/src/ui/manager/windowmanager.cpp +++ b/src/ui/manager/windowmanager.cpp @@ -146,18 +146,6 @@ void WindowManager::closeMainWindow() m_mainWindow = nullptr; } -void WindowManager::setupTrayIcon() -{ - m_trayIcon = new TrayIcon(this); - - connect(m_trayIcon, &QSystemTrayIcon::messageClicked, this, - &WindowManager::onTrayMessageClicked); - - auto confManager = IoC(); - connect(confManager, &ConfManager::appAlerted, m_trayIcon, - [&] { m_trayIcon->updateTrayIcon(/*alerted=*/true); }); -} - void WindowManager::setupHomeWindow() { m_homeWindow = new HomeWindow(); @@ -245,11 +233,24 @@ void WindowManager::closeAll() closeMainWindow(); } +void WindowManager::setupTrayIcon() +{ + if (m_trayIcon) + return; + + m_trayIcon = new TrayIcon(this); + + connect(m_trayIcon, &QSystemTrayIcon::messageClicked, this, + &WindowManager::onTrayMessageClicked); + + auto confManager = IoC(); + connect(confManager, &ConfManager::appAlerted, m_trayIcon, + [&] { m_trayIcon->updateTrayIcon(/*alerted=*/true); }); +} + void WindowManager::showTrayIcon() { - if (!m_trayIcon) { - setupTrayIcon(); - } + setupTrayIcon(); m_trayIcon->show(); } @@ -290,6 +291,10 @@ void WindowManager::closeHomeWindow() m_homeWindow->deleteLater(); m_homeWindow = nullptr; + + if (!trayIcon()->isVisible()) { + trayIcon()->quitProgram(); + } } void WindowManager::showProgramsWindow() diff --git a/src/ui/manager/windowmanager.h b/src/ui/manager/windowmanager.h index c2568b9b..3226ac24 100644 --- a/src/ui/manager/windowmanager.h +++ b/src/ui/manager/windowmanager.h @@ -51,6 +51,7 @@ signals: public slots: void setupAppPalette(); + void setupTrayIcon(); void showTrayIcon(); void closeTrayIcon(); void showTrayMessage( @@ -106,8 +107,6 @@ private: void setupMainWindow(); void closeMainWindow(); - void setupTrayIcon(); - void setupHomeWindow(); void setupProgramsWindow(); void setupOptionsWindow(); diff --git a/src/ui/user/iniuser.h b/src/ui/user/iniuser.h index cd5753dc..038ca894 100644 --- a/src/ui/user/iniuser.h +++ b/src/ui/user/iniuser.h @@ -41,6 +41,9 @@ public: "filterModeAllow", "filterModeIgnore" }; } + bool trayShowIcon() const { return valueBool("tray/showIcon", true); } + void setTrayShowIcon(bool v) { setValue("tray/showIcon", v, true); } + bool trayAnimateAlert() const { return valueBool("tray/animateAlert"); } void setTrayAnimateAlert(bool v) { setValue("tray/animateAlert", v); }