diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/animation/stickman/lifecycle.cpp | 4 | ||||
-rw-r--r-- | examples/animation/stickman/main.cpp | 38 | ||||
-rw-r--r-- | examples/animation/stickman/node.cpp | 4 | ||||
-rw-r--r-- | examples/animation/stickman/node.h | 3 | ||||
-rw-r--r-- | examples/animation/stickman/stickman.cpp | 5 | ||||
-rw-r--r-- | examples/animation/stickman/stickman.h | 15 |
6 files changed, 32 insertions, 37 deletions
diff --git a/examples/animation/stickman/lifecycle.cpp b/examples/animation/stickman/lifecycle.cpp index 0fff529..463a27d 100644 --- a/examples/animation/stickman/lifecycle.cpp +++ b/examples/animation/stickman/lifecycle.cpp @@ -100,7 +100,7 @@ LifeCycle::LifeCycle(StickMan *stickMan, GraphicsView *keyReceiver) m_animationGroup = new QParallelAnimationGroup(); const int stickManNodeCount = m_stickMan->nodeCount(); for (int i=0; i<stickManNodeCount; ++i) { - QPropertyAnimation *pa = new QPropertyAnimation(m_stickMan->node(i), "position"); + QPropertyAnimation *pa = new QPropertyAnimation(m_stickMan->node(i), "pos"); m_animationGroup->addAnimation(pa); } @@ -186,7 +186,7 @@ QState *LifeCycle::makeState(QState *parentState, const QString &animationFileNa QState *frameState = new QState(topLevel); const int nodeCount = animation.nodeCount(); for (int j=0; j<nodeCount; ++j) - frameState->assignProperty(m_stickMan->node(j), "position", animation.nodePos(j)); + frameState->assignProperty(m_stickMan->node(j), "pos", animation.nodePos(j)); //! [1] frameState->setObjectName(QString::fromLatin1("frame %0").arg(i)); diff --git a/examples/animation/stickman/main.cpp b/examples/animation/stickman/main.cpp index 799f45c..f363d5d 100644 --- a/examples/animation/stickman/main.cpp +++ b/examples/animation/stickman/main.cpp @@ -73,30 +73,30 @@ int main(int argc, char **argv) QRectF stickManBoundingRect = stickMan->mapToScene(stickMan->boundingRect()).boundingRect(); textItem->setPos(-w / 2.0, stickManBoundingRect.bottom() + 25.0); - QGraphicsScene *scene = new QGraphicsScene(); - scene->addItem(stickMan); - scene->addItem(textItem); - scene->setBackgroundBrush(Qt::black); + QGraphicsScene scene; + scene.addItem(stickMan); + scene.addItem(textItem); + scene.setBackgroundBrush(Qt::black); - GraphicsView *view = new GraphicsView(); - view->setRenderHints(QPainter::Antialiasing); - view->setTransformationAnchor(QGraphicsView::NoAnchor); - view->setScene(scene); - view->show(); - view->setFocus(); + GraphicsView view; + view.setRenderHints(QPainter::Antialiasing); + view.setTransformationAnchor(QGraphicsView::NoAnchor); + view.setScene(&scene); + view.show(); + view.setFocus(); - QRectF sceneRect = scene->sceneRect(); + QRectF sceneRect = scene.sceneRect(); // making enough room in the scene for stickman to jump and die - view->resize(sceneRect.width() + 100, sceneRect.height() + 100); - view->setSceneRect(sceneRect); + view.resize(sceneRect.width() + 100, sceneRect.height() + 100); + view.setSceneRect(sceneRect); - LifeCycle *cycle = new LifeCycle(stickMan, view); - cycle->setDeathAnimation(":/animations/dead"); + LifeCycle cycle(stickMan, &view); + cycle.setDeathAnimation(":/animations/dead"); - cycle->addActivity(":/animations/jumping", Qt::Key_J); - cycle->addActivity(":/animations/dancing", Qt::Key_D); - cycle->addActivity(":/animations/chilling", Qt::Key_C); - cycle->start(); + cycle.addActivity(":/animations/jumping", Qt::Key_J); + cycle.addActivity(":/animations/dancing", Qt::Key_D); + cycle.addActivity(":/animations/chilling", Qt::Key_C); + cycle.start(); return app.exec(); } diff --git a/examples/animation/stickman/node.cpp b/examples/animation/stickman/node.cpp index 69ff906..1a138b2 100644 --- a/examples/animation/stickman/node.cpp +++ b/examples/animation/stickman/node.cpp @@ -47,7 +47,7 @@ #include <QGraphicsSceneMouseEvent> Node::Node(const QPointF &pos, QGraphicsItem *parent) - : QGraphicsItem(parent), m_dragging(false) + : QGraphicsObject(parent), m_dragging(false) { setPos(pos); setFlag(QGraphicsItem::ItemSendsGeometryChanges); @@ -73,7 +73,7 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) if (change == QGraphicsItem::ItemPositionChange) emit positionChanged(); - return QGraphicsItem::itemChange(change, value); + return QGraphicsObject::itemChange(change, value); } void Node::mousePressEvent(QGraphicsSceneMouseEvent *) diff --git a/examples/animation/stickman/node.h b/examples/animation/stickman/node.h index 66ee565..4360d2e 100644 --- a/examples/animation/stickman/node.h +++ b/examples/animation/stickman/node.h @@ -44,10 +44,9 @@ #include <QGraphicsItem> -class Node: public QObject, public QGraphicsItem +class Node: public QGraphicsObject { Q_OBJECT - Q_PROPERTY(QPointF position READ pos WRITE setPos) public: Node(const QPointF &pos, QGraphicsItem *parent = 0); ~Node(); diff --git a/examples/animation/stickman/stickman.cpp b/examples/animation/stickman/stickman.cpp index 78e9e5b..67e022b 100644 --- a/examples/animation/stickman/stickman.cpp +++ b/examples/animation/stickman/stickman.cpp @@ -52,7 +52,6 @@ #define M_PI 3.14159265358979323846 #endif -static const int NodeCount = 16; static const qreal Coords[NodeCount * 2] = { 0.0, -150.0, // head, #0 @@ -79,7 +78,6 @@ static const qreal Coords[NodeCount * 2] = { }; -static const int BoneCount = 24; static const int Bones[BoneCount * 2] = { 0, 1, // neck @@ -116,7 +114,6 @@ static const int Bones[BoneCount * 2] = { StickMan::StickMan() { - m_nodes = new Node*[NodeCount]; m_sticks = true; m_isDead = false; m_pixmap = QPixmap("images/head.png"); @@ -129,7 +126,6 @@ StickMan::StickMan() connect(m_nodes[i], SIGNAL(positionChanged()), this, SLOT(childPositionChanged())); } - m_perfectBoneLengths = new qreal[BoneCount]; for (int i=0; i<BoneCount; ++i) { int n1 = Bones[i * 2]; int n2 = Bones[i * 2 + 1]; @@ -146,7 +142,6 @@ StickMan::StickMan() StickMan::~StickMan() { - delete m_nodes; } void StickMan::childPositionChanged() diff --git a/examples/animation/stickman/stickman.h b/examples/animation/stickman/stickman.h index e8eedfa..d663c04 100644 --- a/examples/animation/stickman/stickman.h +++ b/examples/animation/stickman/stickman.h @@ -42,15 +42,15 @@ #ifndef STICKMAN_H #define STICKMAN_H -#include <QGraphicsItem> +#include <QGraphicsObject> -const int LimbCount = 16; +static const int NodeCount = 16; +static const int BoneCount = 24; class Node; QT_BEGIN_NAMESPACE -class QTimer; QT_END_NAMESPACE -class StickMan: public QObject, public QGraphicsItem +class StickMan: public QGraphicsObject { Q_OBJECT Q_PROPERTY(QColor penColor WRITE setPenColor READ penColor) @@ -77,7 +77,7 @@ public: bool isDead() const { return m_isDead; } void setIsDead(bool isDead) { m_isDead = isDead; } - + public slots: void stabilize(); void childPositionChanged(); @@ -86,10 +86,11 @@ protected: void timerEvent(QTimerEvent *e); private: + QPointF posFor(int idx) const; - Node **m_nodes; - qreal *m_perfectBoneLengths; + Node *m_nodes[NodeCount]; + qreal m_perfectBoneLengths[BoneCount]; uint m_sticks : 1; uint m_isDead : 1; |