aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/animation/widget/mcontentfadeandslideanimation.cpp
blob: c594cb86ac2dc6980354cc127b6d5736a044bc79 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/***************************************************************************
**
** 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 "mcontentfadeandslideanimation.h"
#include "mcontentfadeandslideanimation_p.h"
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QSequentialAnimationGroup>
#include <QPauseAnimation>

namespace {
    const QPointF farFarAway(-100000, -100000);
}

void SnapshotItem::updateSnapshot(QGraphicsWidget* target)
{
    QSize newSize = target->sceneTransform().mapRect(target->boundingRect()).size().toSize();

    if (newSize != pixmap.size())
        pixmap = QPixmap(newSize);

    pixmap.fill(Qt::transparent);

    QPainter painter(&pixmap);

    const QPointF oldPos(target->pos());
    // move target far away so nothing is below
    target->setPos(farFarAway);
    target->scene()->render(&painter, QRectF(), target->sceneBoundingRect());
    target->setPos(oldPos);
}

QRectF SnapshotItem::boundingRect() const
{
    return QRectF(0, 0, pixmap.width(), pixmap.height());
}

void SnapshotItem::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
{
    painter->drawPixmap(QPoint(0, 0), pixmap);
}


MContentFadeAndSlideAnimationPrivate::MContentFadeAndSlideAnimationPrivate()
    : snapshotItem(0)
    , contentItem(0)
    , previous(0)
    , fadeOut(0)
    , slideOut(0)
    , current(0)
    , fadeIn(0)
    , slideIn(0)
    , currentWithDelay(0)
    , delay(0)
{
}

void MContentFadeAndSlideAnimationPrivate::init()
{
    Q_Q(MContentFadeAndSlideAnimation);

    snapshotItem = new SnapshotItem;

    previous = new QParallelAnimationGroup();

    fadeOut = new QPropertyAnimation();
    fadeOut->setPropertyName("opacity");
    fadeOut->setTargetObject(snapshotItem);
    previous->addAnimation(fadeOut);

    slideOut = new QPropertyAnimation();
    slideOut->setPropertyName("pos");
    slideOut->setTargetObject(snapshotItem);
    previous->addAnimation(slideOut);

    current = new QParallelAnimationGroup();

    fadeIn = new QPropertyAnimation();
    fadeIn->setPropertyName("opacity");
    current->addAnimation(fadeIn);

    slideIn = new QPropertyAnimation();
    slideIn->setPropertyName("pos");
    current->addAnimation(slideIn);

    currentWithDelay = new QSequentialAnimationGroup();
    delay = new QPauseAnimation;

    currentWithDelay->addAnimation(delay);
    currentWithDelay->addAnimation(current);

    q->addAnimation(previous);
    q->addAnimation(currentWithDelay);
}


MContentFadeAndSlideAnimation::MContentFadeAndSlideAnimation(QObject *parent) :
    MAbstractWidgetAnimation(new MContentFadeAndSlideAnimationPrivate, parent)
{
    Q_D(MContentFadeAndSlideAnimation);

    d->init();
}

MContentFadeAndSlideAnimation::~MContentFadeAndSlideAnimation()
{
    Q_D(MContentFadeAndSlideAnimation);

    delete d->snapshotItem;
}

void MContentFadeAndSlideAnimation::restoreTargetWidgetState()
{}

void MContentFadeAndSlideAnimation::setTransitionDirection(MAbstractWidgetAnimation::TransitionDirection /*direction*/)
{}

void MContentFadeAndSlideAnimation::takeContentSnapshot()
{
    Q_D(MContentFadeAndSlideAnimation);

    if (d->contentItem)
        d->snapshotItem->updateSnapshot(d->contentItem);
}

void MContentFadeAndSlideAnimation::setTargetWidget(MWidgetController *targetWidget)
{
    MAbstractWidgetAnimation::setTargetWidget(targetWidget);

    Q_D(MContentFadeAndSlideAnimation);

    d->contentItem = 0;

    if (!targetWidget)
        return;

    foreach(QGraphicsItem* item, targetWidget->childItems()) {
        if (!item->isWidget())
            continue;
        QGraphicsWidget *widget = static_cast<QGraphicsWidget*>(item);
        if (widget->objectName() == style()->contentObjectName()) {
            d->contentItem = widget;
            break;
        }
    }

}

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

    if (!d->contentItem)
        return;

    if (oldState == QAbstractAnimation::Stopped &&
        newState == QAbstractAnimation::Running)
    {
        d->fadeOut->setStartValue(d->contentItem->opacity());
        d->fadeOut->setEndValue(0.0);
        d->fadeOut->setDuration(style()->fadeOutDuration());
        d->fadeOut->setEasingCurve(style()->fadeOutEasingCurve());
        d->fadeOut->setTargetObject(d->snapshotItem);

        QRectF startRect = d->contentItem->boundingRect();
        QRectF finalRect = startRect.translated(style()->slideOutDisplacement());
        QTransform transform = d->contentItem->sceneTransform();
        d->slideOut->setStartValue(transform.mapRect(startRect).topLeft());
        d->slideOut->setEndValue(transform.mapRect(finalRect).topLeft());
        d->slideOut->setDuration(style()->slideOutDuration());
        d->slideOut->setEasingCurve(style()->slideOutEasingCurve());
        d->slideOut->setTargetObject(d->snapshotItem);

        d->delay->setDuration(style()->delay());

        d->fadeIn->setStartValue(0.0);
        d->fadeIn->setEndValue(d->contentItem->opacity());
        d->fadeIn->setDuration(style()->fadeInDuration());
        d->fadeIn->setEasingCurve(style()->fadeInEasingCurve());
        d->fadeIn->setTargetObject(d->contentItem);

        d->slideIn->setStartValue(d->contentItem->pos() + style()->slideInDisplacement());
        d->slideIn->setEndValue(d->contentItem->pos());
        d->slideIn->setDuration(style()->slideInDuration());
        d->slideIn->setEasingCurve(style()->slideInEasingCurve());
        d->slideIn->setTargetObject(d->contentItem);

        d->targetWidget->scene()->addItem(d->snapshotItem);

        // animation is delayed so apply initial values now
        d->contentItem->setOpacity(d->fadeIn->startValue().toReal());
        d->contentItem->setPos(d->slideIn->startValue().toPointF());

    } else if (oldState == QAbstractAnimation::Running &&
               newState == QAbstractAnimation::Stopped)
    {
        d->targetWidget->scene()->removeItem(d->snapshotItem);

        // apply final values (for cases when animation was stopped in the middle)
        d->contentItem->setOpacity(d->fadeIn->endValue().toReal());
        d->contentItem->setPos(d->slideIn->endValue().toPointF());
    }

    MAbstractWidgetAnimation::updateState(newState, oldState);
}