Skip to content

Commit

Permalink
Progress on porting to Qt 6
Browse files Browse the repository at this point in the history
* qHash changed signature
* qrand() was removed (replaced with QRandomGenerator)
* QDesktopWidget was removed (now using QWidget::screen)
* QDataStream open mode enum changed location
* Qt::AA_UseHighDpiPixmaps was removed
* Adjustments due to flags deleting operator+
* QWheelEvent API changed
* QWidget::enterEvent takes a custom QEnterEvent
* Qt::MidButton was removed (replaced with Qt::MiddleButton)
* QTextCodec was removed (replaced with encoding enum, but decided to
  remove TextFile.codec for now)
* QLinkedList deprecated (replaced with std::list)
* QMap::insertMulti removed (need to use QMultiMap)
* Need to depend on openglwidgets module for QOpenGLWidget
  • Loading branch information
bjorn committed Oct 12, 2020
1 parent dccb1fa commit 83ee9ab
Show file tree
Hide file tree
Showing 28 changed files with 115 additions and 34 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
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
11 changes: 11 additions & 0 deletions src/tiled/automapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#include "tilelayer.h"

#include <QDebug>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
#include <QRandomGenerator>
#endif

#include "qtcompat_p.h"

Expand Down Expand Up @@ -746,6 +749,10 @@ QRect AutoMapper::applyRule(int ruleIndex, const QRect &where)

const TileLayer dummy(QString(), 0, 0, mMapWork->width(), mMapWork->height());

#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
QRandomGenerator *randomGenerator = QRandomGenerator::global();
#endif

