Skip to content

Commit

Permalink
Merge pull request #1488 from ghutchis/add-text-type-for-scripts
Browse files Browse the repository at this point in the history
Add a "text" option for scripts to add text labels / help
  • Loading branch information
ghutchis authored Dec 2, 2023
2 parents 9898a5c + 65d3144 commit 219b47d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions avogadro/qtgui/jsonwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ QWidget* JsonWidget::createOptionWidget(const QJsonValue& option)
return createFloatWidget(obj);
else if (type == QLatin1String("boolean"))
return createBooleanWidget(obj);
else if (type == QLatin1String("text"))
return createTextWidget(obj);

qDebug() << "Unrecognized option type:" << type;
return nullptr;
Expand Down Expand Up @@ -398,6 +400,19 @@ QWidget* JsonWidget::createStringWidget(const QJsonObject& obj)
return edit;
}

QWidget* JsonWidget::createTextWidget(const QJsonObject& obj)
{
auto* text = new QLabel(this);
text->setWordWrap(true);

if (obj.contains(QStringLiteral("toolTip")) &&
obj.value(QStringLiteral("toolTip")).isString()) {
text->setToolTip(obj[QStringLiteral("toolTip")].toString());
}

return text;
}

QWidget* JsonWidget::createFilePathWidget(const QJsonObject& obj)
{
auto* fileBrowse = new QtGui::FileBrowseWidget(this);
Expand Down Expand Up @@ -551,6 +566,8 @@ void JsonWidget::setOption(const QString& name, const QJsonValue& defaultValue)
return setFloatOption(name, defaultValue);
else if (type == QLatin1String("boolean"))
return setBooleanOption(name, defaultValue);
else if (type == QLatin1String("text"))
return setTextOption(name, defaultValue);

qWarning()
<< tr("Unrecognized option type '%1' for option '%2'.").arg(type).arg(name);
Expand Down Expand Up @@ -614,6 +631,27 @@ void JsonWidget::setStringOption(const QString& name, const QJsonValue& value)
lineEdit->setText(value.toString());
}

void JsonWidget::setTextOption(const QString& name, const QJsonValue& value)
{
auto* text = qobject_cast<QLabel*>(m_widgets.value(name, nullptr));
if (text == nullptr) {
qWarning() << tr("Error setting default for option '%1'. "
"Bad widget type.")
.arg(name);
return;
}

if (!value.isString()) {
qWarning() << tr("Error setting default for option '%1'. "
"Bad default value:")
.arg(name)
<< value;
return;
}

text->setText(value.toString());
}

void JsonWidget::setFilePathOption(const QString& name, const QJsonValue& value)
{
auto* fileBrowse =
Expand Down
2 changes: 2 additions & 0 deletions avogadro/qtgui/jsonwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class AVOGADROQTGUI_EXPORT JsonWidget : public QWidget
QWidget* createIntegerWidget(const QJsonObject& obj);
QWidget* createFloatWidget(const QJsonObject& obj);
QWidget* createBooleanWidget(const QJsonObject& obj);
QWidget* createTextWidget(const QJsonObject& obj);
/**@}*/

/**
Expand All @@ -106,6 +107,7 @@ class AVOGADROQTGUI_EXPORT JsonWidget : public QWidget
void setIntegerOption(const QString& name, const QJsonValue& value);
void setFloatOption(const QString& name, const QJsonValue& value);
void setBooleanOption(const QString& name, const QJsonValue& value);
void setTextOption(const QString& name, const QJsonValue& value);
/**@}*/

/**
Expand Down

0 comments on commit 219b47d

Please sign in to comment.