aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/layout/mbasiclayoutanimation_p.cpp
blob: a5eac860a76187857bb7db6814c1dc174e01c293 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/***************************************************************************
**
** 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 "mbasiclayoutanimation_p.h"
#include "mlayout_p.h"

MBasicLayoutAnimationPrivate::MBasicLayoutAnimationPrivate()
{
    recordedTimeSinceLastUpdate = 0;
}

MBasicLayoutAnimationPrivate::~MBasicLayoutAnimationPrivate()
{ }

void MBasicLayoutAnimationPrivate::tick()
{
    Q_Q(MBasicLayoutAnimation);
    recordedTimeSinceLastUpdate = timeSinceLastUpdate.elapsed();
    timeSinceLastUpdate.restart();
    bool layout_is_done(true);
    for(int i = states.count() - 1; i >= 0; --i) {
        MItemState &state = states[i];
        if (state.isAnimationDone())
            continue;
        animate(state);

        if (state.isAnimationDone()) {
            if(state.isSet(MItemState::STATE_FLAG_TO_BE_DELETED))
                q->layout()->removeAt(i);
        } else {
            layout_is_done = false;
        }

    }
    if (layout_is_done)
        q->stop();
}
void MBasicLayoutAnimationPrivate::animate(MItemState &item)
{
    Q_Q(MBasicLayoutAnimation);

    qreal delta_progress = recordedTimeSinceLastUpdate / q->style()->duration(); //Both times are in milliseconds

    //First animate the geometry if needed
    if (item.translateProgress() != 1) {
        //animate the geometry
        qreal new_geometry_progress = item.translateProgress() + delta_progress;
        if (new_geometry_progress >= 1.0) {
            item.setTransformProgress(1.0);
            item.item()->setTransform( QTransform() );
        } else {
            item.setTransformProgress(new_geometry_progress);
            qreal x_translation_animation_value = q->style()->xTranslationEasingCurve().valueForProgress(new_geometry_progress);
            qreal y_translation_animation_value = q->style()->yTranslationEasingCurve().valueForProgress(new_geometry_progress);
            qreal x_offset = item.sourceTranslate().x() * (1 - x_translation_animation_value);
            qreal y_offset = item.sourceTranslate().y() * (1 - y_translation_animation_value);
            item.item()->setTransform( QTransform::fromTranslate(x_offset, y_offset) );
        }
    }

    //Then animate the opacity if needed
    if (item.opacityProgress() != 1 && item.targetOpacity() != -1) { //-1 target opacity means to not animate anyway
        //animate the opacity
        qreal new_opacity_progress = qMin(item.opacityProgress() + delta_progress, (qreal)1.0); //between 0 and 1
        item.setOpacityProgress(new_opacity_progress);
        qreal opacity_animation_value = q->style()->opacityEasingCurve().valueForProgress(new_opacity_progress);

        qreal current_opacity = item.item()->opacity();
        if (current_opacity != -1) {
            qreal new_opacity = item.sourceOpacity() + (item.targetOpacity() - item.sourceOpacity()) * opacity_animation_value;
            new_opacity = qAbs(new_opacity); //make sure it is between 0 and 1
            if (new_opacity > 1)
                new_opacity = qMax((float)qAbs(2 - new_opacity), 1.0f);
            item.item()->setOpacity(new_opacity);
        }
    }

    //Have we finished with this item now?
    if (item.opacityProgress() == 1 && item.translateProgress() == 1) {
        if (item.isSet(MItemState::STATE_FLAG_TO_BE_HIDDEN)) {
            if (item.targetOpacity() != -1)
                item.item()->setOpacity(1); //Restore the opacity to 1, since we are hiding it now anyway
            q->hideItemNow(item.item());
        }
        item.animationDone();
    }
}

void MBasicLayoutAnimationPrivate::doItemHiddenAnimation(MItemState *itemstate)
{
    Q_Q(MBasicLayoutAnimation);
    Q_ASSERT(itemstate->item());
    if (!q->style()->animateOpacity())
        return;
    QGraphicsItem *graphicsItem = itemstate->item()->graphicsItem();
    if (graphicsItem) {
        qreal finalOpacity = q->style()->finalHidingOpacity();
        if (finalOpacity >= 0 && finalOpacity < 1) //If it ==1 there's no need to do anything
            itemstate->setTargetOpacity(finalOpacity);
    }
}
void MBasicLayoutAnimationPrivate::doItemShownAnimation(MItemState *itemstate)
{
    Q_Q(MBasicLayoutAnimation);
    Q_ASSERT(itemstate->item());
    q->showItemNow(itemstate->item());
    if (!q->style()->animateOpacity())
        return;
    QGraphicsItem *graphicsItem = itemstate->item()->graphicsItem();
    if(!graphicsItem)
        return;
    if (itemstate->isSet(MItemState::STATE_FLAG_SHOWING)) {
        //If the item is already visible, this can be called because we were currently doing a hide animation.
        //So just make sure that we make everything visible
        itemstate->setTargetOpacity(1);
        graphicsItem->setOpacity(1);
        return;
    }

    qreal initialOpacity = q->style()->initialShowingOpacity();
    if (initialOpacity >= 0 && initialOpacity < 1) { //If it ==1 there's no need to do anything
        graphicsItem->setOpacity(initialOpacity);
        itemstate->setTargetOpacity(1);
    }
}