Context menu for tray icon + switching of main window.

This commit is contained in:
Martin Rotter 2013-06-25 09:46:52 +02:00
parent cb9f5521ed
commit 524fd819fe
5 changed files with 59 additions and 11 deletions

View File

@ -88,15 +88,16 @@ bool SystemFactory::setAutoStartStatus(const AutoStartStatus &new_status) {
} }
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
QSettings registy_key("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings registry_key("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run",
QSettings::NativeFormat); QSettings::NativeFormat);
switch (new_status) { switch (new_status) {
case SystemFactory::Enabled: case SystemFactory::Enabled:
registy_key.setValue(APP_LOW_NAME, registry_key.setValue(APP_LOW_NAME,
QtSingleApplication::applicationFilePath().replace('/', '\\')); QtSingleApplication::applicationFilePath().replace('/',
'\\'));
return true; return true;
case SystemFactory::Disabled: case SystemFactory::Disabled:
registy_key.remove(APP_LOW_NAME); registry_key.remove(APP_LOW_NAME);
return true; return true;
default: default:
return false; return false;

View File

@ -22,6 +22,8 @@ FormMain::FormMain(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::FormMain
// Establish connections. // Establish connections.
createConnections(); createConnections();
prepareMenus();
} }
FormMain::~FormMain() { FormMain::~FormMain() {
@ -32,6 +34,21 @@ FormMain *FormMain::getInstance() {
return m_this; 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) { void FormMain::processExecutionMessage(const QString &message) {
// TODO: Implement proper reaction when application is launched more than once. // TODO: Implement proper reaction when application is launched more than once.
qDebug("Received '%s' execution message from another application instance.", qDebug("Received '%s' execution message from another application instance.",
@ -44,6 +61,15 @@ void FormMain::quit() {
qApp->quit(); qApp->quit();
} }
void FormMain::switchVisibility() {
if (isVisible()) {
hide();
}
else {
display();
}
}
void FormMain::display() { void FormMain::display() {
// Make sure window is not minimized. // Make sure window is not minimized.
setWindowState(windowState() & ~Qt::WindowMinimized); setWindowState(windowState() & ~Qt::WindowMinimized);

View File

@ -13,9 +13,13 @@ class FormMain : public QMainWindow {
explicit FormMain(QWidget *parent = 0); explicit FormMain(QWidget *parent = 0);
~FormMain(); ~FormMain();
// Returns menu for the tray icon.
QMenu *getTrayMenu();
static FormMain *getInstance(); static FormMain *getInstance();
protected: protected:
void prepareMenus();
void createConnections(); void createConnections();
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);
@ -30,6 +34,7 @@ class FormMain : public QMainWindow {
void processExecutionMessage(const QString &message); void processExecutionMessage(const QString &message);
void quit(); void quit();
void display(); void display();
void switchVisibility();
protected slots: protected slots:
void cleanupResources(); void cleanupResources();
@ -37,6 +42,7 @@ class FormMain : public QMainWindow {
private: private:
Ui::FormMain *m_ui; Ui::FormMain *m_ui;
QMenu *m_trayMenu;
static FormMain *m_this; static FormMain *m_this;
}; };

View File

@ -1,5 +1,6 @@
#include <QPainter> #include <QPainter>
#include <QTimer> #include <QTimer>
#include <QMessageBox>
#include "gui/systemtrayicon.h" #include "gui/systemtrayicon.h"
#include "gui/formmain.h" #include "gui/formmain.h"
@ -18,6 +19,10 @@ SystemTrayIcon::SystemTrayIcon(const QString &normal_icon,
// Initialize icon. // Initialize icon.
setNumber(); setNumber();
setContextMenu(parent->getTrayMenu());
// Create necessary connections.
connect(this, &SystemTrayIcon::activated, this, &SystemTrayIcon::onActivated);
} }
SystemTrayIcon::~SystemTrayIcon() { SystemTrayIcon::~SystemTrayIcon() {
@ -25,6 +30,17 @@ SystemTrayIcon::~SystemTrayIcon() {
hide(); hide();
} }
void SystemTrayIcon::onActivated(const ActivationReason &reason) {
switch (reason) {
case SystemTrayIcon::Trigger:
case SystemTrayIcon::DoubleClick:
case SystemTrayIcon::MiddleClick:
static_cast<FormMain*>(parent())->switchVisibility();
default:
break;
}
}
bool SystemTrayIcon::isSystemTrayAvailable() { bool SystemTrayIcon::isSystemTrayAvailable() {
return QSystemTrayIcon::isSystemTrayAvailable() && QSystemTrayIcon::supportsMessages(); return QSystemTrayIcon::isSystemTrayAvailable() && QSystemTrayIcon::supportsMessages();
} }
@ -53,7 +69,7 @@ void SystemTrayIcon::deleteInstance() {
} }
} }
void SystemTrayIcon::show_private() { void SystemTrayIcon::showPrivate() {
QSystemTrayIcon::show(); QSystemTrayIcon::show();
qDebug("Tray icon displayed."); qDebug("Tray icon displayed.");
} }
@ -68,7 +84,7 @@ void SystemTrayIcon::show() {
qDebug("Showing tray icon with 1000 ms delay."); qDebug("Showing tray icon with 1000 ms delay.");
QTimer::singleShot(1000, QTimer::singleShot(1000,
Qt::CoarseTimer, Qt::CoarseTimer,
this, SLOT(show_private())); this, SLOT(showPrivate()));
#endif #endif

View File

@ -30,16 +30,15 @@ class SystemTrayIcon : public QSystemTrayIcon {
// Sets the number to be visible in the tray icon, -1 removes it. // Sets the number to be visible in the tray icon, -1 removes it.
void setNumber(int number = -1); void setNumber(int number = -1);
// TODO: Implement method for manual clearing of the tray icon. Creating of tray icon // Explicitle clears SystemTrayIcon instance from the memory.
// handled by getInstance().
static void deleteInstance(); static void deleteInstance();
signals:
public slots: public slots:
void show(); void show();
private slots: private slots:
void show_private(); void showPrivate();
void onActivated(const ActivationReason &reason);
private: private:
QString m_normalIcon; QString m_normalIcon;