FaceAccess/Linguist/utility/DndModeCountDownItem.cpp
2024-07-11 11:27:12 +08:00

64 lines
2.0 KiB
C++

#include "DndModeCountDownItem.h"
#include "rw_zlog.h"
#include <QHBoxLayout>
#include <QLabel>
#include <sstream>
DndModeCountDownItem::DndModeCountDownItem(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) {
m_label = new QLabel(this);
QFont font;
m_label->setMargin(0);
font.setPointSize(20);
m_label->setFont(font);
auto hint = new QLabel(this);
font.setPointSize(12);
hint->setFont(font);
hint->setText("后结束");
hint->setMargin(0);
auto layout = new QHBoxLayout(this);
layout->addWidget(m_label);
layout->addWidget(hint);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
QPalette palette(this->palette());
palette.setColor(QPalette::WindowText, Qt::black);
setPalette(palette);
}
void DndModeCountDownItem::setEndTime(const std::chrono::system_clock::time_point &endTime) {
auto now = std::chrono::system_clock::now();
m_endTime = endTime;
if (m_endTime > now) {
if (m_timerId < 0) m_timerId = startTimer(500);
} else {
if (m_timerId >= 0) {
killTimer(m_timerId);
m_timerId = -1;
}
}
}
void DndModeCountDownItem::timerEvent(QTimerEvent *event) {
constexpr int SencondsOfMinute = 60;
constexpr int SencondsOfHour = SencondsOfMinute * 60;
constexpr int SencondsOfDay = SencondsOfHour * 24;
using namespace std::chrono;
auto now = system_clock::now();
int duration = duration_cast<seconds>(m_endTime - now).count();
if (duration <= 0) {
killTimer(m_timerId);
m_timerId = -1;
}
int days = duration / SencondsOfDay;
int hours = (duration - days * SencondsOfDay) / SencondsOfHour;
int minutes = (duration - days * SencondsOfDay - hours * SencondsOfHour) / SencondsOfMinute;
int sec = (duration - days * SencondsOfDay - hours * SencondsOfHour - minutes * SencondsOfMinute);
m_label->setText(
QString("%1:%2:%3").arg(hours, 2, 10, QLatin1Char('0')).arg(minutes, 2, 10, QLatin1Char('0')).arg(sec, 2, 10, QLatin1Char('0')));
}