aboutsummaryrefslogtreecommitdiff
path: root/installer/fadestackedwidget.cpp
blob: e925c9921e5c8daee84551519ecfc8e9045a2871 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "fadestackedwidget.h"

FadeStackedWidget::FadeStackedWidget(QWidget *parent) : QStackedWidget(parent)
{

}

void FadeStackedWidget::setCurrentIndex(int index) {
    //Do some checks before setting the current index.
    if (currentIndex() != index) {
        doSetCurrentIndex(index);
    }
}

void FadeStackedWidget::doSetCurrentIndex(int index) {
    QWidget* currentWidget = widget(currentIndex());
    QWidget* nextWidget = widget(index);
    if (nextWidget == nullptr) {
        QStackedWidget::setCurrentIndex(index);
    } else {
        if (doingNewAnimation) {
            anim->stop();
            anim->deleteLater();
        }

        doingNewAnimation = true;
        QGraphicsOpacityEffect* currentOpacity = new QGraphicsOpacityEffect();
        QGraphicsOpacityEffect* nextOpacity = new QGraphicsOpacityEffect();
        currentWidget->setGraphicsEffect(currentOpacity);
        nextWidget->setGraphicsEffect(nextOpacity);

        anim = new QVariantAnimation();
        anim->setStartValue((float) 1);
        anim->setEndValue((float) 0);
        anim->setDuration(500);
        anim->setEasingCurve(QEasingCurve::InCubic);
        connect(anim, &QVariantAnimation::finished, [=] {
            if (anim->direction() == QVariantAnimation::Forward) {
                anim->setDirection(QVariantAnimation::Backward);
                QStackedWidget::setCurrentIndex(index);
                anim->start();
            } else {
                doingNewAnimation = false;
                anim->deleteLater();
                anim = nullptr;
            }
        });
        connect(anim, &QVariantAnimation::valueChanged, [=](QVariant value) {
            if (anim->direction() == QVariantAnimation::Forward) {
                currentOpacity->setOpacity(value.toFloat());
            } else {
                nextOpacity->setOpacity(value.toFloat());
            }
        });
        anim->start();
    }
}