summaryrefslogtreecommitdiffstats
path: root/src/timeoutdialog.cpp
blob: 6f12ca277c94202745e813dbfce29adb248f8d46 (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
// Copyright 2013, University of Freiburg,
// Author: Manuel Schneider <ms1144>

#include "timeoutdialog.h"
#include "ui_timeoutdialog.h"
#include <QProgressBar>
#include <QDebug>
#include <QPushButton>

TimeOutDialog::TimeOutDialog(int time, QWidget *parent)
	: QDialog(parent), _ui(new Ui::TimeOutDialog), _time(time)
{
	_ui->setupUi(this);
	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;
		}
	});
	QObject::connect(_ui->buttonBox, &QDialogButtonBox::rejected, [this]() {
		_timer.stop();
		_time = 0; // So wasCanceled() returns true
	});

	// 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();
}

void TimeOutDialog::update()
{
	--_time;
	if (_time == 0) {
		_timer.stop();
	} else {
		_ui->progressBar->setValue(_time);
	}
}