summaryrefslogtreecommitdiffstats
path: root/examples/widgets/animation/stickman
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/animation/stickman')
-rw-r--r--examples/widgets/animation/stickman/animation.cpp18
-rw-r--r--examples/widgets/animation/stickman/animation.h4
-rw-r--r--examples/widgets/animation/stickman/graphicsview.cpp12
-rw-r--r--examples/widgets/animation/stickman/graphicsview.h12
-rw-r--r--examples/widgets/animation/stickman/lifecycle.cpp19
-rw-r--r--examples/widgets/animation/stickman/lifecycle.h11
-rw-r--r--examples/widgets/animation/stickman/node.h4
-rw-r--r--examples/widgets/animation/stickman/rectbutton.cpp7
-rw-r--r--examples/widgets/animation/stickman/rectbutton.h8
-rw-r--r--examples/widgets/animation/stickman/stickman.cpp29
-rw-r--r--examples/widgets/animation/stickman/stickman.h13
11 files changed, 56 insertions, 81 deletions
diff --git a/examples/widgets/animation/stickman/animation.cpp b/examples/widgets/animation/stickman/animation.cpp
index 5c2d1682af..73d79adc84 100644
--- a/examples/widgets/animation/stickman/animation.cpp
+++ b/examples/widgets/animation/stickman/animation.cpp
@@ -50,16 +50,13 @@
#include "animation.h"
-#include <QPointF>
-#include <QVector>
#include <QIODevice>
#include <QDataStream>
class Frame
{
public:
- Frame() {
- }
+ Frame() = default;
int nodeCount() const
{
@@ -85,9 +82,8 @@ private:
QVector<QPointF> m_nodePositions;
};
-Animation::Animation()
+Animation::Animation() : m_currentFrame(0)
{
- m_currentFrame = 0;
m_frames.append(new Frame);
}
@@ -103,6 +99,8 @@ void Animation::setTotalFrames(int totalFrames)
while (totalFrames < m_frames.size())
delete m_frames.takeLast();
+
+ setCurrentFrame(m_currentFrame);
}
int Animation::totalFrames() const
@@ -112,7 +110,7 @@ int Animation::totalFrames() const
void Animation::setCurrentFrame(int currentFrame)
{
- m_currentFrame = qMax(qMin(currentFrame, totalFrames()-1), 0);
+ m_currentFrame = qBound(0, currentFrame, totalFrames() - 1);
}
int Animation::currentFrame() const
@@ -177,18 +175,16 @@ void Animation::load(QIODevice *device)
int frameCount;
stream >> frameCount;
- for (int i=0; i<frameCount; ++i) {
-
+ for (int i = 0; i < frameCount; ++i) {
int nodeCount;
stream >> nodeCount;
Frame *frame = new Frame;
frame->setNodeCount(nodeCount);
- for (int j=0; j<nodeCount; ++j) {
+ for (int j = 0; j < nodeCount; ++j) {
QPointF pos;
stream >> pos;
-
frame->setNodePos(j, pos);
}
diff --git a/examples/widgets/animation/stickman/animation.h b/examples/widgets/animation/stickman/animation.h
index e57847aeaa..5cc1133ac0 100644
--- a/examples/widgets/animation/stickman/animation.h
+++ b/examples/widgets/animation/stickman/animation.h
@@ -52,8 +52,8 @@
#define ANIMATION_H
#include <QPointF>
-#include <QList>
#include <QString>
+#include <QVector>
class Frame;
QT_BEGIN_NAMESPACE
@@ -85,7 +85,7 @@ public:
private:
QString m_name;
- QList<Frame *> m_frames;
+ QVector<Frame *> m_frames;
int m_currentFrame;
};
diff --git a/examples/widgets/animation/stickman/graphicsview.cpp b/examples/widgets/animation/stickman/graphicsview.cpp
index 7058e15345..0f5800cff3 100644
--- a/examples/widgets/animation/stickman/graphicsview.cpp
+++ b/examples/widgets/animation/stickman/graphicsview.cpp
@@ -51,13 +51,8 @@
#include "graphicsview.h"
#include "stickman.h"
-#include <QtGui/QKeyEvent>
-#include <QtWidgets/QGraphicsScene>
-#include <QtWidgets/QGraphicsView>
-
-GraphicsView::GraphicsView(QWidget *parent)
- : QGraphicsView(parent), m_editor(nullptr)
-{}
+#include <QKeyEvent>
+#include <QGraphicsScene>
void GraphicsView::keyPressEvent(QKeyEvent *e)
{
@@ -66,7 +61,8 @@ void GraphicsView::keyPressEvent(QKeyEvent *e)
emit keyPressed(Qt::Key(e->key()));
}
-void GraphicsView::resizeEvent(QResizeEvent *)
+void GraphicsView::resizeEvent(QResizeEvent *e)
{
fitInView(scene()->sceneRect());
+ QGraphicsView::resizeEvent(e);
}
diff --git a/examples/widgets/animation/stickman/graphicsview.h b/examples/widgets/animation/stickman/graphicsview.h
index 361fee219d..29f4c6237e 100644
--- a/examples/widgets/animation/stickman/graphicsview.h
+++ b/examples/widgets/animation/stickman/graphicsview.h
@@ -51,24 +51,20 @@
#ifndef GRAPHICSVIEW_H
#define GRAPHICSVIEW_H
-#include <QtWidgets/QGraphicsView>
+#include <QGraphicsView>
-class MainWindow;
class GraphicsView: public QGraphicsView
{
Q_OBJECT
public:
- GraphicsView(QWidget *parent = nullptr);
+ using QGraphicsView::QGraphicsView;
protected:
- void resizeEvent(QResizeEvent *event) override;
- void keyPressEvent(QKeyEvent *) override;
+ void resizeEvent(QResizeEvent *e) override;
+ void keyPressEvent(QKeyEvent *e) override;
signals:
void keyPressed(int key);
-
-private:
- MainWindow *m_editor;
};
#endif
diff --git a/examples/widgets/animation/stickman/lifecycle.cpp b/examples/widgets/animation/stickman/lifecycle.cpp
index 046e3f4cd1..5ad284c590 100644
--- a/examples/widgets/animation/stickman/lifecycle.cpp
+++ b/examples/widgets/animation/stickman/lifecycle.cpp
@@ -54,8 +54,15 @@
#include "animation.h"
#include "graphicsview.h"
-#include <QtCore>
-#include <QtWidgets>
+#include <QEventTransition>
+#include <QFile>
+#include <QParallelAnimationGroup>
+#include <QPropertyAnimation>
+#include <QRandomGenerator>
+#include <QSignalTransition>
+#include <QState>
+#include <QStateMachine>
+#include <QTimer>
class KeyPressTransition: public QSignalTransition
{
@@ -107,7 +114,7 @@ LifeCycle::LifeCycle(StickMan *stickMan, GraphicsView *keyReceiver)
// Create animation group to be used for all transitions
m_animationGroup = new QParallelAnimationGroup();
const int stickManNodeCount = m_stickMan->nodeCount();
- for (int i=0; i<stickManNodeCount; ++i) {
+ for (int i = 0; i < stickManNodeCount; ++i) {
QPropertyAnimation *pa = new QPropertyAnimation(m_stickMan->node(i), "pos");
m_animationGroup->addAnimation(pa);
}
@@ -175,7 +182,7 @@ void LifeCycle::addActivity(const QString &fileName, Qt::Key key, QObject *sende
QState *state = makeState(m_alive, fileName);
m_alive->addTransition(new KeyPressTransition(m_keyReceiver, key, state));
- if (sender || signal)
+ if (sender && signal)
m_alive->addTransition(sender, signal, state);
}
@@ -192,13 +199,13 @@ QState *LifeCycle::makeState(QState *parentState, const QString &animationFileNa
const int frameCount = animation.totalFrames();
QState *previousState = nullptr;
- for (int i=0; i<frameCount; ++i) {
+ for (int i = 0; i < frameCount; ++i) {
animation.setCurrentFrame(i);
//! [1]
QState *frameState = new QState(topLevel);
const int nodeCount = animation.nodeCount();
- for (int j=0; j<nodeCount; ++j)
+ for (int j = 0; j < nodeCount; ++j)
frameState->assignProperty(m_stickMan->node(j), "pos", animation.nodePos(j));
//! [1]
diff --git a/examples/widgets/animation/stickman/lifecycle.h b/examples/widgets/animation/stickman/lifecycle.h
index e3f9876676..21ab99276d 100644
--- a/examples/widgets/animation/stickman/lifecycle.h
+++ b/examples/widgets/animation/stickman/lifecycle.h
@@ -53,16 +53,16 @@
#include <Qt>
-class StickMan;
QT_BEGIN_NAMESPACE
-class QStateMachine;
-class QAnimationGroup;
-class QState;
class QAbstractState;
class QAbstractTransition;
+class QAnimationGroup;
class QObject;
+class QState;
+class QStateMachine;
QT_END_NAMESPACE
class GraphicsView;
+class StickMan;
class LifeCycle
{
public:
@@ -70,7 +70,8 @@ public:
~LifeCycle();
void setDeathAnimation(const QString &fileName);
- void addActivity(const QString &fileName, Qt::Key key, QObject *sender = NULL, const char *signal = NULL);
+ void addActivity(const QString &fileName, Qt::Key key,
+ QObject *sender = nullptr, const char *signal = nullptr);
void start();
diff --git a/examples/widgets/animation/stickman/node.h b/examples/widgets/animation/stickman/node.h
index 679999b7e8..2b393c60c1 100644
--- a/examples/widgets/animation/stickman/node.h
+++ b/examples/widgets/animation/stickman/node.h
@@ -51,13 +51,13 @@
#ifndef NODE_H
#define NODE_H
-#include <QGraphicsItem>
+#include <QGraphicsObject>
class Node: public QGraphicsObject
{
Q_OBJECT
public:
- explicit Node(const QPointF &pos, QGraphicsItem *parent = 0);
+ explicit Node(const QPointF &pos, QGraphicsItem *parent = nullptr);
~Node();
QRectF boundingRect() const override;
diff --git a/examples/widgets/animation/stickman/rectbutton.cpp b/examples/widgets/animation/stickman/rectbutton.cpp
index 7eea94ae6f..5174d0aeaf 100644
--- a/examples/widgets/animation/stickman/rectbutton.cpp
+++ b/examples/widgets/animation/stickman/rectbutton.cpp
@@ -51,12 +51,7 @@
#include "rectbutton.h"
#include <QPainter>
-RectButton::RectButton(QString buttonText) : m_ButtonText(buttonText)
-{
-}
-
-
-RectButton::~RectButton()
+RectButton::RectButton(const QString &buttonText) : m_ButtonText(buttonText)
{
}
diff --git a/examples/widgets/animation/stickman/rectbutton.h b/examples/widgets/animation/stickman/rectbutton.h
index ab47bad0f7..ee6cd3f530 100644
--- a/examples/widgets/animation/stickman/rectbutton.h
+++ b/examples/widgets/animation/stickman/rectbutton.h
@@ -57,19 +57,19 @@ class RectButton : public QGraphicsObject
{
Q_OBJECT
public:
- RectButton(QString buttonText);
- ~RectButton();
+ RectButton(const QString &buttonText);
QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
protected:
- QString m_ButtonText;
-
void mousePressEvent (QGraphicsSceneMouseEvent *event) override;
signals:
void clicked();
+
+private:
+ QString m_ButtonText;
};
#endif // RECTBUTTON_H
diff --git a/examples/widgets/animation/stickman/stickman.cpp b/examples/widgets/animation/stickman/stickman.cpp
index 5725f64eec..3f373b6b52 100644
--- a/examples/widgets/animation/stickman/stickman.cpp
+++ b/examples/widgets/animation/stickman/stickman.cpp
@@ -52,10 +52,9 @@
#include "node.h"
#include <QPainter>
-#include <QTimer>
-#include <qmath.h>
+#include <QtMath>
-static const qreal Coords[NodeCount * 2] = {
+static constexpr qreal Coords[NodeCount * 2] = {
0.0, -150.0, // head, #0
0.0, -100.0, // body pentagon, top->bottom, left->right, #1 - 5
@@ -81,7 +80,7 @@ static const qreal Coords[NodeCount * 2] = {
};
-static const int Bones[BoneCount * 2] = {
+static constexpr int Bones[BoneCount * 2] = {
0, 1, // neck
1, 2, // body
@@ -117,19 +116,13 @@ static const int Bones[BoneCount * 2] = {
StickMan::StickMan()
{
- m_sticks = true;
- m_isDead = false;
- m_pixmap = QPixmap("images/head.png");
- m_penColor = Qt::white;
- m_fillColor = Qt::black;
-
// Set up start position of limbs
- for (int i=0; i<NodeCount; ++i) {
+ for (int i = 0; i < NodeCount; ++i) {
m_nodes[i] = new Node(QPointF(Coords[i * 2], Coords[i * 2 + 1]), this);
connect(m_nodes[i], &Node::positionChanged, this, &StickMan::childPositionChanged);
}
- for (int i=0; i<BoneCount; ++i) {
+ for (int i = 0; i < BoneCount; ++i) {
int n1 = Bones[i * 2];
int n2 = Bones[i * 2 + 1];
@@ -137,16 +130,12 @@ StickMan::StickMan()
Node *node2 = m_nodes[n2];
QPointF dist = node1->pos() - node2->pos();
- m_perfectBoneLengths[i] = sqrt(pow(dist.x(),2) + pow(dist.y(),2));
+ m_perfectBoneLengths[i] = sqrt(pow(dist.x(), 2) + pow(dist.y(), 2));
}
startTimer(10);
}
-StickMan::~StickMan()
-{
-}
-
void StickMan::childPositionChanged()
{
prepareGeometryChange();
@@ -155,7 +144,7 @@ void StickMan::childPositionChanged()
void StickMan::setDrawSticks(bool on)
{
m_sticks = on;
- for (int i=0;i<nodeCount();++i) {
+ for (int i = 0; i < nodeCount(); ++i) {
Node *node = m_nodes[i];
node->setVisible(on);
}
@@ -188,7 +177,7 @@ void StickMan::stabilize()
{
static const qreal threshold = 0.001;
- for (int i=0; i<BoneCount; ++i) {
+ for (int i = 0; i < BoneCount; ++i) {
int n1 = Bones[i * 2];
int n2 = Bones[i * 2 + 1];
@@ -236,7 +225,7 @@ void StickMan::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge
stabilize();
if (m_sticks) {
painter->setPen(Qt::white);
- for (int i=0; i<BoneCount; ++i) {
+ for (int i = 0; i < BoneCount; ++i) {
int n1 = Bones[i * 2];
int n2 = Bones[i * 2 + 1];
diff --git a/examples/widgets/animation/stickman/stickman.h b/examples/widgets/animation/stickman/stickman.h
index f2311a0358..63c02abc8f 100644
--- a/examples/widgets/animation/stickman/stickman.h
+++ b/examples/widgets/animation/stickman/stickman.h
@@ -57,8 +57,6 @@ static const int NodeCount = 16;
static const int BoneCount = 24;
class Node;
-QT_BEGIN_NAMESPACE
-QT_END_NAMESPACE
class StickMan: public QGraphicsObject
{
Q_OBJECT
@@ -67,7 +65,6 @@ class StickMan: public QGraphicsObject
Q_PROPERTY(bool isDead WRITE setIsDead READ isDead)
public:
StickMan();
- ~StickMan();
QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
@@ -101,13 +98,11 @@ private:
Node *m_nodes[NodeCount];
qreal m_perfectBoneLengths[BoneCount];
- uint m_sticks : 1;
- uint m_isDead : 1;
- uint m_reserved : 30;
+ bool m_sticks = true;
+ bool m_isDead = false;
- QPixmap m_pixmap;
- QColor m_penColor;
- QColor m_fillColor;
+ QColor m_penColor = Qt::white;
+ QColor m_fillColor = Qt::black;
};
#endif // STICKMAN_H