Skip to content

Commit

Permalink
Merge pull request mapeditor#2909 from bjorn/wip/qt6
Browse files Browse the repository at this point in the history
Porting Tiled to Qt 6
  • Loading branch information
bjorn authored Oct 13, 2020
2 parents 2c415b9 + 238186a commit e5f65a5
Show file tree
Hide file tree
Showing 113 changed files with 567 additions and 1,722 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ You can now run Tiled as follows:

qbs run -p tiled

Qt 6
-------------------------------------------------------------------------------

For compiling libtiledquick you'll need to install the Vulkan headers:

* On Unbuntu/Debian: `sudo apt install libvulkan-dev`

### Working with Visual Studio 2017

Once Qbs is set up (see previous instructions), it is possible to generate a
Expand Down
6 changes: 5 additions & 1 deletion src/libtiled/imagecache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ bool TilesheetParameters::operator==(const TilesheetParameters &other) const
transparentColor == other.transparentColor;
}

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
uint qHash(const TilesheetParameters &key, uint seed) Q_DECL_NOTHROW
#else
size_t qHash(const TilesheetParameters &key, size_t seed) Q_DECL_NOTHROW
#endif
{
uint h = ::qHash(key.fileName, seed);
auto h = ::qHash(key.fileName, seed);
h = ::qHash(key.tileWidth, h);
h = ::qHash(key.tileHeight, h);
h = ::qHash(key.margin, h);
Expand Down
4 changes: 4 additions & 0 deletions src/libtiled/imagecache.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ struct TILEDSHARED_EXPORT TilesheetParameters
bool operator==(const TilesheetParameters &other) const;
};

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
uint TILEDSHARED_EXPORT qHash(const TilesheetParameters &key, uint seed = 0) Q_DECL_NOTHROW;
#else
size_t TILEDSHARED_EXPORT qHash(const TilesheetParameters &key, size_t seed = 0) Q_DECL_NOTHROW;
#endif

struct LoadedImage
{
Expand Down
37 changes: 24 additions & 13 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

#include <memory>

#include "qtcompat_p.h"

using namespace Tiled;
using namespace Tiled::Internal;

Expand Down Expand Up @@ -506,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 @@ -619,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 @@ -820,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 @@ -834,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 @@ -883,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 @@ -1149,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 @@ -1244,12 +1246,21 @@ QPolygonF MapReaderPrivate::readPolygon()
break;
}

#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const qreal x = QStringView(point).left(commaPos).toDouble(&ok);
if (!ok)
break;
const qreal y = QStringView(point).mid(commaPos + 1).toDouble(&ok);
if (!ok)
break;
#else
const qreal x = point.leftRef(commaPos).toDouble(&ok);
if (!ok)
break;
const qreal y = point.midRef(commaPos + 1).toDouble(&ok);
if (!ok)
break;
#endif

polygon.append(QPointF(x, y));
}
Expand Down Expand Up @@ -1289,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 @@ -1305,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
4 changes: 4 additions & 0 deletions src/libtiled/qtcompat_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ void qAsConst(const T &&) Q_DECL_EQ_DELETE;
#endif

#endif

#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
using QStringRef = QStringView;
#endif
2 changes: 2 additions & 0 deletions src/libtiled/tilelayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@

#include <functional>

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
inline uint qHash(QPoint key, uint seed = 0) Q_DECL_NOTHROW
{
uint h1 = qHash(key.x(), seed);
uint h2 = qHash(key.y(), seed);
return ((h1 << 16) | (h1 >> 16)) ^ h2 ^ seed;
}
#endif

namespace Tiled {

Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ bool Tileset::loadImage()
}
}

mNextTileId = std::max(mNextTileId, tiles.size());
mNextTileId = std::max<int>(mNextTileId, tiles.size());

mImageReference.size = image.size();
mColumnCount = columnCountForWidth(mImageReference.size.width());
Expand Down
2 changes: 2 additions & 0 deletions src/libtiled/wangset.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <QString>
#include <QList>

#include "qtcompat_p.h"

