forked from CountMurphy/QTalarm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdowntimer.cpp
More file actions
69 lines (54 loc) · 1.92 KB
/
Copy pathcountdowntimer.cpp
File metadata and controls
69 lines (54 loc) · 1.92 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
#include "countdowntimer.h"
#include "ui_countdowntimer.h"
#include <QSettings>
#include <QPushButton>
#include <QToolButton>
#include <QIcon>
#include <QDebug>
CountdownTimer::CountdownTimer(QWidget *parent) :
QDialog(parent),
ui(new Ui::CountdownTimer)
{
ui->setupUi(this);
// Initialize the 6 preset timers
initPresets();
}
CountdownTimer::~CountdownTimer()
{
delete ui;
}
int CountdownTimer::getTotalSeconds() const {
QTime t = ui->timeEdit->time();
return (t.hour() * 3600) + (t.minute() * 60) + t.second();
}
void CountdownTimer::initPresets() {
QSettings settings;
QTime defaultTimes[6] = {
QTime(0, 30, 0),
QTime(0, 5, 0),
QTime(1, 0, 0),
QTime(0, 10, 0),
QTime(1, 30, 0),
QTime(0, 15, 0)
};
for (int i = 0; i < 6; ++i) {
QString settingKey = QString("presetTimer_%1").arg(i);
QPushButton* timeBtn = findChild<QPushButton*>(QString("timeBtn_%1").arg(i));
QToolButton* saveBtn = findChild<QToolButton*>(QString("saveBtn_%1").arg(i));
if (timeBtn && saveBtn) {
saveBtn->setIcon(QIcon::fromTheme("document-save"));
QTime savedTime = settings.value(settingKey, defaultTimes[i]).toTime();
timeBtn->setText(savedTime.toString("HH:mm:ss"));
connect(timeBtn, &QPushButton::clicked, this, [this, timeBtn]() {
ui->timeEdit->setTime(QTime::fromString(timeBtn->text(), "HH:mm:ss"));
});
connect(saveBtn, &QToolButton::clicked, this, [this, settingKey, timeBtn]() {
QTime currentTime = ui->timeEdit->time();
timeBtn->setText(currentTime.toString("HH:mm:ss"));
QSettings settings;
settings.setValue(settingKey, currentTime);
qDebug() << "Saved preset" << settingKey << ":" << currentTime;
});
}
}
}