diff options
author | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2023-03-31 15:45:19 +0200 |
---|---|---|
committer | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2023-04-20 11:00:21 +0200 |
commit | af126cbe72a7ed631f56393862548e8fa713c26c (patch) | |
tree | 848bd2380bfaf558ab0fce2f5096280f2609d0de | |
parent | 5e5eacd20432c8939707a70e6c5bd9cebde76e72 (diff) |
rogue example modernization
Use 'auto' where appropriate.
Use constexprs instead of defines for WIDTH/HEIGHT.
Use inline class member initializations, update the documentation
accordingly.
Use QT_FORWARD_DECLARE_CLASS for Qt classes.
Use clang-format to reformat the code in some places.
Use std::hypot() for distance calculation.
Use per-class includes and sort them.
Pick-to: 6.5
Task-number: QTBUG-111448
Change-Id: I52d67fa63e53ce598200067bb283139e5bf9be4b
Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
-rw-r--r-- | examples/statemachine/doc/src/rogue.qdoc | 9 | ||||
-rw-r--r-- | examples/statemachine/rogue/main.cpp | 2 | ||||
-rw-r--r-- | examples/statemachine/rogue/movementtransition.h | 19 | ||||
-rw-r--r-- | examples/statemachine/rogue/window.cpp | 59 | ||||
-rw-r--r-- | examples/statemachine/rogue/window.h | 17 |
5 files changed, 52 insertions, 54 deletions
diff --git a/examples/statemachine/doc/src/rogue.qdoc b/examples/statemachine/doc/src/rogue.qdoc index 8c41104..870d066 100644 --- a/examples/statemachine/doc/src/rogue.qdoc +++ b/examples/statemachine/doc/src/rogue.qdoc @@ -87,8 +87,8 @@ The \c map is an array with the characters that are currently displayed. We set up the array in \c setupMap(), and update it when the rogue is moved. \c pX and \c pY is the current position - of the rogue. \c WIDTH and \c HEIGHT are macros specifying the - dimensions of the map. + of the rogue, initially set to (5, 5). \c WIDTH and \c HEIGHT are + constants specifying the dimensions of the map. The \c paintEvent() function is left out of this walkthrough. We also do not discuss other code that does not concern the state @@ -105,9 +105,8 @@ \dots \snippet rogue/window.cpp 1 - The player starts off at position (5, 5). We then set up the map - and statemachine. Let's proceed with the \c buildMachine() - function: + Here we set up the map and statemachine. Let's proceed with the + \c buildMachine() function: \snippet rogue/window.cpp 2 diff --git a/examples/statemachine/rogue/main.cpp b/examples/statemachine/rogue/main.cpp index 0720f50..a6e4012 100644 --- a/examples/statemachine/rogue/main.cpp +++ b/examples/statemachine/rogue/main.cpp @@ -1,7 +1,7 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QtWidgets> +#include <QtWidgets/QApplication> #include "window.h" diff --git a/examples/statemachine/rogue/movementtransition.h b/examples/statemachine/rogue/movementtransition.h index 9e1c257..62b82c1 100644 --- a/examples/statemachine/rogue/movementtransition.h +++ b/examples/statemachine/rogue/movementtransition.h @@ -4,8 +4,9 @@ #ifndef MOVEMENTTRANSITION_H #define MOVEMENTTRANSITION_H -#include <QtStateMachine> -#include <QtWidgets> +#include <QtGui/QKeyEvent> +#include <QtStateMachine/QEventTransition> +#include <QtStateMachine/QStateMachine> #include "window.h" @@ -15,9 +16,9 @@ class MovementTransition : public QEventTransition Q_OBJECT public: - MovementTransition(Window *window) : - QEventTransition(window, QEvent::KeyPress) { - this->window = window; + explicit MovementTransition(Window *window) + : QEventTransition(window, QEvent::KeyPress), window(window) + { } //![0] @@ -26,9 +27,9 @@ protected: bool eventTest(QEvent *event) override { if (event->type() == QEvent::StateMachineWrapped && static_cast<QStateMachine::WrappedEvent *>(event)->event()->type() == QEvent::KeyPress) { - QEvent *wrappedEvent = static_cast<QStateMachine::WrappedEvent *>(event)->event(); + auto wrappedEvent = static_cast<QStateMachine::WrappedEvent *>(event)->event(); - QKeyEvent *keyEvent = static_cast<QKeyEvent *>(wrappedEvent); + auto keyEvent = static_cast<QKeyEvent *>(wrappedEvent); int key = keyEvent->key(); return key == Qt::Key_2 || key == Qt::Key_8 || key == Qt::Key_6 || @@ -41,8 +42,8 @@ protected: //![2] void onTransition(QEvent *event) override { - QKeyEvent *keyEvent = static_cast<QKeyEvent *>( - static_cast<QStateMachine::WrappedEvent *>(event)->event()); + auto keyEvent = static_cast<QKeyEvent *>( + static_cast<QStateMachine::WrappedEvent *>(event)->event()); int key = keyEvent->key(); switch (key) { diff --git a/examples/statemachine/rogue/window.cpp b/examples/statemachine/rogue/window.cpp index 087830a..40761bd 100644 --- a/examples/statemachine/rogue/window.cpp +++ b/examples/statemachine/rogue/window.cpp @@ -1,16 +1,22 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QtWidgets> - #include "window.h" + +#include <QtCore/QRandomGenerator> +#include <QtGui/QFont> +#include <QtGui/QPainter> +#include <QtStateMachine/QFinalState> +#include <QtStateMachine/QKeyEventTransition> +#include <QtWidgets/QApplication> + +#include <cmath> + #include "movementtransition.h" //![0] Window::Window() { - pX = 5; - pY = 5; //![0] QFont font("Monospace"); font.setStyleHint(QFont::TypeWriter); @@ -64,22 +70,22 @@ void Window::paintEvent(QPaintEvent * /* event */) double x2 = static_cast<double>(x); double y2 = static_cast<double>(y); - if (x2<x1) { - x2+=0.5; - } else if (x2>x1) { - x2-=0.5; + if (x2 < x1) { + x2 += 0.5; + } else if (x2 > x1) { + x2 -= 0.5; } - if (y2<y1) { - y2+=0.5; - } else if (y2>y1) { - y2-=0.5; + if (y2 < y1) { + y2 += 0.5; + } else if (y2 > y1) { + y2 -= 0.5; } double dx = x2 - x1; double dy = y2 - y1; - double length = qSqrt(dx*dx+dy*dy); + double length = std::hypot(dx, dy); dx /= length; dy /= length; @@ -88,15 +94,14 @@ void Window::paintEvent(QPaintEvent * /* event */) double yi = y1; while (length > 0) { - int cx = static_cast<int>(xi+0.5); - int cy = static_cast<int>(yi+0.5); + int cx = static_cast<int>(xi + 0.5); + int cy = static_cast<int>(yi + 0.5); if (x2 == cx && y2 == cy) break; - if (!(x1==cx && y1==cy) - && (map[cx][cy] == '#' || (length-10) > 0)) { - painter.setPen(QColor(60,60,60)); + if (!(x1 == cx && y1 == cy) && (map[cx][cy] == '#' || (length - 10) > 0)) { + painter.setPen(QColor(60, 60, 60)); break; } @@ -125,31 +130,28 @@ void Window::buildMachine() { machine = new QStateMachine; - QState *inputState = new QState(machine); + auto inputState = new QState(machine); inputState->assignProperty(this, "status", "Move the rogue with 2, 4, 6, and 8"); - MovementTransition *transition = new MovementTransition(this); + auto transition = new MovementTransition(this); inputState->addTransition(transition); //![2] //![3] - QState *quitState = new QState(machine); + auto quitState = new QState(machine); quitState->assignProperty(this, "status", "Really quit(y/n)?"); - QKeyEventTransition *yesTransition = new - QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Y); + auto yesTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Y); yesTransition->setTargetState(new QFinalState(machine)); quitState->addTransition(yesTransition); - QKeyEventTransition *noTransition = - new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_N); + auto noTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_N); noTransition->setTargetState(inputState); quitState->addTransition(noTransition); //![3] //![4] - QKeyEventTransition *quitTransition = - new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Q); + auto quitTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Q); quitTransition->setTargetState(quitState); inputState->addTransition(quitTransition); //![4] @@ -157,8 +159,7 @@ void Window::buildMachine() //![5] machine->setInitialState(inputState); - connect(machine, &QStateMachine::finished, - qApp, &QApplication::quit); + connect(machine, &QStateMachine::finished, qApp, &QApplication::quit); machine->start(); } diff --git a/examples/statemachine/rogue/window.h b/examples/statemachine/rogue/window.h index faca764..2db57ff 100644 --- a/examples/statemachine/rogue/window.h +++ b/examples/statemachine/rogue/window.h @@ -4,16 +4,9 @@ #ifndef WINDOW_H #define WINDOW_H -#include <QWidget> +#include <QtWidgets/QWidget> -QT_BEGIN_NAMESPACE -class QState; -class QStateMachine; -class QTransition; -QT_END_NAMESPACE - -#define WIDTH 35 -#define HEIGHT 20 +QT_FORWARD_DECLARE_CLASS(QStateMachine); //![0] class Window : public QWidget @@ -41,8 +34,12 @@ private: void buildMachine(); void setupMap(); + static constexpr int WIDTH = 35; + static constexpr int HEIGHT = 20; + QChar map[WIDTH][HEIGHT]; - int pX, pY; + int pX = 5; + int pY = 5; QStateMachine *machine; QString myStatus; |