-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
69 lines (62 loc) · 1.88 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initTimers(int max_time)
{
for (auto timer: {black_timer, white_timer}){
timer->setTimerType(Qt::PreciseTimer);
timer->setInterval(max_time);
connect(timer, &QTimer::timeout, this, &MainWindow::gameTimeout);
}
sec_counter->setTimerType(Qt::PreciseTimer);
sec_counter->setInterval(1000);
connect(sec_counter, &QTimer::timeout, this, &MainWindow::updateTimer);
ui->label->setText(zero_time.addMSecs(max_time).toString("mm:ss"));
ui->label_2->setText(zero_time.addMSecs(max_time).toString("mm:ss"));
black_remains = max_time;
white_remains = max_time;
}
void MainWindow::updateTimer()
{
QLabel* label = black_timer->isActive() ? ui->label : ui->label_2;
int rem = black_timer->isActive() ? black_timer->remainingTime() : white_timer->remainingTime();
label->setText(zero_time.addMSecs(rem).toString("mm:ss"));
}
void MainWindow::gameTimeout()
{
black_timer->stop();
white_timer->stop();
sec_counter->stop();
ui->label->setText(zero_time.addMSecs(max_time).toString("mm:ss"));
ui->label_2->setText(zero_time.addMSecs(max_time).toString("mm:ss"));
}
void MainWindow::on_start_button_clicked()
{
max_time = ui->lineEdit->text().toInt()*60000;
initTimers(max_time);
white_timer->start();
sec_counter->start();
}
void MainWindow::on_switch_button_clicked()
{
if (black_timer->isActive()){
black_remains = black_timer->remainingTime();
black_timer->stop();
white_timer->start(white_remains);
}
else{
white_remains = white_timer->remainingTime();
white_timer->stop();
black_timer->start(black_remains);
}
}