#include "countdownwidget.h"
CountdownWidget::CountdownWidget(QWidget *parent) : QWidget(parent), m_remainingSeconds(5)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    m_timer = new QTimer(this);
    connect(m_timer, &QTimer::timeout, this, &CountdownWidget::onTimerTimeout);
    m_timer->start(1000);
}
CountdownWidget::~CountdownWidget()
{
    if (m_timer->isActive()) {
        m_timer->stop();
    }
}
void CountdownWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);


    QBrush brush(Qt::red);
    painter.setBrush(brush);
    painter.drawEllipse(rect());
    QFont font;
    font.setPointSize(58);
    font.setBold(true);
    painter.setFont(font);
    painter.setPen(Qt::white);
    if (m_remainingSeconds > 0) {
        painter.drawText(rect(), Qt::AlignCenter, QString::number(m_remainingSeconds));
    } else {
        painter.drawText(rect(), Qt::AlignCenter, "0");
    }
}

void CountdownWidget::onTimerTimeout()
{
    m_remainingSeconds--;
    if (m_remainingSeconds == 0) {
        m_timer->stop();
        close();
        emit countdownFinished(); // 发出自定义信号
    }
    update();
}
