-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
155 lines (130 loc) · 4.59 KB
/
Copy pathmainwindow.cpp
File metadata and controls
155 lines (130 loc) · 4.59 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
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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->pushButton->setText(startTimerString);
ui->radioButtonTimer->setText(modeTimerString);
ui->radioButtonAlarm->setText(modeAlarmString);
ui->radioButtonStopwatch->setText(modeStopwatchString);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slotTimer()
{
QTime targetTime = ui->timeEdit->time();
QTime currentTime;
QString modeString;
switch (MainWindow::timerMode) {
case 0:
currentTime = timerTime = timerTime.addSecs(1);
modeString = modeTimerString + ": ";
break;
case 1:
currentTime = QTime::currentTime();
modeString = modeAlarmString + ": ";
break;
case 2:
timerTime = timerTime.addSecs(1);
modeString = modeStopwatchString + ": ";
break;
default:
break;
}
if (timerMode < 2) {
int secondsRemaining = targetTime.secsTo(currentTime); // Вычисляем разницу в секундах
if (secondsRemaining < 0) {
// Если заданное время меньше текущего, вычитаем
secondsRemaining = -secondsRemaining; // Делаем положительным
}
// Преобразуем оставшиеся секунды в QTime
QTime remainingTime = QTime(0, 0).addSecs(secondsRemaining);
if (remainingTime != QTime(0, 0, 0, 0)) {
sendNotification(modeString + remainingTime.toString("HH:mm:ss") + " / " + targetTime.toString("HH:mm:ss"), NULL);
}
else {
sendNotification("The countdown is over!", NULL);
stop();
}
}
else {
sendNotification(modeString + timerTime.toString("HH:mm:ss"), NULL);
}
}
void MainWindow::on_pushButton_clicked()
{
if (timerState == false) start();
else stop();
}
void MainWindow::sendNotification(const QString &summary, const QString &body) {
QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
"Notify");
message << QString("sillyTimer") // app_name
<< static_cast<quint32>(QCoreApplication::applicationPid()) // id (uint32)
<< QString() // icon (string)
<< summary // summary (string)
<< body // body (string)
<< QStringList() // actions (array of string)
<< QVariantMap() // hints (dictionary of string to variant)
<< 2000; // expire_timeout (int32)
QDBusMessage reply = QDBusConnection::sessionBus().call(message, QDBus::Block);
if (reply.type() == QDBusMessage::ErrorMessage) {
qDebug() << "Error:" << reply.errorMessage();
}
}
void MainWindow::start() {
timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(slotTimer()));
timer->start(1000);
timerState = true;
ui->pushButton->setText(stopTimerString);
ui->radioButtonTimer->setDisabled(true);
ui->radioButtonAlarm->setDisabled(true);
ui->radioButtonStopwatch->setDisabled(true);
}
void MainWindow::stop() {
timer->stop();
timerState = false;
timerTime = QTime(0,0,0);
disconnect(timer, SIGNAL(timeout()), this, SLOT(slotTimer()));
delete timer;
ui->pushButton->setText(startTimerString);
ui->radioButtonTimer->setEnabled(true);
ui->radioButtonAlarm->setEnabled(true);
ui->radioButtonStopwatch->setEnabled(true);
}
void MainWindow::on_radioButtonTimer_clicked()
{
ui->radioButtonAlarm->setChecked(false);
ui->radioButtonStopwatch->setChecked(false);
ui->timeEdit->setEnabled(true);
ui->timeEdit->setTime(QTime(0, 15, 0));
timerMode = 0;
}
void MainWindow::on_radioButtonAlarm_clicked()
{
ui->radioButtonTimer->setChecked(false);
ui->radioButtonStopwatch->setChecked(false);
int currentHour = QTime::currentTime().hour() + 1;
ui->timeEdit->setEnabled(true);
ui->timeEdit->setTime(QTime(currentHour, 30, 0));
timerMode = 1;
}
void MainWindow::on_radioButtonStopwatch_clicked()
{
ui->radioButtonTimer->setChecked(false);
ui->radioButtonAlarm->setChecked(false);
ui->timeEdit->setDisabled(true);
ui->timeEdit->setTime(QTime(0,0,0));
timerMode = 2;
}
void MainWindow::closeEvent(QCloseEvent* event) {
event->ignore();
this->hide();
}