-
Notifications
You must be signed in to change notification settings - Fork 11
/
trayiconwindow.cpp
120 lines (100 loc) · 3.14 KB
/
trayiconwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2020-2024 XMuli
// SPDX-GitHub: https://github.com/XMuli/shotx
// SPDX-Author: XMuli <xmulitech@gmail.com>
#include "trayiconwindow.h"
#include <QMenu>
#include <QDebug>
#include <QKeyEvent>
#include <QHotkey>
#include "screenshots.h"
TrayIconWindow::TrayIconWindow()
{
init();
connect(m_trayIcon, &QSystemTrayIcon::activated, this, &TrayIconWindow::onActiveTray);
// connect(m_actMainWindow, &QAction::triggered, this, &TrayIconWindow::onActMainWindow);
connect(m_actAbout, &QAction::triggered, this, &TrayIconWindow::onAbout);
QHotkey *hotkey = new QHotkey(QKeySequence("ctrl+F1"), true); // Alt 和 P 之间不能有空格
qDebug() << "Is Registered: " << hotkey->isRegistered();
connect(hotkey, &QHotkey::activated, this, &TrayIconWindow::onCreateScreen);
}
TrayIconWindow::~TrayIconWindow()
{
m_trayIcon->deleteLater(); // 后面仔细看下与 delete 的差异
}
void TrayIconWindow::init()
{
m_screenShot = nullptr;
m_menu = new QMenu();
// m_actMainWindow = new QAction(tr("偏好设置")); // 显示主窗口
// m_menu->addAction(m_actMainWindow);
m_actAbout = new QAction(tr("关于")); // 显示主窗口
m_menu->addAction(m_actAbout);
m_actExit = new QAction(tr("退出")); // 退出
m_menu->addSeparator();
m_menu->addAction(m_actExit);
m_trayIcon = new QSystemTrayIcon(this);
m_trayIcon->setIcon(QIcon(":/icons/logo.svg"));
m_trayIcon->setToolTip(tr("TrayIconWindow"));
m_trayIcon->setContextMenu(m_menu);
m_trayIcon->show();
}
void TrayIconWindow::onActiveTray(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger: { // 单击系统托盘
qDebug()<<"---单击---";
m_screenShot = ScreenShots::instances();
m_screenShot->show();
break;
}
// case QSystemTrayIcon::DoubleClick: { // 双击(在macOS上,仅在未设置上下文菜单的情况下才会发出双击,因为菜单是在鼠标按下时打开的)
// qDebug()<<"---双击---";
// }
// case QSystemTrayIcon::Context: { // 请求系统托盘上下文菜单
// qDebug()<<"---请求上下文---";
// m_menu->show();
// break;
// }
default:
break;
}
}
void TrayIconWindow::onActMainWindow()
{
qDebug()<<"---配置---";
m_mainWindow = new MainWindow();
m_mainWindow->show();
}
void TrayIconWindow::onAbout()
{
m_aboutWindow = new About();
m_aboutWindow->show();
}
void TrayIconWindow::onCreateScreen()
{
if (m_screenShot != nullptr)
return;
m_screenShot = ScreenShots::instances();
m_screenShot->show();
}
void TrayIconWindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Escape : {
qDebug()<<"---Qt::Key_Escape---";
if (m_screenShot != nullptr) {
m_screenShot->close();
m_screenShot = nullptr;
}
break;
}
case Qt::Key_F2: {
qDebug()<<"---Qt::Key_F2---";
break;
}
default:
break;
}
// QWidget::keyPressEvent(event);
}