diff --git a/src/core/systemfactory.cpp b/src/core/systemfactory.cpp index a1ebd4e21..b98a9c6ff 100644 --- a/src/core/systemfactory.cpp +++ b/src/core/systemfactory.cpp @@ -88,15 +88,16 @@ bool SystemFactory::setAutoStartStatus(const AutoStartStatus &new_status) { } #if defined(Q_OS_WIN) - QSettings registy_key("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", - QSettings::NativeFormat); + QSettings registry_key("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", + QSettings::NativeFormat); switch (new_status) { case SystemFactory::Enabled: - registy_key.setValue(APP_LOW_NAME, - QtSingleApplication::applicationFilePath().replace('/', '\\')); + registry_key.setValue(APP_LOW_NAME, + QtSingleApplication::applicationFilePath().replace('/', + '\\')); return true; case SystemFactory::Disabled: - registy_key.remove(APP_LOW_NAME); + registry_key.remove(APP_LOW_NAME); return true; default: return false; diff --git a/src/gui/formmain.cpp b/src/gui/formmain.cpp index ab43e5e35..b069b490e 100644 --- a/src/gui/formmain.cpp +++ b/src/gui/formmain.cpp @@ -22,6 +22,8 @@ FormMain::FormMain(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::FormMain // Establish connections. createConnections(); + + prepareMenus(); } FormMain::~FormMain() { @@ -32,6 +34,21 @@ FormMain *FormMain::getInstance() { return m_this; } +QMenu *FormMain::getTrayMenu() { + return m_trayMenu; +} + +void FormMain::prepareMenus() { + // Setup menu for tray icon. + if (SystemTrayIcon::isSystemTrayAvailable()) { + m_trayMenu = new QMenu(APP_NAME, this); + + // Add needed items to the menu. + m_trayMenu->addAction(m_ui->m_actionSettings); + m_trayMenu->addAction(m_ui->m_actionQuit); + } +} + void FormMain::processExecutionMessage(const QString &message) { // TODO: Implement proper reaction when application is launched more than once. qDebug("Received '%s' execution message from another application instance.", @@ -44,6 +61,15 @@ void FormMain::quit() { qApp->quit(); } +void FormMain::switchVisibility() { + if (isVisible()) { + hide(); + } + else { + display(); + } +} + void FormMain::display() { // Make sure window is not minimized. setWindowState(windowState() & ~Qt::WindowMinimized); diff --git a/src/gui/formmain.h b/src/gui/formmain.h index e3d9dba3a..b24751fca 100644 --- a/src/gui/formmain.h +++ b/src/gui/formmain.h @@ -13,9 +13,13 @@ class FormMain : public QMainWindow { explicit FormMain(QWidget *parent = 0); ~FormMain(); + // Returns menu for the tray icon. + QMenu *getTrayMenu(); + static FormMain *getInstance(); protected: + void prepareMenus(); void createConnections(); void closeEvent(QCloseEvent *event); @@ -30,6 +34,7 @@ class FormMain : public QMainWindow { void processExecutionMessage(const QString &message); void quit(); void display(); + void switchVisibility(); protected slots: void cleanupResources(); @@ -37,6 +42,7 @@ class FormMain : public QMainWindow { private: Ui::FormMain *m_ui; + QMenu *m_trayMenu; static FormMain *m_this; }; diff --git a/src/gui/systemtrayicon.cpp b/src/gui/systemtrayicon.cpp index 9a01038e9..2b2e1b053 100644 --- a/src/gui/systemtrayicon.cpp +++ b/src/gui/systemtrayicon.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "gui/systemtrayicon.h" #include "gui/formmain.h" @@ -18,6 +19,10 @@ SystemTrayIcon::SystemTrayIcon(const QString &normal_icon, // Initialize icon. setNumber(); + setContextMenu(parent->getTrayMenu()); + + // Create necessary connections. + connect(this, &SystemTrayIcon::activated, this, &SystemTrayIcon::onActivated); } SystemTrayIcon::~SystemTrayIcon() { @@ -25,6 +30,17 @@ SystemTrayIcon::~SystemTrayIcon() { hide(); } +void SystemTrayIcon::onActivated(const ActivationReason &reason) { + switch (reason) { + case SystemTrayIcon::Trigger: + case SystemTrayIcon::DoubleClick: + case SystemTrayIcon::MiddleClick: + static_cast(parent())->switchVisibility(); + default: + break; + } +} + bool SystemTrayIcon::isSystemTrayAvailable() { return QSystemTrayIcon::isSystemTrayAvailable() && QSystemTrayIcon::supportsMessages(); } @@ -53,7 +69,7 @@ void SystemTrayIcon::deleteInstance() { } } -void SystemTrayIcon::show_private() { +void SystemTrayIcon::showPrivate() { QSystemTrayIcon::show(); qDebug("Tray icon displayed."); } @@ -68,7 +84,7 @@ void SystemTrayIcon::show() { qDebug("Showing tray icon with 1000 ms delay."); QTimer::singleShot(1000, Qt::CoarseTimer, - this, SLOT(show_private())); + this, SLOT(showPrivate())); #endif diff --git a/src/gui/systemtrayicon.h b/src/gui/systemtrayicon.h index b3cfbf85d..b158f506e 100644 --- a/src/gui/systemtrayicon.h +++ b/src/gui/systemtrayicon.h @@ -30,16 +30,15 @@ class SystemTrayIcon : public QSystemTrayIcon { // Sets the number to be visible in the tray icon, -1 removes it. void setNumber(int number = -1); - // TODO: Implement method for manual clearing of the tray icon. Creating of tray icon - // handled by getInstance(). + // Explicitle clears SystemTrayIcon instance from the memory. static void deleteInstance(); - signals: public slots: void show(); private slots: - void show_private(); + void showPrivate(); + void onActivated(const ActivationReason &reason); private: QString m_normalIcon;