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/easing/window.cpp | 7 +-- 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 ++-- .../animation/sub-attaq/animationmanager.cpp | 17 ++--- .../widgets/animation/sub-attaq/animationmanager.h | 10 +-- examples/widgets/animation/sub-attaq/boat.cpp | 19 +++--- examples/widgets/animation/sub-attaq/boat.h | 7 +-- examples/widgets/animation/sub-attaq/boat_p.h | 20 +++--- examples/widgets/animation/sub-attaq/bomb.cpp | 19 +++--- examples/widgets/animation/sub-attaq/bomb.h | 6 +- .../widgets/animation/sub-attaq/graphicsscene.cpp | 73 ++++++++++++---------- .../widgets/animation/sub-attaq/graphicsscene.h | 21 +++---- .../widgets/animation/sub-attaq/mainwindow.cpp | 7 +-- examples/widgets/animation/sub-attaq/mainwindow.h | 8 +-- .../widgets/animation/sub-attaq/pixmapitem.cpp | 5 +- examples/widgets/animation/sub-attaq/pixmapitem.h | 8 +-- .../widgets/animation/sub-attaq/progressitem.cpp | 7 ++- .../widgets/animation/sub-attaq/progressitem.h | 8 +-- .../animation/sub-attaq/qanimationstate.cpp | 4 +- .../widgets/animation/sub-attaq/qanimationstate.h | 10 +-- examples/widgets/animation/sub-attaq/states.cpp | 28 ++++----- examples/widgets/animation/sub-attaq/states.h | 24 +++---- examples/widgets/animation/sub-attaq/submarine.cpp | 26 ++++---- examples/widgets/animation/sub-attaq/submarine.h | 11 ++-- examples/widgets/animation/sub-attaq/submarine_p.h | 9 ++- .../animation/sub-attaq/textinformationitem.cpp | 9 +-- .../animation/sub-attaq/textinformationitem.h | 6 +- examples/widgets/animation/sub-attaq/torpedo.cpp | 23 ++++--- examples/widgets/animation/sub-attaq/torpedo.h | 6 +- 38 files changed, 244 insertions(+), 291 deletions(-) (limited to 'examples/widgets/animation') diff --git a/examples/widgets/animation/easing/window.cpp b/examples/widgets/animation/easing/window.cpp index aa12147388..d1d6348361 100644 --- a/examples/widgets/animation/easing/window.cpp +++ b/examples/widgets/animation/easing/window.cpp @@ -55,11 +55,10 @@ Window::Window(QWidget *parent) m_iconSize(64, 64) { m_ui.setupUi(this); - QButtonGroup *buttonGroup = findChild(); // ### workaround for uic in 4.4 m_ui.easingCurvePicker->setIconSize(m_iconSize); m_ui.easingCurvePicker->setMinimumHeight(m_iconSize.height() + 50); - buttonGroup->setId(m_ui.lineRadio, 0); - buttonGroup->setId(m_ui.circleRadio, 1); + m_ui.buttonGroup->setId(m_ui.lineRadio, 0); + m_ui.buttonGroup->setId(m_ui.circleRadio, 1); QEasingCurve dummy; m_ui.periodSpinBox->setValue(dummy.period()); @@ -68,7 +67,7 @@ Window::Window(QWidget *parent) connect(m_ui.easingCurvePicker, &QListWidget::currentRowChanged, this, &Window::curveChanged); - connect(buttonGroup, QOverload::of(&QButtonGroup::buttonClicked), + connect(m_ui.buttonGroup, QOverload::of(&QButtonGroup::buttonClicked), this, &Window::pathChanged); connect(m_ui.periodSpinBox, QOverload::of(&QDoubleSpinBox::valueChanged), this, &Window::periodChanged); 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 -#include - -// the universe's only animation manager -AnimationManager *AnimationManager::instance = nullptr; - -AnimationManager::AnimationManager() -{ -} +#include AnimationManager *AnimationManager::self() { - if (!instance) - instance = new AnimationManager; - return instance; + // the universe's only animation manager + static AnimationManager s_instance; + return &s_instance; } void AnimationManager::registerAnimation(QAbstractAnimation *anim) diff --git a/examples/widgets/animation/sub-attaq/animationmanager.h b/examples/widgets/animation/sub-attaq/animationmanager.h index 9365fa1f1e..5ddea66e80 100644 --- a/examples/widgets/animation/sub-attaq/animationmanager.h +++ b/examples/widgets/animation/sub-attaq/animationmanager.h @@ -51,7 +51,7 @@ #ifndef ANIMATIONMANAGER_H #define ANIMATIONMANAGER_H -#include +#include QT_BEGIN_NAMESPACE class QAbstractAnimation; @@ -59,9 +59,10 @@ QT_END_NAMESPACE class AnimationManager : public QObject { -Q_OBJECT + Q_OBJECT + AnimationManager() = default; + ~AnimationManager() = default; public: - AnimationManager(); void registerAnimation(QAbstractAnimation *anim); void unregisterAnimation(QAbstractAnimation *anim); void unregisterAllAnimations(); @@ -75,8 +76,7 @@ private slots: void unregisterAnimation_helper(QObject *obj); private: - static AnimationManager *instance; - QList animations; + QVector animations; }; #endif // ANIMATIONMANAGER_H diff --git a/examples/widgets/animation/sub-attaq/boat.cpp b/examples/widgets/animation/sub-attaq/boat.cpp index 9037d54878..d5fa314b60 100644 --- a/examples/widgets/animation/sub-attaq/boat.cpp +++ b/examples/widgets/animation/sub-attaq/boat.cpp @@ -52,18 +52,17 @@ #include "boat.h" #include "boat_p.h" #include "bomb.h" -#include "pixmapitem.h" #include "graphicsscene.h" #include "animationmanager.h" #include "qanimationstate.h" //Qt -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include static QAbstractAnimation *setupDestroyAnimation(Boat *boat) { @@ -181,7 +180,7 @@ Boat::Boat() launchStateLeft->addTransition(historyState); launchStateRight->addTransition(historyState); - QFinalState *final = new QFinalState(machine); + QFinalState *finalState = new QFinalState(machine); //This state play the destroyed animation QAnimationState *destroyedState = new QAnimationState(machine); @@ -191,10 +190,10 @@ Boat::Boat() moving->addTransition(this, &Boat::boatDestroyed, destroyedState); //Transition to final state when the destroyed animation is finished - destroyedState->addTransition(destroyedState, &QAnimationState::animationFinished, final); + destroyedState->addTransition(destroyedState, &QAnimationState::animationFinished, finalState); //The machine has finished to be executed, then the boat is dead - connect(machine,&QState::finished, this, &Boat::boatExecutionFinished); + connect(machine, &QState::finished, this, &Boat::boatExecutionFinished); } diff --git a/examples/widgets/animation/sub-attaq/boat.h b/examples/widgets/animation/sub-attaq/boat.h index a75e2b1474..22f2f0f7c1 100644 --- a/examples/widgets/animation/sub-attaq/boat.h +++ b/examples/widgets/animation/sub-attaq/boat.h @@ -48,12 +48,11 @@ ** ****************************************************************************/ -#ifndef __BOAT__H__ -#define __BOAT__H__ +#ifndef BOAT_H +#define BOAT_H #include "pixmapitem.h" -class Bomb; QT_BEGIN_NAMESPACE class QVariantAnimation; class QAbstractAnimation; @@ -101,4 +100,4 @@ private: QStateMachine *machine; }; -#endif //__BOAT__H__ +#endif // BOAT_H diff --git a/examples/widgets/animation/sub-attaq/boat_p.h b/examples/widgets/animation/sub-attaq/boat_p.h index 8ebfeb27f5..bb1a783392 100644 --- a/examples/widgets/animation/sub-attaq/boat_p.h +++ b/examples/widgets/animation/sub-attaq/boat_p.h @@ -67,7 +67,9 @@ #include "graphicsscene.h" // Qt -#include +#include +#include +#include static const int MAX_BOMB = 5; @@ -88,7 +90,7 @@ protected: return (boat->currentSpeed() == 1); } private: - Boat * boat; + Boat *boat; }; //These transtion test if we have to move the boat (i.e current speed was 0 or another value) @@ -118,7 +120,7 @@ protected: boat->updateBoatMovement(); } private: - Boat * boat; + Boat *boat; int key; }; @@ -139,7 +141,7 @@ protected: return (boat->bombsLaunched() < MAX_BOMB); } private: - Boat * boat; + Boat *boat; }; //This state is describing when the boat is moving right @@ -157,7 +159,7 @@ protected: boat->updateBoatMovement(); } private: - Boat * boat; + Boat *boat; }; //This state is describing when the boat is moving left @@ -175,7 +177,7 @@ protected: boat->updateBoatMovement(); } private: - Boat * boat; + Boat *boat; }; //This state is describing when the boat is in a stand by position @@ -194,7 +196,7 @@ protected: boat->updateBoatMovement(); } private: - Boat * boat; + Boat *boat; }; //This state is describing the launch of the torpedo on the right @@ -216,7 +218,7 @@ protected: boat->setBombsLaunched(boat->bombsLaunched() + 1); } private: - Boat * boat; + Boat *boat; }; //This state is describing the launch of the torpedo on the left @@ -238,7 +240,7 @@ protected: boat->setBombsLaunched(boat->bombsLaunched() + 1); } private: - Boat * boat; + Boat *boat; }; #endif // BOAT_P_H diff --git a/examples/widgets/animation/sub-attaq/bomb.cpp b/examples/widgets/animation/sub-attaq/bomb.cpp index 2b865137dd..0b9c365662 100644 --- a/examples/widgets/animation/sub-attaq/bomb.cpp +++ b/examples/widgets/animation/sub-attaq/bomb.cpp @@ -51,15 +51,14 @@ //Own #include "bomb.h" #include "submarine.h" -#include "pixmapitem.h" #include "animationmanager.h" #include "qanimationstate.h" //Qt -#include -#include -#include -#include +#include +#include +#include +#include Bomb::Bomb() : PixmapItem(QString("bomb"), GraphicsScene::Big) { @@ -83,7 +82,7 @@ void Bomb::launch(Bomb::Direction direction) anim->setEndValue(QPointF(x() + delta*2,scene()->height())); anim->setDuration(y()/2*60); launchAnimation->addAnimation(anim); - connect(anim,&QVariantAnimation::valueChanged,this,&Bomb::onAnimationLaunchValueChanged); + connect(anim, &QVariantAnimation::valueChanged, this, &Bomb::onAnimationLaunchValueChanged); connect(this, &Bomb::bombExploded, launchAnimation, &QAbstractAnimation::stop); //We setup the state machine of the bomb QStateMachine *machine = new QStateMachine(this); @@ -93,18 +92,18 @@ void Bomb::launch(Bomb::Direction direction) launched->setAnimation(launchAnimation); //End - QFinalState *final = new QFinalState(machine); + QFinalState *finalState = new QFinalState(machine); machine->setInitialState(launched); //### Add a nice animation when the bomb is destroyed - launched->addTransition(this, &Bomb::bombExploded,final); + launched->addTransition(this, &Bomb::bombExploded, finalState); //If the animation is finished, then we move to the final state - launched->addTransition(launched, &QAnimationState::animationFinished, final); + launched->addTransition(launched, &QAnimationState::animationFinished, finalState); //The machine has finished to be executed, then the boat is dead - connect(machine,&QState::finished,this, &Bomb::bombExecutionFinished); + connect(machine,&QState::finished, this, &Bomb::bombExecutionFinished); machine->start(); diff --git a/examples/widgets/animation/sub-attaq/bomb.h b/examples/widgets/animation/sub-attaq/bomb.h index 8e893f81e3..9ae54b4d81 100644 --- a/examples/widgets/animation/sub-attaq/bomb.h +++ b/examples/widgets/animation/sub-attaq/bomb.h @@ -48,8 +48,8 @@ ** ****************************************************************************/ -#ifndef __BOMB__H__ -#define __BOMB__H__ +#ifndef BOMB_H +#define BOMB_H #include "pixmapitem.h" @@ -73,4 +73,4 @@ private slots: void onAnimationLaunchValueChanged(const QVariant &); }; -#endif //__BOMB__H__ +#endif // BOMB_H diff --git a/examples/widgets/animation/sub-attaq/graphicsscene.cpp b/examples/widgets/animation/sub-attaq/graphicsscene.cpp index 8f0dfc1357..c7e2d269c8 100644 --- a/examples/widgets/animation/sub-attaq/graphicsscene.cpp +++ b/examples/widgets/animation/sub-attaq/graphicsscene.cpp @@ -55,38 +55,33 @@ #include "submarine.h" #include "torpedo.h" #include "bomb.h" -#include "pixmapitem.h" #include "animationmanager.h" #include "qanimationstate.h" #include "progressitem.h" #include "textinformationitem.h" //Qt -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -GraphicsScene::GraphicsScene(int x, int y, int width, int height, Mode mode) - : QGraphicsScene(x , y, width, height), mode(mode), boat(new Boat) +#include +#include +#include +#include +#include +#include +#include +#include +#include + +GraphicsScene::GraphicsScene(int x, int y, int width, int height, Mode mode, QObject *parent) + : QGraphicsScene(x, y, width, height, parent), mode(mode), boat(new Boat) { - PixmapItem *backgroundItem = new PixmapItem(QString("background"),mode); + PixmapItem *backgroundItem = new PixmapItem(QStringLiteral("background"), mode); backgroundItem->setZValue(1); backgroundItem->setPos(0,0); addItem(backgroundItem); - PixmapItem *surfaceItem = new PixmapItem(QString("surface"),mode); + PixmapItem *surfaceItem = new PixmapItem(QStringLiteral("surface"), mode); surfaceItem->setZValue(3); - surfaceItem->setPos(0,sealLevel() - surfaceItem->boundingRect().height()/2); + surfaceItem->setPos(0, sealLevel() - surfaceItem->boundingRect().height() / 2); addItem(surfaceItem); //The item that display score and level @@ -137,8 +132,8 @@ qreal GraphicsScene::sealLevel() const void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction) { - static const int nLetters = 10; - static struct { + static constexpr int nLetters = 10; + static constexpr struct { char const *pix; qreal initX, initY; qreal destX, destY; @@ -154,8 +149,8 @@ void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction) {"q", 200, 2000, 510, 250 }, {"excl", 0, 2000, 570, 220 } }; - QSequentialAnimationGroup * lettersGroupMoving = new QSequentialAnimationGroup(this); - QParallelAnimationGroup * lettersGroupFading = new QParallelAnimationGroup(this); + QSequentialAnimationGroup *lettersGroupMoving = new QSequentialAnimationGroup(this); + QParallelAnimationGroup *lettersGroupFading = new QParallelAnimationGroup(this); for (int i = 0; i < nLetters; ++i) { PixmapItem *logo = new PixmapItem(QLatin1String(":/logo-") + logoData[i].pix, this); @@ -180,7 +175,7 @@ void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction) PlayState *gameState = new PlayState(this, machine); //Final state - QFinalState *final = new QFinalState(machine); + QFinalState *finalState = new QFinalState(machine); //Animation when the player enter in the game QAnimationState *lettersMovingState = new QAnimationState(machine); @@ -198,8 +193,8 @@ void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction) gameState->addTransition(newAction, &QAction::triggered, gameState); //Wanna quit, then connect to CTRL+Q - gameState->addTransition(quitAction, &QAction::triggered, final); - lettersMovingState->addTransition(quitAction, &QAction::triggered, final); + gameState->addTransition(quitAction, &QAction::triggered, finalState); + lettersMovingState->addTransition(quitAction, &QAction::triggered, finalState); //Welcome screen is the initial state machine->setInitialState(lettersMovingState); @@ -213,21 +208,24 @@ void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction) void GraphicsScene::addItem(Bomb *bomb) { bombs.insert(bomb); - connect(bomb,&Bomb::bombExecutionFinished,this, &GraphicsScene::onBombExecutionFinished); + connect(bomb, &Bomb::bombExecutionFinished, + this, &GraphicsScene::onBombExecutionFinished); QGraphicsScene::addItem(bomb); } void GraphicsScene::addItem(Torpedo *torpedo) { torpedos.insert(torpedo); - connect(torpedo,&Torpedo::torpedoExecutionFinished,this, &GraphicsScene::onTorpedoExecutionFinished); + connect(torpedo, &Torpedo::torpedoExecutionFinished, + this, &GraphicsScene::onTorpedoExecutionFinished); QGraphicsScene::addItem(torpedo); } void GraphicsScene::addItem(SubMarine *submarine) { submarines.insert(submarine); - connect(submarine,&SubMarine::subMarineExecutionFinished,this, &GraphicsScene::onSubMarineExecutionFinished); + connect(submarine, &SubMarine::subMarineExecutionFinished, + this, &GraphicsScene::onSubMarineExecutionFinished); QGraphicsScene::addItem(submarine); } @@ -239,15 +237,18 @@ void GraphicsScene::addItem(QGraphicsItem *item) void GraphicsScene::onBombExecutionFinished() { Bomb *bomb = qobject_cast(sender()); + if (!bomb) + return; bombs.remove(bomb); bomb->deleteLater(); - if (boat) - boat->setBombsLaunched(boat->bombsLaunched() - 1); + boat->setBombsLaunched(boat->bombsLaunched() - 1); } void GraphicsScene::onTorpedoExecutionFinished() { Torpedo *torpedo = qobject_cast(sender()); + if (!torpedo) + return; torpedos.remove(torpedo); torpedo->deleteLater(); } @@ -255,6 +256,8 @@ void GraphicsScene::onTorpedoExecutionFinished() void GraphicsScene::onSubMarineExecutionFinished() { SubMarine *submarine = qobject_cast(sender()); + if (!submarine) + return; submarines.remove(submarine); if (submarines.count() == 0) emit allSubMarineDestroyed(submarine->points()); @@ -266,16 +269,22 @@ void GraphicsScene::onSubMarineExecutionFinished() void GraphicsScene::clearScene() { for (SubMarine *sub : qAsConst(submarines)) { + // make sure to not go into onSubMarineExecutionFinished + sub->disconnect(this); sub->destroy(); sub->deleteLater(); } for (Torpedo *torpedo : qAsConst(torpedos)) { + // make sure to not go into onTorpedoExecutionFinished + torpedo->disconnect(this); torpedo->destroy(); torpedo->deleteLater(); } for (Bomb *bomb : qAsConst(bombs)) { + // make sure to not go into onBombExecutionFinished + bomb->disconnect(this); bomb->destroy(); bomb->deleteLater(); } diff --git a/examples/widgets/animation/sub-attaq/graphicsscene.h b/examples/widgets/animation/sub-attaq/graphicsscene.h index 86c3414bbb..dd3719bc10 100644 --- a/examples/widgets/animation/sub-attaq/graphicsscene.h +++ b/examples/widgets/animation/sub-attaq/graphicsscene.h @@ -48,13 +48,12 @@ ** ****************************************************************************/ -#ifndef __GRAPHICSSCENE__H__ -#define __GRAPHICSSCENE__H__ +#ifndef GRAPHICSSCENE_H +#define GRAPHICSSCENE_H //Qt -#include -#include -#include +#include +#include class Boat; @@ -78,18 +77,18 @@ public: }; struct SubmarineDescription { - int type; - int points; + int type = 0; + int points = 0; QString name; }; struct LevelDescription { - int id; + int id = 0; QString name; - QList > submarines; + QVector> submarines; }; - GraphicsScene(int x, int y, int width, int height, Mode mode = Big); + GraphicsScene(int x, int y, int width, int height, Mode mode, QObject *parent = nullptr); qreal sealLevel() const; void setupScene(QAction *newAction, QAction *quitAction); void addItem(Bomb *bomb); @@ -127,5 +126,5 @@ private: friend class UpdateScoreTransition; }; -#endif //__GRAPHICSSCENE__H__ +#endif // GRAPHICSSCENE_H diff --git a/examples/widgets/animation/sub-attaq/mainwindow.cpp b/examples/widgets/animation/sub-attaq/mainwindow.cpp index a4bb15b383..8f545ecebd 100644 --- a/examples/widgets/animation/sub-attaq/mainwindow.cpp +++ b/examples/widgets/animation/sub-attaq/mainwindow.cpp @@ -56,11 +56,10 @@ #include #include #include -#include #include #ifndef QT_NO_OPENGL -# include +# include #endif MainWindow::MainWindow(QWidget *parent) @@ -74,10 +73,10 @@ MainWindow::MainWindow(QWidget *parent) quitAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q)); if (QApplication::arguments().contains("-fullscreen")) { - scene = new GraphicsScene(0, 0, 750, 400, GraphicsScene::Small); + scene = new GraphicsScene(0, 0, 750, 400, GraphicsScene::Small, this); setWindowState(Qt::WindowFullScreen); } else { - scene = new GraphicsScene(0, 0, 880, 630); + scene = new GraphicsScene(0, 0, 880, 630, GraphicsScene::Big, this); layout()->setSizeConstraint(QLayout::SetFixedSize); } diff --git a/examples/widgets/animation/sub-attaq/mainwindow.h b/examples/widgets/animation/sub-attaq/mainwindow.h index c4fb9d324d..660acfaa0a 100644 --- a/examples/widgets/animation/sub-attaq/mainwindow.h +++ b/examples/widgets/animation/sub-attaq/mainwindow.h @@ -48,11 +48,11 @@ ** ****************************************************************************/ -#ifndef __MAINWINDOW__H__ -#define __MAINWINDOW__H__ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H //Qt -#include +#include class GraphicsScene; QT_BEGIN_NAMESPACE class QGraphicsView; @@ -69,4 +69,4 @@ private: QGraphicsView *view; }; -#endif //__MAINWINDOW__H__ +#endif // MAINWINDOW_H diff --git a/examples/widgets/animation/sub-attaq/pixmapitem.cpp b/examples/widgets/animation/sub-attaq/pixmapitem.cpp index 9475d5c3f8..a8581d881a 100644 --- a/examples/widgets/animation/sub-attaq/pixmapitem.cpp +++ b/examples/widgets/animation/sub-attaq/pixmapitem.cpp @@ -54,7 +54,7 @@ //Qt #include -PixmapItem::PixmapItem(const QString &fileName,GraphicsScene::Mode mode, QGraphicsItem * parent) +PixmapItem::PixmapItem(const QString &fileName, GraphicsScene::Mode mode, QGraphicsItem *parent) : QGraphicsObject(parent) { if (mode == GraphicsScene::Big) @@ -63,7 +63,8 @@ PixmapItem::PixmapItem(const QString &fileName,GraphicsScene::Mode mode, QGraphi pix = QPixmap(QStringLiteral(":/small/") + fileName); } -PixmapItem::PixmapItem(const QString &fileName, QGraphicsScene *scene) : QGraphicsObject(), pix(fileName) +PixmapItem::PixmapItem(const QString &fileName, QGraphicsScene *scene) + : QGraphicsObject(), pix(fileName) { scene->addItem(this); } diff --git a/examples/widgets/animation/sub-attaq/pixmapitem.h b/examples/widgets/animation/sub-attaq/pixmapitem.h index ec5c01857f..45e2ca806f 100644 --- a/examples/widgets/animation/sub-attaq/pixmapitem.h +++ b/examples/widgets/animation/sub-attaq/pixmapitem.h @@ -48,14 +48,14 @@ ** ****************************************************************************/ -#ifndef __PIXMAPITEM__H__ -#define __PIXMAPITEM__H__ +#ifndef PIXMAPITEM_H +#define PIXMAPITEM_H //Own #include "graphicsscene.h" //Qt -#include +#include class PixmapItem : public QGraphicsObject { @@ -69,4 +69,4 @@ private: QPixmap pix; }; -#endif //__PIXMAPITEM__H__ +#endif // PIXMAPITEM_H diff --git a/examples/widgets/animation/sub-attaq/progressitem.cpp b/examples/widgets/animation/sub-attaq/progressitem.cpp index 8b6b367710..350dbb7bbd 100644 --- a/examples/widgets/animation/sub-attaq/progressitem.cpp +++ b/examples/widgets/animation/sub-attaq/progressitem.cpp @@ -49,10 +49,11 @@ ****************************************************************************/ #include "progressitem.h" -#include "pixmapitem.h" -ProgressItem::ProgressItem (QGraphicsItem * parent) - : QGraphicsTextItem(parent), currentLevel(1), currentScore(0) +#include + +ProgressItem::ProgressItem(QGraphicsItem *parent) + : QGraphicsTextItem(parent) { setFont(QFont("Comic Sans MS")); setPos(parentItem()->boundingRect().topRight() - QPointF(180, -5)); diff --git a/examples/widgets/animation/sub-attaq/progressitem.h b/examples/widgets/animation/sub-attaq/progressitem.h index 23f5407978..f76b168151 100644 --- a/examples/widgets/animation/sub-attaq/progressitem.h +++ b/examples/widgets/animation/sub-attaq/progressitem.h @@ -52,19 +52,19 @@ #define PROGRESSITEM_H //Qt -#include +#include class ProgressItem : public QGraphicsTextItem { public: - ProgressItem(QGraphicsItem * parent = 0); + ProgressItem(QGraphicsItem *parent = nullptr); void setLevel(int level); void setScore(int score); private: void updateProgress(); - int currentLevel; - int currentScore; + int currentLevel = 1; + int currentScore = 0; }; #endif // PROGRESSITEM_H diff --git a/examples/widgets/animation/sub-attaq/qanimationstate.cpp b/examples/widgets/animation/sub-attaq/qanimationstate.cpp index ce99f9080d..6da085561b 100644 --- a/examples/widgets/animation/sub-attaq/qanimationstate.cpp +++ b/examples/widgets/animation/sub-attaq/qanimationstate.cpp @@ -50,7 +50,7 @@ #include "qanimationstate.h" -#include +#include QT_BEGIN_NAMESPACE @@ -106,7 +106,7 @@ void QAnimationState::setAnimation(QAbstractAnimation *animation) return; //Disconnect from the previous animation if exist - if(m_animation) + if (m_animation) disconnect(m_animation, &QAbstractAnimation::finished, this, &QAnimationState::animationFinished); m_animation = animation; diff --git a/examples/widgets/animation/sub-attaq/qanimationstate.h b/examples/widgets/animation/sub-attaq/qanimationstate.h index 063b119058..24759851ba 100644 --- a/examples/widgets/animation/sub-attaq/qanimationstate.h +++ b/examples/widgets/animation/sub-attaq/qanimationstate.h @@ -51,13 +51,7 @@ #ifndef QANIMATIONSTATE_H #define QANIMATIONSTATE_H -#ifndef QT_STATEMACHINE_SOLUTION -# include -# include -#else -# include "qstate.h" -# include "qabstractanimation.h" -#endif +#include QT_BEGIN_NAMESPACE @@ -67,7 +61,7 @@ class QAnimationState : public QState { Q_OBJECT public: - QAnimationState(QState *parent = 0); + QAnimationState(QState *parent = nullptr); ~QAnimationState(); void setAnimation(QAbstractAnimation *animation); diff --git a/examples/widgets/animation/sub-attaq/states.cpp b/examples/widgets/animation/sub-attaq/states.cpp index cda10ccdaf..c7e2738aad 100644 --- a/examples/widgets/animation/sub-attaq/states.cpp +++ b/examples/widgets/animation/sub-attaq/states.cpp @@ -59,12 +59,12 @@ #include "textinformationitem.h" //Qt -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include PlayState::PlayState(GraphicsScene *scene, QState *parent) : QState(parent), scene(scene), machine(nullptr), @@ -146,7 +146,7 @@ void PlayState::onEntry(QEvent *) machine->setInitialState(levelState); //Final state - QFinalState *final = new QFinalState(machine); + QFinalState *finalState = new QFinalState(machine); //This transition is triggered when the player press space after completing a level CustomSpaceTransition *spaceTransition = new CustomSpaceTransition(scene->views().at(0), this, QEvent::KeyPress, Qt::Key_Space); @@ -154,7 +154,7 @@ void PlayState::onEntry(QEvent *) winState->addTransition(spaceTransition); //We lost we should reach the final state - lostState->addTransition(lostState, &QState::finished, final); + lostState->addTransition(lostState, &QState::finished, finalState); machine->start(); } @@ -181,11 +181,9 @@ void LevelState::initializeLevel() scene->progressItem->setScore(game->score); scene->progressItem->setLevel(game->currentLevel + 1); - GraphicsScene::LevelDescription currentLevelDescription = scene->levelsData.value(game->currentLevel); + const GraphicsScene::LevelDescription currentLevelDescription = scene->levelsData.value(game->currentLevel); + for (const QPair &subContent : currentLevelDescription.submarines) { - for (int i = 0; i < currentLevelDescription.submarines.size(); ++i ) { - - QPair subContent = currentLevelDescription.submarines.at(i); GraphicsScene::SubmarineDescription submarineDesc = scene->submarinesData.at(subContent.first); for (int j = 0; j < subContent.second; ++j ) { @@ -202,9 +200,10 @@ void LevelState::initializeLevel() } /** Pause State */ -PauseState::PauseState(GraphicsScene *scene, QState *parent) : QState(parent),scene(scene) +PauseState::PauseState(GraphicsScene *scene, QState *parent) : QState(parent), scene(scene) { } + void PauseState::onEntry(QEvent *) { AnimationManager::self()->pauseAll(); @@ -324,8 +323,7 @@ bool WinTransition::eventTest(QEvent *event) /** Space transition */ CustomSpaceTransition::CustomSpaceTransition(QWidget *widget, PlayState *game, QEvent::Type type, int key) - : QKeyEventTransition(widget, type, key), - game(game) + : QKeyEventTransition(widget, type, key), game(game) { } diff --git a/examples/widgets/animation/sub-attaq/states.h b/examples/widgets/animation/sub-attaq/states.h index cd68e319c2..b3651e1c82 100644 --- a/examples/widgets/animation/sub-attaq/states.h +++ b/examples/widgets/animation/sub-attaq/states.h @@ -52,15 +52,11 @@ #define STATES_H //Qt -#include -#include -#include -#include -#include +#include +#include +#include class GraphicsScene; -class Boat; -class SubMarine; QT_BEGIN_NAMESPACE class QStateMachine; QT_END_NAMESPACE @@ -68,7 +64,7 @@ QT_END_NAMESPACE class PlayState : public QState { public: - explicit PlayState(GraphicsScene *scene, QState *parent = 0); + explicit PlayState(GraphicsScene *scene, QState *parent = nullptr); ~PlayState(); protected: @@ -92,7 +88,7 @@ private : class LevelState : public QState { public: - LevelState(GraphicsScene *scene, PlayState *game, QState *parent = 0); + LevelState(GraphicsScene *scene, PlayState *game, QState *parent = nullptr); protected: void onEntry(QEvent *) override; private : @@ -104,7 +100,7 @@ private : class PauseState : public QState { public: - explicit PauseState(GraphicsScene *scene, QState *parent = 0); + explicit PauseState(GraphicsScene *scene, QState *parent = nullptr); protected: void onEntry(QEvent *) override; @@ -116,7 +112,7 @@ private : class LostState : public QState { public: - LostState(GraphicsScene *scene, PlayState *game, QState *parent = 0); + LostState(GraphicsScene *scene, PlayState *game, QState *parent = nullptr); protected: void onEntry(QEvent *) override; @@ -129,7 +125,7 @@ private : class WinState : public QState { public: - WinState(GraphicsScene *scene, PlayState *game, QState *parent = 0); + WinState(GraphicsScene *scene, PlayState *game, QState *parent = nullptr); protected: void onEntry(QEvent *) override; @@ -154,7 +150,7 @@ public: protected: bool eventTest(QEvent *event) override; private: - PlayState * game; + PlayState *game; GraphicsScene *scene; }; @@ -166,7 +162,7 @@ public: protected: bool eventTest(QEvent *event) override; private: - PlayState * game; + PlayState *game; GraphicsScene *scene; }; diff --git a/examples/widgets/animation/sub-attaq/submarine.cpp b/examples/widgets/animation/sub-attaq/submarine.cpp index 775e75ceed..a4ca376045 100644 --- a/examples/widgets/animation/sub-attaq/submarine.cpp +++ b/examples/widgets/animation/sub-attaq/submarine.cpp @@ -52,15 +52,14 @@ #include "submarine.h" #include "submarine_p.h" #include "torpedo.h" -#include "pixmapitem.h" #include "graphicsscene.h" #include "animationmanager.h" #include "qanimationstate.h" -#include -#include -#include -#include +#include +#include +#include +#include static QAbstractAnimation *setupDestroyAnimation(SubMarine *sub) { @@ -86,9 +85,8 @@ SubMarine::SubMarine(int type, const QString &name, int points) : PixmapItem(QSt graphicsRotation = new QGraphicsRotation(this); graphicsRotation->setAxis(Qt::YAxis); - graphicsRotation->setOrigin(QVector3D(size().width()/2, size().height()/2, 0)); - QList r; - r.append(graphicsRotation); + graphicsRotation->setOrigin(QVector3D(size().width() / 2, size().height() / 2, 0)); + QList r({graphicsRotation}); setTransformations(r); //We setup the state machine of the submarine @@ -112,7 +110,7 @@ SubMarine::SubMarine(int type, const QString &name, int points) : PixmapItem(QSt machine->setInitialState(moving); //End - QFinalState *final = new QFinalState(machine); + QFinalState *finalState = new QFinalState(machine); //If the moving animation is finished we move to the return state movement->addTransition(movement, &QAnimationState::animationFinished, rotation); @@ -128,7 +126,7 @@ SubMarine::SubMarine(int type, const QString &name, int points) : PixmapItem(QSt moving->addTransition(this, &SubMarine::subMarineDestroyed, destroyedState); //Transition to final state when the destroyed animation is finished - destroyedState->addTransition(destroyedState, &QAnimationState::animationFinished, final); + destroyedState->addTransition(destroyedState, &QAnimationState::animationFinished, finalState); //The machine has finished to be executed, then the submarine is dead connect(machine,&QState::finished,this, &SubMarine::subMarineExecutionFinished); @@ -145,9 +143,8 @@ void SubMarine::setCurrentDirection(SubMarine::Movement direction) { if (this->direction == direction) return; - if (direction == SubMarine::Right && this->direction == SubMarine::None) { + if (direction == SubMarine::Right && this->direction == SubMarine::None) graphicsRotation->setAngle(180); - } this->direction = direction; } @@ -158,9 +155,8 @@ enum SubMarine::Movement SubMarine::currentDirection() const void SubMarine::setCurrentSpeed(int speed) { - if (speed < 0 || speed > 3) { + if (speed < 0 || speed > 3) qWarning("SubMarine::setCurrentSpeed : The speed is invalid"); - } this->speed = speed; emit subMarineStateChanged(); } @@ -172,7 +168,7 @@ int SubMarine::currentSpeed() const void SubMarine::launchTorpedo(int speed) { - Torpedo * torp = new Torpedo(); + Torpedo *torp = new Torpedo; GraphicsScene *scene = static_cast(this->scene()); scene->addItem(torp); torp->setPos(pos()); diff --git a/examples/widgets/animation/sub-attaq/submarine.h b/examples/widgets/animation/sub-attaq/submarine.h index d145c9cbee..256683ec70 100644 --- a/examples/widgets/animation/sub-attaq/submarine.h +++ b/examples/widgets/animation/sub-attaq/submarine.h @@ -48,15 +48,12 @@ ** ****************************************************************************/ -#ifndef __SUBMARINE__H__ -#define __SUBMARINE__H__ - -//Qt -#include +#ifndef SUBMARINE_H +#define SUBMARINE_H #include "pixmapitem.h" -class Torpedo; +#include class SubMarine : public PixmapItem { @@ -99,4 +96,4 @@ private: QGraphicsRotation *graphicsRotation; }; -#endif //__SUBMARINE__H__ +#endif // SUBMARINE_H diff --git a/examples/widgets/animation/sub-attaq/submarine_p.h b/examples/widgets/animation/sub-attaq/submarine_p.h index 1c2cb7ceac..36807dade3 100644 --- a/examples/widgets/animation/sub-attaq/submarine_p.h +++ b/examples/widgets/animation/sub-attaq/submarine_p.h @@ -68,16 +68,15 @@ #include "qanimationstate.h" //Qt -#include -#include -#include +#include +#include //This state is describing when the boat is moving right class MovementState : public QAnimationState { Q_OBJECT public: - explicit MovementState(SubMarine *submarine, QState *parent = 0) : QAnimationState(parent) + explicit MovementState(SubMarine *submarine, QState *parent = nullptr) : QAnimationState(parent) { movementAnimation = new QPropertyAnimation(submarine, "pos"); connect(movementAnimation, &QPropertyAnimation::valueChanged, @@ -117,7 +116,7 @@ private: class ReturnState : public QAnimationState { public: - explicit ReturnState(SubMarine *submarine, QState *parent = 0) : QAnimationState(parent) + explicit ReturnState(SubMarine *submarine, QState *parent = nullptr) : QAnimationState(parent) { returnAnimation = new QPropertyAnimation(submarine->rotation(), "angle"); returnAnimation->setDuration(500); diff --git a/examples/widgets/animation/sub-attaq/textinformationitem.cpp b/examples/widgets/animation/sub-attaq/textinformationitem.cpp index 16f787125d..4d4934f63d 100644 --- a/examples/widgets/animation/sub-attaq/textinformationitem.cpp +++ b/examples/widgets/animation/sub-attaq/textinformationitem.cpp @@ -50,14 +50,15 @@ #include "textinformationitem.h" #include "pixmapitem.h" -TextInformationItem::TextInformationItem (QGraphicsItem * parent) +TextInformationItem::TextInformationItem (QGraphicsItem *parent) : QGraphicsTextItem(parent) { setFont(QFont("Comic Sans MS", 15)); } -#include -void TextInformationItem::setMessage(const QString& text) + +void TextInformationItem::setMessage(const QString &text) { setHtml(text); - setPos(parentItem()->boundingRect().center().x() - boundingRect().size().width()/2 , parentItem()->boundingRect().center().y()); + setPos(parentItem()->boundingRect().center().x() - boundingRect().size().width() / 2, + parentItem()->boundingRect().center().y()); } diff --git a/examples/widgets/animation/sub-attaq/textinformationitem.h b/examples/widgets/animation/sub-attaq/textinformationitem.h index aa6f913e48..0a0b618460 100644 --- a/examples/widgets/animation/sub-attaq/textinformationitem.h +++ b/examples/widgets/animation/sub-attaq/textinformationitem.h @@ -52,13 +52,13 @@ #define TEXTINFORMATIONITEM_H //Qt -#include +#include class TextInformationItem : public QGraphicsTextItem { public: - TextInformationItem(QGraphicsItem * parent = 0); - void setMessage(const QString& text); + TextInformationItem(QGraphicsItem *parent = nullptr); + void setMessage(const QString &text); }; #endif // TEXTINFORMATIONITEM_H diff --git a/examples/widgets/animation/sub-attaq/torpedo.cpp b/examples/widgets/animation/sub-attaq/torpedo.cpp index 92a3833452..7395aa39ac 100644 --- a/examples/widgets/animation/sub-attaq/torpedo.cpp +++ b/examples/widgets/animation/sub-attaq/torpedo.cpp @@ -50,15 +50,14 @@ //Own #include "torpedo.h" -#include "pixmapitem.h" #include "boat.h" #include "graphicsscene.h" #include "animationmanager.h" #include "qanimationstate.h" -#include -#include -#include +#include +#include +#include Torpedo::Torpedo() : PixmapItem(QString::fromLatin1("torpedo"),GraphicsScene::Big), currentSpeed(0) @@ -70,11 +69,11 @@ void Torpedo::launch() { QPropertyAnimation *launchAnimation = new QPropertyAnimation(this, "pos"); AnimationManager::self()->registerAnimation(launchAnimation); - launchAnimation->setEndValue(QPointF(x(),qobject_cast(scene())->sealLevel() - 15)); + launchAnimation->setEndValue(QPointF(x(), qobject_cast(scene())->sealLevel() - 15)); launchAnimation->setEasingCurve(QEasingCurve::InQuad); - launchAnimation->setDuration(y()/currentSpeed*10); - connect(launchAnimation,&QVariantAnimation::valueChanged,this,&Torpedo::onAnimationLaunchValueChanged); - connect(this,&Torpedo::torpedoExploded, launchAnimation, &QAbstractAnimation::stop); + launchAnimation->setDuration(y() / currentSpeed * 10); + connect(launchAnimation, &QVariantAnimation::valueChanged, this, &Torpedo::onAnimationLaunchValueChanged); + connect(this, &Torpedo::torpedoExploded, launchAnimation, &QAbstractAnimation::stop); //We setup the state machine of the torpedo QStateMachine *machine = new QStateMachine(this); @@ -84,18 +83,18 @@ void Torpedo::launch() launched->setAnimation(launchAnimation); //End - QFinalState *final = new QFinalState(machine); + QFinalState *finalState = new QFinalState(machine); machine->setInitialState(launched); //### Add a nice animation when the torpedo is destroyed - launched->addTransition(this, &Torpedo::torpedoExploded,final); + launched->addTransition(this, &Torpedo::torpedoExploded, finalState); //If the animation is finished, then we move to the final state - launched->addTransition(launched, &QAnimationState::animationFinished, final); + launched->addTransition(launched, &QAnimationState::animationFinished, finalState); //The machine has finished to be executed, then the boat is dead - connect(machine,&QState::finished,this, &Torpedo::torpedoExecutionFinished); + connect(machine, &QState::finished, this, &Torpedo::torpedoExecutionFinished); machine->start(); } diff --git a/examples/widgets/animation/sub-attaq/torpedo.h b/examples/widgets/animation/sub-attaq/torpedo.h index bd04bd79aa..7ac853d4e9 100644 --- a/examples/widgets/animation/sub-attaq/torpedo.h +++ b/examples/widgets/animation/sub-attaq/torpedo.h @@ -48,8 +48,8 @@ ** ****************************************************************************/ -#ifndef __TORPEDO__H__ -#define __TORPEDO__H__ +#ifndef TORPEDO_H +#define TORPEDO_H #include "pixmapitem.h" @@ -73,4 +73,4 @@ private: int currentSpeed; }; -#endif //__TORPEDO__H__ +#endif // TORPEDO_H -- cgit v1.2.3