Skip to content

Commit

Permalink
Qt 6: Avoid remaining usage of QStringRef
Browse files Browse the repository at this point in the history
Further removal of QStringRef would be possible if the minimum Qt
version would be raised to Qt 5.10, though even with 5.15 QtringView
does not entirely replace QStringRef unfortunately.
  • Loading branch information
bjorn committed Oct 12, 2020
1 parent cd09fbc commit 4469ecd
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 19 deletions.
26 changes: 13 additions & 13 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ void MapReaderPrivate::readTilesetTile(Tileset &tileset)
}

// Read tile probability
QStringRef probability = atts.value(QLatin1String("probability"));
const auto probability = atts.value(QLatin1String("probability"));
if (!probability.isEmpty())
tile->setProbability(probability.toDouble());

Expand Down Expand Up @@ -621,7 +621,7 @@ ImageReference MapReaderPrivate::readImage()
while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("data")) {
const QXmlStreamAttributes atts = xml.attributes();
QStringRef encoding = atts.value(QLatin1String("encoding"));
const auto encoding = atts.value(QLatin1String("encoding"));

image.data = xml.readElementText().toLatin1();
if (encoding == QLatin1String("base64"))
Expand Down Expand Up @@ -822,10 +822,10 @@ void MapReaderPrivate::readTilesetWangSets(Tileset &tileset)
static void readLayerAttributes(Layer &layer,
const QXmlStreamAttributes &atts)
{
const QStringRef idRef = atts.value(QLatin1String("id"));
const QStringRef opacityRef = atts.value(QLatin1String("opacity"));
const QStringRef visibleRef = atts.value(QLatin1String("visible"));
const QStringRef lockedRef = atts.value(QLatin1String("locked"));
const auto idRef = atts.value(QLatin1String("id"));
const auto opacityRef = atts.value(QLatin1String("opacity"));
const auto visibleRef = atts.value(QLatin1String("visible"));
const auto lockedRef = atts.value(QLatin1String("locked"));

bool ok;
const int id = idRef.toInt(&ok);
Expand All @@ -836,7 +836,7 @@ static void readLayerAttributes(Layer &layer,
if (ok)
layer.setOpacity(opacity);

const QStringRef tintColor = atts.value(QLatin1String("tintcolor"));
const auto tintColor = atts.value(QLatin1String("tintcolor"));
if (!tintColor.isEmpty())
layer.setTintColor(QColor(tintColor.toString()));

Expand Down Expand Up @@ -885,8 +885,8 @@ void MapReaderPrivate::readTileLayerData(TileLayer &tileLayer)
Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("data"));

const QXmlStreamAttributes atts = xml.attributes();
QStringRef encoding = atts.value(QLatin1String("encoding"));
QStringRef compression = atts.value(QLatin1String("compression"));
const auto encoding = atts.value(QLatin1String("encoding"));
const auto compression = atts.value(QLatin1String("compression"));

Map::LayerDataFormat layerDataFormat;
if (encoding.isEmpty()) {
Expand Down Expand Up @@ -1151,7 +1151,7 @@ std::unique_ptr<MapObject> MapReaderPrivate::readObject()
const qreal width = atts.value(QLatin1String("width")).toDouble();
const qreal height = atts.value(QLatin1String("height")).toDouble();
const QString type = atts.value(QLatin1String("type")).toString();
const QStringRef visibleRef = atts.value(QLatin1String("visible"));
const auto visibleRef = atts.value(QLatin1String("visible"));

const QPointF pos(x, y);
const QSizeF size(width, height);
Expand Down Expand Up @@ -1300,13 +1300,13 @@ TextData MapReaderPrivate::readObjectText()
textData.font.setStrikeOut(intAttribute(atts, "strikeout", 0) == 1);
textData.font.setKerning(intAttribute(atts, "kerning", 1) == 1);

QStringRef colorString = atts.value(QLatin1String("color"));
const auto colorString = atts.value(QLatin1String("color"));
if (!colorString.isEmpty())
textData.color = QColor(colorString.toString());

Qt::Alignment alignment;

QStringRef hAlignString = atts.value(QLatin1String("halign"));
const auto hAlignString = atts.value(QLatin1String("halign"));
if (hAlignString == QLatin1String("center"))
alignment |= Qt::AlignHCenter;
else if (hAlignString == QLatin1String("right"))
Expand All @@ -1316,7 +1316,7 @@ TextData MapReaderPrivate::readObjectText()
else
alignment |= Qt::AlignLeft;

QStringRef vAlignString = atts.value(QLatin1String("valign"));
const auto vAlignString = atts.value(QLatin1String("valign"));
if (vAlignString == QLatin1String("center"))
alignment |= Qt::AlignVCenter;
else if (vAlignString == QLatin1String("bottom"))
Expand Down
9 changes: 8 additions & 1 deletion src/plugins/tbin/tbinplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

#include <QCoreApplication>
#include <QDir>
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
#include <QStringView>
#endif

#include <cmath>
#include <fstream>
Expand Down Expand Up @@ -159,7 +162,11 @@ std::unique_ptr<Tiled::Map> TbinMapFormat::read(const QString &fileName)
continue;

const QString name = QString::fromStdString(prop.first);
const QVector<QStringRef> strs = name.splitRef('@');
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const auto strs = QStringView(name).split(QLatin1Char('@'));
#else
const auto strs = name.splitRef('@');
#endif
if (strs[1] == QLatin1String("TileIndex")) {
int index = strs[2].toInt();
tbin::Properties dummyProps;
Expand Down
11 changes: 11 additions & 0 deletions src/plugins/tengine/tengineplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include <QCoreApplication>
#include <QHash>
#include <QList>
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
#include <QStringView>
#endif
#include <QTextStream>

#include <QtMath>
Expand All @@ -54,9 +57,17 @@ bool TenginePlugin::write(const Tiled::Map *map, const QString &fileName, Option
}
QTextStream out(file.device());

#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
using Qt::endl;
#endif

// Write the header
const QString header = map->property("header").toString();
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const auto lines = QStringView(header).split(QStringLiteral("\\n"));
#else
const auto lines = header.splitRef("\\n");
#endif
for (const auto &line : lines)
out << line << endl;

Expand Down
9 changes: 7 additions & 2 deletions src/tiled/newversionchecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ static const char versionInfoUrl[] = "https://www.mapeditor.org/versions.json";

static bool versionLessThan(const QString &a, const QString &b)
{
const QVector<QStringRef> aParts = a.splitRef(QLatin1Char('.'));
const QVector<QStringRef> bParts = b.splitRef(QLatin1Char('.'));
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const auto aParts = QStringView(a).split(QLatin1Char('.'));
const auto bParts = QStringView(b).split(QLatin1Char('.'));
#else
const auto aParts = a.splitRef(QLatin1Char('.'));
const auto bParts = b.splitRef(QLatin1Char('.'));
#endif
const int commonLength = std::min(aParts.size(), bParts.size());

for (int i = 0; i < commonLength; ++i) {
Expand Down
6 changes: 5 additions & 1 deletion src/tiled/projectmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ static void findFiles(const FolderEntry &entry, int offset, const QStringList &w
{
for (const auto &childEntry : entry.entries) {
if (childEntry->entries.empty()) {
const QStringRef relativePath = childEntry->filePath.midRef(offset);
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const auto relativePath = QStringView(childEntry->filePath).mid(offset);
#else
const auto relativePath = childEntry->filePath.midRef(offset);
#endif
const int totalScore = Utils::matchingScore(words, relativePath);

if (totalScore > 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/tiledproxystyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,11 @@ void TiledProxyStyle::drawControl(ControlElement element,

QRect textRect(xpos, y + windowsItemVMargin, w - xm - windowsRightBorder - tab + 1, h - 2 * windowsItemVMargin);
QRect vTextRect = visualRect(opt->direction, menuitem->rect, textRect);
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
QStringView s(menuitem->text);
#else
QStringRef s(&menuitem->text);
#endif
if (!s.isEmpty()) { // draw text
p->save();
int t = s.indexOf(QLatin1Char('\t'));
Expand Down
4 changes: 2 additions & 2 deletions src/tiled/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ static bool matchingRanges(const QString &word, QStringRef string, int offset, R

int matchingScore(const QStringList &words, QStringRef string)
{
const QStringRef fileName = string.mid(string.lastIndexOf(QLatin1Char('/')) + 1);
const auto fileName = string.mid(string.lastIndexOf(QLatin1Char('/')) + 1);

int totalScore = 1; // no words matches everything

Expand All @@ -245,7 +245,7 @@ int matchingScore(const QStringList &words, QStringRef string)
RangeSet<int> matchingRanges(const QStringList &words, QStringRef string)
{
const int startOfFileName = string.lastIndexOf(QLatin1Char('/')) + 1;
const QStringRef fileName = string.mid(startOfFileName);
const auto fileName = string.mid(startOfFileName);

RangeSet<int> result;

Expand Down
2 changes: 2 additions & 0 deletions src/tiled/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include <memory>

#include "qtcompat_p.h"

class QAction;
class QKeyEvent;
class QMenu;
Expand Down

0 comments on commit 4469ecd

Please sign in to comment.