6f1e53943d
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>
69 lines
1.4 KiB
C++
69 lines
1.4 KiB
C++
// Copyright (C) 2016 The Qt Company Ltd.
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
#include "level.h"
|
|
|
|
#include <QJsonArray>
|
|
#include <QTextStream>
|
|
|
|
Level::Level(const QString &name) : mName(name)
|
|
{
|
|
}
|
|
|
|
QString Level::name() const
|
|
{
|
|
return mName;
|
|
}
|
|
|
|
QList<Character> Level::npcs() const
|
|
{
|
|
return mNpcs;
|
|
}
|
|
|
|
void Level::setNpcs(const QList<Character> &npcs)
|
|
{
|
|
mNpcs = npcs;
|
|
}
|
|
|
|
//! [fromJson]
|
|
Level Level::fromJson(const QJsonObject &json)
|
|
{
|
|
Level result;
|
|
|
|
if (const QJsonValue v = json["name"]; v.isString())
|
|
result.mName = v.toString();
|
|
|
|
if (const QJsonValue v = json["npcs"]; v.isArray()) {
|
|
const QJsonArray npcs = v.toArray();
|
|
result.mNpcs.reserve(npcs.size());
|
|
for (const QJsonValue &npc : npcs)
|
|
result.mNpcs.append(Character::fromJson(npc.toObject()));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
//! [fromJson]
|
|
|
|
//! [toJson]
|
|
QJsonObject Level::toJson() const
|
|
{
|
|
QJsonObject json;
|
|
json["name"] = mName;
|
|
QJsonArray npcArray;
|
|
for (const Character &npc : mNpcs)
|
|
npcArray.append(npc.toJson());
|
|
json["npcs"] = npcArray;
|
|
return json;
|
|
}
|
|
//! [toJson]
|
|
|
|
void Level::print(QTextStream &s, int indentation) const
|
|
{
|
|
const QString indent(indentation * 2, ' ');
|
|
|
|
s << indent << "Name:\t" << mName << "\n"
|
|
<< indent << "NPCs:\n";
|
|
for (const Character &character : mNpcs)
|
|
character.print(s, indentation + 1);
|
|
}
|