2020-06-18 00:34:05 +00:00
|
|
|
|
#pragma execution_character_set("utf-8")
|
|
|
|
|
|
|
|
|
|
#include "trayicon.h"
|
|
|
|
|
#include "qmutex.h"
|
|
|
|
|
#include "qmenu.h"
|
|
|
|
|
#include "qapplication.h"
|
|
|
|
|
#include "qdebug.h"
|
|
|
|
|
|
|
|
|
|
QScopedPointer<TrayIcon> TrayIcon::self;
|
|
|
|
|
TrayIcon *TrayIcon::Instance()
|
|
|
|
|
{
|
|
|
|
|
if (self.isNull()) {
|
2021-05-30 07:59:42 +00:00
|
|
|
|
static QMutex mutex;
|
2020-06-18 00:34:05 +00:00
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
|
if (self.isNull()) {
|
|
|
|
|
self.reset(new TrayIcon);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self.data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TrayIcon::TrayIcon(QObject *parent) : QObject(parent)
|
|
|
|
|
{
|
|
|
|
|
mainWidget = 0;
|
|
|
|
|
trayIcon = new QSystemTrayIcon(this);
|
|
|
|
|
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
|
|
|
|
|
this, SLOT(iconIsActived(QSystemTrayIcon::ActivationReason)));
|
2021-05-30 07:59:42 +00:00
|
|
|
|
menu = new QMenu;
|
2020-06-18 00:34:05 +00:00
|
|
|
|
exitDirect = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TrayIcon::iconIsActived(QSystemTrayIcon::ActivationReason reason)
|
|
|
|
|
{
|
|
|
|
|
switch (reason) {
|
2021-05-30 07:59:42 +00:00
|
|
|
|
case QSystemTrayIcon::Trigger:
|
|
|
|
|
case QSystemTrayIcon::DoubleClick: {
|
2023-06-12 02:17:15 +00:00
|
|
|
|
this->showMainWidget();
|
2021-05-30 07:59:42 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2020-06-18 00:34:05 +00:00
|
|
|
|
|
2021-05-30 07:59:42 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
2020-06-18 00:34:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TrayIcon::setExitDirect(bool exitDirect)
|
|
|
|
|
{
|
|
|
|
|
if (this->exitDirect != exitDirect) {
|
|
|
|
|
this->exitDirect = exitDirect;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TrayIcon::setMainWidget(QWidget *mainWidget)
|
|
|
|
|
{
|
|
|
|
|
this->mainWidget = mainWidget;
|
2023-06-12 02:17:15 +00:00
|
|
|
|
menu->addAction("主界面", this, SLOT(showMainWidget()));
|
2020-06-18 00:34:05 +00:00
|
|
|
|
|
|
|
|
|
if (exitDirect) {
|
|
|
|
|
menu->addAction("退出", this, SLOT(closeAll()));
|
|
|
|
|
} else {
|
|
|
|
|
menu->addAction("退出", this, SIGNAL(trayIconExit()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trayIcon->setContextMenu(menu);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-12 02:17:15 +00:00
|
|
|
|
void TrayIcon::showMainWidget()
|
|
|
|
|
{
|
|
|
|
|
if (mainWidget) {
|
|
|
|
|
mainWidget->showNormal();
|
|
|
|
|
mainWidget->activateWindow();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 00:34:05 +00:00
|
|
|
|
void TrayIcon::showMessage(const QString &title, const QString &msg, QSystemTrayIcon::MessageIcon icon, int msecs)
|
|
|
|
|
{
|
|
|
|
|
trayIcon->showMessage(title, msg, icon, msecs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TrayIcon::setIcon(const QString &strIcon)
|
|
|
|
|
{
|
|
|
|
|
trayIcon->setIcon(QIcon(strIcon));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TrayIcon::setToolTip(const QString &tip)
|
|
|
|
|
{
|
|
|
|
|
trayIcon->setToolTip(tip);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 05:51:39 +00:00
|
|
|
|
bool TrayIcon::getVisible() const
|
|
|
|
|
{
|
|
|
|
|
return trayIcon->isVisible();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 00:34:05 +00:00
|
|
|
|
void TrayIcon::setVisible(bool visible)
|
|
|
|
|
{
|
|
|
|
|
trayIcon->setVisible(visible);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TrayIcon::closeAll()
|
|
|
|
|
{
|
|
|
|
|
trayIcon->hide();
|
|
|
|
|
trayIcon->deleteLater();
|
|
|
|
|
qApp->exit();
|
|
|
|
|
}
|