-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
85 lines (77 loc) · 1.91 KB
/
Copy pathMainWindow.cpp
File metadata and controls
85 lines (77 loc) · 1.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
#include "MainWindow.h"
#include "qnamespace.h"
#include "qwidget.h"
#include <QDebug>
#include <QKeyEvent>
#include <QTime>
#include <ctime>
#include <string>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow), timer(new QElapsedTimer)
{
timerStarted = false;
presses = 0;
ui->setupUi(this);
ui->resetButton->setFocusPolicy(Qt::NoFocus);
totalPressLabelText = ui->totalPressLabel->text();
rateLabelText = ui->rateLabel->text();
timeLabelText = ui->timeLabel->text();
QObject::connect(ui->resetButton, SIGNAL(clicked()), this, SLOT(reset()));
}
MainWindow::~MainWindow()
{
delete ui;
delete timer;
}
void MainWindow::update()
{
int time;
if (timerStarted == false)
{
timerStarted = true;
timer->start();
}
++presses;
time = timer->elapsed();
ui->timeLabel->setText(timeString(time));
ui->rateLabel->setText(rateString(time, presses));
ui->totalPressLabel->setText(totalPressString(presses));
}
void MainWindow::reset()
{
timerStarted = false;
presses = 0;
ui->rateLabel->setText(rateLabelText);
ui->timeLabel->setText(timeLabelText);
ui->totalPressLabel->setText(totalPressLabelText);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space)
{
update();
}
}
QString MainWindow::timeString(unsigned int n)
{
int s, msecs;
QString ret = timeLabelText;
s = n / 1000;
msecs = n - s * 1000;
ret.append(QString::number(s));
ret.append(".");
ret.append(QString::number(msecs));
return ret;
}
QString MainWindow::totalPressString(int presses)
{
QString ret = totalPressLabelText;
ret.append(QString::number(presses));
return ret;
}
QString MainWindow::rateString(double timeMsec, double presses)
{
QString ret = rateLabelText;
ret.append(QString::number(presses / (timeMsec / 1000.0)));
return ret;
}