Skip to content

Commit

Permalink
Fixed remaining compilation issues with Qt 6
Browse files Browse the repository at this point in the history
* QSet::const_iterator no longer supports + operator
* Further adjusting to removal of QString::midRef / QStringRef
* QGuiApplication::setFallbackSessionManagementEnabled was removed
* Qt::AA_DisableWindowContextHelpButton was removed
* Shortcut modifiers no longer support + operator
* Re-introduced TextFile.codec based on QStringConverter, but supporting
  less encodings
* JSON binary data for tile stamps is no longer supported (but it appears
  Tiled never wrote these files in binary data, fortunately)
* QShortcut parent is now QObject* instead of QWidget*

Unfortunately, a linker issue remains when compiling with Qbs:

    /usr/bin/ld: cannot find -l-pthread
  • Loading branch information
bjorn committed Oct 13, 2020
1 parent 1d6df01 commit 4616513
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/tiled/editpolygontool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ void EditPolygonTool::showHandleContextMenu(QPoint screenPos)

bool canDeleteSegment = false;
if (n == 2) {
const PointHandle *secondHandle = *(mSelectedHandles.constBegin() + 1);
const PointHandle *secondHandle = *(++mSelectedHandles.constBegin());
const MapObject *secondMapObject = secondHandle->mapObject();

int indexDifference = std::abs(firstHandle->pointIndex() - secondHandle->pointIndex());
Expand Down Expand Up @@ -1005,8 +1005,8 @@ void EditPolygonTool::deleteSegment()
if (mSelectedHandles.size() != 2)
return;

const auto &firstHandle = *mSelectedHandles.begin();
const auto &secondHandle = *(mSelectedHandles.begin() + 1);
PointHandle *firstHandle = *mSelectedHandles.constBegin();
PointHandle *secondHandle = *(++mSelectedHandles.constBegin());

MapObject *mapObject = firstHandle->mapObject();

Expand Down
1 change: 1 addition & 0 deletions src/tiled/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <QPoint>
#include <QRegion>
#include <QTransform>
#include <QVector>

namespace Tiled {
Expand Down
4 changes: 2 additions & 2 deletions src/tiled/layermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ bool LayerModel::dropMimeData(const QMimeData *data, Qt::DropAction action,

GroupLayer *groupLayer = static_cast<GroupLayer*>(parentLayer);

QByteArray encodedData = data->data(QLatin1String(LAYERS_MIMETYPE));
QDataStream stream(&encodedData, QIODevice::ReadOnly);
const QByteArray encodedData = data->data(QLatin1String(LAYERS_MIMETYPE));
QDataStream stream(encodedData);
QList<Layer*> layers;

while (!stream.atEnd()) {
Expand Down
21 changes: 16 additions & 5 deletions src/tiled/locatorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ QVariant MatchesModel::data(const QModelIndex &index, int role) const
switch (role) {
case Qt::DisplayRole: {
const ProjectModel::Match &match = mMatches.at(index.row());
return match.path.mid(match.offset);
return match.relativePath().toString();
}
}
return QVariant();
Expand Down Expand Up @@ -146,7 +146,11 @@ void MatchDelegate::paint(QPainter *painter,

QString filePath = index.data().toString();
const int lastSlash = filePath.lastIndexOf(QLatin1Char('/'));
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
const auto ranges = Utils::matchingRanges(mWords, &filePath);
#else
const auto ranges = Utils::matchingRanges(mWords, filePath);
#endif

filePath = QDir::toNativeSeparators(filePath);

Expand Down Expand Up @@ -372,16 +376,23 @@ void LocatorWidget::setFilterText(const QString &text)
if (currentIndex.isValid())
previousSelected = mListModel->data(currentIndex).toString();

const QStringList words = QDir::fromNativeSeparators(text).split(QLatin1Char(' '),
QString::SkipEmptyParts);
const QString normalized = QDir::fromNativeSeparators(text);
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
const QStringList words = normalized.split(QLatin1Char(' '), QString::SkipEmptyParts);
#else
const QStringList words = normalized.split(QLatin1Char(' '), Qt::SkipEmptyParts);
#endif

auto projectModel = ProjectManager::instance()->projectModel();
auto matches = projectModel->findFiles(words);

std::stable_sort(matches.begin(), matches.end(), [] (const ProjectModel::Match &a, const ProjectModel::Match &b) {
// Sort based on score first
if (a.score != b.score)
return a.score > b.score;
return a.path.midRef(a.offset).compare(b.path.midRef(b.offset), Qt::CaseInsensitive) < 0;

// If score is the same, sort alphabetically
return a.relativePath().compare(b.relativePath(), Qt::CaseInsensitive) < 0;
});

mDelegate->setWords(words);
Expand All @@ -396,7 +407,7 @@ void LocatorWidget::setFilterText(const QString &text)

if (!previousSelected.isEmpty()) {
auto it = std::find_if(matches.cbegin(), matches.cend(), [&] (const ProjectModel::Match &match) {
return match.path.midRef(match.offset) == previousSelected;
return match.relativePath() == previousSelected;
});
if (it != matches.cend())
row = std::distance(matches.cbegin(), it);
Expand Down
11 changes: 7 additions & 4 deletions src/tiled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,18 @@ int main(int argc, char *argv[])

qInstallMessageHandler(messagesToConsole);

QGuiApplication::setFallbackSessionManagementEnabled(false);

// Enable support for highres images (added in Qt 5.1, but off by default)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
// Enable support for highres images (added in Qt 5.1, but off by default, always enabled in Qt 6)
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif

// Fallback session management was removed from Qt 6
QGuiApplication::setFallbackSessionManagementEnabled(false);

// Window context help buttons are disabled by default in Qt 6
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
QCoreApplication::setAttribute(Qt::AA_DisableWindowContextHelpButton);
#endif
#endif

#ifdef Q_OS_MAC
QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
Expand Down
14 changes: 7 additions & 7 deletions src/tiled/mapdocumentactionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ MapDocumentActionHandler::MapDocumentActionHandler(QObject *parent)
mActionSelectInverse = new QAction(this);
mActionSelectInverse->setShortcut(Qt::CTRL + Qt::Key_I);
mActionSelectNone = new QAction(this);
mActionSelectNone->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_A);
mActionSelectNone->setShortcut((Qt::CTRL | Qt::SHIFT) + Qt::Key_A);

mActionCropToSelection = new QAction(this);
mActionAutocrop = new QAction(this);
Expand All @@ -92,13 +92,13 @@ MapDocumentActionHandler::MapDocumentActionHandler(QObject *parent)
mActionLayerViaCopy->setShortcut(Qt::CTRL + Qt::Key_J);

mActionLayerViaCut = new QAction(this);
mActionLayerViaCut->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_J);
mActionLayerViaCut->setShortcut((Qt::CTRL | Qt::SHIFT) + Qt::Key_J);

mActionGroupLayers = new QAction(this);
mActionUngroupLayers = new QAction(this);

mActionDuplicateLayers = new QAction(this);
mActionDuplicateLayers->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_D);
mActionDuplicateLayers->setShortcut((Qt::CTRL | Qt::SHIFT) + Qt::Key_D);
mActionDuplicateLayers->setIcon(
QIcon(QLatin1String(":/images/16/stock-duplicate-16.png")));

Expand All @@ -115,12 +115,12 @@ MapDocumentActionHandler::MapDocumentActionHandler(QObject *parent)
mActionSelectNextLayer->setShortcut(Qt::CTRL + Qt::Key_PageUp);

mActionMoveLayersUp = new QAction(this);
mActionMoveLayersUp->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_Up);
mActionMoveLayersUp->setShortcut((Qt::CTRL | Qt::SHIFT) + Qt::Key_Up);
mActionMoveLayersUp->setIcon(
QIcon(QLatin1String(":/images/16/go-up.png")));

mActionMoveLayersDown = new QAction(this);
mActionMoveLayersDown->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_Down);
mActionMoveLayersDown->setShortcut((Qt::CTRL | Qt::SHIFT) + Qt::Key_Down);
mActionMoveLayersDown->setIcon(
QIcon(QLatin1String(":/images/16/go-down.png")));

Expand All @@ -143,12 +143,12 @@ MapDocumentActionHandler::MapDocumentActionHandler(QObject *parent)
mActionToggleLockSelectedLayers->setIcon(lockedIcon);

mActionToggleOtherLayers = new QAction(this);
mActionToggleOtherLayers->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_H);
mActionToggleOtherLayers->setShortcut((Qt::CTRL | Qt::SHIFT) + Qt::Key_H);
mActionToggleOtherLayers->setIcon(
QIcon(QLatin1String(":/images/16/show_hide_others.png")));

