-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
126 lines (120 loc) · 3.91 KB
/
main.cpp
File metadata and controls
126 lines (120 loc) · 3.91 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <QDir>
#include <QFile>
#include <QLabel>
#include <QTimer>
#include <QColor>
#include <QWidget>
#include <QPalette>
#include <QDateTime>
#include <QKeyEvent>
#include <QJsonObject>
#include <QVBoxLayout>
#include <QApplication>
#include <QJsonDocument>
#include <QFontDatabase>
struct ClockConfig {
QString foreground = "#00ff00";
QString background = "#000000";
QString fontFile = "font.ttf";
int fontSize = 72;
};
QString getResourcePath(const QString &filename) {
#ifdef Q_OS_MAC
return QDir(QCoreApplication::applicationDirPath() + "/../Resources").filePath(filename);
#else
return QDir(QCoreApplication::applicationDirPath()).filePath(filename);
#endif
}
ClockConfig loadConfig() {
ClockConfig config;
QFile file(getResourcePath("config.json"));
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QByteArray data = file.readAll();
file.close();
QJsonDocument doc = QJsonDocument::fromJson(data);
if (doc.isObject()) {
QJsonObject obj = doc.object();
if (obj.contains("foreground") && obj["foreground"].isString())
config.foreground = obj["foreground"].toString();
if (obj.contains("background") && obj["background"].isString())
config.background = obj["background"].toString();
if (obj.contains("font") && obj["font"].isString())
config.fontFile = obj["font"].toString();
if (obj.contains("fontSize") && obj["fontSize"].isDouble())
config.fontSize = obj["fontSize"].toInt();
}
}
return config;
}
class Clock : public QWidget {
Q_OBJECT
public:
Clock(const ClockConfig &config) : cfg(config) {
QString fontPath = getResourcePath(cfg.fontFile);
int id = QFontDatabase::addApplicationFont(fontPath);
QString family;
if (id != -1) {
family = QFontDatabase::applicationFontFamilies(id).value(0);
}
QFont font;
int size = (cfg.fontSize > 0) ? cfg.fontSize : 72;
if (!family.isEmpty()) {
font = QFont(family, size);
} else {
font = QFont("Sans Serif", size);
}
timeLabel = new QLabel(this);
timeLabel->setAlignment(Qt::AlignCenter);
timeLabel->setFont(font);
timeLabel->setStyleSheet(QString("QLabel { color: %1; background-color: transparent; }")
.arg(cfg.foreground));
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(timeLabel);
setLayout(layout);
setWindowTitle("clock");
setWindowIcon(QIcon(":/favicon.ico"));
setAutoFillBackground(true);
QPalette pal = palette();
pal.setColor(QPalette::Window, QColor(cfg.background));
setPalette(pal);
currentTime = QDateTime::currentDateTime();
updateLabel();
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &Clock::tick);
int msToNextSecond = 1000 - QTime::currentTime().msec();
QTimer::singleShot(msToNextSecond, this, [this]() {
timer->start(1000);
tick();
});
}
protected:
void keyPressEvent(QKeyEvent *event) override {
if (event->key() == Qt::Key_Return && (event->modifiers() & Qt::AltModifier)) {
if (isFullScreen())
showNormal();
else
showFullScreen();
}
}
private slots:
void tick() {
currentTime = currentTime.addSecs(1);
updateLabel();
}
private:
void updateLabel() {
timeLabel->setText(currentTime.toString("HH:mm:ss"));
}
QLabel *timeLabel;
QTimer *timer;
QDateTime currentTime;
ClockConfig cfg;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
ClockConfig cfg = loadConfig();
Clock clock(cfg);
clock.showFullScreen();
return app.exec();
}
#include "main.moc"