summaryrefslogtreecommitdiffstats
path: root/examples/animation/research/memberfunctions
diff options
context:
space:
mode:
authorAlexis Menard <alexis.menard@nokia.com>2009-04-17 14:06:06 (GMT)
committerAlexis Menard <alexis.menard@nokia.com>2009-04-17 14:06:06 (GMT)
commitf15b8a83e2e51955776a3f07cb85ebfc342dd8ef (patch)
treec5dc684986051654898db11ce73e03b9fec8db99 /examples/animation/research/memberfunctions
downloadQt-f15b8a83e2e51955776a3f07cb85ebfc342dd8ef.zip
Qt-f15b8a83e2e51955776a3f07cb85ebfc342dd8ef.tar.gz
Qt-f15b8a83e2e51955776a3f07cb85ebfc342dd8ef.tar.bz2
Initial import of statemachine branch from the old kinetic repository
Diffstat (limited to 'examples/animation/research/memberfunctions')
-rw-r--r--examples/animation/research/memberfunctions/main.cpp48
-rw-r--r--examples/animation/research/memberfunctions/memberfunctions.pro16
-rw-r--r--examples/animation/research/memberfunctions/qvalueanimation.cpp73
-rw-r--r--examples/animation/research/memberfunctions/qvalueanimation.h89
-rw-r--r--examples/animation/research/memberfunctions/qvalueanimation_p.h47
5 files changed, 273 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