summaryrefslogtreecommitdiffstats
path: root/src/corelib/animation/qvariantanimation.cpp
blob: 52e2901eb12a6e34cb903e95049a373a52e31515 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used 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.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QT_NO_ANIMATION

#include "qvariantanimation.h"
#include "qvariantanimation_p.h"

#include <QtCore/QRect>
#include <QtCore/QLineF>
#include <QtCore/QLine>
#include <QtCore/QReadWriteLock>
#include <QtCore/qdebug.h>

QT_BEGIN_NAMESPACE

/*!
    \class QVariantAnimation
    \ingroup animation
    \brief The QVariantAnimation class provides an abstract base class for animations.
    \since 4.5
    \preliminary

    This class is part of {The Animation Framework}. It serves as a base class
    for property and item animations, with functions for shared functionality.

    If you want to create an animation, you should look at QPropertyAnimation instead.

    You can then set start and end values for the property by calling
    setStartValue() and setEndValue(), and finally call start() to
    start the animation. When control goes back to the event loop, QVariantAnimation
    will interpolate the property of the target object and emit the valueChanged
    signal. To react to a change in the current value you have to reimplement the
    updateCurrentValue virtual method.

    There are two ways to affect how QVariantAnimation interpolates the values.
    You can set an easing curve by calling setEasingCurve(), and configure the
    duration by calling setDuration(). You can change how the QVariants are
    interpolated by creating a subclass of QVariantAnimation, and reimplementing the
    virtual interpolated() function.


    \sa QPropertyAnimation, {The Animation Framework}
*/

/*!
    \fn void QVariantAnimation::valueChanged(const QVariant &value)

    QVariantAnimation emits this signal whenever the current \a value changes.

    \sa currentValue, startValue, endValue
*/

static bool animationValueLessThan(const QVariantAnimation::KeyValue &p1, const QVariantAnimation::KeyValue &p2)
{
    return p1.first < p2.first;
}

template<> Q_INLINE_TEMPLATE QRect _q_interpolate(const QRect &f, const QRect &t, qreal progress)
{
    QRect ret;
    ret.setCoords(_q_interpolate(f.left(), t.left(), progress),
                  _q_interpolate(f.top(), t.top(), progress),
                  _q_interpolate(f.right(), t.right(), progress),
                  _q_interpolate(f.bottom(), t.bottom(), progress));
    return ret;
}

template<> Q_INLINE_TEMPLATE QRectF _q_interpolate(const QRectF &f, const QRectF &t, qreal progress)
{
    qreal x1, y1, w1, h1;
    f.getRect(&x1, &y1, &w1, &h1);
    qreal x2, y2, w2, h2;
    t.getRect(&x2, &y2, &w2, &h2);
    return QRectF( _q_interpolate(x1, x2, progress), _q_interpolate(y1, y2, progress),
                  _q_interpolate(w1, w2, progress), _q_interpolate(h1, h2, progress));
}

template<> Q_INLINE_TEMPLATE QLine _q_interpolate(const QLine &f, const QLine &t, qreal progress)
{
    return QLine( _q_interpolate(f.p1(), t.p1(), progress), _q_interpolate(f.p2(), t.p2(), progress));
}

template<> Q_INLINE_TEMPLATE QLineF _q_interpolate(const QLineF &f, const QLineF &t, qreal progress)
{
    return QLineF( _q_interpolate(f.p1(), t.p1(), progress), _q_interpolate(f.p2(), t.p2(), progress));
}

void QVariantAnimationPrivate::convertValues(int t)
{
    //this ensures that all the keyValues are of type t
    for (int i = 0; i < keyValues.count(); ++i) {
        QVariantAnimation::KeyValue &pair = keyValues[i];
        if (pair.second.userType() != t)
            pair.second.convert(static_cast<QVariant::Type>(t));
    }
    currentInterval.start.first = 2; // this will force the refresh
    interpolator = 0;               // if the type changed we need to update the interpolator
}

/*!
    \fn void QVariantAnimation::updateCurrentValue(const QVariant &value) = 0;
    This pure virtual function is called when the animated value is changed.
    \a value is the new value.
*/

