-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
104 lines (87 loc) · 3.48 KB
/
Copy pathapp.cpp
File metadata and controls
104 lines (87 loc) · 3.48 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
93
94
95
96
97
98
99
100
101
102
103
104
#include "app.h"
#include <QDateTime>
#include <QScreen>
#include <QGuiApplication>
#include <QHBoxLayout>
#include <QGraphicsDropShadowEffect>
#include "macos_window.h"
app::app(QWidget *parent): QMainWindow(parent)
{
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool | Qt::WindowTransparentForInput | Qt::X11BypassWindowManagerHint);
setAttribute(Qt::WA_TranslucentBackground);
QWidget *container = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout(container);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(20);
auto setupLabel = [this](const QString &color) {
QLabel *label = new QLabel(this);
label->setFixedWidth(500);
label->setAlignment(Qt::AlignCenter);
label->setStyleSheet(QString("color: %1;"
"font-family: 'Consolas', 'Menlo', monospace;"
"font-size: 50px;"
"font-weight: bold;"
"background: transparent;").arg(color));
// Change font-size when needed
QGraphicsDropShadowEffect *outline = new QGraphicsDropShadowEffect(this);
outline->setBlurRadius(30);
outline->setColor(Qt::white);
outline->setOffset(0);
label->setGraphicsEffect(outline);
return label;
};
localLabel = setupLabel("#CCFF9800");
utcLabel = setupLabel("#CC4CAF50");
layout->addWidget(localLabel);
layout->addWidget(utcLabel);
setCentralWidget(container);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &app::updateTime);
timer->start(1000);
updateTime();
this->adjustSize();
QScreen *screen = QGuiApplication::primaryScreen();
if (screen) {
int x = (screen->geometry().width() - this->width()) / 2;
move(x, 75); // Gap between top of the screen and time text, default to 75. Adjust when needed
}
setupTrayIcon();
// Dealing with Mac OS's floating view issue
#ifdef Q_OS_MAC
QTimer::singleShot(0, this, [this]() { setMacOSFloatOnTop(this->windowHandle()); });
#endif
}
void app::updateTime()
{
// Local
QDateTime now = QDateTime::currentDateTime();
QString localTime = now.toString("hh:mm:ss");
QString localDate = now.toString("d MMM yyyy").toUpper();
localLabel->setText(QString(
"NOW %1<br>"
"<span style='font-size: %2px; font-weight: normal;'>%3</span>")
.arg(localTime)
.arg(qRound(65 * 0.5))
.arg(localDate));
// UTC
QDateTime utc = QDateTime::currentDateTimeUtc();
QString utcTime = utc.toString("hh:mm:ss");
QString utcDate = utc.toString("d MMM yyyy").toUpper();
utcLabel->setText(QString(
"UTC %1<br>"
"<span style='font-size: %2px; font-weight: normal;'>%3</span>")
.arg(utcTime)
.arg(qRound(65 * 0.5))
.arg(utcDate));
}
void app::setupTrayIcon() {
trayMenu = new QMenu(this);
QAction *quitAction = new QAction("Quit!", this);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
trayMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayMenu);
trayIcon->setIcon(QIcon(":/AppIcon.png"));
trayIcon->show();
}
app::~app() = default;