Commit 45e703a5 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Add helper to give labels keyboard focus if an assistive tool is active

If assistive tools are active, then certain labels need to be focusable,
to make the text of those labels accessible. The LabelHelper takes care
of setting the appropriate focus policy for the associated labels.

GnuPG-bug-id: 5916
parent b2d2d278
......@@ -13,9 +13,13 @@
#include <KLocalizedString>
#include <QAccessible>
#include <QLabel>
#include <QObject>
#include <algorithm>
using namespace Kleo;
namespace
{
QString getAccessibleText(QObject *object, QAccessible::Text t)
......@@ -51,3 +55,31 @@ QString Kleo::requiredText()
"such as a form field must be filled out",
"required");
}
LabelHelper::LabelHelper()
{
QAccessible::installActivationObserver(this);
}
LabelHelper::~LabelHelper()
{
QAccessible::removeActivationObserver(this);
}
void LabelHelper::addLabel(QLabel *label)
{
mLabels.push_back(label);
accessibilityActiveChanged(QAccessible::isActive());
}
void LabelHelper::accessibilityActiveChanged(bool active)
{
// Allow text labels to get focus if accessibility is active
const auto focusPolicy = active ? Qt::StrongFocus : Qt::ClickFocus;
std::for_each(std::cbegin(mLabels), std::cend(mLabels),
[focusPolicy](const auto &label) {
if (label) {
label->setFocusPolicy(focusPolicy);
}
});
}
......@@ -8,6 +8,10 @@
*/
#pragma once
#include <QAccessible>
#include <QPointer>
class QLabel;
class QObject;
class QString;
......@@ -17,4 +21,23 @@ namespace Kleo
QString getAccessibleDescription(QObject *object);
QString invalidEntryText();
QString requiredText();
/**
* Simple helper that sets the focus policy of the associated labels
* to \c Qt::StrongFocus if an assistive tool is active.
*/
class LabelHelper: public QAccessible::ActivationObserver
{
public:
LabelHelper();
~LabelHelper() override;
Q_DISABLE_COPY_MOVE(LabelHelper)
void addLabel(QLabel *label);
private:
void accessibilityActiveChanged(bool active) override;
std::vector<QPointer<QLabel>> mLabels;
};
}
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