-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimeEditWidget.cpp
61 lines (48 loc) · 1.52 KB
/
TimeEditWidget.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
#include <QKeyEvent>
#include <QTimer>
#include "TimeEditWidget.h"
#include "ui_TimeEditWidget.h"
TimeEditWidget::TimeEditWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::TimeEditWidget)
{
ui->setupUi(this);
ui->secondsSpinBox->installEventFilter(this);
ui->minutesSpinBox->installEventFilter(this);
ui->hoursSpinBox->installEventFilter(this);
}
TimeEditWidget::~TimeEditWidget()
{
delete ui;
}
void TimeEditWidget::setTime(int time) {
int seconds = time % 60;
int minutes = (time / 60) % 60;
int hours = time / 3600;
ui->secondsSpinBox->setValue(seconds);
ui->minutesSpinBox->setValue(minutes);
ui->hoursSpinBox->setValue(hours);
}
void TimeEditWidget::setFocus() {
ui->hoursSpinBox->setFocus();
ui->hoursSpinBox->selectAll();
}
int TimeEditWidget::getTime() {
int seconds = ui->secondsSpinBox->value();
int minutes = ui->minutesSpinBox->value();
int hours = ui->hoursSpinBox->value();
int time = seconds + 60 * minutes + 3600 * hours;
return time;
}
bool TimeEditWidget::eventFilter(QObject *obj, QEvent *event) {
if(obj == ui->secondsSpinBox || obj == ui->minutesSpinBox || obj == ui->hoursSpinBox) {
if(event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
emit returnPressed();
return true;
}
}
}
return QWidget::eventFilter(obj, event);
}