blob: 9eefc421e4b1211c513d8551fc089aac60cb2ee3 (
plain) (
blame)
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
|
// Copyright 2013, University of Freiburg,
// Author: Manuel Schneider <ms1144>
#include "timeoutdialog.h"
#include "ui_timeoutdialog.h"
#include <QProgressBar>
#include <QDebug>
#include <QPushButton>
#include <QKeyEvent>
TimeOutDialog::TimeOutDialog(int time, QWidget *parent)
: QDialog(parent), _ui(new Ui::TimeOutDialog), _time(time)
{
_ui->setupUi(this);
this->installEventFilter(this);
_ui->buttonBox->installEventFilter(this);
for (auto b : _ui->buttonBox->buttons()) {
b->installEventFilter(this);
b->setFocusPolicy(Qt::NoFocus);
}
this->setFocus();
QObject::connect(&_timer, &QTimer::timeout, this, &TimeOutDialog::update);
QObject::connect(_ui->buttonBox, &QDialogButtonBox::clicked, [this](QAbstractButton *button) {
_timer.stop();
if (button == _ui->buttonBox->button(QDialogButtonBox::Discard)) {
_time = 0;
}
});
// QProgressDialog takes ownership of QProgressBar
_ui->progressBar->setMaximum(_time);
_ui->progressBar->setValue(_time);
setWindowTitle(" ");
setWindowFlag(Qt::WindowStaysOnTopHint, true);
_timer.start(1000);
}
void TimeOutDialog::hideEvent(QHideEvent *e)
{
QDialog::hideEvent(e);
_timer.stop();
}
bool TimeOutDialog::eventFilter(QObject *, QEvent *event)
{
if (event->type() != QEvent::KeyPress)
return false;
if (!_timer.isActive())
return false;
QKeyEvent *e = static_cast<QKeyEvent *>(event);
if (e->key() == Qt::Key_Escape) {
// Handle as discard
_time = 0;
_timer.stop();
return true;
}
if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
// Handle as accept
_timer.stop();
return true;
}
return false;
}
void TimeOutDialog::update()
{
--_time;
if (_time == 0) {
_timer.stop();
} else {
_ui->progressBar->setValue(_time);
}
}
|