2018-08-16 15:19:43 -04:00
|
|
|
#include "SelfDestructMessageBox.h"
|
|
|
|
|
|
|
|
SelfDestructMessageBox::SelfDestructMessageBox(
|
|
|
|
int timeout,
|
|
|
|
const QString& title,
|
|
|
|
const QString& text,
|
|
|
|
QMessageBox::Icon icon,
|
|
|
|
QMessageBox::StandardButtons buttons,
|
|
|
|
QMessageBox::StandardButton defaultButton,
|
2019-02-07 14:31:11 -05:00
|
|
|
bool show_countdown,
|
2018-08-16 15:19:43 -04:00
|
|
|
QWidget* parent,
|
|
|
|
Qt::WindowFlags flags)
|
|
|
|
: QMessageBox(icon, title, text, buttons, parent, flags),
|
|
|
|
m_timeout(timeout),
|
2019-02-07 14:31:11 -05:00
|
|
|
m_text(text),
|
|
|
|
m_show_countdown(show_countdown)
|
2018-08-16 15:19:43 -04:00
|
|
|
{
|
2020-03-28 22:44:21 -04:00
|
|
|
setDefaultButton(defaultButton);
|
|
|
|
|
2018-08-16 15:19:43 -04:00
|
|
|
connect(&m_timer, &QTimer::timeout, this, &SelfDestructMessageBox::tick);
|
|
|
|
m_timer.setInterval(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelfDestructMessageBox::showEvent(QShowEvent* event)
|
|
|
|
{
|
|
|
|
tick();
|
|
|
|
m_timer.start();
|
|
|
|
QMessageBox::showEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelfDestructMessageBox::tick(){
|
|
|
|
m_timeout--;
|
|
|
|
|
|
|
|
if(m_timeout){
|
2019-02-07 14:31:11 -05:00
|
|
|
if(m_show_countdown){
|
|
|
|
setText(m_text.arg(m_timeout));
|
2020-03-28 22:44:21 -04:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// if we don't show the countdown in the text, show it on the default button
|
|
|
|
auto d = defaultButton();
|
|
|
|
if(d){
|
|
|
|
auto text = d->text();
|
|
|
|
if(text.contains(" (")){
|
|
|
|
text = text.split(" (").first();
|
|
|
|
}
|
|
|
|
|
|
|
|
text = text.append(QString(" (%1) ").arg(m_timeout));
|
|
|
|
d->setText(text);
|
|
|
|
}
|
|
|
|
|
2019-02-07 14:31:11 -05:00
|
|
|
}
|
2018-08-16 15:19:43 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-28 22:44:21 -04:00
|
|
|
// stop the timer
|
2018-08-16 15:19:43 -04:00
|
|
|
m_timer.stop();
|
2020-03-28 22:44:21 -04:00
|
|
|
|
|
|
|
// click the default
|
|
|
|
auto d = defaultButton();
|
|
|
|
if(d){
|
|
|
|
d->click();
|
|
|
|
}
|
2018-08-16 15:19:43 -04:00
|
|
|
}
|