void QVariantAnimationPrivate::updateCurrentValue()
{
    Q_Q(QVariantAnimation);
    const qreal progress = easing.valueForProgress(((duration == 0) ? qreal(1) : qreal(currentTime) / qreal(duration)));

    if (progress < currentInterval.start.first || progress > currentInterval.end.first) {
        //let's update currentInterval
        QVariantAnimation::KeyValues::const_iterator itStart = qLowerBound(keyValues.constBegin(), keyValues.constEnd(), qMakePair(progress, QVariant()), animationValueLessThan);
        QVariantAnimation::KeyValues::const_iterator itEnd = itStart;

        // If we are at the end we should continue to use the last keyValues in case of extrapolation (progress > 1.0).
        // This is because the easing function can return a value slightly outside the range [0, 1]
        if (itStart != keyValues.constEnd()) {

            //this can't happen because we always prepend the default start value there
            if (itStart == keyValues.begin()) {
                ++itEnd;
                if (itEnd == keyValues.constEnd())
                    return; //there is no upper bound
            } else {
                --itStart;
            }

            //update all the values of the currentInterval
            currentInterval.start = *itStart;
            currentInterval.end = *itEnd;
        }
    }

    const qreal startProgress = currentInterval.start.first,
                endProgress = currentInterval.end.first;
    const qreal localProgress = (progress - startProgress) / (endProgress - startProgress);

    bool changed = false;
    {
      //we do that here in a limited scope so that ret is dereferenced and frees memory
      //and the call to updateCurrentValue can recreate QVariant of the same type (for ex. in
      //QGraphicsItem::setPos
      QVariant ret = q->interpolated(currentInterval.start.second,
                                    currentInterval.end.second,
                                    localProgress);
      if (currentValue != ret) {
          changed = true;
          qSwap(currentValue, ret);
      }
    }

    if (changed) {
        //the value has changed
        q->updateCurrentValue(currentValue);
#ifndef QT_EXPERIMENTAL_SOLUTION
        if (connectedSignals & changedSignalMask)
#endif
            emit q->valueChanged(currentValue);
    }
}


QVariant QVariantAnimationPrivate::valueAt(qreal step) const
{
    QVariantAnimation::KeyValues::const_iterator result =
        qBinaryFind(keyValues.begin(), keyValues.end(), qMakePair(step, QVariant()), animationValueLessThan);
    if (result != keyValues.constEnd())
        return result->second;

    return QVariant();
}


void QVariantAnimationPrivate::setValueAt(qreal step, const QVariant &value)
{
    if (step < qreal(0.0) || step > qreal(1.0)) {
        qWarning("QVariantAnimation::setValueAt: invalid step = %f", step);
        return;
    }

    QVariantAnimation::KeyValue pair(step, value);

    QVariantAnimation::KeyValues::iterator result = qLowerBound(keyValues.begin(), keyValues.end(), pair, animationValueLessThan);
    if (result == keyValues.end() || result->first != step) {
        keyValues.insert(result, pair);
    } else {
        if (value.isValid())
            result->second = value; //remove the previous value
        else if (step == 0 && !hasStartValue && defaultStartValue.isValid())
            result->second = defaultStartValue; //we reset to the default start value
        else
            keyValues.erase(result); //replace the previous value
    }

    currentInterval.start.first = 2; // this will force the refresh
    updateCurrentValue();
}

void QVariantAnimationPrivate::setDefaultStartValue(const QVariant &value)
{
    defaultStartValue = value;
    if (!hasStartValue)
      setValueAt(0, value);
}

/*!
    Construct a QVariantAnimation object. \a parent is passed to QAbstractAnimation's
    constructor.
*/
QVariantAnimation::QVariantAnimation(QObject *parent) : QAbstractAnimation(*new QVariantAnimationPrivate, parent)
{
   d_func()->init();
}

/*!
    \internal
*/
QVariantAnimation::QVariantAnimation(QVariantAnimationPrivate &dd, QObject *parent) : QAbstractAnimation(dd, parent)
{
   d_func()->init();
}

/*!
    Destroys the animation.
*/
QVariantAnimation::~QVariantAnimation()
{
}

/*!
    \property QVariantAnimation::easingCurve
    \brief the easing curve of the animation

    This property defines the easing curve of the animation. By default, a
    linear easing curve is used, resulting in linear interpolation of the
    end property. For many animations, it's useful to try different easing
    curves, including QEasingCurve::InCirc, which provides a circular entry curve,
    and QEasingCurve::InOutElastic, which provides an elastic effect on the values
    of the interpolated property.

    The easing curve is used with the interpolator, the interpolated() virtual
    function, the animation's duration, and loopCount, to control how the
    current value changes as the animation progresses.
*/
QEasingCurve QVariantAnimation::easingCurve() const
{
    Q_D(const QVariantAnimation);
    return d->easing;
}

void QVariantAnimation::setEasingCurve(const QEasingCurve &easing)
{
    Q_D(QVariantAnimation);
    d->easing = easing;
    d->updateCurrentValue();
}

