-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
92 lines (77 loc) · 2.44 KB
/
mainwindow.cpp
File metadata and controls
92 lines (77 loc) · 2.44 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QScreen>
#include <QIcon>
#include <QPixmap>
#include <QPainter>
#include <QColor>
#include <QMessageBox>
#include <QDateTime>
#include <QMenu>
#include <QAction>
#include <QCloseEvent>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, notificationCount(0)
{
ui->setupUi(this);
connect(ui->notifyButton, &QPushButton::clicked, this, &MainWindow::onNotifyClicked);
QScreen *screen = QApplication::primaryScreen();
QRect sg = screen->availableGeometry();
move(sg.center().x() - width() / 2, sg.center().y() - height() / 2);
setupTrayIcon();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
event->accept();
QApplication::quit();
}
void MainWindow::setupTrayIcon()
{
QPixmap iconPixmap(32, 32);
iconPixmap.fill(Qt::transparent);
QPainter painter(&iconPixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(QColor("#0078d4"));
painter.setPen(Qt::NoPen);
painter.drawEllipse(2, 2, 28, 28);
painter.end();
QIcon appIcon(iconPixmap);
setWindowIcon(appIcon);
if (!QSystemTrayIcon::isSystemTrayAvailable())
return;
trayIcon = new QSystemTrayIcon(appIcon, this);
trayIcon->setToolTip("UniDummyapp");
QMenu *trayMenu = new QMenu(this);
connect(trayMenu->addAction("Show"), &QAction::triggered, this, &QWidget::show);
connect(trayMenu->addAction("Quit"), &QAction::triggered, qApp, &QApplication::quit);
connect(trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::onTrayIconActivated);
trayIcon->setContextMenu(trayMenu);
trayIcon->show();
}
void MainWindow::onNotifyClicked()
{
notificationCount++;
QString title = "UniDummyapp";
QString message = QString("Notification #%1 — %2")
.arg(notificationCount)
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"));
if (trayIcon && QSystemTrayIcon::isSystemTrayAvailable() && QSystemTrayIcon::supportsMessages())
trayIcon->showMessage(title, message, QSystemTrayIcon::Information, 4000);
else
QMessageBox::information(this, title, message);
}
void MainWindow::onTrayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::DoubleClick) {
show();
raise();
activateWindow();
}
}