Skip to content

Commit

Permalink
add optional tooltips for radio buttons. add individual radioButtonGr…
Browse files Browse the repository at this point in the history
…oup.addItem(value, tooltip?) method for adding a single radio button
  • Loading branch information
dogboydog committed Nov 28, 2024
1 parent a85c90b commit c513e24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/tiled/scriptdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,16 @@ ScriptDialog::NewRowMode ScriptDialog::newRowMode() const
return m_newRowMode;
}

ScriptButtonGroup *ScriptDialog::addRadioButtonGroup(const QString &labelText, const QStringList &values, const QString &toolTip)
ScriptButtonGroup *ScriptDialog::addRadioButtonGroup(const QString &labelText, const QStringList &values, const QString &toolTip, const QStringList &buttonToolTips)
{
QGroupBox *groupParent = new QGroupBox(this);
groupParent->setFlat(true);
QHBoxLayout *hBox = new QHBoxLayout(groupParent);
ScriptButtonGroup *buttonGroup = new ScriptButtonGroup(groupParent, hBox);
buttonGroup->addItems(values);
buttonGroup->addItems(values, buttonToolTips);
hBox->addStretch(rightColumnStretch);
groupParent->setLayout(hBox);
if (!toolTip.isEmpty())
groupParent->setToolTip(toolTip);
addDialogWidget(groupParent, labelText);
addDialogWidget(groupParent, labelText, toolTip);
return buttonGroup;
}

Expand All @@ -293,7 +291,7 @@ int ScriptDialog::exec()
return QDialog::exec();
}

QWidget *ScriptDialog::addDialogWidget(QWidget *widget, const QString &label)
QWidget *ScriptDialog::addDialogWidget(QWidget *widget, const QString &label, const QString &labelToolTip)
{
determineWidgetGrouping(widget);
if (m_widgetsInRow == 0)
Expand All @@ -307,6 +305,8 @@ QWidget *ScriptDialog::addDialogWidget(QWidget *widget, const QString &label)

if (!label.isEmpty()) {
QLabel *widgetLabel = newLabel(label);
if (!labelToolTip.isEmpty())
widgetLabel->setToolTip(labelToolTip);
widgetLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
widgetLabel->setBuddy(widget);
m_rowLayout->addWidget(widgetLabel);
Expand Down
30 changes: 21 additions & 9 deletions src/tiled/scriptdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,30 @@ class ScriptButtonGroup : public QButtonGroup
mLayout(layout)
{}

Q_INVOKABLE void addItems(const QStringList &values)
Q_INVOKABLE void addItems(const QStringList &values, const QStringList &toolTips)
{
int nextIndex = QButtonGroup::buttons().length();
int toolTipIndex = 0;
for (const QString &value : values) {
QRadioButton *radioButton = new QRadioButton;
mLayout->addWidget(radioButton);
radioButton->setText(value);
QButtonGroup::addButton(radioButton, nextIndex);
nextIndex++;

QString toolTip = toolTipIndex < toolTips.count() ?
toolTips.at(toolTipIndex) : QString();
addItem(value, toolTip);
toolTipIndex++;
}
}

Q_INVOKABLE QAbstractButton *addItem(const QString &value, const QString &toolTip = QString())
{
int nextIndex = QButtonGroup::buttons().length();
QRadioButton *radioButton = new QRadioButton;
mLayout->addWidget(radioButton);
radioButton->setText(value);
if (!toolTip.isEmpty())
radioButton->setToolTip(toolTip);
QButtonGroup::addButton(radioButton, nextIndex);
return radioButton;
}

QList<QAbstractButton *> buttons() const
{
return QButtonGroup::buttons();
Expand Down Expand Up @@ -126,7 +138,7 @@ class ScriptDialog : public QDialog
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 ScriptButtonGroup *addRadioButtonGroup(const QString &labelText, const QStringList &values, const QString &toolTip);
Q_INVOKABLE ScriptButtonGroup *addRadioButtonGroup(const QString &labelText, const QStringList &values, const QString &toolTip, const QStringList &buttonToolTips);
Q_INVOKABLE void clear();
Q_INVOKABLE void addNewRow();

Expand All @@ -141,7 +153,7 @@ class ScriptDialog : public QDialog
QLabel *newLabel(const QString &labelText);
void initializeLayout();
void determineWidgetGrouping(QWidget *widget);
QWidget *addDialogWidget(QWidget * widget, const QString &label = QString());
QWidget *addDialogWidget(QWidget * widget, const QString &label = QString(), const QString &labelToolTip = QString());

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

0 comments on commit c513e24

Please sign in to comment.