Skip to content

Commit

Permalink
Scripting: Fixed issues with absolute file paths on Windows
Browse files Browse the repository at this point in the history
The strings were converted into URLs was broken on Windows, where an
absolute file path like "C:/..." was interpreted as a URL with the
scheme "C", which would then fail to load.

In addition to checking whether the resulting URL would be relative
(without scheme), we now also check whether the given string is a valid
absolute file path. In both cases, we produce a URL pointing to the
local file.

Closes mapeditor#2841
  • Loading branch information
bjorn committed Jun 24, 2020
1 parent d4cf48e commit f8da3b5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
4 changes: 1 addition & 3 deletions src/libtiled/imagelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ bool ImageLayer::loadFromImage(const QPixmap &image, const QUrl &source)
*/
bool ImageLayer::loadFromImage(const QImage &image, const QString &source)
{
const QUrl url(source);
return loadFromImage(QPixmap::fromImage(image), url.isRelative() ? QUrl::fromLocalFile(source)
: url);
return loadFromImage(QPixmap::fromImage(image), Tiled::toUrl(source));
}

bool ImageLayer::loadFromImage(const QUrl &url)
Expand Down
5 changes: 1 addition & 4 deletions src/libtiled/properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ QString FilePath::toString(const FilePath &path)

FilePath FilePath::fromString(const QString &string)
{
QUrl url(string);
if (url.isRelative())
url = QUrl::fromLocalFile(string);
return { url };
return { Tiled::toUrl(string) };
}


Expand Down
12 changes: 12 additions & 0 deletions src/libtiled/tiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ QUrl Tiled::toUrl(const QString &filePathOrUrl, const QDir &dir)
return QUrl::fromLocalFile(absolutePath);
}

QUrl Tiled::toUrl(const QString &filePathOrUrl)
{
QUrl url(filePathOrUrl);

if (filePathOrUrl.startsWith(QLatin1String(":/")))
url = QUrl(QLatin1String("qrc") + filePathOrUrl);
else if (QDir::isAbsolutePath(filePathOrUrl) || url.isRelative())
url = QUrl::fromLocalFile(filePathOrUrl);

return url;
}

/*
If \a url is a local file returns a path suitable for passing to QFile.
Otherwise returns an empty string.
Expand Down
3 changes: 2 additions & 1 deletion src/libtiled/tiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ static const char PROPERTIES_MIMETYPE[] = "application/vnd.properties.list";
TILEDSHARED_EXPORT QPointF alignmentOffset(const QRectF &r, Alignment alignment);

TILEDSHARED_EXPORT QString toFileReference(const QUrl &url, const QDir &dir);
TILEDSHARED_EXPORT QUrl toUrl(const QString &reference, const QDir &dir);
TILEDSHARED_EXPORT QUrl toUrl(const QString &filePathOrUrl, const QDir &dir);
TILEDSHARED_EXPORT QUrl toUrl(const QString &filePathOrUrl);
TILEDSHARED_EXPORT QString urlToLocalFileOrQrc(const QUrl &url);

inline QString colorToString(const QColor &color)
Expand Down
10 changes: 4 additions & 6 deletions src/libtiled/tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ bool Tileset::loadFromImage(const QImage &image, const QUrl &source)
*/
bool Tileset::loadFromImage(const QImage &image, const QString &source)
{
const QUrl url(source);
return loadFromImage(image, url.isRelative() ? QUrl::fromLocalFile(source) : url);
return loadFromImage(image, Tiled::toUrl(source));
}

/**
Expand Down Expand Up @@ -405,15 +404,14 @@ void Tileset::setImageSource(const QUrl &imageSource)

/**
* Exists only because the Python plugin interface does not handle QUrl (would
* be nice to add this). Assumes \a source is a local file when it would
* otherwise be a relative URL (without scheme).
* be nice to add this). Assumes \a source is a local file when it is either
* an absolute file path or would otherwise be a relative URL (without scheme).
*
* \sa loadFromImage
*/
void Tileset::setImageSource(const QString &source)
{
const QUrl url(source);
setImageSource(url.isRelative() ? QUrl::fromLocalFile(source) : url);
setImageSource(Tiled::toUrl(source));
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/tiled/fileedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "fileedit.h"

#include "tiled.h"

#include <QFileDialog>
#include <QFocusEvent>
#include <QHBoxLayout>
Expand Down Expand Up @@ -71,10 +73,7 @@ void FileEdit::setFileUrl(const QUrl &url)
QUrl FileEdit::fileUrl() const
{
const QString path = mLineEdit->text();
QUrl url(path);
if (url.isRelative())
url = QUrl::fromLocalFile(path);
return url;
return Tiled::toUrl(path);
}

void FileEdit::focusInEvent(QFocusEvent *e)
Expand Down

0 comments on commit f8da3b5

Please sign in to comment.