diff options
Diffstat (limited to 'examples/animation/research')
-rw-r--r-- | examples/animation/research/memberfunctions/main.cpp | 48 | ||||
-rw-r--r-- | examples/animation/research/memberfunctions/memberfunctions.pro | 16 | ||||
-rw-r--r-- | examples/animation/research/memberfunctions/qvalueanimation.cpp | 73 | ||||
-rw-r--r-- | examples/animation/research/memberfunctions/qvalueanimation.h | 89 | ||||
-rw-r--r-- | examples/animation/research/memberfunctions/qvalueanimation_p.h | 47 | ||||
-rw-r--r-- | examples/animation/research/propertytransform/main.cpp | 47 | ||||
-rw-r--r-- | examples/animation/research/propertytransform/propertytransform.pro | 14 | ||||
-rw-r--r-- | examples/animation/research/propertytransform/qpropertytransform.h | 78 | ||||
-rw-r--r-- | examples/animation/research/sound/main.cpp | 74 | ||||
-rw-r--r-- | examples/animation/research/sound/media/sax.mp3 | bin | 0 -> 417844 bytes | |||
-rw-r--r-- | examples/animation/research/sound/sound.pro | 14 | ||||
-rw-r--r-- | examples/animation/research/sound/sound.qrc | 5 |
12 files changed, 505 insertions, 0 deletions
diff --git a/examples/animation/research/memberfunctions/main.cpp b/examples/animation/research/memberfunctions/main.cpp new file mode 100644 index 0000000..9142f29 --- /dev/null +++ b/examples/animation/research/memberfunctions/main.cpp @@ -0,0 +1,48 @@ +#include <QtGui> +#include "qvalueanimation.h" + +AbstractProperty *qGraphicsItemProperty(QGraphicsItem *item, const char *property) +{ + if (qstrcmp(property, "pos") == 0) { + return new MemberFunctionProperty<QGraphicsItem, QPointF>(item, &QGraphicsItem::pos, &QGraphicsItem::setPos); + } + return 0; +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + QGraphicsScene scene; + QGraphicsView view(&scene); + + QGraphicsItem *item = new QGraphicsRectItem(QRectF(0,0, 200, 100)); + scene.addItem(item); + + QValueAnimation *posAnim = new QValueAnimation; + posAnim->setStartValue(QPointF(0,0)); + posAnim->setEndValue(QPointF(400, 0)); + posAnim->setDuration(1000); + // Alternative 1 + //posAnim->setMemberFunction(item, &QGraphicsItem::pos, &QGraphicsItem::setPos); + + // Alternative 2 + //posAnim->setProperty(qMemberFunctionProperty(item, &QGraphicsItem::pos, &QGraphicsItem::setPos)); + + // Alternative 3 + posAnim->setProperty(qGraphicsItemProperty(item, "pos")); + + // Alternative 4, (by implementing the qGraphicsItemProperty QGraphicsItem::property()) + //posAnim->setProperty(item->property("pos")); + + // can also do this, which abstracts away the whole property thing. + // i.e. this interface can also be used for QObject-properties: + //posAnim->setAnimationProperty(animationProperty); + + posAnim->start(); + + view.resize(800,600); + view.show(); + return app.exec(); +} + diff --git a/examples/animation/research/memberfunctions/memberfunctions.pro b/examples/animation/research/memberfunctions/memberfunctions.pro new file mode 100644 index 0000000..6b67895 --- /dev/null +++ b/examples/animation/research/memberfunctions/memberfunctions.pro @@ -0,0 +1,16 @@ +###################################################################### +# Automatically generated by qmake (2.01a) fr 26. sep 13:21:57 2008 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp \ + qvalueanimation.cpp +HEADERS += qvalueanimation.h + +CONFIG += console + diff --git a/examples/animation/research/memberfunctions/qvalueanimation.cpp b/examples/animation/research/memberfunctions/qvalueanimation.cpp new file mode 100644 index 0000000..2fe9be9 --- /dev/null +++ b/examples/animation/research/memberfunctions/qvalueanimation.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +****************************************************************************/ + + + +#include "qvalueanimation.h" +#include "qvalueanimation_p.h" + +QT_BEGIN_NAMESPACE + + +void QValueAnimationPrivate::initDefaultStartValue() +{ + Q_Q(QValueAnimation); + if (animProp && !q->startValue().isValid() + && ((currentTime == 0 && (currentIteration || currentIteration == 0)) + || (currentTime == duration && currentIteration == (iterationCount - 1)))) { + setDefaultStartValue(animProp->read()); + } +} + + +QValueAnimation::QValueAnimation(QObject *parent) : QVariantAnimation(*new QValueAnimationPrivate, parent) +{ +} + +QValueAnimation::~QValueAnimation() +{ +} + +void QValueAnimation::setProperty(AbstractProperty *animProp) +{ + Q_D(QValueAnimation); + d->animProp = animProp; +} + +/*! + \reimp + */ +void QValueAnimation::updateCurrentValue(const QVariant &value) +{ + Q_D(QValueAnimation); + if (state() == QAbstractAnimation::Stopped) + return; + + d->animProp->write(value); +} + + +/*! + \reimp +*/ +void QValueAnimation::updateState(QAbstractAnimation::State oldState, QAbstractAnimation::State newState) +{ + Q_D(QValueAnimation); + // Initialize start value + if (oldState == QAbstractAnimation::Stopped && newState == QAbstractAnimation::Running) + d->initDefaultStartValue(); +} + + + +#include "moc_qvalueanimation.cpp" + +QT_END_NAMESPACE diff --git a/examples/animation/research/memberfunctions/qvalueanimation.h b/examples/animation/research/memberfunctions/qvalueanimation.h new file mode 100644 index 0000000..a4aa213 --- /dev/null +++ b/examples/animation/research/memberfunctions/qvalueanimation.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +****************************************************************************/ + +#ifndef QVALUEANIMATION_H +#define QVALUEANIMATION_H + +#if defined(QT_EXPERIMENTAL_SOLUTION) +# include "qvariantanimation.h" +#else +# include <QtCore/qvariantanimation.h> +#endif + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +class QGraphicsItem; +class QValueAnimationPrivate; + +QT_MODULE(Gui) + +struct AbstractProperty { + virtual void write(const QVariant &value) = 0; + virtual QVariant read() const = 0; +}; + +# define CALL_MEMBER_FN(object,ptrToMember) ((object)->*(ptrToMember)) +template <typename Target, typename T> +class MemberFunctionProperty : public AbstractProperty { +public: + typedef void (Target::*RefWrite)(const T &); + typedef T (Target::*ValRead)(void) const; + + MemberFunctionProperty(Target *target, ValRead readFunc, RefWrite writeFunc) + : m_target(target), m_readFn(readFunc), m_writeFn(writeFunc) {} + + virtual void write(const QVariant &value) { + CALL_MEMBER_FN(m_target, m_writeFn)(qVariantValue<T>(value)); + } + + virtual QVariant read() const { + if (m_readFn) + return qVariantFromValue<T>(CALL_MEMBER_FN(m_target, m_readFn)()); + return QVariant(); + } + +private: + Target *m_target; + ValRead m_readFn; + RefWrite m_writeFn; +}; + + +class QValueAnimation : public QVariantAnimation +{ + Q_OBJECT + +public: + QValueAnimation(QObject *parent = 0); + ~QValueAnimation(); + + template <typename Target, typename T> + void setMemberFunction(Target *target, + T (Target::*readFunc)(void) const, // ### useValRead typedef + void (Target::*writeFunc)(const T &) // ### use RefWrite typedef + ) { + // ### ownership of MemberFunctionProperty + AbstractProperty *animProp = new MemberFunctionProperty<Target, T>(target, readFunc, writeFunc); + setProperty(animProp); + } + + void updateCurrentValue(const QVariant &value); + void updateState(QAbstractAnimation::State oldState, QAbstractAnimation::State newState); + void setProperty(AbstractProperty *animProp); + +private: + Q_DISABLE_COPY(QValueAnimation); + Q_DECLARE_PRIVATE(QValueAnimation); +}; + +#endif // QVALUEANIMATION_H diff --git a/examples/animation/research/memberfunctions/qvalueanimation_p.h b/examples/animation/research/memberfunctions/qvalueanimation_p.h new file mode 100644 index 0000000..e6e7682 --- /dev/null +++ b/examples/animation/research/memberfunctions/qvalueanimation_p.h @@ -0,0 +1,47 @@ +/**************************************************************************** +** +** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +****************************************************************************/ + +#ifndef QVALUEANIMATION_P_H +#define QVALUEANIMATION_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of QIODevice. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <private/qvariantanimation_p.h> //### + +QT_BEGIN_NAMESPACE + +class QValueAnimationPrivate : public QVariantAnimationPrivate +{ + Q_DECLARE_PUBLIC(QValueAnimation) +public: + QValueAnimationPrivate() : QVariantAnimationPrivate(), animProp(0) + { + } + + void initDefaultStartValue(); + + AbstractProperty *animProp; + + //###TODO +}; + +QT_END_NAMESPACE + +#endif //QVALUEANIMATION_P_H diff --git a/examples/animation/research/propertytransform/main.cpp b/examples/animation/research/propertytransform/main.cpp new file mode 100644 index 0000000..80002c9 --- /dev/null +++ b/examples/animation/research/propertytransform/main.cpp @@ -0,0 +1,47 @@ +#include <QtGui> + +#include "qpropertytransform.h" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + QGraphicsScene scene; + QGraphicsView view(&scene); + + QGraphicsItem *item = new QGraphicsRectItem(QRectF(0,0, 200, 100)); + scene.addItem(item); + QPropertyTransform transform; + transform.setTargetItem(item); + + QAnimationGroup *group = new QAnimationGroup(QAnimationGroup::Parallel, &scene); + QPropertyAnimation *scaleAnim = new QPropertyAnimation(&transform, "scaleX"); + scaleAnim->setStartValue(1.0); + scaleAnim->setTargetValue(2.0); + scaleAnim->setDuration(10000); + group->add(scaleAnim); + + QPropertyAnimation *scaleAnim2 = new QPropertyAnimation(&transform, "scaleY"); + scaleAnim2->setStartValue(.0); + scaleAnim2->setTargetValue(2.0); + scaleAnim2->setDuration(10000); + QEasingCurve curve(QEasingCurve::InElastic); + curve.setPeriod(2); + curve.setAmplitude(2); + + //scaleAnim2->setEasingCurve(curve); + //scaleAnim2->setEasingCurve(QEasingCurve(QEasingCurve::OutElastic , 2, 2 )); + group->add(scaleAnim2); + + QPropertyAnimation *rotAnim = new QPropertyAnimation(&transform, "rotation"); + rotAnim->setStartValue(0); + rotAnim->setTargetValue(90); + rotAnim->setDuration(10000); + group->add(rotAnim); + + group->start(); + + view.resize(800,600); + view.show(); + return app.exec(); +} diff --git a/examples/animation/research/propertytransform/propertytransform.pro b/examples/animation/research/propertytransform/propertytransform.pro new file mode 100644 index 0000000..94c36b8 --- /dev/null +++ b/examples/animation/research/propertytransform/propertytransform.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) fr 26. sep 13:21:57 2008 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +HEADERS += qpropertytransform.h +CONFIG += console + diff --git a/examples/animation/research/propertytransform/qpropertytransform.h b/examples/animation/research/propertytransform/qpropertytransform.h new file mode 100644 index 0000000..e052625 --- /dev/null +++ b/examples/animation/research/propertytransform/qpropertytransform.h @@ -0,0 +1,78 @@ +#include <QtCore/QObject> +#include <QtCore/QPoint> +#include <QtGui/QGraphicsItem> +#include <QtGui/QTransform> +#include <QtCore/QDebug> + +/** + * Experimental. + * Pros: + * 1. Does not add ugly/confusing API to QGraphicsItem. + * + * Cons: + * 1. apply() /m_item->setTransform() is called too many times. (FIXED NOW?) + * + * + */ +class QPropertyTransform : public QObject { + Q_OBJECT +public: + Q_PROPERTY(QPointF center READ center WRITE setCenter); + Q_PROPERTY(qreal rotation READ rotation WRITE setRotation); + Q_PROPERTY(qreal scaleX READ scaleX WRITE setScaleX); + Q_PROPERTY(qreal scaleY READ scaleY WRITE setScaleY); +public: + QPropertyTransform() : m_item(0), m_rotationZ(0), m_scaleX(1.), m_scaleY(1.) {} + + void setTargetItem(QGraphicsItem *item) { + m_item = item; + } + + void setCenter(const QPointF ¢er) { + m_center = center; + apply(); + } + + QPointF center() const { return m_center; } + + void setRotation(qreal rotation) { + m_rotationZ = rotation; + apply(); + } + + qreal rotation() const { return m_rotationZ; } + + void setScaleX(qreal scale) { + m_scaleX = scale; + apply(); + } + + qreal scaleX() const { return m_scaleX; } + + void setScaleY(qreal scale) { + m_scaleY = scale; + apply(); + } + + qreal scaleY() const { return m_scaleY; } + +private: + QTransform transform() const { + return QTransform().translate(m_center.x(), m_center.y()) + .rotate(m_rotationZ) + .scale(m_scaleX, m_scaleY) + .translate(-m_center.x(), -m_center.y()); + } + + void apply() { + if (m_item) + m_item->setTransform(transform()); + } + + QGraphicsItem *m_item; + QPointF m_center; + qreal m_rotationZ; + qreal m_scaleX; + qreal m_scaleY; +}; + diff --git a/examples/animation/research/sound/main.cpp b/examples/animation/research/sound/main.cpp new file mode 100644 index 0000000..2c9fa47 --- /dev/null +++ b/examples/animation/research/sound/main.cpp @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +****************************************************************************/ + +//The purpose of this example is to show that it is possible to have your own +// animation with undefined duration + +#include <QtGui/QtGui> +#include <phonon> + +class SoundAnimation : public QAbstractAnimation +{ +public: + SoundAnimation(const QString &file) + { + //in an idea case we should also control the errors + Phonon::createPath(&m_media, &m_audio); + m_media.setCurrentSource(file); + connect(&m_media, SIGNAL(finished()), SLOT(stop())); + } + + int duration() const + { + return -1; + } + + void updateCurrentTime(int msecs) + { + //nothing to do here... + qDebug() << "updateCurrentTime" << msecs; + } + + void updateState(State state) + { + switch(state) + { + case Running: + m_media.play(); + break; + case Stopped: + m_media.stop(); + break; + case Paused: + m_media.pause(); + break; + } + } + + +private: + Phonon::MediaObject m_media; + Phonon::AudioOutput m_audio; + +}; + + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + Q_INIT_RESOURCE(sound); + + SoundAnimation anim(QLatin1String(":/media/sax.mp3")); + app.connect(&anim, SIGNAL(finished()), SLOT(quit())); + anim.start(); + + return app.exec(); +}
\ No newline at end of file diff --git a/examples/animation/research/sound/media/sax.mp3 b/examples/animation/research/sound/media/sax.mp3 Binary files differnew file mode 100644 index 0000000..0a078b1 --- /dev/null +++ b/examples/animation/research/sound/media/sax.mp3 diff --git a/examples/animation/research/sound/sound.pro b/examples/animation/research/sound/sound.pro new file mode 100644 index 0000000..0ad3050 --- /dev/null +++ b/examples/animation/research/sound/sound.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) mer. 21. janv. 13:53:26 2009 +###################################################################### + +TEMPLATE = app +QT += phonon +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +RESOURCES = sound.qrc + +# Input +SOURCES += main.cpp diff --git a/examples/animation/research/sound/sound.qrc b/examples/animation/research/sound/sound.qrc new file mode 100644 index 0000000..8919142 --- /dev/null +++ b/examples/animation/research/sound/sound.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> +<file>media/sax.mp3</file> +</qresource> +</RCC> |