File: SelfDestructMessageBox.cpp

package info (click to toggle)
js8call 2.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,416 kB
  • sloc: cpp: 563,285; f90: 9,265; ansic: 937; python: 132; sh: 93; makefile: 6
file content (63 lines) | stat: -rw-r--r-- 1,554 bytes parent folder | download | duplicates (3)
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
#include "SelfDestructMessageBox.h"

SelfDestructMessageBox::SelfDestructMessageBox(
        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_timeout(timeout),
    m_text(text),
    m_show_countdown(show_countdown)
{
    setDefaultButton(defaultButton);

    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){
        if(m_show_countdown){
            setText(m_text.arg(m_timeout));
        } 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);
            }

        }
        return;
    }

    // stop the timer
    m_timer.stop();

    // click the default
    auto d = defaultButton();
    if(d){
        d->click();
    }
}