This repository has been archived on 2022-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
modern-linbo-gui/sources/qmodernstackedwidget.cpp

53 lines
1.7 KiB
C++
Raw Normal View History

#include "../headers/qmodernstackedwidget.h"
QModernStackedWidget::QModernStackedWidget(QWidget* parent) : QStackedWidget(parent)
{
opacityAnimation = new QPropertyAnimation();
opacityAnimation->setPropertyName("opacity");
opacityAnimation->setEasingCurve(QEasingCurve::InOutQuad);
opacityAnimation->setDuration(400);
opacityAnimation->setLoopCount(1);
connect(opacityAnimation, SIGNAL(finished()), this, SLOT(handleAnimationFinished()));
}
void QModernStackedWidget::setCurrentWidgetAnimated(QWidget* widget) {
2020-11-24 21:42:47 +01:00
if(widget == nullptr || widget == this->currentWidget())
return;
this->newWidget = widget;
// fade old widget out
QGraphicsOpacityEffect* opacityEffect = new QGraphicsOpacityEffect(this);
this->currentWidget()->setGraphicsEffect(opacityEffect);
opacityEffect->setOpacity(1);
opacityAnimation->setStartValue(1);
opacityAnimation->setEndValue(0);
opacityAnimation->setTargetObject(opacityEffect);
opacityAnimation->start();
}
void QModernStackedWidget::handleAnimationFinished() {
// disable graphical effect to prevent errors
this->currentWidget()->graphicsEffect()->setEnabled(false);
if(this->newWidget == nullptr) {
return;
}
// hide new widget
QGraphicsOpacityEffect* opacityEffect = new QGraphicsOpacityEffect(this);
this->newWidget->setGraphicsEffect(opacityEffect);
opacityEffect->setOpacity(0);
// set new widget
this->setCurrentWidget(this->newWidget);
this->newWidget = nullptr;
// fade new widget in
opacityAnimation->setTargetObject(opacityEffect);
opacityAnimation->setStartValue(0);
opacityAnimation->setEndValue(1);
opacityAnimation->start();
}