From 9b1de047e416631efcc4f569d90a3aed06e989e8 Mon Sep 17 00:00:00 2001 From: Frederick Thomssen Date: Sat, 9 Apr 2016 10:02:53 +0200 Subject: [PATCH] Only use tray icon if it is available --- RedTimer.cpp | 48 +++++++++++++++++++++++++++++++++--------------- RedTimer.h | 5 +++++ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/RedTimer.cpp b/RedTimer.cpp index 584a1a3..b416ade 100644 --- a/RedTimer.cpp +++ b/RedTimer.cpp @@ -31,7 +31,8 @@ RedTimer::~RedTimer() settings_->save(); - trayIcon_->hide(); + if( trayIcon_ ) + trayIcon_->hide(); RETURN(); } @@ -65,16 +66,9 @@ RedTimer::init() flags |= Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint; win_->setFlags( flags ); - display(); - - trayIcon_ = new QSystemTrayIcon( win_ ); - trayIcon_->setIcon( QIcon(":/icons/clock_red.svg") ); - trayIcon_->show(); + initTrayIcon(); - QMenu* trayMenu = new QMenu( "RedTimer", qobject_cast(win_) ); - trayMenu->addAction( QIcon(":/icons/clock_red.svg"), tr("S&how/hide"), this, &RedTimer::toggle ); - trayMenu->addAction( QIcon(":/open-iconic/svg/x.svg"), tr("E&xit"), this, &RedTimer::exit ); - trayIcon_->setContextMenu( trayMenu ); + display(); // Main window access members ctx_ = win_->rootContext(); @@ -147,22 +141,46 @@ RedTimer::init() // Connect the timer to the tracking counter connect( timer_, &QTimer::timeout, this, &RedTimer::refreshCounter ); - // Connect the tray icon to the window show slot - connect( trayIcon_, &QSystemTrayIcon::activated, this, &RedTimer::trayEvent ); - // Initially update the GUI refresh(); RETURN(); } +void +RedTimer::initTrayIcon() +{ + ENTER(); + + if( QSystemTrayIcon::isSystemTrayAvailable() ) + { + trayIcon_ = new QSystemTrayIcon( win_ ); + trayIcon_->setIcon( QIcon(":/icons/clock_red.svg") ); + trayIcon_->show(); + + QMenu* trayMenu = new QMenu( "RedTimer", qobject_cast(win_) ); + trayMenu->addAction( QIcon(":/icons/clock_red.svg"), tr("S&how/hide"), this, &RedTimer::toggle ); + trayMenu->addAction( QIcon(":/open-iconic/svg/x.svg"), tr("E&xit"), this, &RedTimer::exit ); + trayIcon_->setContextMenu( trayMenu ); + + // Connect the tray icon to the window show slot + connect( trayIcon_, &QSystemTrayIcon::activated, this, &RedTimer::trayEvent ); + } + + RETURN(); +} + bool RedTimer::eventFilter( QObject* obj, QEvent* event ) { // Show warning on close and if timer is running if( event->type() == QEvent::Close ) { - win_->hide(); + if( trayIcon_ ) + win_->hide(); + else + exit(); + return true; } @@ -239,7 +257,7 @@ RedTimer::exit() } default: - DEBUG() << "Passing the close event to QObject"; + DEBUG() << "Closing the application"; break; } } diff --git a/RedTimer.h b/RedTimer.h index 813cdc7..88310a4 100644 --- a/RedTimer.h +++ b/RedTimer.h @@ -125,6 +125,11 @@ class RedTimer : public QObject */ void init(); + /** + * @brief Initialise the tray icon + */ + void initTrayIcon(); + protected: /** * @brief Filter Qt events