2018-08-16 15:19:43 -04:00
|
|
|
#include "SelfDestructMessageBox.h"
|
|
|
|
|
|
|
|
|
|
SelfDestructMessageBox::SelfDestructMessageBox(
|
2026-01-01 15:12:24 -06:00
|
|
|
int timeout, const QString &title, const QString &text,
|
|
|
|
|
QMessageBox::Icon icon, QMessageBox::StandardButtons buttons,
|
|
|
|
|
QMessageBox::StandardButton defaultButton, bool show_countdown,
|
|
|
|
|
QWidget *parent, Qt::WindowFlags flags)
|
|
|
|
|
: QMessageBox(icon, title, text, buttons, parent, flags),
|
|
|
|
|
m_show_countdown(show_countdown), m_timeout(timeout), m_text(text) {
|
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);
|
2025-07-16 22:08:39 -04:00
|
|
|
|
2026-01-01 15:12:24 -06:00
|
|
|
connect(this, &SelfDestructMessageBox::finished, this,
|
|
|
|
|
&SelfDestructMessageBox::stopTimer);
|
2018-08-16 15:19:43 -04:00
|
|
|
}
|
|
|
|
|
|
2026-01-01 15:12:24 -06:00
|
|
|
void SelfDestructMessageBox::showEvent(QShowEvent *event) {
|
2018-08-16 15:19:43 -04:00
|
|
|
tick();
|
|
|
|
|
m_timer.start();
|
|
|
|
|
QMessageBox::showEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 15:12:24 -06:00
|
|
|
void SelfDestructMessageBox::stopTimer() { m_timer.stop(); }
|
2025-07-16 22:08:39 -04:00
|
|
|
|
2026-01-01 15:12:24 -06:00
|
|
|
void SelfDestructMessageBox::tick() {
|
2018-08-16 15:19:43 -04:00
|
|
|
m_timeout--;
|
|
|
|
|
|
2026-01-01 15:12:24 -06:00
|
|
|
if (m_timeout) {
|
|
|
|
|
if (m_show_countdown) {
|
2019-02-07 14:31:11 -05:00
|
|
|
setText(m_text.arg(m_timeout));
|
2020-03-28 22:44:21 -04:00
|
|
|
} else {
|
|
|
|
|
|
2026-01-01 15:12:24 -06:00
|
|
|
// if we don't show the countdown in the text, show it on the
|
|
|
|
|
// default button
|
2020-03-28 22:44:21 -04:00
|
|
|
auto d = defaultButton();
|
2026-01-01 15:12:24 -06:00
|
|
|
if (d) {
|
2020-03-28 22:44:21 -04:00
|
|
|
auto text = d->text();
|
2026-01-01 15:12:24 -06:00
|
|
|
if (text.contains(" (")) {
|
2020-03-28 22:44:21 -04:00
|
|
|
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();
|
2026-01-01 15:12:24 -06:00
|
|
|
if (d) {
|
2020-03-28 22:44:21 -04:00
|
|
|
d->click();
|
|
|
|
|
}
|
2018-08-16 15:19:43 -04:00
|
|
|
}
|