for (int y = minY; y <= maxY; ++y)
for (int x = minX; x <= maxX; ++x) {
bool anyMatch = false;
Expand Down Expand Up @@ -777,7 +784,11 @@ QRect AutoMapper::applyRule(int ruleIndex, const QRect &where)

if (anyMatch) {
// choose by chance which group of rule_layers should be used:
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
const int r = randomGenerator->generate() % mLayerList.size();
#else
const int r = qrand() % mLayerList.size();
#endif
const RuleOutput &translationTable = mLayerList.at(r);

if (mOptions.noOverlappingRules) {
Expand Down
15 changes: 14 additions & 1 deletion src/tiled/imagecolorpickerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@

#include "utils.h"

#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
#include <QDesktopWidget>
#else
#include <QScreen>
#endif
#include <QMouseEvent>

using namespace Tiled;
Expand Down Expand Up @@ -58,8 +62,13 @@ bool ImageColorPickerWidget::selectColor(const QString &image)
mScaleX = 1;
mScaleY = 1;

#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QRectF rct = QApplication::desktop()->availableGeometry(this);
double maxW = rct.width() * (2.0/3.0), maxH = rct.height() * (2.0/3.0);
#else
QRectF rct = screen()->availableGeometry();
#endif
const qreal maxW = rct.width() * (2.0/3.0);
const qreal maxH = rct.height() * (2.0/3.0);

if (mImage.width() > maxW || mImage.height() > maxH) {
pix = pix.scaled((int)maxW, (int)maxH, Qt::KeepAspectRatio);
Expand Down Expand Up @@ -106,5 +115,9 @@ void ImageColorPickerWidget::onMouseRelease(QMouseEvent * event)

void ImageColorPickerWidget::resizeEvent(QResizeEvent *)
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
move(QApplication::desktop()->availableGeometry(parentWidget()).center() - rect().center());
#else
move(screen()->availableGeometry().center() - rect().center());
#endif
}
4 changes: 4 additions & 0 deletions src/tiled/layermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ QMimeData *LayerModel::mimeData(const QModelIndexList &indexes) const

QMimeData *mimeData = new QMimeData;
QByteArray encodedData;
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
QDataStream stream(&encodedData, QIODevice::WriteOnly);
#else
QDataStream stream(&encodedData, QDataStream::WriteOnly);
#endif
QVector<Layer*> layers;

for (const QModelIndex &index : indexes) {
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ int main(int argc, char *argv[])
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)
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
QCoreApplication::setAttribute(Qt::AA_DisableWindowContextHelpButton);
#endif
Expand Down
4 changes: 3 additions & 1 deletion src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
#include "macsupport.h"
#endif

#include <QActionGroup>
#include <QCloseEvent>
#include <QDesktopServices>
#include <QFileDialog>
Expand All @@ -92,6 +93,7 @@
#include <QRegularExpression>
#include <QSessionManager>
#include <QShortcut>
#include <QStandardPaths>
#include <QStatusBar>
#include <QTextStream>
#include <QToolBar>
Expand Down Expand Up @@ -810,7 +812,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
QShortcut *switchToLeftDocument = new QShortcut(Qt::ALT + Qt::Key_Left, this);
connect(switchToLeftDocument, &QShortcut::activated,
mDocumentManager, &DocumentManager::switchToLeftDocument);
QShortcut *switchToLeftDocument1 = new QShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_Tab, this);
QShortcut *switchToLeftDocument1 = new QShortcut((Qt::CTRL | Qt::SHIFT) + Qt::Key_Tab, this);
connect(switchToLeftDocument1, &QShortcut::activated,
mDocumentManager, &DocumentManager::switchToLeftDocument);

Expand Down
8 changes: 4 additions & 4 deletions src/tiled/mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,11 @@ void MapView::wheelEvent(QWheelEvent *event)
bool wheelZoomsByDefault = Preferences::instance()->wheelZoomsByDefault();
bool control = event->modifiers() & Qt::ControlModifier;

if ((wheelZoomsByDefault != control) && event->orientation() == Qt::Vertical) {
if ((wheelZoomsByDefault != control) && event->angleDelta().y()) {
// No automatic anchoring since we'll do it manually
setTransformationAnchor(QGraphicsView::NoAnchor);

mZoomable->handleWheelDelta(event->delta());
mZoomable->handleWheelDelta(event->angleDelta().y());
adjustCenterFromMousePosition(mLastMousePos);

// Restore the centering anchor
Expand Down Expand Up @@ -430,7 +430,7 @@ void MapView::wheelEvent(QWheelEvent *event)
*/
void MapView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton && isActiveWindow()) {
if (event->button() == Qt::MiddleButton && isActiveWindow()) {
setHandScrolling(true);
return;
}
Expand All @@ -443,7 +443,7 @@ void MapView::mousePressEvent(QMouseEvent *event)
*/
void MapView::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton) {
if (event->button() == Qt::MiddleButton) {
setHandScrolling(false);
return;
}
Expand Down
12 changes: 8 additions & 4 deletions src/tiled/minimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void MiniMap::renderMapToImage()
miniMapRenderer.renderToImage(mMapImage, mRenderFlags);
}

void MiniMap::centerViewOnLocalPixel(QPoint centerPos, int delta)
void MiniMap::centerViewOnLocalPixel(const QPointF &centerPos, int delta)
{
MapView *mapView = DocumentManager::instance()->currentMapView();
if (!mapView)
Expand All @@ -215,8 +215,12 @@ void MiniMap::redrawTimeout()

void MiniMap::wheelEvent(QWheelEvent *event)
{
if (event->orientation() == Qt::Vertical) {
centerViewOnLocalPixel(event->pos(), event->delta());
if (event->angleDelta().y()) {
#if QT_VERSION < QT_VERSION_CHECK(5,15,0)
centerViewOnLocalPixel(event->pos(), event->angleDelta().y());
#else
centerViewOnLocalPixel(event->position(), event->angleDelta().y());
#endif
return;
}

Expand Down Expand Up @@ -302,7 +306,7 @@ QRect MiniMap::viewportRect() const
viewRect.height() / mapRect.height() * mImageRect.height());
}

QPointF MiniMap::mapToScene(QPoint p) const
QPointF MiniMap::mapToScene(QPointF p) const
{
if (mImageRect.isEmpty())
return QPointF();
Expand Down
4 changes: 2 additions & 2 deletions src/tiled/minimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public slots:
MiniMapRenderer::RenderFlags mRenderFlags;

QRect viewportRect() const;
QPointF mapToScene(QPoint p) const;
QPointF mapToScene(QPointF p) const;
void updateImageRect();
void renderMapToImage();
void centerViewOnLocalPixel(QPoint centerPos, int delta = 0);
void centerViewOnLocalPixel(const QPointF &centerPos, int delta = 0);
};

} // namespace Tiled
2 changes: 1 addition & 1 deletion src/tiled/resizehelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void ResizeHelper::mouseMoveEvent(QMouseEvent *event)

void ResizeHelper::wheelEvent(QWheelEvent *event)
{
if (event->delta() > 0)// zooming in
if (event->angleDelta().y() > 0)// zooming in
mZoom += 0.2;
else
mZoom -= 0.2;
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/scriptfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
#include <QFile>
#include <QFileInfo>
#include <QSaveFile>
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
#include <QTextCodec>
#endif
#include <QTextStream>

namespace Tiled {
Expand Down Expand Up @@ -242,6 +244,7 @@ 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())
Expand All @@ -255,6 +258,7 @@ void ScriptTextFile::setCodec(const QString &codec)
return;
m_stream->setCodec(codec.toLatin1());
}
#endif

QString ScriptTextFile::readLine()
{
Expand Down
6 changes: 6 additions & 0 deletions src/tiled/scriptfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ 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 @@ -93,8 +96,11 @@ class ScriptTextFile : public QObject
~ScriptTextFile() override;

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
6 changes: 3 additions & 3 deletions src/tiled/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

#include <QDir>
#include <QHash>
#include <QLinkedList>
#include <QPointF>
#include <QSettings>
#include <QSize>
#include <QStringList>
#include <QTimer>
#include <QVariantMap>

#include <list>
#include <memory>

namespace Tiled {
Expand Down Expand Up @@ -178,7 +178,7 @@ class Session : protected FileHelper
QMap<QString, QVariantMap> fileStates;

using ChangedCallback = std::function<void()>;
using Callbacks = QLinkedList<ChangedCallback>;
using Callbacks = std::list<ChangedCallback>;
using CallbackIterator = Callbacks::iterator;

private:
Expand Down Expand Up @@ -241,7 +241,7 @@ template<typename T>
Session::CallbackIterator SessionOption<T>::onChange(const Session::ChangedCallback &callback)
{
Session::Callbacks &callbacks = Session::mChangedCallbacks[mKey];
callbacks.prepend(callback);
callbacks.push_front(callback);
return callbacks.begin();
}

Expand Down
2 changes: 1 addition & 1 deletion src/tiled/shortcutsettingspage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void ActionsModel::refreshConflicts()
if (!mConflictsDirty)
return;

QMap<QKeySequence, Id> actionsByKey;
QMultiMap<QKeySequence, Id> actionsByKey;

for (const auto &actionId : qAsConst(mActions)) {
if (auto action = ActionManager::findAction(actionId))
Expand Down
6 changes: 3 additions & 3 deletions src/tiled/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ TabBar::TabBar(QWidget *parent)

void TabBar::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton)
if (event->button() == Qt::MiddleButton)
mPressedIndex = tabAt(event->pos());

QTabBar::mousePressEvent(event);
}

void TabBar::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton && tabsClosable()) {
if (event->button() == Qt::MiddleButton && tabsClosable()) {
if (mPressedIndex != -1 && mPressedIndex == tabAt(event->pos())) {
emit tabCloseRequested(mPressedIndex);
return;
Expand All @@ -57,7 +57,7 @@ void TabBar::wheelEvent(QWheelEvent *event)

int index = currentIndex();
if (index != -1) {
index += event->delta() > 0 ? -1 : 1;
index += event->angleDelta().y() > 0 ? -1 : 1;
if (index >= 0 && index < count())
setCurrentIndex(index);
}
Expand Down
1 change: 1 addition & 0 deletions src/tiled/tiled.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ QtGuiApplication {
Depends { name: "qtsingleapplication" }
Depends { name: "ib"; condition: qbs.targetOS.contains("macos") }
Depends { name: "Qt"; submodules: ["core", "widgets", "qml"]; versionAtLeast: "5.6" }
Depends { name: "Qt.openglwidgets"; condition: Qt.core.versionMajor >= 6 }

property bool qtcRunnable: true

Expand Down
1 change: 1 addition & 0 deletions src/tiled/tilesetdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "zoomable.h"

#include <QAction>
#include <QActionGroup>
#include <QComboBox>
#include <QDropEvent>
#include <QFileDialog>
Expand Down
Loading

0 comments on commit 83ee9ab

Please sign in to comment.