?? qtimeline.cpp
字號(hào):
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.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 GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qtimeline.h"#include <private/qobject_p.h>#include <QtCore/qdatetime.h>#include <QtCore/qcoreevent.h>#include <math.h>static const qreal pi = 3.14159265359;static const qreal halfPi = pi / 2.0;static inline qreal qt_sinProgress(qreal value){ return ::sin((value * pi) - halfPi) / 2.0 + 0.5;}static inline qreal qt_smoothBeginEndMixFactor(qreal value){ return qMin(qMax((1.0 - value * 2.0 + 0.3), 0.0), 1.0);}class QTimeLinePrivate : public QObjectPrivate{ Q_DECLARE_PUBLIC(QTimeLine)public: inline QTimeLinePrivate() : startTime(0), duration(1000), startFrame(0), endFrame(0), updateInterval(1000 / 25), totalLoopCount(1), currentLoopCount(0), currentTime(0), timerId(0), direction(QTimeLine::Forward), curveShape(QTimeLine::EaseInOutCurve), state(QTimeLine::NotRunning) { } int startTime; int duration; int startFrame; int endFrame; int updateInterval; int totalLoopCount; int currentLoopCount; int currentTime; int timerId; QTime timer; QTimeLine::Direction direction; QTimeLine::CurveShape curveShape; QTimeLine::State state; inline void setState(QTimeLine::State newState) { Q_Q(QTimeLine); if (newState != state) emit q->stateChanged(state = newState); } void setCurrentTime(int msecs);};/*! \internal*/void QTimeLinePrivate::setCurrentTime(int msecs){ Q_Q(QTimeLine); if (msecs == currentTime) return; qreal lastValue = q->currentValue(); int lastFrame = q->currentFrame(); currentTime = msecs; while (currentTime < 0) currentTime += duration; bool looped = (msecs < 0 || msecs > duration); currentTime %= (duration + 1); if (looped) ++currentLoopCount; bool finished = false; if (totalLoopCount && looped && currentLoopCount >= totalLoopCount) { finished = true; currentTime = (direction == QTimeLine::Backward) ? 0 : duration; } if (lastValue != q->currentValue()) emit q->valueChanged(q->currentValue()); if (lastFrame != q->currentFrame()) emit q->frameChanged(q->currentFrame()); if (finished) { emit q->finished(); q->stop(); }}/*! \class QTimeLine \brief The QTimeLine class provides a timeline for controlling animations. \since 4.2 \ingroup multimedia It's most commonly used to animate a GUI control by calling a slot periodically. You can construct a timeline by passing its duration in milliseconds to QTimeLine's constructor. The timeline's duration describes for how long the animation will run. Then you set a suitable frame range by calling setFrameRange(). Finally connect the frameChanged() signal to a suitable slot in the widget you wish to animate (e.g., setValue() in QProgressBar). When you proceed to calling start(), QTimeLine will enter Running state, and start emitting frameChanged() at regular intervals, causing your widget's connected property's value to grow from the lower end to the upper and of your frame range, at a steady rate. You can specify the update interval by calling setUpdateInterval(). When done, QTimeLine enters NotRunning state, and emits finished(). Example: \code ... progressBar = new QProgressBar(this); progressBar->setRange(0, 100); // Construct a 1-second timeline with a frame range of 0 - 100 QTimeLine *timeLine = new QTimeLine(1000, this); timeLine->setFrameRange(0, 100); connect(timeLine, SIGNAL(frameChanged(int)), progressBar, SLOT(setValue(int))); // Clicking the pushbutton will start the progress bar animation pushButton = new QPushButton(tr("Start animation"), this); connect(pushButton, SIGNAL(clicked()), timeLine, SLOT(start())); ... \endcode You can also use QTimeLine with the \l{Graphics View}{Graphics View framework} for animations. The QGraphicsItemAnimation class implements animation of \l{QGraphicsItem}{QGraphicsItems} with a timeline. By default the timeline runs once, from the beginning and towards the end, upon which you must call start() again to restart from the beginning. To make the timeline loop, you can call setLoopCount(), passing the number of times the timeline should run before finishing. The direction can also be changed, causing the timeline to run backward, by calling setDirection(). You can also pause and unpause the timeline while it's running by calling setPaused(). For interactive control, the setCurrentTime() function is provided, which sets the time position of the time line directly. Although most useful in NotRunning state, (e.g., connected to a valueChanged() signal in a QSlider,) this function can be called at any time. The frame interface is useful for standard widgets, but QTimeLine can be used to control any type of animation. The heart of QTimeLine lies in the valueForTime() function, which generates a \e value between 0 and 1 for a given time. This value is typically used to describe the steps of an animation, where 0 is the first step of an animation, and 1 is the last step. When running, QTimeLine generates values between 0 and 1 by calling valueForTime() and emitting valueChanged(). By default, valueForTime() applies an interpolation algorithm to generate these value. You can choose from a set of predefined timeline algorithms by calling setCurveShape(). By default, QTimeLine uses the EaseInOut curve shape, which provides a value that grows slowly, then grows steadily, and finally grows slowly. For a custom timeline, you can reimplement valueForTime(), in which case QTimeLine's curveShape property is ignored. \sa QProgressBar, QProgressDialog, QGraphicsItemAnimation*//*! \enum QTimeLine::State This enum describes the state of the timeline. \value NotRunning The timeline is not running. This is the initial state of QTimeLine, and the state QTimeLine reenters when finished. The current time, frame and value remain unchanged until either setCurrentTime() is called, or the timeline is started by calling start(). \value Paused The timeline is paused (i.e., temporarily suspended). Calling setPaused(false) will resume timeline activity. \value Running The timeline is running. While control is in the event loop, QTimeLine will update its current time at regular intervals, emitting valueChanged() and frameChanged() when appropriate. \sa state(), stateChanged()*//*! \enum QTimeLine::Direction This enum describes the direction of the timeline when in \l Running state. \value Forward The current time of the timeline increases with time (i.e., moves from 0 and towards the end / duration). \value Backward The current time of the timeline decreases with time (i.e., moves from the end / duration and towards 0). \sa setDirection()*//*! \enum QTimeLine::CurveShape This enum describes the default shape of QTimeLine's value curve. The default, shape is EaseInOutCurve. The curve defines the relation between the value and the timeline. \value EaseInCurve The value starts growing slowly, then increases in speed. \value EaseOutCurve The value starts growing steadily, then ends slowly. \value EaseInOutCurve The value starts growing slowly, the runs steadily, then grows slowly again. \value LinearCurve The value grows linearly (e.g., if the duration is 1000 ms, the value at time 500 ms is 0.5). \value SineCurve The value grows sinusoidally. \sa setCurveShape()*//*! \fn QTimeLine::valueChanged(qreal value) QTimeLine emits this signal at regular intervals when in \l Running state, but only if the current value changes. \a value is the current value. \a value is a number between 0.0 and 1.0 \sa QTimeLine::setDuration(), QTimeLine::valueForTime(), QTimeLine::updateInterval*//*! \fn QTimeLine::frameChanged(int frame) QTimeLine emits this signal at regular intervals when in \l Running state, but only if the current frame changes. \a frame is the current frame number. \sa QTimeLine::setFrameRange(), QTimeLine::updateInterval*//*! \fn QTimeLine::stateChanged(QTimeLine::State newState) This signal is emitted whenever QTimeLine's state changes. The new state is \a newState.*//*! \fn QTimeLine::finished() This signal is emitted when QTimeLine finishes (i.e., reaches the end of its time line), and does not loop.*//*! Constructs a timeline with a duration of \a duration milliseconds. \a parent is passed to QObject's constructor. The default duration is 1000 milliseconds. */QTimeLine::QTimeLine(int duration, QObject *parent) : QObject(*new QTimeLinePrivate, parent){ setDuration(duration);}/*! Destroys the timeline. */QTimeLine::~QTimeLine(){}/*! Returns the state of the timeline. \sa start(), setPaused(), stop()*/QTimeLine::State QTimeLine::state() const{ Q_D(const QTimeLine); return d->state;}/*! \property QTimeLine::loopCount \brief the number of times the timeline should loop before it's finished. A loop count of of 0 means that the timeline will loop forever.*/int QTimeLine::loopCount() const{ Q_D(const QTimeLine); return d->totalLoopCount;}void QTimeLine::setLoopCount(int count){ Q_D(QTimeLine); d->totalLoopCount = count;}/*! \property QTimeLine::direction \brief the direction of the timeline when QTimeLine is in \l Running state. This direction indicates whether the time moves from 0 towards the timeline duration, or from the value of the duration and towards 0 after start() has been called.*/QTimeLine::Direction QTimeLine::direction() const{ Q_D(const QTimeLine); return d->direction;}void QTimeLine::setDirection(Direction direction){ Q_D(QTimeLine); d->direction = direction;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -