diff --git a/NEWS.md b/NEWS.md index 85089c6b0b..36ea580abf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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) diff --git a/docs/scripting-doc/index.d.ts b/docs/scripting-doc/index.d.ts index 3dd9d8c1c7..b406141a35 100644 --- a/docs/scripting-doc/index.d.ts +++ b/docs/scripting-doc/index.d.ts @@ -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. @@ -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 @@ -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 @@ -5757,8 +5775,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). */ - 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 @@ -5766,16 +5786,25 @@ 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 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 @@ -5783,8 +5812,12 @@ 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 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 diff --git a/src/tiled/editableasset.h b/src/tiled/editableasset.h index 3be675054e..cadc36ecba 100644 --- a/src/tiled/editableasset.h +++ b/src/tiled/editableasset.h @@ -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; } diff --git a/src/tiled/editablemap.cpp b/src/tiled/editablemap.cpp index 5f43da8f37..3ab640b5be 100644 --- a/src/tiled/editablemap.cpp +++ b/src/tiled/editablemap.cpp @@ -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 EditableMap::tilesets() const { QList editableTilesets; diff --git a/src/tiled/editablemap.h b/src/tiled/editablemap.h index a69c1e6fb7..f8aaf49751 100644 --- a/src/tiled/editablemap.h +++ b/src/tiled/editablemap.h @@ -118,6 +118,7 @@ class EditableMap final : public EditableAsset explicit EditableMap(std::unique_ptr map, QObject *parent = nullptr); ~EditableMap() override; + QString fileName() const override; bool isReadOnly() const override; AssetType::Value assetType() const override { return AssetType::TileMap; } diff --git a/src/tiled/editabletileset.cpp b/src/tiled/editabletileset.cpp index cdade611b5..008ab6412e 100644 --- a/src/tiled/editabletileset.cpp +++ b/src/tiled/editabletileset.cpp @@ -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())->originalTileset()->fileName(); +} + TilesetDocument *EditableTileset::tilesetDocument() const { return static_cast(document()); diff --git a/src/tiled/editabletileset.h b/src/tiled/editabletileset.h index bb864399e6..21f8422183 100644 --- a/src/tiled/editabletileset.h +++ b/src/tiled/editabletileset.h @@ -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; } diff --git a/src/tiled/scriptdialog.cpp b/src/tiled/scriptdialog.cpp index 689c0a5300..a86b27513a 100644 --- a/src/tiled/scriptdialog.cpp +++ b/src/tiled/scriptdialog.cpp @@ -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) @@ -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 @@ -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; diff --git a/src/tiled/scriptdialog.h b/src/tiled/scriptdialog.h index bd2b5ef404..aadedba60b 100644 --- a/src/tiled/scriptdialog.h +++ b/src/tiled/scriptdialog.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -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(), @@ -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;