savegame ex.: revamp the way print() works

Basically, instead of re-creating QTextStreams all the time, create it
once, in main(), and then pass it to print() alongside the int
indentation.

Also fix a hard-coded indentation value that should have been relative
to the caller's indentation level.

Pick-to: 6.5 6.2
Task-number: QTBUG-108857
Change-Id: I811447295c9c3fdef23f61aff31ebe82941eb3b4
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2023-02-23 11:58:51 +01:00
parent 7b7a01c266
commit 6f1e53943d
8 changed files with 29 additions and 20 deletions

View File

@ -77,12 +77,12 @@ QJsonObject Character::toJson() const
}
//! [toJson]
void Character::print(int indentation) const
void Character::print(QTextStream &s, int indentation) const
{
const QString indent(indentation * 2, ' ');
QTextStream(stdout) << indent << "Name:\t" << mName << "\n";
QTextStream(stdout) << indent << "Level:\t" << mLevel << "\n";
const QString className = QMetaEnum::fromType<ClassType>().valueToKey(mClassType);
QString className = QMetaEnum::fromType<ClassType>().valueToKey(mClassType);
QTextStream(stdout) << indent << "Class:\t" << className << "\n";
s << indent << "Name:\t" << mName << "\n"
<< indent << "Level:\t" << mLevel << "\n"
<< indent << "Class:\t" << className << "\n";
}

View File

@ -8,6 +8,8 @@
#include <QObject>
#include <QString>
QT_FORWARD_DECLARE_CLASS(QTextStream)
//! [0]
class Character
{
@ -34,7 +36,7 @@ public:
static Character fromJson(const QJsonObject &json);
QJsonObject toJson() const;
void print(int indentation = 0) const;
void print(QTextStream &s, int indentation = 0) const;
private:
QString mName;
int mLevel = 0;

View File

@ -35,7 +35,9 @@
subclasses, for example), other patterns may be more suitable. See the
\l{xml/dombookmarks} and \l{xml/streambookmarks} examples for XML, and the
implementation of \l QListWidgetItem::read() and \l QListWidgetItem::write()
for idiomatic QDataStream serialization.
for idiomatic QDataStream serialization. The \c{print()} functions in this example
are good examples of QTextStream serialization, even though they, of course, lack
the deserialization side.
\snippet serialization/savegame/character.h 0

View File

@ -140,13 +140,13 @@ QJsonObject Game::toJson() const
}
//! [toJson]
void Game::print(int indentation) const
void Game::print(QTextStream &s, int indentation) const
{
const QString indent(indentation * 2, ' ');
QTextStream(stdout) << indent << "Player\n";
mPlayer.print(indentation + 1);
s << indent << "Player\n";
mPlayer.print(s, indentation + 1);
QTextStream(stdout) << indent << "Levels\n";
s << indent << "Levels\n";
for (const Level &level : mLevels)
level.print(indentation + 1);
level.print(s, indentation + 1);
}

View File

@ -10,6 +10,8 @@
#include <QJsonObject>
#include <QList>
QT_FORWARD_DECLARE_CLASS(QTextStream)
//! [0]
class Game
{
@ -28,7 +30,7 @@ public:
void read(const QJsonObject &json);
QJsonObject toJson() const;
void print(int indentation = 0) const;
void print(QTextStream &s, int indentation = 0) const;
private:
Character mPlayer;
QList<Level> mLevels;

View File

@ -57,12 +57,12 @@ QJsonObject Level::toJson() const
}
//! [toJson]
void Level::print(int indentation) const
void Level::print(QTextStream &s, int indentation) const
{
const QString indent(indentation * 2, ' ');
QTextStream(stdout) << indent << "Name:\t" << mName << "\n";
QTextStream(stdout) << indent << "NPCs:\n";
s << indent << "Name:\t" << mName << "\n"
<< indent << "NPCs:\n";
for (const Character &character : mNpcs)
character.print(2);
character.print(s, indentation + 1);
}

View File

@ -9,6 +9,8 @@
#include <QJsonObject>
#include <QList>
QT_FORWARD_DECLARE_CLASS(QTextStream)
//! [0]
class Level
{
@ -24,7 +26,7 @@ public:
static Level fromJson(const QJsonObject &json);
QJsonObject toJson() const;
void print(int indentation = 0) const;
void print(QTextStream &s, int indentation = 0) const;
private:
QString mName;
QList<Character> mNpcs;

View File

@ -29,8 +29,9 @@ int main(int argc, char *argv[])
// Game is played; changes are made...
//! [0]
//! [1]
QTextStream(stdout) << "Game ended in the following state:\n";
game.print();
QTextStream s(stdout);
s << "Game ended in the following state:\n";
game.print(s);
if (!game.saveGame(json ? Game::Json : Game::Binary))
return 1;