diff --git a/src/gui/formmain.cpp b/src/gui/formmain.cpp index 1315a2f5b..3f14e7acf 100644 --- a/src/gui/formmain.cpp +++ b/src/gui/formmain.cpp @@ -46,6 +46,13 @@ void FormMain::quit() { qApp->quit(); } +void FormMain::display() { + setWindowState(windowState() & ~Qt::WindowMinimized); + raise(); + activateWindow(); + QtSingleApplication::alert(this); +} + void FormMain::cleanupResources() { qDebug("Cleaning up resources before the application exits."); } diff --git a/src/gui/formmain.h b/src/gui/formmain.h index fcc9afe89..485b65165 100644 --- a/src/gui/formmain.h +++ b/src/gui/formmain.h @@ -28,6 +28,7 @@ class FormMain : public QMainWindow { public slots: void processExecutionMessage(const QString &message); void quit(); + void display(); protected slots: void cleanupResources(); diff --git a/src/gui/formsettings.cpp b/src/gui/formsettings.cpp index 77d67aac9..64e8be88a 100644 --- a/src/gui/formsettings.cpp +++ b/src/gui/formsettings.cpp @@ -92,6 +92,12 @@ void FormSettings::saveInterface() { // TODO: Switch tray icon here (destroy it/create it) and // if icon is should be destroyed and no main window is visible, // then show main window and then destroy tray icon. + if (Settings::getInstance()->value(APP_CFG_GUI, "use_tray_icon", true).toBool()) { + SystemTrayIcon::getInstance()->show(); + } + else { + SystemTrayIcon::deleteInstance(); + } } // Save selected icon theme. diff --git a/src/gui/systemtrayicon.cpp b/src/gui/systemtrayicon.cpp index a44925021..57a29cf1f 100644 --- a/src/gui/systemtrayicon.cpp +++ b/src/gui/systemtrayicon.cpp @@ -7,11 +7,11 @@ #include "core/defs.h" -QPointer SystemTrayIcon::m_trayIcon; +QPointer SystemTrayIcon::s_trayIcon; SystemTrayIcon::SystemTrayIcon(const QString &normal_icon, const QString &plain_icon, - QObject *parent) + FormMain *parent) : QSystemTrayIcon(parent), m_normalIcon(normal_icon), m_plainIcon(plain_icon) { qDebug("Creating SystemTrayIcon instance."); @@ -35,13 +35,21 @@ bool SystemTrayIcon::isSystemTrayActivated() { } SystemTrayIcon *SystemTrayIcon::getInstance() { - if (m_trayIcon.isNull()) { - m_trayIcon = new SystemTrayIcon(APP_ICON_PATH, + if (s_trayIcon.isNull()) { + s_trayIcon = new SystemTrayIcon(APP_ICON_PATH, APP_ICON_PLAIN_PATH, FormMain::getInstance()); } - return m_trayIcon; + return s_trayIcon; +} + +void SystemTrayIcon::deleteInstance() { + if (!s_trayIcon.isNull()) { + qDebug("Disabling tray icon and raising main application window."); + static_cast((*s_trayIcon).parent())->display(); + delete s_trayIcon.data(); + } } void SystemTrayIcon::show_private() { diff --git a/src/gui/systemtrayicon.h b/src/gui/systemtrayicon.h index b8026da78..4a0ee126f 100644 --- a/src/gui/systemtrayicon.h +++ b/src/gui/systemtrayicon.h @@ -5,12 +5,14 @@ #include +class FormMain; + class SystemTrayIcon : public QSystemTrayIcon { Q_OBJECT public: explicit SystemTrayIcon(const QString &normal_icon, const QString &plain_icon, - QObject *parent = 0); + FormMain *parent = 0); ~SystemTrayIcon(); // Returns true if tray icon CAN be constructed on this machine. @@ -29,6 +31,7 @@ class SystemTrayIcon : public QSystemTrayIcon { // TODO: Implement method for manual clearing of the tray icon. Creating of tray icon // handled by getInstance(). + static void deleteInstance(); signals: public slots: @@ -41,7 +44,7 @@ class SystemTrayIcon : public QSystemTrayIcon { QString m_normalIcon; QString m_plainIcon; - static QPointer m_trayIcon; + static QPointer s_trayIcon; }; #endif // SYSTEMTRAYICON_H