Use QPropertyAnimation::DeleteWhenStopped PulseWidget() when once is set

Also fixes potential crash when waiting for single shot timer when
looping and widget and animations are deleted in the meantime
This commit is contained in:
WarmUpTill 2022-02-25 23:28:55 +01:00 committed by WarmUpTill
parent 4253c99cf2
commit 6fb8297275

View File

@ -883,36 +883,39 @@ bool windowPosValid(QPoint pos)
QMetaObject::Connection PulseWidget(QWidget *widget, QColor startColor,
QColor endColor, bool once)
{
QGraphicsColorizeEffect *eEffect = new QGraphicsColorizeEffect(widget);
widget->setGraphicsEffect(eEffect);
QPropertyAnimation *paAnimation =
new QPropertyAnimation(eEffect, "color");
paAnimation->setStartValue(startColor);
paAnimation->setEndValue(endColor);
paAnimation->setDuration(1000);
QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect(widget);
widget->setGraphicsEffect(effect);
QPropertyAnimation *animation =
new QPropertyAnimation(effect, "color", widget);
animation->setStartValue(startColor);
animation->setEndValue(endColor);
animation->setDuration(1000);
QMetaObject::Connection con;
if (once) {
auto widgetPtr = widget;
con = QWidget::connect(
paAnimation, &QPropertyAnimation::finished,
animation, &QPropertyAnimation::finished,
[widgetPtr]() {
if (widgetPtr) {
widgetPtr->setGraphicsEffect(nullptr);
}
});
animation->start(QPropertyAnimation::DeleteWhenStopped);
} else {
auto widgetPtr = widget;
con = QWidget::connect(
paAnimation, &QPropertyAnimation::finished,
[paAnimation]() {
QTimer::singleShot(1000, [paAnimation] {
paAnimation->start();
});
animation, &QPropertyAnimation::finished,
[animation, widgetPtr]() {
QTimer *timer = new QTimer(widgetPtr);
QWidget::connect(timer, &QTimer::timeout,
[animation] {
animation->start();
});
timer->start(1000);
});
animation->start();
}
paAnimation->start();
return con;
}