aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/animation/widget/mwidgetfadeanimation.cpp
blob: 2a93a73f7dc7d7f98b93d4800caa045aea598cd3 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "mwidgetfadeanimation.h"
#include "mwidgetfadeanimation_p.h"
#include "manimationcreator.h"

#include <QPauseAnimation>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>
#include <mwidgetcontroller.h>

void MWidgetFadeAnimationPrivate::init()
{
    Q_Q(MWidgetFadeAnimation);

    direction = MWidgetFadeAnimation::In;
    QSequentialAnimationGroup *delayedAnimation = new QSequentialAnimationGroup;
    delay = new QPauseAnimation;
    opacityAnimation = new QPropertyAnimation;
    opacityAnimation->setPropertyName("opacity");
    delayedAnimation->addAnimation(delay);
    delayedAnimation->addAnimation(opacityAnimation);
    q->addAnimation(delayedAnimation);
}

MWidgetFadeAnimation::MWidgetFadeAnimation(MWidgetFadeAnimationPrivate *dd, QObject *parent) :
    MAbstractWidgetAnimation(dd, parent)
{
    Q_D(MWidgetFadeAnimation);

    d->init();
}

MWidgetFadeAnimation::MWidgetFadeAnimation(QObject *parent) :
    MAbstractWidgetAnimation(new MWidgetFadeAnimationPrivate, parent)
{
    Q_D(MWidgetFadeAnimation);

    d->init();
}

MWidgetFadeAnimation::~MWidgetFadeAnimation()
{
}

void MWidgetFadeAnimation::setTargetWidget(MWidgetController *widget)
{
    Q_D(MWidgetFadeAnimation);
    MAbstractWidgetAnimation::setTargetWidget(widget);

    d->played = false;
    d->opacityAnimation->setTargetObject(targetWidget());
}

void MWidgetFadeAnimation::restoreTargetWidgetState()
{
    Q_D(MWidgetFadeAnimation);
    if (d->played)
        targetWidget()->setOpacity(d->originalOpacity);
}

void MWidgetFadeAnimation::setTransitionDirection(TransitionDirection direction)
{
    Q_D(MWidgetFadeAnimation);

    d->direction = direction;

    if (d->direction == In)
        style().setObjectName("In");
    else
        style().setObjectName("Out");
}

void MWidgetFadeAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
{
    Q_D(MWidgetFadeAnimation);

    if (!d->targetWidget)
        return;

    if (oldState == QAbstractAnimation::Stopped &&
        newState == QAbstractAnimation::Running)
    {
        if (style().objectName().isNull())
            style().setObjectName("In");

        d->originalOpacity = d->targetWidget->opacity();

        if (d->direction == In) {
            targetWidget()->setOpacity(0);
            d->opacityAnimation->setStartValue(0);
            d->opacityAnimation->setEndValue(style()->opacity());
        } else {
            d->opacityAnimation->setStartValue(style()->opacity());
            d->opacityAnimation->setEndValue(0);
        }

        d->played = true;

        d->delay->setDuration(style()->delay());
        d->opacityAnimation->setDuration(style()->duration());
    }
}

M_REGISTER_ANIMATION(MWidgetFadeAnimation)