Q_GLOBAL_STATIC(QVector<QVariantAnimation::Interpolator>, registeredInterpolators)
Q_GLOBAL_STATIC(QReadWriteLock, registeredInterpolatorsLock)

/*!
    \fn void qRegisterAnimationInterpolator(QVariant (*func)(const T &from, const T &to, qreal progress))
    \relates QVariantAnimation
    \threadsafe

    Registers a custom interpolator \a func for the template type \c{T}.
    The interpolator has to be registered before the animation is constructed.
    To unregister (and use the default interpolator) set \a func to 0.
 */

/*!
    \internal
    \typedef QVariantAnimation::Interpolator

    This is a typedef for a pointer to a function with the following
    signature:
    \code
    QVariant myInterpolator(const QVariant &from, const QVariant &to, qreal progress);
    \endcode

*/

/*! \internal
 * Registers a custom interpolator \a func for the specific \a interpolationType.
 * The interpolator has to be registered before the animation is constructed.
 * To unregister (and use the default interpolator) set \a func to 0.
 */
void QVariantAnimation::registerInterpolator(QVariantAnimation::Interpolator func, int interpolationType)
{
    // will override any existing interpolators
    QWriteLocker locker(registeredInterpolatorsLock());
    if (int(interpolationType) >= registeredInterpolators()->count())
        registeredInterpolators()->resize(int(interpolationType) + 1);
    registeredInterpolators()->replace(interpolationType, func);
}


template<typename T> static inline QVariantAnimation::Interpolator castToInterpolator(QVariant (*func)(const T &from, const T &to, qreal progress))
{
     return reinterpret_cast<QVariantAnimation::Interpolator>(func);
}

static QVariantAnimation::Interpolator getInterpolator(int interpolationType)
{
    QReadLocker locker(registeredInterpolatorsLock());
    QVariantAnimation::Interpolator ret = 0;
    if (interpolationType < registeredInterpolators()->count()) {
        ret = registeredInterpolators()->at(interpolationType);
        if (ret) return ret;
    }

    switch(interpolationType)
    {
    case QMetaType::Int:
        return castToInterpolator(_q_interpolateVariant<int>);
    case QMetaType::Double:
        return castToInterpolator(_q_interpolateVariant<double>);
    case QMetaType::Float:
        return castToInterpolator(_q_interpolateVariant<float>);
    case QMetaType::QLine:
        return castToInterpolator(_q_interpolateVariant<QLine>);
    case QMetaType::QLineF:
        return castToInterpolator(_q_interpolateVariant<QLineF>);
    case QMetaType::QPoint:
        return castToInterpolator(_q_interpolateVariant<QPoint>);
    case QMetaType::QPointF:
        return castToInterpolator(_q_interpolateVariant<QPointF>);
    case QMetaType::QSize:
        return castToInterpolator(_q_interpolateVariant<QSize>);
    case QMetaType::QSizeF:
        return castToInterpolator(_q_interpolateVariant<QSizeF>);
    case QMetaType::QRect:
        return castToInterpolator(_q_interpolateVariant<QRect>);
    case QMetaType::QRectF:
        return castToInterpolator(_q_interpolateVariant<QRectF>);
    default:
        return 0; //this type is not handled
    }
}

/*!
    \property QVariantAnimation::duration
    \brief the duration of the animation

    This property describes the duration of the animation. The default
    duration is 250 milliseconds.

    \sa QAbstractAnimation::duration()
 */
int QVariantAnimation::duration() const
{
    Q_D(const QVariantAnimation);
    return d->duration;
}

void QVariantAnimation::setDuration(int msecs)
{
    Q_D(QVariantAnimation);
    if (msecs < 0) {
        qWarning("QVariantAnimation::setDuration: cannot set a negative duration");
        return;
    }
    if (d->duration == msecs)
        return;
    d->duration = msecs;
    d->updateCurrentValue();
}

/*!
    \property QVariantAnimation::startValue
    \brief the optional start value of the animation

    This property describes the optional start value of the animation. If
    omitted, or if a null QVariant is assigned as the start value, the
    animation will use the current position of the end when the animation
    is started.

    \sa endValue
*/
QVariant QVariantAnimation::startValue() const
{
    return keyValueAt(0);
}

void QVariantAnimation::setStartValue(const QVariant &value)
{
    setKeyValueAt(0, value);
}

/*!
    \property QVariantAnimation::endValue
    \brief the end value of the animation

    This property describes the end value of the animation.

    \sa startValue
 */
QVariant QVariantAnimation::endValue() const
{
    return keyValueAt(1);
}

