Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
* Fixed undo behavior after resizing objects certain ways (by Kanishka, #4339)
* Scripting: Added API for custom property types (with dogboydog, #3971)
* Scripting: Added TileMap.chunkSize and TileMap.compressionLevel properties
* Scripting: Added optional defaultValue and toolTip params to Dialog add widget methods (by Oval, #4358)
* Scripting: Added tiled.session to read and write session properties (by Kanishka, #4345)
* Scripting: Added MapEditor.selectedTool and MapEditor.tool (#4330)
* Scripting: Fixed the `fileName` property of map/tileset passed to `FileFormat.write` (by Shuvam Pal, #4359)
* AutoMapping: Don't match rules based on empty input indexes
* AutoMapping: Optimized reloading of rule maps and load rule maps on-demand
* tBIN plugin: Added support for the tIDE XML format (by Casey Warrington, #4308)
Expand Down
59 changes: 46 additions & 13 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5691,10 +5691,12 @@ declare class Dialog extends Qt.QWidget {
addHeading(labelText: string, maxWidth?: boolean): Qt.QLabel;

/**
* Add a label to the dialog with the given test. A label will always be the
* Add a label to the dialog with the given text. A label will always be the
* first widget in a row.
*
* The optional toolTip parameter sets a tooltip on the label (since 1.12).
*/
addLabel(labelText: string): Qt.QLabel;
addLabel(labelText: string, toolTip?: string): Qt.QLabel;

/**
* Adds a separator line with optional label to the dialog.
Expand All @@ -5704,40 +5706,54 @@ declare class Dialog extends Qt.QWidget {

/**
* Adds an image widget that can display an image in a dialog.
*
* The optional toolTip parameter sets a tooltip on the widget (since 1.12).
*/
addImage(labelText: string, image: Image): ImageWidget;
addImage(labelText: string, image: Image, toolTip?: string): ImageWidget;

/**
* Add a {@link Qt.QSlider} widget to the dialog to allow a user to type a
* Add a {@link Qt.QDoubleSpinBox} widget to the dialog to allow a user to type a
* numerical value or use up and down controls on the widget to manipulate
* the value.
*
* This can be used to enter integer or decimal values.
*
* The optional defaultValue parameter sets the initial value of the spin box (since 1.12).
*
* The optional toolTip parameter sets a tooltip on the widget (since 1.12).
*/
addNumberInput(labelText?: string): Qt.QDoubleSpinBox;
addNumberInput(labelText?: string, defaultValue?: number, toolTip?: string): Qt.QDoubleSpinBox;

/**
* Add a {@link Qt.QSlider} widget to the dialog to allow a user to
* slide a handle within a number range.
*
* This can only be used to enter integer-type values.
*
* The optional defaultValue parameter sets the initial value of the slider.
*
* The optional toolTip parameter sets a tooltip on the widget (since 1.12).
*/
addSlider(labelText?: string): Qt.QSlider;
addSlider(labelText?: string, defaultValue?: number, toolTip?: string): Qt.QSlider;

/**
* Add a {@link Qt.QCheckBox} widget with the given text to the dialog.
* Allows a user to toggle a boolean value.
*
* If the defaultValue parameter is true the checkbox is checked by default.
*
* The optional toolTip parameter sets a tooltip on the widget (since 1.12).
*/
addCheckBox(text?: string, defaultValue?: boolean): Qt.QCheckBox;
addCheckBox(text?: string, defaultValue?: boolean, toolTip?: string): Qt.QCheckBox;

/**
* Add a {@link Qt.QPushButton} widget with the given text to the dialog.
* Allows the user to press a button that you can respond to the clicked
* signal of.
*
* The optional toolTip parameter sets a tooltip on the widget (since 1.12).
*/
addButton(text?: string): Qt.QPushButton;
addButton(text?: string, toolTip?: string): Qt.QPushButton;

/**
* Add a {@link Qt.QLineEdit} widget to the dialog to allow the user to enter
Expand All @@ -5746,8 +5762,10 @@ declare class Dialog extends Qt.QWidget {
*
* If the labelText is non-empty, a label widget will be added to the left of
* the widget.
*
* The optional toolTip parameter sets a tooltip on the widget (since 1.12).
*/
addTextInput(labelText?: string, defaultValue?: string): Qt.QLineEdit;
addTextInput(labelText?: string, defaultValue?: string, toolTip?: string): Qt.QLineEdit;

/**
* Add a {@link Qt.QTextEdit} widget to the dialog to allow the user to edit
Expand All @@ -5757,34 +5775,49 @@ declare class Dialog extends Qt.QWidget {
*
* If the labelText is non-empty, a label widget will be added to the left of
* the widget.
*
* The optional toolTip parameter sets a tooltip on the widget (since 1.12).
*/
addTextEdit(labelText?: string, defaultValue?: string): Qt.QTextEdit;
addTextEdit(labelText?: string, defaultValue?: string, toolTip?: string): Qt.QTextEdit;

/**
* Add a {@link Qt.QComboBox} widget (also known as a dropdown) allowing the
* user to select one of the given values.
*
* If the labelText is non-empty, a label widget will be added to the left of
* the widget.
*
* The optional defaultIndex parameter sets the initially selected index (0-based) (since 1.12).
*
* The optional toolTip parameter sets a tooltip on the widget (since 1.12).
*/
addComboBox(labelText: string, values: string[]): Qt.QComboBox;
addComboBox(labelText: string, values: string[], defaultIndex?: number, toolTip?: string): Qt.QComboBox;

/**
* Add a {@link ColorButton} widget that allows the user to pick a color.
*
* If the labelText is non-empty, a label widget will be added to the left of
* the widget.
*
* The optional defaultValue parameter sets the initial color (since 1.12). Pass `undefined`
* to skip setting a default color while still providing a toolTip.
*
* The optional toolTip parameter sets a tooltip on the widget (since 1.12).
*/
addColorButton(labelText?: string): ColorButton;
addColorButton(labelText?: string, defaultValue?: color, toolTip?: string): ColorButton;

/**
* Add a {@link FileEdit} widget with a button which opens a file picker
* dialog and displays the path in the dialog.
*
* If the labelText is non-empty, a label widget will be added to the left of
* the widget.
*
* The optional defaultValue parameter sets the initial file path (since 1.12).
*
* The optional toolTip parameter sets a tooltip on the widget (since 1.12).
*/
addFilePicker(labelText?: string): FileEdit;
addFilePicker(labelText?: string, defaultValue?: string, toolTip?: string): FileEdit;

/**
* Add a {@link QButtonGroup} widget which allows you to add multiple radio
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/editableasset.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class EditableAsset : public EditableObject
public:
EditableAsset(Object *object, QObject *parent = nullptr);

QString fileName() const;
virtual QString fileName() const;
bool isReadOnly() const override = 0;
bool isMap() const { return assetType() == AssetType::TileMap; }
bool isTileset() const { return assetType() == AssetType::Tileset; }
Expand Down
7 changes: 7 additions & 0 deletions src/tiled/editablemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ EditableMap::~EditableMap()
setObject(nullptr);
}

QString EditableMap::fileName() const
{
if (auto fileName = EditableAsset::fileName(); !fileName.isEmpty())
return fileName;
return map()->fileName;
}

QList<QObject *> EditableMap::tilesets() const
{
QList<QObject *> editableTilesets;
Expand Down
1 change: 1 addition & 0 deletions src/tiled/editablemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class EditableMap final : public EditableAsset
explicit EditableMap(std::unique_ptr<Map> map, QObject *parent = nullptr);
~EditableMap() override;

QString fileName() const override;
bool isReadOnly() const override;
AssetType::Value assetType() const override { return AssetType::TileMap; }

Expand Down
7 changes: 7 additions & 0 deletions src/tiled/editabletileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ void EditableTileset::removeWangSet(EditableWangSet *editableWangSet)
}
}

QString EditableTileset::fileName() const
{
if (auto fileName = EditableAsset::fileName(); !fileName.isEmpty())
return fileName;
return const_cast<Tileset*>(tileset())->originalTileset()->fileName();
}

TilesetDocument *EditableTileset::tilesetDocument() const
{
return static_cast<TilesetDocument*>(document());
Expand Down
1 change: 1 addition & 0 deletions src/tiled/editabletileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class EditableTileset final : public EditableAsset
QObject *parent = nullptr);
~EditableTileset() override;

QString fileName() const override;
bool isReadOnly() const final;
AssetType::Value assetType() const override { return AssetType::Tileset; }

Expand Down
75 changes: 48 additions & 27 deletions src/tiled/scriptdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ QWidget *ScriptDialog::addHeading(const QString &text, bool fillRow)
return label;
}

QWidget *ScriptDialog::addLabel(const QString &text)
QWidget *ScriptDialog::addLabel(const QString &text, const QString &toolTip)
{
return addDialogWidget(newLabel(text));
QLabel *label = newLabel(text);
return addDialogWidget(label, QString(), QString(), toolTip);
}

QWidget *ScriptDialog::addSeparator(const QString &labelText)
Expand Down Expand Up @@ -207,65 +208,81 @@ QWidget *ScriptDialog::addSeparator(const QString &labelText)
return line;
}

QWidget *ScriptDialog::addTextInput(const QString &labelText, const QString &defaultValue)
QWidget *ScriptDialog::addTextInput(const QString &labelText, const QString &defaultValue, const QString &toolTip)
{
return addDialogWidget(new QLineEdit(defaultValue, this), labelText);
QLineEdit *lineEdit = new QLineEdit(defaultValue, this);
return addDialogWidget(lineEdit, labelText, QString(), toolTip);
}

QWidget *ScriptDialog::addTextEdit(const QString &labelText, const QString &defaultValue)
QWidget *ScriptDialog::addTextEdit(const QString &labelText, const QString &defaultValue, const QString &toolTip)
{
QTextEdit *textEdit = new QTextEdit(defaultValue, this);
addDialogWidget(textEdit, labelText);
addDialogWidget(textEdit, labelText, QString(), toolTip);
textEdit->setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::TextEditorInteraction);
textEdit->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
return textEdit;
}

QWidget *ScriptDialog::addImage(const QString &labelText, Tiled::ScriptImage *image)
QWidget *ScriptDialog::addImage(const QString &labelText, Tiled::ScriptImage *image, const QString &toolTip)
{
return addDialogWidget(new ScriptImageWidget(image, this), labelText);
ScriptImageWidget *imageWidget = new ScriptImageWidget(image, this);
return addDialogWidget(imageWidget, labelText, QString(), toolTip);
}

QWidget *ScriptDialog::addNumberInput(const QString &labelText)
QWidget *ScriptDialog::addNumberInput(const QString &labelText, double defaultValue, const QString &toolTip)
{
return addDialogWidget(new ExpressionDoubleSpinBox(this), labelText);
ExpressionDoubleSpinBox *spinBox = new ExpressionDoubleSpinBox(this);
spinBox->setValue(defaultValue);
return addDialogWidget(spinBox, labelText, QString(), toolTip);
}

QWidget *ScriptDialog::addSlider(const QString &labelText)
QWidget *ScriptDialog::addSlider(const QString &labelText, int defaultValue, const QString &toolTip)
{
QSlider *horizontalSlider = new QSlider(this);
horizontalSlider->setOrientation(Qt::Horizontal);
return addDialogWidget(horizontalSlider, labelText);
horizontalSlider->setMaximum(qMax(defaultValue, horizontalSlider->maximum()));
horizontalSlider->setValue(defaultValue);
return addDialogWidget(horizontalSlider, labelText, QString(), toolTip);
}

QWidget *ScriptDialog::addCheckBox(const QString &text, bool defaultValue)
QWidget *ScriptDialog::addCheckBox(const QString &text, bool defaultValue, const QString &toolTip)
{
QCheckBox *checkBox = new QCheckBox(text, this);
checkBox->setCheckState(defaultValue ? Qt::Checked: Qt::Unchecked);
return addDialogWidget(checkBox);
checkBox->setCheckState(defaultValue ? Qt::Checked : Qt::Unchecked);
return addDialogWidget(checkBox, QString(), QString(), toolTip);
}
QWidget *ScriptDialog::addComboBox(const QString &labelText, const QStringList &values)

QWidget *ScriptDialog::addComboBox(const QString &labelText, const QStringList &values, int defaultIndex, const QString &toolTip)
{
QComboBox *comboBox = new ScriptComboBox(this);
ScriptComboBox *comboBox = new ScriptComboBox(this);
comboBox->addItems(values);
return addDialogWidget(comboBox, labelText);
if (defaultIndex > 0 && defaultIndex < comboBox->count())
comboBox->setCurrentIndex(defaultIndex);
return addDialogWidget(comboBox, labelText, QString(), toolTip);
}

QWidget *ScriptDialog::addButton(const QString &text)
QWidget *ScriptDialog::addButton(const QString &text, const QString &toolTip)
{
return addDialogWidget(new QPushButton(text, this));
QPushButton *button = new QPushButton(text, this);
return addDialogWidget(button, QString(), QString(), toolTip);
}

QWidget *ScriptDialog::addFilePicker(const QString &labelText)
QWidget *ScriptDialog::addFilePicker(const QString &labelText, const QString &defaultValue, const QString &toolTip)
{
return addDialogWidget(new FileEdit(this), labelText);
FileEdit *fileEdit = new FileEdit(this);
if (!defaultValue.isEmpty())
fileEdit->setFileName(defaultValue);
return addDialogWidget(fileEdit, labelText, QString(), toolTip);
}

QWidget *ScriptDialog::addColorButton(const QString &labelText)
QWidget *ScriptDialog::addColorButton(const QString &labelText, const QColor &defaultValue, const QString &toolTip)
{
QWidget *colorButton = addDialogWidget(new ColorButton(this), labelText);
colorButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
return colorButton;
ColorButton *colorButton = new ColorButton(this);
if (defaultValue.isValid())
colorButton->setColor(defaultValue);
QWidget *widget = addDialogWidget(colorButton, labelText, QString(), toolTip);
widget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
return widget;
}

ScriptDialog::NewRowMode ScriptDialog::newRowMode() const
Expand Down Expand Up @@ -299,8 +316,12 @@ int ScriptDialog::exec()

QWidget *ScriptDialog::addDialogWidget(QWidget *widget,
const QString &label,
const QString &labelToolTip)
const QString &labelToolTip,
const QString &widgetToolTip)
{
if (!widgetToolTip.isEmpty())
widget->setToolTip(widgetToolTip);

determineWidgetGrouping(widget);
if (m_widgetsInRow == 0)
m_widgetsInRow = 1;
Expand Down
28 changes: 15 additions & 13 deletions src/tiled/scriptdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QAbstractButton>
#include <QBoxLayout>
#include <QButtonGroup>
#include <QColor>
#include <QDialog>
#include <QLabel>

Expand Down Expand Up @@ -98,18 +99,18 @@ class ScriptDialog : public QDialog
~ScriptDialog() override;

Q_INVOKABLE QWidget *addHeading(const QString &text, bool fillRow = false);
Q_INVOKABLE QWidget *addLabel(const QString &text);
Q_INVOKABLE QWidget *addLabel(const QString &text, const QString &toolTip = QString());
Q_INVOKABLE QWidget *addSeparator(const QString &labelText = QString());
Q_INVOKABLE QWidget *addTextInput(const QString &labelText = QString(), const QString &defaultValue = QString());
Q_INVOKABLE QWidget *addTextEdit(const QString &labelText, const QString &defaultValue= QString());
Q_INVOKABLE QWidget *addNumberInput(const QString &labelText);
Q_INVOKABLE QWidget *addSlider(const QString &labelText);
Q_INVOKABLE QWidget *addComboBox(const QString &labelText, const QStringList &values);
Q_INVOKABLE QWidget *addCheckBox(const QString &text = QString(), bool defaultValue = false);
Q_INVOKABLE QWidget *addButton(const QString &text = QString());
Q_INVOKABLE QWidget *addFilePicker(const QString &labelText = QString());
Q_INVOKABLE QWidget *addColorButton(const QString &labelText = QString());
Q_INVOKABLE QWidget *addImage(const QString &labelText, Tiled::ScriptImage *image);
Q_INVOKABLE QWidget *addTextInput(const QString &labelText = QString(), const QString &defaultValue = QString(), const QString &toolTip = QString());
Q_INVOKABLE QWidget *addTextEdit(const QString &labelText = QString(), const QString &defaultValue = QString(), const QString &toolTip = QString());
Q_INVOKABLE QWidget *addNumberInput(const QString &labelText = QString(), double defaultValue = 0.0, const QString &toolTip = QString());
Q_INVOKABLE QWidget *addSlider(const QString &labelText = QString(), int defaultValue = 0, const QString &toolTip = QString());
Q_INVOKABLE QWidget *addComboBox(const QString &labelText, const QStringList &values, int defaultIndex = 0, const QString &toolTip = QString());
Q_INVOKABLE QWidget *addCheckBox(const QString &text = QString(), bool defaultValue = false, const QString &toolTip = QString());
Q_INVOKABLE QWidget *addButton(const QString &text = QString(), const QString &toolTip = QString());
Q_INVOKABLE QWidget *addFilePicker(const QString &labelText = QString(), const QString &defaultValue = QString(), const QString &toolTip = QString());
Q_INVOKABLE QWidget *addColorButton(const QString &labelText = QString(), const QColor &defaultValue = QColor(), const QString &toolTip = QString());
Q_INVOKABLE QWidget *addImage(const QString &labelText, Tiled::ScriptImage *image, const QString &toolTip = QString());
Q_INVOKABLE ScriptButtonGroup *addRadioButtonGroup(const QString &labelText,
const QStringList &values,
const QString &toolTip = QString(),
Expand All @@ -128,9 +129,10 @@ class ScriptDialog : public QDialog
QLabel *newLabel(const QString &labelText);
void initializeLayout();
void determineWidgetGrouping(QWidget *widget);
QWidget *addDialogWidget(QWidget * widget,
QWidget *addDialogWidget(QWidget *widget,
const QString &label = QString(),
const QString &labelToolTip = QString());
const QString &labelToolTip = QString(),
const QString &widgetToolTip = QString());

int m_rowIndex = 0;
int m_widgetsInRow = 0;
Expand Down
Loading