refs #412 System Tray Window base class

It inherits from QMainWindow automatically acts as SystemTrayWindow.
If minimized it will not quit, but minimize to tray only.
This commit is contained in:
Roland Winklmeier
2015-05-17 20:24:16 +02:00
parent f2a280a387
commit aa51bd40dc
2 changed files with 218 additions and 0 deletions

View File

@@ -0,0 +1,133 @@
/* Copyright (C) 2014
* swift project Community / Contributors
*
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "systemtraywindow.h"
#include "blackmisc/icons.h"
#include "blackmisc/blackmiscfreefunctions.h"
#include <QStatusBar>
#include <QPushButton>
#include <QApplication>
#include <QMessageBox>
#include <QCloseEvent>
#include <QObject>
using namespace BlackMisc;
namespace BlackGui
{
CSystemTrayWindow::CSystemTrayWindow(const QIcon &icon, QWidget *parent) :
QMainWindow(parent)
{
createActions();
createTrayIcon(icon);
}
void CSystemTrayWindow::setSystemTrayMode(SystemTrayMode mode)
{
m_systemTrayMode = mode;
}
void CSystemTrayWindow::setSystemTrayIcon(const QIcon &icon)
{
Q_ASSERT(m_systemTrayIcon);
m_systemTrayIcon->setIcon(icon);
}
void CSystemTrayWindow::setSystemTrayToolTip(const QString &tooltip)
{
Q_ASSERT(m_systemTrayIcon);
m_systemTrayIcon->setToolTip(tooltip);
}
void CSystemTrayWindow::ps_activateWindow(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
showNormal();
activateWindow();
break;
case QSystemTrayIcon::MiddleClick:
break;
default:
;
}
}
void CSystemTrayWindow::ps_showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon,
int millisecondsTimeoutHint)
{
Q_ASSERT(m_systemTrayIcon);
m_systemTrayIcon->showMessage(title, message, icon, millisecondsTimeoutHint);
}
void CSystemTrayWindow::closeEvent(QCloseEvent *event)
{
int result = QMessageBox::Close;
if (m_systemTrayMode.testFlag(AskOnClose))
{
QMessageBox msgBox;
msgBox.setText("Are you sure you want to close? This will quit swiftcorectrl!");
msgBox.setStandardButtons(QMessageBox::Close | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Close);
msgBox.setIcon(QMessageBox::Warning);
}
result = QMessageBox::Close; // msgBox.exec();
switch (result)
{
case QMessageBox::Close:
event->accept();
break;
case QMessageBox::Cancel:
event->ignore();
break;
default:
closeEvent(event);
break;
}
}
void CSystemTrayWindow::changeEvent(QEvent *event)
{
QMainWindow::changeEvent(event);
if(event->type() == QEvent::WindowStateChange)
{
if(isMinimized()) { hide(); }
}
}
void CSystemTrayWindow::createActions()
{
m_actionRestore = new QAction(tr("&Restore"), this);
connect(m_actionRestore, &QAction::triggered, this, &QWidget::showNormal);
m_actionQuit = new QAction(tr("&Quit"), this);
connect(m_actionQuit, &QAction::triggered, qApp, &QApplication::quit);
}
void CSystemTrayWindow::createTrayIcon(const QIcon &icon)
{
m_trayIconMenu = new QMenu(this);
m_trayIconMenu->addAction(m_actionRestore);
m_trayIconMenu->addSeparator();
m_trayIconMenu->addAction(m_actionQuit);
m_systemTrayIcon = new QSystemTrayIcon(this);
m_systemTrayIcon->setContextMenu(m_trayIconMenu);
m_systemTrayIcon->setIcon(icon);
m_systemTrayIcon->show();
connect(m_systemTrayIcon, &QSystemTrayIcon::activated, this, &CSystemTrayWindow::ps_activateWindow);
}
} // namespace

View File

@@ -0,0 +1,85 @@
/* Copyright (C) 2014
* swift project Community / Contributors
*
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_ENABLEFORSYSTEMTRAY_H
#define BLACKGUI_ENABLEFORSYSTEMTRAY_H
#include "blackgui/blackguiexport.h"
#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
namespace BlackGui
{
//! QDialog which can minimize to system tray
class BLACKGUI_EXPORT CSystemTrayWindow : public QMainWindow
{
public:
//! System Tray Flag
enum SystemTrayFlag
{
MinimizeToTray = 0x0,
QuitOnClose = 0x1,
AskOnClose = 0x2
};
Q_DECLARE_FLAGS(SystemTrayMode, SystemTrayFlag)
//! Constructor
CSystemTrayWindow(const QIcon &icon, QWidget *parent = nullptr);
//! System tray mode
void setSystemTrayMode(SystemTrayMode mode);
//! Set icon
void setSystemTrayIcon(const QIcon &icon);
//! Set tool tip
void setSystemTrayToolTip(const QString &tooltip);
protected slots:
//! Activate window from system tray
void ps_activateWindow(QSystemTrayIcon::ActivationReason reason);
//! Show message in the system tray
void ps_showMessage(const QString &title, const QString &message,
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information,
int millisecondsTimeoutHint = 10000);
protected:
//! \copydoc QMainWindow::changeEvent
virtual void changeEvent(QEvent *event) override;
//! \copydoc QMainWindow::closeEvent
virtual void closeEvent(QCloseEvent *event) override;
private:
void createActions();
void createTrayIcon(const QIcon &icon);
SystemTrayMode m_systemTrayMode = SystemTrayMode { MinimizeToTray | QuitOnClose }; //!< Minimize mode, \sa MinimizeMode
// System Tray actions
QAction *m_actionRestore = nullptr;
QAction *m_actionQuit = nullptr;
QSystemTrayIcon *m_systemTrayIcon = nullptr;
QMenu *m_trayIconMenu = nullptr;
};
} // namespace
Q_DECLARE_OPERATORS_FOR_FLAGS(BlackGui::CSystemTrayWindow::SystemTrayMode)
#endif // guard