namespace Tiled {

class TILEDSHARED_EXPORT WangId
Expand Down
10 changes: 10 additions & 0 deletions src/libtiled/worldmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,13 @@ QRect World::mapRect(const QString &fileName) const
for (const World::Pattern &pattern : patterns) {
QRegularExpressionMatch match = pattern.regexp.match(fileName);
if (match.hasMatch()) {
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const int x = match.capturedView(1).toInt();
const int y = match.capturedView(2).toInt();
#else
const int x = match.capturedRef(1).toInt();
const int y = match.capturedRef(2).toInt();
#endif

return QRect(QPoint(x * pattern.multiplierX,
y * pattern.multiplierY) + pattern.offset,
Expand All @@ -517,8 +522,13 @@ QVector<World::MapEntry> World::allMaps() const
for (const QString &fileName : entries) {
QRegularExpressionMatch match = pattern.regexp.match(fileName);
if (match.hasMatch()) {
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const int x = match.capturedView(1).toInt();
const int y = match.capturedView(2).toInt();
#else
const int x = match.capturedRef(1).toInt();
const int y = match.capturedRef(2).toInt();
#endif

MapEntry entry;
entry.fileName = dir.filePath(fileName);
Expand Down
36 changes: 23 additions & 13 deletions src/plugins/flare/flareplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#include <QDir>
#include <QFileInfo>
#include <QStringList>
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
#include <QStringView>
#endif
#include <QTextStream>

#include <memory>
Expand Down Expand Up @@ -78,6 +81,11 @@ std::unique_ptr<Tiled::Map> FlarePlugin::read(const QString &fileName)

while (!stream.atEnd()) {
line = stream.readLine();
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const QStringView lineView(line);
#else
const QStringRef lineView(&line);
#endif

if (!line.length())
continue;
Expand All @@ -94,8 +102,8 @@ std::unique_ptr<Tiled::Map> FlarePlugin::read(const QString &fileName)
//get map properties
int epos = line.indexOf(QChar('='));
if (epos != -1) {
const QStringRef key = line.leftRef(epos).trimmed();
const QStringRef value = line.midRef(epos + 1, -1).trimmed();
const auto key = lineView.left(epos).trimmed();
const auto value = lineView.mid(epos + 1, -1).trimmed();
if (key == QLatin1String("width"))
map->setWidth(value.toInt());
else if (key == QLatin1String("height"))
Expand All @@ -107,7 +115,7 @@ std::unique_ptr<Tiled::Map> FlarePlugin::read(const QString &fileName)
else if (key == QLatin1String("orientation"))
map->setOrientation(orientationFromString(value.toString()));
else if (key == QLatin1String("background_color")){
QVector<QStringRef> rgbaList = value.split(',');
auto rgbaList = value.split(',');

if (!rgbaList.isEmpty())
backgroundColor.setRed(rgbaList.takeFirst().toInt());
Expand All @@ -126,10 +134,10 @@ std::unique_ptr<Tiled::Map> FlarePlugin::read(const QString &fileName)
} else if (sectionName == QLatin1String("tilesets")) {
tilesetsSectionFound = true;
int epos = line.indexOf(QChar('='));
const QStringRef key = line.leftRef(epos).trimmed();
const QStringRef value = line.midRef(epos + 1, -1).trimmed();
const auto key = lineView.left(epos).trimmed();
const auto value = lineView.mid(epos + 1, -1).trimmed();
if (key == QLatin1String("tileset")) {
const QVector<QStringRef> list = value.split(QChar(','));
const auto list = value.split(QChar(','));

QString absoluteSource(list.first().toString());
if (QDir::isRelativePath(absoluteSource))
Expand Down Expand Up @@ -171,8 +179,8 @@ std::unique_ptr<Tiled::Map> FlarePlugin::read(const QString &fileName)
tilelayerSectionFound = true;
int epos = line.indexOf(QChar('='));
if (epos != -1) {
const QStringRef key = line.leftRef(epos).trimmed();
const QStringRef value = line.midRef(epos + 1, -1).trimmed();
const auto key = lineView.left(epos).trimmed();
const auto value = lineView.mid(epos + 1, -1).trimmed();

if (key == QLatin1String("type")) {
tilelayer = new TileLayer(value.toString(), 0, 0,
Expand Down Expand Up @@ -220,18 +228,18 @@ std::unique_ptr<Tiled::Map> FlarePlugin::read(const QString &fileName)
continue;

if (startsWith == QChar('#')) {
QString name = line.midRef(1).trimmed().toString();
QString name = lineView.mid(1).trimmed().toString();
mapobject->setName(name);
}

int epos = line.indexOf(QChar('='));
if (epos != -1) {
const QStringRef key = line.leftRef(epos).trimmed();
const QStringRef value = line.midRef(epos + 1, -1).trimmed();
const auto key = lineView.left(epos).trimmed();
const auto value = lineView.mid(epos + 1, -1).trimmed();
if (key == QLatin1String("type")) {
mapobject->setType(value.toString());
} else if (key == QLatin1String("location")) {
const QVector<QStringRef> loc = value.split(QChar(','));
const auto loc = value.split(QChar(','));
qreal x,y;
qreal w,h;
if (map->orientation() == Map::Orthogonal) {
Expand Down Expand Up @@ -305,11 +313,13 @@ bool FlarePlugin::write(const Tiled::Map *map, const QString &fileName, Options
}

QTextStream out(file.device());
QColor backgroundColor = map->backgroundColor();
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
out.setCodec("UTF-8");
#endif

const int mapWidth = map->width();
const int mapHeight = map->height();
const QColor backgroundColor = map->backgroundColor();

// write [header]
out << "[header]\n";
Expand Down
Loading

0 comments on commit e5f65a5

Please sign in to comment.