From da3422ca150ab52c1c2184cdd467ff105ba1de3c Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Fri, 23 May 2025 10:59:32 +0200 Subject: [PATCH] a11y: Introduce QAccessible::Attribute::Locale, bridge to AT-SPI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduce a new Locale value for the QAccessible::Attribute enum class that can be used to specify the locale of an accessible object. Use this to implement support for the "Locale" property of the AT-SPI Accessible interface. [1] If no locale has explicitly been specified, report the default locale in the AT-SPI adaptor, as is already the case in the implementation of the "GetLocale" method of the AT-SPI Application interface. Being able to explicitly specify a locale is of particular interest when an application contains objects whose locale differs from the default application locale, e.g. documents or paragraphs written in a different language. Demo LibreOffice change making use of this new API: [2] Sample use case with the above-mentioned LibreOffice change in place: 1) start LibreOffice Writer, using English UI and document language 2) type "Hello world" for the first paragraph 3) press Enter to create a new paragraph 4) copy-paste "你好世界" ("Hello world" in Chinese) into the new paragraph 5) start Accerciser 6) select the first paragraph in Accerciser's treeview of the LibreOffice a11y hierarchy 7) query locale in Accerciser's IPython console In [1]: acc.get_object_locale() Out[1]: 'en_GB' 8) select the second paragraph in Accerciser's treeview of the LibreOffice a11y hierarchy 9) query locale in Accerciser's IPython console In [2]: acc.get_object_locale() Out[2]: 'zh_CN' For UIA on Windows, UIA_CulturePropertyId [3] seems to be the equivalent property that the new attribute could be mapped to (to be done separately, not implemented in this commit). [1] https://gitlab.gnome.org/GNOME/at-spi2-core/-/blob/2e65b0877d67667b06e0dc6ad918a6fd4943c371/xml/Accessible.xml#L55-67 [2] https://gerrit.libreoffice.org/c/core/+/185709 [3] https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-automation-element-propids [ChangeLog][QtGui][QAccessible::Attribute] Added new Locale enum value that can be used to specify the locale of an accessible object. Fixes: QTBUG-137144 Change-Id: Ice11b9e45b512305dbb8195961b8b08e1389c69e Reviewed-by: Volker Hilsheimer --- src/gui/accessible/linux/atspiadaptor.cpp | 10 ++++++++++ src/gui/accessible/qaccessible.cpp | 6 ++++++ src/gui/accessible/qaccessible_base.h | 1 + 3 files changed, 17 insertions(+) diff --git a/src/gui/accessible/linux/atspiadaptor.cpp b/src/gui/accessible/linux/atspiadaptor.cpp index b45321add36..79fcecc7610 100644 --- a/src/gui/accessible/linux/atspiadaptor.cpp +++ b/src/gui/accessible/linux/atspiadaptor.cpp @@ -1640,6 +1640,16 @@ bool AtSpiAdaptor::accessibleInterface(QAccessibleInterface *interface, const QS } else if (function == "GetApplication"_L1) { sendReply(connection, message, QVariant::fromValue( QSpiObjectReference(connection, QDBusObjectPath(ATSPI_DBUS_PATH_ROOT)))); + } else if (function == "GetLocale"_L1) { + QLocale locale; + if (QAccessibleAttributesInterface *attributesIface = interface->attributesInterface()) { + const QVariant localeVariant = attributesIface->attributeValue(QAccessible::Attribute::Locale); + if (localeVariant.isValid()) { + Q_ASSERT(localeVariant.canConvert()); + locale = localeVariant.toLocale(); + } + } + sendReply(connection, message, QVariant::fromValue(QDBusVariant(locale.name()))); } else if (function == "GetChildren"_L1) { QSpiObjectReferenceArray children; const int numChildren = interface->childCount(); diff --git a/src/gui/accessible/qaccessible.cpp b/src/gui/accessible/qaccessible.cpp index 4ac8eafdf93..f605ada029c 100644 --- a/src/gui/accessible/qaccessible.cpp +++ b/src/gui/accessible/qaccessible.cpp @@ -450,6 +450,12 @@ Q_STATIC_LOGGING_CATEGORY(lcAccessibilityCore, "qt.accessibility.core"); Defines the hierarchical level of an element within a structure, e.g. the heading level of a heading. This attribute conceptually matches the "aria-level" property in ARIA. + \value [since 6.10] Locale value type: \a QLocale + Locale of the element. + This can be used to specify that an element has a locale that + differs from the application's default locale, e.g. for documents + or paragraphs within a document that use a language that differs + from the application's user interface language. \sa QAccessibleAttributesInterface */ diff --git a/src/gui/accessible/qaccessible_base.h b/src/gui/accessible/qaccessible_base.h index 27a0fd4f92e..9538d126ede 100644 --- a/src/gui/accessible/qaccessible_base.h +++ b/src/gui/accessible/qaccessible_base.h @@ -377,6 +377,7 @@ public: enum class Attribute { Custom, Level, + Locale, }; Q_ENUM(Attribute)