void QVariantAnimation::setEndValue(const QVariant &value)
{
    setKeyValueAt(1, value);
}


/*!
    Returns the key frame value for the given \a step. The given \a step
    must be in the range 0 to 1. If there is no KeyValue for \a step,
    it returns an invalid QVariant.

    \sa keyValues(), setKeyValueAt()
*/
QVariant QVariantAnimation::keyValueAt(qreal step) const
{
    Q_D(const QVariantAnimation);
    if (step == 0 && !d->hasStartValue)
        return QVariant(); //special case where we don't have an explicit startValue

    return d->valueAt(step);
}

/*!
    \typedef QVariantAnimation::KeyValue

    This is a typedef for QPair<qreal, QVariant>.
*/
/*!
    \typedef QVariantAnimation::KeyValues

    This is a typedef for QVector<KeyValue>
*/

/*!
    Creates a key frame at the given \a step with the given \a value.
    The given \a step must be in the range 0 to 1.

    \sa setKeyValues(), keyValueAt()
*/
void QVariantAnimation::setKeyValueAt(qreal step, const QVariant &value)
{
    Q_D(QVariantAnimation);
    if (step == 0)
        d->hasStartValue = value.isValid();
    d->setValueAt(step, value);
}

/*!
    Returns the key frames of this animation.

    \sa keyValueAt(), setKeyValues()
*/
QVariantAnimation::KeyValues QVariantAnimation::keyValues() const
{
    Q_D(const QVariantAnimation);
    QVariantAnimation::KeyValues ret = d->keyValues;
    //in case we added the default start value, we remove it
    if (!d->hasStartValue && !ret.isEmpty() && ret.at(0).first == 0)
        ret.remove(0);
    return ret;
}

/*!
    Replaces the current set of key frames with the given \a keyValues.
    the step of the key frames must be in the range 0 to 1.

    \sa keyValues(), keyValueAt()
*/
void QVariantAnimation::setKeyValues(const KeyValues &keyValues)
{
    Q_D(QVariantAnimation);
    d->keyValues = keyValues;
    qSort(d->keyValues.begin(), d->keyValues.end(), animationValueLessThan);
    d->currentInterval.start.first = 2; // this will force the refresh
    d->hasStartValue = !d->keyValues.isEmpty() && d->keyValues.at(0).first == 0;
}

/*!
    \property QVariantAnimation::currentValue
    \brief the current value of the animation

    This property describes the current value; an interpolation between the
    start value and the end value, using the current time for progress.

    QVariantAnimation calls the virtual updateCurrentValue() function when the
    current value changes. This is particularily useful for subclasses that
    need to track updates.

    \sa startValue, endValue
 */
QVariant QVariantAnimation::currentValue() const
{
    Q_D(const QVariantAnimation);
    if (!d->currentValue.isValid())
        const_cast<QVariantAnimationPrivate*>(d)->updateCurrentValue();
    return d->currentValue;
}

/*!
    \reimp
 */
bool QVariantAnimation::event(QEvent *event)
{
    return QAbstractAnimation::event(event);
}

/*!
    \reimp
*/
void QVariantAnimation::updateState(QAbstractAnimation::State oldState,
                             QAbstractAnimation::State newState)
{
    Q_UNUSED(oldState);
    Q_UNUSED(newState);
    Q_D(QVariantAnimation);
    d->currentValue = QVariant(); // this will force the refresh
}

/*!
    This virtual function returns the linear interpolation between variants \a
    from and \a to, at \a progress, usually a value between 0 and 1. You can reimplement
    this function in a subclass of QVariantAnimation to provide your own interpolation
    algorithm. Note that in order for the interpolation to work with a QEasingCurve
    that return a value smaller than 0 or larger than 1 (such as QEasingCurve::InBack)
    you should make sure that it can extrapolate. If the semantic of the datatype
    does not allow extrapolation this function should handle that gracefully.

    \sa QEasingCurve
 */
QVariant QVariantAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
{
    Q_D(const QVariantAnimation);
    if (d->interpolator == 0) {
        if (from.userType() == to.userType())
            d->interpolator = getInterpolator(from.userType());
        if (d->interpolator == 0) //no interpolator found
            return QVariant();
    }

    return d->interpolator(from.constData(), to.constData(), progress);
}

/*!
    \reimp
 */
void QVariantAnimation::updateCurrentTime(int msecs)
{
    Q_D(QVariantAnimation);
    Q_UNUSED(msecs);
    d->updateCurrentValue();
}

QT_END_NAMESPACE

#include "moc_qvariantanimation.cpp"

#endif //QT_NO_ANIMATION