Commit 82aa98db authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Set label text and optional accessible name with one setter

This makes the API more similar to setHint().

GnuPG-bug-id: 5916
parent c2ae61e8
......@@ -54,7 +54,9 @@ public:
, mInvalidEntryErrorMessage{defaultInvalidEntryErrorMessage()}
{}
QString annotatedIfRequired(const QString &text) const;
void updateLabel();
void setLabelText(const QString &text, const QString &accessibleName);
void setHint(const QString &text, const QString &accessibleDescription);
QString errorMessage(Error error) const;
void updateError();
......@@ -75,15 +77,26 @@ public:
bool mEditingInProgress = false;
};
QString FormTextInputBase::Private::annotatedIfRequired(const QString &text) const
{
return mRequired
? i18nc("@label label text (required)", "%1 (required)", text)
: text;
}
void FormTextInputBase::Private::updateLabel()
{
if (!mLabel) {
return;
if (mLabel) {
mLabel->setText(annotatedIfRequired(mLabelText));
}
const auto text = mRequired
? i18nc("@label label text (required)", "%1 (required)", mLabelText)
: mLabelText;
mLabel->setText(text);
}
void FormTextInputBase::Private::setLabelText(const QString &text, const QString &accessibleName)
{
mLabelText = text;
mAccessibleName = accessibleName.isEmpty() ? text : accessibleName;
updateLabel();
updateAccessibleNameAndDescription();
}
void FormTextInputBase::Private::setHint(const QString &text, const QString &accessibleDescription)
......@@ -166,7 +179,7 @@ void FormTextInputBase::Private::updateAccessibleNameAndDescription()
// screen readers say something like "invalid entry" if this state is set;
// emulate this by adding "invalid entry" to the accessible name of the input field
// and its label
QString name = mAccessibleName;
QString name = annotatedIfRequired(mAccessibleName);
if (errorShown) {
name += QLatin1String{", "} + invalidEntryText();
};
......@@ -205,10 +218,9 @@ ErrorLabel *FormTextInputBase::errorLabel() const
return d->mErrorLabel;
}
void FormTextInputBase::setLabelText(const QString &text)
void FormTextInputBase::setLabelText(const QString &text, const QString &accessibleName)
{
d->mLabelText = text;
d->updateLabel();
d->setLabelText(text, accessibleName);
}
void FormTextInputBase::setHint(const QString &text, const QString &accessibleDescription)
......@@ -220,6 +232,7 @@ void FormTextInputBase::setIsRequired(bool required)
{
d->mRequired = required;
d->updateLabel();
d->updateAccessibleNameAndDescription();
}
bool FormTextInputBase::isRequired() const
......@@ -260,12 +273,6 @@ void FormTextInputBase::setToolTip(const QString &toolTip)
}
}
void FormTextInputBase::setAccessibleName(const QString &name)
{
d->mAccessibleName = name;
d->updateAccessibleNameAndDescription();
}
void FormTextInputBase::setWidget(QWidget *widget)
{
auto parent = widget ? widget->parentWidget() : nullptr;
......
......@@ -53,10 +53,13 @@ public:
ErrorLabel *errorLabel() const;
/**
* Sets the text of the label. Do not add a colon at the end of the text.
* If input is required, then the label is marked appropriately.
* Sets \p text as text of the label and \p accessibleName as alternative
* text for assistive tools. If \p accessibleName is empty, then \p text is
* used instead.
*
* Note: If input is required, then the label is annotated appropriately.
*/
void setLabelText(const QString &text);
void setLabelText(const QString &text, const QString &accessibleName = {});
/**
* Sets \p text as hint text for this input field and \p accessibleDescription
......@@ -101,12 +104,6 @@ public:
*/
void setToolTip(const QString &toolTip);
/**
* Sets the accessible name of the controlled widget. Use this function
* instead of setting the accessible name directly on the controlled widget.
*/
void setAccessibleName(const QString &name);
/**
* Enables or disables the controlled widget and its associated label.
* If the widget is disables, then the error label is hidden. Otherwise,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment