qt/ledset/waitingdlg.cpp

120 lines
3.4 KiB
C++
Raw Permalink Normal View History

2023-06-06 12:13:33 +08:00
#include "waitingdlg.h"
#include "gutil/qgui.h"
#include <QTimerEvent>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QPainter>
#include <QPainterPath>
WaitingDlg::WaitingDlg(QWidget *parent, QString text, QString sucText) : QDialog{parent, Qt::Tool}, sucText(sucText) {
setAttribute(Qt::WA_DeleteOnClose);
setModal(true);
auto vBox = new VBox(this);
mIndicator = new WaitingIndicator(this);
mIndicator->setFixedSize(120, 120);
vBox->addWidget(mIndicator, 0, Qt::AlignCenter);
fdText = new QLabel(text);
fdText->setAlignment(Qt::AlignCenter);
gFont(fdText, 18, true);
vBox->addWidget(fdText);
}
2023-06-07 19:11:03 +08:00
void WaitingDlg::closeEvent(QCloseEvent *event) {
if(showTimerId) {
killTimer(showTimerId);
showTimerId = 0;
}
if(closeTimerId) {
killTimer(closeTimerId);
closeTimerId = 0;
}
QDialog::closeEvent(event);
}
2023-06-06 12:13:33 +08:00
void WaitingDlg::timerEvent(QTimerEvent *event) {
if(showTimerId==event->timerId()) {
killTimer(showTimerId);
showTimerId = 0;
show();
2023-06-07 19:11:03 +08:00
raise();
activateWindow();
2023-06-06 12:13:33 +08:00
} else if(closeTimerId==event->timerId()) {
killTimer(closeTimerId);
closeTimerId = 0;
close();
} else QDialog::timerEvent(event);
}
void WaitingDlg::showLater() {
2023-06-07 19:11:03 +08:00
if(isVisible()) return;
2023-06-06 12:13:33 +08:00
if(showTimerId) killTimer(showTimerId);
2023-06-07 19:11:03 +08:00
showTimerId = startTimer(200);
2023-06-06 12:13:33 +08:00
}
void WaitingDlg::success() {
fdText->setText(sucText.isEmpty() ? tr("Success") : sucText);
mIndicator->success();
2023-06-07 19:11:03 +08:00
if(showTimerId) {
killTimer(showTimerId);
showTimerId = 0;
}
if(! isVisible()) show();
2023-06-06 12:13:33 +08:00
if(closeTimerId) killTimer(closeTimerId);
closeTimerId = startTimer(1000);
}
void WaitingIndicator::success() {
if(timerId > 0) killTimer(timerId);
timerId = -1;
angle = 0;
update();
}
void WaitingIndicator::timerEvent(QTimerEvent *event) {
if(timerId!=event->timerId()) QWidget::timerEvent(event);
else if(isVisible()) {
angle += 30;
if(angle>=360) angle -= 360;
update();
} else if(timerId > 0) {
killTimer(timerId);
timerId = 0;
angle = 0;
}
}
void WaitingIndicator::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
if(timerId > 0) {
int radius = qMin(width(), height()) * 0.33;
int innerRadius = radius >> 1;
QPen pen(mColor, radius / 6, Qt::SolidLine, Qt::RoundCap);
painter.translate(width()>>1, height()>>1);
auto color = mColor;
for(int i=0; i<12; ++i) {
if(i) {
color.setAlphaF(1 - i/12.0);
pen.setColor(color);
painter.rotate(-30);
} else if(angle) painter.rotate(angle);
painter.setPen(pen);
painter.drawLine(0, innerRadius, 0, radius);
}
} else if(timerId==0) timerId = startTimer(33);
else {
int radius = qMin(width(), height()) >> 1;
int lineWidth = radius / 8;
radius -= lineWidth>>1;
QPen pen(QColor(0x00aa00), lineWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
painter.setPen(pen);
painter.translate(width()>>1, height()>>1);
painter.drawEllipse(QPoint(0, 0), radius, radius);
QPainterPath path({-0.8*radius, 0});
path.lineTo(-0.25*radius, 0.6*radius);
path.lineTo(0.6*radius, -0.5*radius);
painter.drawPath(path);
}
}