mActionToggleLockOtherLayers = new QAction(this);
mActionToggleLockOtherLayers->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_L);
mActionToggleLockOtherLayers->setShortcut((Qt::CTRL | Qt::SHIFT) + Qt::Key_L);
mActionToggleLockOtherLayers->setIcon(lockedIcon);

mActionLayerProperties = new QAction(this);
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/mapeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ void MapEditor::setupQuickStamps()
connect(createStamp, &QShortcut::activated, [=] { mTileStampManager->createQuickStamp(i); });

// Set up shortcut for extending this quick stamp
QShortcut *extendStamp = new QShortcut(Qt::CTRL + Qt::SHIFT + key, mMainWindow);
QShortcut *extendStamp = new QShortcut((Qt::CTRL | Qt::SHIFT) + key, mMainWindow);
connect(extendStamp, &QShortcut::activated, [=] { mTileStampManager->extendQuickStamp(i); });
}

Expand Down
6 changes: 6 additions & 0 deletions src/tiled/projectmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ class ProjectModel : public QAbstractItemModel
int score;
int offset;
QString path;

#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QStringRef relativePath() const { return path.midRef(offset); }
#else
QStringView relativePath() const { return QStringView(path).mid(offset); }
#endif
};

QVector<Match> findFiles(const QStringList &words) const;
Expand Down
17 changes: 15 additions & 2 deletions src/tiled/scriptfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,21 +244,34 @@ QString ScriptTextFile::filePath() const
return QFileInfo(m_file->fileName()).absoluteFilePath();
}

