-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
116 lines (94 loc) · 2.98 KB
/
main.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
#include "nazar.h"
#ifdef Q_OS_LINUX
#include <unistd.h>
#endif
QRect centeredRect(const QPoint &pos, const QSize &size)
{
const auto topLeft = pos - QPoint(size.width() / 2, size.height() / 2);
return QRect(topLeft, size);
}
QPixmap screenshot(const QRect &rect)
{
QPixmap result(rect.size());
result.fill(Qt::black);
const QList<QScreen *> screens = QApplication::screens();
for (int i = 0; i < screens.size(); i++) {
auto s = screens[i];
if (s->geometry().intersects(rect)) {
auto part = s->geometry().intersected(rect);
auto winid = QApplication::desktop()->screen(i)->topLevelWidget()->winId();
auto pix = s->grabWindow(winid, part.x(), part.y(), part.width(), part.height());
QPainter painter(&result);
painter.drawPixmap(part.topLeft() - rect.topLeft(), pix);
}
}
return result;
}
void drawCrossHairs(QPainter *painter, const QRect &rect)
{
PainterStateSaver saver(painter);
painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
painter->setBackground(Qt::white);
painter->setBackgroundMode(Qt::OpaqueMode);
painter->drawLine(rect.left(), rect.center().y(),
rect.right(), rect.center().y());
painter->drawLine(rect.center().x(), rect.top(),
rect.center().x(), rect.bottom());
}
QPixmap gradientPixmap(const QSize &s, const QGradient &g)
{
QPixmap pix(s);
pix.fill(Qt::transparent);
QPainter p(&pix);
p.fillRect(pix.rect(), g);
return pix;
}
int main(int argc, char *argv[])
{
#ifdef Q_OS_LINUX
if (daemon(0, 0) == -1) {
perror("daemon");
}
#endif
#ifdef Q_OS_WIN
const int iconSize = 16;
#else
const int iconSize = 22;
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
#endif
QApplication a(argc, argv);
QLinearGradient gradient(0, 0, iconSize, iconSize);
gradient.setStops({
{ 0.0, Qt::red },
{ 0.5, Qt::green },
{ 1.0, Qt::blue }
});
const auto icon = gradientPixmap(QSize(iconSize, iconSize), gradient);
a.setWindowIcon(icon);
Viewer w;
QSystemTrayIcon tray;
QMenu trayMenu;
tray.setIcon(icon);
auto showAction = trayMenu.addAction("Show...");
QObject::connect(showAction, &QAction::triggered, &w, &QWidget::show);
auto exitAction = trayMenu.addAction("Exit");
QObject::connect(exitAction, &QAction::triggered, &a, &QApplication::quit);
tray.setContextMenu(&trayMenu);
w.show();
tray.show();
QObject::connect(&tray, &QSystemTrayIcon::activated, [&] (QSystemTrayIcon::ActivationReason reason) {
switch (reason) {
case QSystemTrayIcon::Context:
break;
case QSystemTrayIcon::Unknown:
case QSystemTrayIcon::DoubleClick:
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::MiddleClick:
w.show();
break;
}
});
return a.exec();
}