File indexing completed on 2024-06-23 05:14:14

0001 /*  utils/accessibility.h
0002 
0003     This file is part of Kleopatra, the KDE keymanager
0004     SPDX-FileCopyrightText: 2022 g10 Code GmbH
0005     SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #pragma once
0010 
0011 #include <QAccessible>
0012 #include <QPointer>
0013 
0014 class QAction;
0015 class QLabel;
0016 class QObject;
0017 class QString;
0018 
0019 namespace Kleo
0020 {
0021 QString getAccessibleName(QWidget *widget);
0022 QString getAccessibleDescription(QWidget *widget);
0023 
0024 /**
0025  * Sets the accessible name of the action \p action.
0026  *
0027  * \note Qt does not provide an accessible object for a QAction. Therefore,
0028  *       we store the accessible name as custom property of the action.
0029  * \sa getAccessibleName
0030  */
0031 void setAccessibleName(QAction *action, const QString &name);
0032 /**
0033  * Returns the accessible name of the action \p action.
0034  * \sa setAccessibleName
0035  */
0036 QString getAccessibleName(const QAction *action);
0037 
0038 /**
0039  * Sets \p value as accessible value of \p widget.
0040  *
0041  * Stores the string \p value as custom property of the widget \p widget
0042  * for retrieval by a QAccessibleWidget.
0043  *
0044  * \sa getAccessibleValue
0045  */
0046 void setAccessibleValue(QWidget *widget, const QString &value);
0047 /**
0048  * Returns the accessible value of \p widget.
0049  *
0050  * \sa setAccessibleValue
0051  */
0052 QString getAccessibleValue(const QWidget *widget);
0053 
0054 /**
0055  * Mark \p widget as being represented as AccessibleValueWidget.
0056  *
0057  * This is useful, if you want Windows UI Automation to treat the widget
0058  * as labelled value, i.e. a custom widget with a value and a name.
0059  *
0060  * \note: Don't use this on other platforms than Windows, unless you made
0061  *        sure that it works as expected.
0062  * \sa representAsAccessibleValueWidget
0063  */
0064 void setRepresentAsAccessibleValueWidget(QWidget *widget, bool);
0065 /**
0066  * Returns whether \p widget is marked as being represented as
0067  * AccessibleValueWidget.
0068  *
0069  * \sa setRepresentAsAccessibleValueWidget
0070  */
0071 bool representAsAccessibleValueWidget(const QWidget *widget);
0072 
0073 QString invalidEntryText();
0074 QString requiredText();
0075 
0076 /**
0077  * Selects the text displayed by the label. Only \ref QLabel with text format
0078  * \c Qt::PlainText or \c Qt::RichText are supported.
0079  */
0080 void selectLabelText(QLabel *label);
0081 
0082 /**
0083  * Shows \p text as a tool tip, with the global position \p pos as the point of interest.
0084  * Additionally to QToolTip::showText, it takes care of notifying accessibility clients
0085  * about the tool tip.
0086  * \sa QToolTip::showText
0087  */
0088 void showToolTip(const QPoint &pos, const QString &text, QWidget *w);
0089 
0090 /**
0091  * Simple helper that sets the focus policy of the associated labels
0092  * to \c Qt::StrongFocus if an assistive tool is active.
0093  */
0094 class LabelHelper : public QAccessible::ActivationObserver
0095 {
0096 public:
0097     LabelHelper();
0098     ~LabelHelper() override;
0099     Q_DISABLE_COPY_MOVE(LabelHelper)
0100 
0101     void addLabel(QLabel *label);
0102 
0103 private:
0104     void accessibilityActiveChanged(bool active) override;
0105 
0106     std::vector<QPointer<QLabel>> mLabels;
0107 };
0108 }