#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
QString ScriptTextFile::codec() const
{
if (checkForClosed())
return {};
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
return QString::fromLatin1(m_stream->codec()->name());
#else
return QString::fromLatin1(QStringConverter::nameForEncoding(m_stream->encoding()));
#endif
}

void ScriptTextFile::setCodec(const QString &codec)
{
if (checkForClosed())
return;
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
m_stream->setCodec(codec.toLatin1());
}
#else
auto encoding = QStringConverter::encodingForName(codec.toLatin1());
if (!encoding.has_value()) {
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors",
"Unsupported encoding: %1").arg(codec));
return;
}

m_stream->setEncoding(encoding.value());
#endif
}

QString ScriptTextFile::readLine()
{
Expand Down
5 changes: 0 additions & 5 deletions src/tiled/scriptfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ class ScriptTextFile : public QObject

Q_PROPERTY(QString filePath READ filePath)
Q_PROPERTY(bool atEof READ atEof)

#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
Q_PROPERTY(QString codec READ codec WRITE setCodec)
#endif

public:
enum OpenMode {
Expand All @@ -97,10 +94,8 @@ class ScriptTextFile : public QObject

QString filePath() const;

#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
QString codec() const;
void setCodec(const QString &codec);
#endif

Q_INVOKABLE QString readLine();
Q_INVOKABLE QString readAll();
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/tiledproxystyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,11 @@ void TiledProxyStyle::drawControl(ControlElement element,
}
int x, y, w, h;
menuitem->rect.getRect(&x, &y, &w, &h);
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
int tab = menuitem->reservedShortcutWidth;
#else
int tab = menuitem->tabWidth;
#endif
QColor discol;
if (dis) {
discol = menuitem->palette.text().color();
Expand Down
19 changes: 7 additions & 12 deletions src/tiled/tilestampmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,19 @@ void TileStampManager::loadStamps()
if (!stampFile.open(QIODevice::ReadOnly))
continue;

QByteArray data = stampFile.readAll();

QJsonDocument document = QJsonDocument::fromBinaryData(data);
if (document.isNull()) {
// document not valid binary data, maybe it's an JSON text file
QJsonParseError error;
document = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) {
qDebug().noquote() << "Failed to parse stamp file:" << error.errorString();
continue;
}
QJsonParseError error;
const QByteArray data = stampFile.readAll();
const QJsonDocument document = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) {
qDebug().noquote() << "Failed to parse stamp file:" << error.errorString();
continue;
}

TileStamp stamp = TileStamp::fromJson(document.object(), stampsDir);
if (stamp.isEmpty())
continue;

stamp.setFileName(iterator.fileInfo().fileName());
stamp.setFileName(iterator.fileName());

mTileStampModel->addStamp(stamp);

Expand Down
1 change: 1 addition & 0 deletions src/tiled/toolmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <QAction>
#include <QShortcut>
#include <QWidget>

using namespace Tiled;

Expand Down

0 comments on commit 4616513

Please sign in to comment.