Commit 7d9c716e authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Allow setting an accessible description for a form input

This adds setters for changing the accessible name and description of
the controlled widget. Setting those values directly on the controlled
widget doesn't work because we change those values internally in case
of a invalid entry.

GnuPG-bug-id: 5916
parent 0f7c2314
......@@ -16,14 +16,27 @@
#include <QAccessible>
#include <QObject>
QString Kleo::getAccessibleName(QObject *object)
namespace
{
QString getAccessibleText(QObject *object, QAccessible::Text t)
{
QString name;
if (const auto *const iface = QAccessible::queryAccessibleInterface(object)) {
name = iface->text(QAccessible::Name);
name = iface->text(t);
}
return name;
}
}
QString Kleo::getAccessibleName(QObject *object)
{
return getAccessibleText(object, QAccessible::Name);
}
QString Kleo::getAccessibleDescription(QObject *object)
{
return getAccessibleText(object, QAccessible::Description);
}
QString Kleo::invalidEntryText()
{
......
......@@ -14,5 +14,6 @@ class QString;
namespace Kleo
{
QString getAccessibleName(QObject *object);
QString getAccessibleDescription(QObject *object);
QString invalidEntryText();
}
......@@ -42,6 +42,7 @@ public:
QPointer<ErrorLabel> mErrorLabel;
QPointer<const QValidator> mValidator;
QString mAccessibleName;
QString mAccessibleDescription;
QString mErrorMessage;
bool mEditingInProgress = false;
};
......@@ -77,15 +78,19 @@ void FormTextInputBase::Private::updateError()
void FormTextInputBase::Private::updateAccessibleNameAndDescription()
{
// fall back to default accessible name if accessible name wasn't set explicitly
// fall back to default accessible name/description if accessible name/description wasn't set explicitly
if (mAccessibleName.isEmpty()) {
mAccessibleName = getAccessibleName(mWidget);
}
if (mAccessibleDescription.isEmpty()) {
mAccessibleDescription = getAccessibleDescription(mWidget);
}
const bool errorShown = mErrorLabel && mErrorLabel->isVisible();
// Qt does not support "described-by" relations (like WCAG's "aria-describedby" relationship attribute);
// emulate this by setting the error message as accessible description of the input field
const auto description = errorShown ? mErrorLabel->text() : QString{};
// emulate this by adding the error message to the accessible description of the input field
const auto description = errorShown ? mAccessibleDescription + QLatin1String{" "} + mErrorLabel->text()
: mAccessibleDescription;
if (mWidget && mWidget->accessibleDescription() != description) {
mWidget->setAccessibleDescription(description);
}
......@@ -150,6 +155,18 @@ void FormTextInputBase::setToolTip(const QString &toolTip)
}
}
void FormTextInputBase::setAccessibleName(const QString &name)
{
d->mAccessibleName = name;
d->updateAccessibleNameAndDescription();
}
void FormTextInputBase::setAccessibleDescription(const QString &description)
{
d->mAccessibleDescription = description;
d->updateAccessibleNameAndDescription();
}
void FormTextInputBase::setWidget(QWidget *widget)
{
auto parent = widget ? widget->parentWidget() : nullptr;
......
......@@ -63,6 +63,19 @@ 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);
/**
* Sets the accessible description of the controlled widget. Use this
* function instead of setting the accessible description directly on
* the controlled widget.
*/
void setAccessibleDescription(const QString &description);
/**
* 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