Skip to content

Commit

Permalink
Scripting: Added some alternatives to working with URLs (#3893)
Browse files Browse the repository at this point in the history
* Added FilePath.localFile
* Added FileEdit.fileName
* Added code snippet for converting QUrl to a local file path to docs
  • Loading branch information
bjorn committed Mar 18, 2024
1 parent 1ff8220 commit 5fe44b7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Scripting: Added API for working with worlds (#3539)
* Scripting: Added Tile.image for accessing a tile's image data
* Scripting: Added Tileset.imageFileName and ImageLayer.imageFileName
* Scripting: Added FilePath.localFile and FileEdit.fileName (string alternatives to Qt.QUrl properties)
* Scripting: Made Tileset.margin and Tileset.tileSpacing writable
* Scripting: Restored compatibility for MapObject.polygon (#3845)
* TMX format: Embedded images are now also supported on tilesets and image layers
Expand Down
39 changes: 35 additions & 4 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,18 @@ type MenuItem = MenuAction|MenuSeparator
interface FilePath {
/**
* The URL of the file.
*
* If you need a local file path, use {@link localFile}.
*/
url: Qt.QUrl;

/**
* The local file path, or empty if the current URL value doesn't refer
* to a local file.
*
* @since 1.10.3
*/
localFile: string;
}

/**
Expand Down Expand Up @@ -437,9 +447,19 @@ declare namespace Qt {
/**
* Used in {@link FileEdit} as the URL of the currently selected file.
*/
class QUrl{
class QUrl {
/**
* Get a string representation of the file.
* Get a string representation of the URL.
*
* Note that this representation will generally start with "file://". In
* case you need a local file path, you can use the following code:
*
* ```js
* var path = url.toString().replace(/^file:\/{2}/, '');
* ```
*
* Or have a look at whether an alternative property is available that
* gives you a local file path in the first place.
*/
toString(): string;
}
Expand Down Expand Up @@ -4764,17 +4784,28 @@ declare class ColorButton extends Qt.QWidget {
* and displays the path in the dialog.
*/
declare class FileEdit extends Qt.QWidget {
/**
* The current file path.
*
* @since 1.10.3
*/
fileName: string;

/**
* The {@link Qt.QUrl} of the currently selected file.
*
* If you need the file path as a string, use the {@link fileName} property.
*/
fileUrl: Qt.QUrl;

/**
* Signal emitted when the selected fileUrl changes.
* Signal emitted when the selected file changes.
*/
fileUrlChanged: Signal<Qt.QUrl>;

/**
* If `true`, the user will be prompted for a directory rather than a file. Defaults to `false`.
* If `true`, the user will be prompted for a directory rather than a file.
* Defaults to `false`.
*/
isDirectory: boolean;

Expand Down
5 changes: 5 additions & 0 deletions src/libtiled/properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ class TILEDSHARED_EXPORT FilePath
{
Q_GADGET
Q_PROPERTY(QUrl url MEMBER url)
Q_PROPERTY(QString localFile READ localFile WRITE setLocalFile)

public:
QUrl url;

QString localFile() const { return url.toLocalFile(); }
void setLocalFile(const QString &filePath)
{ url = QUrl::fromLocalFile(filePath); }

bool operator==(const FilePath &o) const
{ return url == o.url; }

Expand Down
18 changes: 13 additions & 5 deletions src/tiled/fileedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,23 @@ FileEdit::FileEdit(QWidget *parent)

void FileEdit::setFileUrl(const QUrl &url)
{
const QString path = url.toString(QUrl::PreferLocalFile);
if (mLineEdit->text() != path)
mLineEdit->setText(path);
setFileName(url.toString(QUrl::PreferLocalFile));
}

QUrl FileEdit::fileUrl() const
{
const QString path = mLineEdit->text();
return Tiled::toUrl(path);
return Tiled::toUrl(fileName());
}

void FileEdit::setFileName(const QString &fileName)
{
if (mLineEdit->text() != fileName)
mLineEdit->setText(fileName);
}

QString FileEdit::fileName() const
{
return mLineEdit->text();
}

void FileEdit::focusInEvent(QFocusEvent *e)
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/fileedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class FileEdit : public QWidget

Q_PROPERTY(QString filter READ filter WRITE setFilter)
Q_PROPERTY(QUrl fileUrl READ fileUrl WRITE setFileUrl)
Q_PROPERTY(QString fileName READ fileName WRITE setFileName)
Q_PROPERTY(bool isDirectory READ isDirectory WRITE setIsDirectory)

public:
Expand All @@ -45,6 +46,9 @@ class FileEdit : public QWidget
void setFileUrl(const QUrl &url);
QUrl fileUrl() const;

void setFileName(const QString &fileName);
QString fileName() const;

void setFilter(const QString &filter) { mFilter = filter; }
QString filter() const { return mFilter; }

Expand Down

0 comments on commit 5fe44b7

Please sign in to comment.