From 5f30277e699ed1ed987d7a33c8976d4c2f711762 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Wed, 19 Jun 2013 21:28:26 +0200 Subject: [PATCH] Application now follows "hide/quit on last window close". --- .../executable_properties/rssguard_win.rc.in | 2 +- src/gui/formmain.cpp | 25 +++++++++++++++---- src/gui/formmain.h | 3 ++- src/gui/formsettings.cpp | 2 +- src/gui/formsettings.ui | 4 +-- src/gui/systemtrayicon.cpp | 8 ++++++ src/gui/systemtrayicon.h | 3 ++- src/main.cpp | 7 ++++++ 8 files changed, 43 insertions(+), 11 deletions(-) diff --git a/resources/executable_properties/rssguard_win.rc.in b/resources/executable_properties/rssguard_win.rc.in index 428a12e47..08ad6313f 100644 --- a/resources/executable_properties/rssguard_win.rc.in +++ b/resources/executable_properties/rssguard_win.rc.in @@ -12,7 +12,7 @@ BEGIN BEGIN VALUE "CompanyName", "@APP_AUTHOR@" VALUE "FileDescription", "@APP_NAME@" - VALUE "FileVersion", "1.0.0.0" + VALUE "FileVersion", "@APP_VERSION@" VALUE "InternalName", "@APP_AUTHOR@" VALUE "LegalCopyright", "Copyright 2011-2013 Martin Rotter" VALUE "LegalTrademarks1", "Some rights reserved" diff --git a/src/gui/formmain.cpp b/src/gui/formmain.cpp index 3f14e7acf..c2713285e 100644 --- a/src/gui/formmain.cpp +++ b/src/gui/formmain.cpp @@ -1,6 +1,7 @@ // for testing #include #include +#include #include "gui/formmain.h" #include "gui/formsettings.h" @@ -21,10 +22,6 @@ FormMain::FormMain(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::FormMain // Establish connections. createConnections(); - - // testing purposes - SystemTrayIcon *icon = SystemTrayIcon::getInstance(); - icon->show(); } FormMain::~FormMain() { @@ -57,7 +54,7 @@ void FormMain::cleanupResources() { qDebug("Cleaning up resources before the application exits."); } -#if defined(Q_OS_LINUX) +#if !defined(Q_OS_WIN) bool FormMain::event(QEvent *event) { if (event->type() == ThemeFactoryEvent::type()) { // Handle the change of icon theme. @@ -84,6 +81,24 @@ void FormMain::createConnections() { connect(qApp, &QCoreApplication::aboutToQuit, this, &FormMain::cleanupResources); } +void FormMain::closeEvent(QCloseEvent *event) { + if (SystemTrayIcon::isSystemTrayActivated()) { + if (Settings::getInstance()->value(APP_CFG_GUI, + "close_win_action", + 0).toInt() == 0) { + // User selected to minimize the application if its main + // window gets closed and tray icon is activated. + hide(); + event->ignore(); + } + else { + // User selected to quit the application if its main + // window gets closed and tray icon is activated. + qApp->quit(); + } + } +} + void FormMain::showSettings() { FormSettings form_settings(this); form_settings.exec(); diff --git a/src/gui/formmain.h b/src/gui/formmain.h index 485b65165..e3d9dba3a 100644 --- a/src/gui/formmain.h +++ b/src/gui/formmain.h @@ -17,8 +17,9 @@ class FormMain : public QMainWindow { protected: void createConnections(); + void closeEvent(QCloseEvent *event); -#if defined(Q_OS_LINUX) +#if !defined(Q_OS_WIN) bool event(QEvent *event); // Sets up proper icons for this widget. diff --git a/src/gui/formsettings.cpp b/src/gui/formsettings.cpp index 64e8be88a..724bb3a89 100644 --- a/src/gui/formsettings.cpp +++ b/src/gui/formsettings.cpp @@ -35,7 +35,6 @@ void FormSettings::saveSettings() { void FormSettings::loadInterface() { // Load settings of tray icon. if (SystemTrayIcon::isSystemTrayAvailable()) { - // načti m_ui->m_radioTrayOff->setChecked(!Settings::getInstance()->value(APP_CFG_GUI, "use_tray_icon", true).toBool()); @@ -43,6 +42,7 @@ void FormSettings::loadInterface() { "close_win_action", 0).toInt()); } + // Tray icon is not supported on this machine. else { m_ui->m_radioTrayOff->setText(tr("disable (Tray icon is not available.)")); m_ui->m_radioTrayOff->setChecked(true); diff --git a/src/gui/formsettings.ui b/src/gui/formsettings.ui index 0dc6c5e09..a01290c87 100644 --- a/src/gui/formsettings.ui +++ b/src/gui/formsettings.ui @@ -45,7 +45,7 @@ - 0 + 1 @@ -68,7 +68,7 @@ QTabWidget::North - 0 + 1 diff --git a/src/gui/systemtrayicon.cpp b/src/gui/systemtrayicon.cpp index 57a29cf1f..9c23d89d8 100644 --- a/src/gui/systemtrayicon.cpp +++ b/src/gui/systemtrayicon.cpp @@ -58,11 +58,19 @@ void SystemTrayIcon::show_private() { } void SystemTrayIcon::show() { +#if defined(Q_OS_WIN) + // Show immediately. + qDebug("Showing tray icon immediately."); + show_private(); +#else // Delay avoids race conditions and tray icon is properly displayed. qDebug("Showing tray icon with 1000 ms delay."); QTimer::singleShot(1000, Qt::CoarseTimer, this, SLOT(show_private())); +#endif + + } void SystemTrayIcon::setNumber(int number) { diff --git a/src/gui/systemtrayicon.h b/src/gui/systemtrayicon.h index 4a0ee126f..b3cfbf85d 100644 --- a/src/gui/systemtrayicon.h +++ b/src/gui/systemtrayicon.h @@ -8,7 +8,8 @@ class FormMain; class SystemTrayIcon : public QSystemTrayIcon { - Q_OBJECT + Q_OBJECT + public: explicit SystemTrayIcon(const QString &normal_icon, const QString &plain_icon, diff --git a/src/main.cpp b/src/main.cpp index 86dcbc14b..5021dae12 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include "gui/themefactory.h" #include "gui/formmain.h" #include "gui/formwelcome.h" +#include "gui/systemtrayicon.h" #include "qtsingleapplication/qtsingleapplication.h" @@ -69,6 +70,7 @@ int main(int argc, char *argv[]) { // Instantiate main application window. FormMain window; + // Display welcome dialog if application is launched for the first time. if (Settings::getInstance()->value(APP_CFG_GEN, "first_start", true).toBool()) { Settings::getInstance()->setValue(APP_CFG_GEN, "first_start", false); FormWelcome(&window).exec(); @@ -77,6 +79,11 @@ int main(int argc, char *argv[]) { // Display main window. window.show(); + // Display tray icon if it is enabled and available. + if (SystemTrayIcon::isSystemTrayActivated()) { + SystemTrayIcon::getInstance()->show(); + } + // Setup single-instance behavior. application.setActivationWindow(&window, true); QObject::connect(&application, &QtSingleApplication::messageReceived,