From 13426aff248c25b44ac377f37dc3e3a54ea0ea86 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sat, 13 Jul 2019 20:39:05 +0200 Subject: Cleanup QtWidgets animation examples Cleanup the QtWidgets animation examples: - use nullptr - use normalized includes, remove unused includes - fix style - fix crash of sub-attaq when the game ended (error during range-based for loop porting) - don't use keyword 'final' for a variable name Change-Id: Id23be8ff8b1b310da005d13c052fe547f6a0d63a Reviewed-by: Friedemann Kleint --- examples/widgets/animation/stickman/animation.cpp | 18 ++++++-------- examples/widgets/animation/stickman/animation.h | 4 +-- .../widgets/animation/stickman/graphicsview.cpp | 12 +++------ examples/widgets/animation/stickman/graphicsview.h | 12 +++------ examples/widgets/animation/stickman/lifecycle.cpp | 19 +++++++++----- examples/widgets/animation/stickman/lifecycle.h | 11 ++++---- examples/widgets/animation/stickman/node.h | 4 +-- examples/widgets/animation/stickman/rectbutton.cpp | 7 +----- examples/widgets/animation/stickman/rectbutton.h | 8 +++--- examples/widgets/animation/stickman/stickman.cpp | 29 +++++++--------------- examples/widgets/animation/stickman/stickman.h | 13 +++------- 11 files changed, 56 insertions(+), 81 deletions(-) (limited to 'examples/widgets/animation/stickman') 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 -#include #include #include class Frame { public: - Frame() { - } + Frame() = default; int nodeCount() const { @@ -85,9 +82,8 @@ private: QVector 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> nodeCount; Frame *frame = new Frame; frame->setNodeCount(nodeCount); - for (int j=0; j> 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 -#include #include +#include class Frame; QT_BEGIN_NAMESPACE @@ -85,7 +85,7 @@ public: private: QString m_name; - QList m_frames; + QVector 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 -#include -#include - -GraphicsView::GraphicsView(QWidget *parent) - : QGraphicsView(parent), m_editor(nullptr) -{} +#include +#include 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 +#include -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 -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include 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; inode(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; iassignProperty(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 -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 +#include 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 -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 -#include -#include +#include -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; ipos() - 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;isetVisible(on); } @@ -188,7 +177,7 @@ void StickMan::stabilize() { static const qreal threshold = 0.001; - for (int i=0; isetPen(Qt::white); - for (int i=0; i