-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
196 lines (167 loc) · 6.63 KB
/
mainwindow.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "mainwindow.h"
#include "ui_mainwindow.h"
// Constructor: Initializes the main window and its components
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
// Setup UI components
ui->setupUi(this);
// Initialize timer and time
timer = new QTimer(this);
currentTime = new QTime(0, 0);
// Set window icon and fixed size
setWindowIcon(QIcon());
setFixedSize(sizeHint());
// Connect signals to slots
connect(timer, &QTimer::timeout, this, &MainWindow::updateTimer);
connect(ui->startButton, &QPushButton::clicked, this, &MainWindow::toggleTimer);
connect(ui->resetButton, &QPushButton::clicked, this, &MainWindow::resetTimer);
// Set style sheets for UI elements
setStyleSheet("QTabBar::tab { background-color: #ffffff; color: #000000; }");
setStyleSheet("border-radius: 10px;");
// Set window flags for custom appearance and behavior
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
setAttribute(Qt::WA_TranslucentBackground);
// Install event filters for buttons
ui->startButton->installEventFilter(this);
ui->resetButton->installEventFilter(this);
appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
filePath = appDataPath + "/log.json";
}
// Destructor: Cleans up allocated resources
MainWindow::~MainWindow() {
delete ui;
}
// Toggle the timer on and off
void MainWindow::toggleTimer() {
if (timer->isActive()) {
// Stop the timer and update button text
timer->stop();
ui->startButton->setText("Start");
// Record stop timestamp
QJsonObject timestamp;
timestamp["type"] = "STOP";
timestamp["time"] = QDateTime::currentDateTime().toString(Qt::ISODate);
timestampsArray.append(timestamp);
} else {
// Start the timer and update button text
timer->start(1000);
ui->startButton->setText("Stop");
timerEmpty = false;
ui->resetButton->setText("Reset");
// Record start timestamp
QJsonObject timestamp;
timestamp["type"] = "START";
timestamp["time"] = QDateTime::currentDateTime().toString(Qt::ISODate);
timestampsArray.append(timestamp);
}
}
// Reset the timer and handle log entries
void MainWindow::resetTimer() {
if (!timerEmpty && currentTime->minute() >= 0) {
// Prompt user for a label for the timestamp
bool ok;
QString label = QInputDialog::getText(this, tr("Label Your Time Stamp"), tr("Label:"), QLineEdit::Normal, "", &ok);
if (ok) {
while (label.isEmpty()) {
QMessageBox::warning(this, "Error", "Label cannot be empty. Please enter a valid label.");
label = QInputDialog::getText(this, tr("Label Your Time Stamp"), tr("Label:"), QLineEdit::Normal, "", &ok);
if (!ok) {
// Revert to paused state
ui->startButton->setText("Start");
return;
}
}
QFile logFile(filePath);
QJsonDocument logDoc;
QJsonArray logArray;
if (logFile.open(QIODevice::ReadOnly)) {
logDoc = QJsonDocument::fromJson(logFile.readAll());
if (logDoc.isArray()) {
logArray = logDoc.array();
}
logFile.close();
}
// Create new log entry
QJsonObject logEntry;
logEntry["label"] = label;
logEntry["total_hours"] = currentTime->hour();
logEntry["total_minutes"] = currentTime->minute();
logEntry["timestamps"] = timestampsArray;
timestampsArray = QJsonArray();
// Append new entry to log array and write to file
logArray.append(logEntry);
logDoc = QJsonDocument(logArray);
if (logFile.open(QIODevice::WriteOnly)) {
logFile.write(logDoc.toJson(QJsonDocument::Indented));
logFile.close();
}
} else {
// Revert to paused state
ui->startButton->setText("Start");
return;
}
// Reset the timer and update UI
timer->stop();
ui->startButton->setText("Start");
currentTime->setHMS(0, 0, 0);
ui->timeLabel->setText(currentTime->toString());
timerEmpty = true;
ui->resetButton->setText("Exit");
} else if (!timerEmpty) {
// Confirm reset action
QMessageBox::StandardButton reply = QMessageBox::question(this, "Exit", "You are about to reset the stopwatch, are you sure?", QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
// Reset the timer and update UI
timer->stop();
ui->startButton->setText("Start");
currentTime->setHMS(0, 0, 0);
ui->timeLabel->setText(currentTime->toString());
timerEmpty = true;
ui->resetButton->setText("Exit");
}
} else {
// Confirm exit action
QMessageBox::StandardButton reply = QMessageBox::warning(this, "Exit", "You are about to exit, are you sure?", QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
close();
}
}
}
// Update the timer display
void MainWindow::updateTimer() {
*currentTime = currentTime->addSecs(1);
ui->timeLabel->setText(currentTime->toString());
}
// Handle mouse press events for dragging the window
void MainWindow::mousePressEvent(QMouseEvent *event) {
QWidget *child = childAt(event->pos());
if (child != ui->startButton && child != ui->resetButton && event->button() == Qt::LeftButton) {
dragStartPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
event->accept();
} else {
dragStartPosition = QPoint();
}
}
// Handle mouse move events for dragging the window
void MainWindow::mouseMoveEvent(QMouseEvent *event) {
if (!(event->buttons() & Qt::LeftButton)) {
return;
}
if (!dragStartPosition.isNull()) {
move(event->globalPosition().toPoint() - dragStartPosition);
event->accept();
}
}
// Handle mouse release events for dragging the window
void MainWindow::mouseReleaseEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
dragStartPosition = QPoint();
}
}
// Event filter for handling custom button events
bool MainWindow::eventFilter(QObject *watched, QEvent *event) {
if (watched == ui->startButton || watched == ui->resetButton) {
return false;
}
return QMainWindow::eventFilter(watched, event);
}