Skip to content

Commit

Permalink
Only use tray icon if it is available
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederick Thomssen committed Apr 9, 2016
1 parent e23e951 commit 9b1de04
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
48 changes: 33 additions & 15 deletions RedTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ RedTimer::~RedTimer()

settings_->save();

trayIcon_->hide();
if( trayIcon_ )
trayIcon_->hide();

RETURN();
}
Expand Down Expand Up @@ -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<QWidget*>(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();
Expand Down Expand Up @@ -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<QWidget*>(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;
}

Expand Down Expand Up @@ -239,7 +257,7 @@ RedTimer::exit()
}

default:
DEBUG() << "Passing the close event to QObject";
DEBUG() << "Closing the application";
break;
}
}
Expand Down
5 changes: 5 additions & 0 deletions RedTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ class RedTimer : public QObject
*/
void init();

/**
* @brief Initialise the tray icon
*/
void initTrayIcon();

protected:
/**
* @brief Filter Qt events
Expand Down

0 comments on commit 9b1de04

Please sign in to comment.