| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | |
| 30 | #ifndef ACCESSIBLEWIDGETS_H |
| 31 | #define ACCESSIBLEWIDGETS_H |
| 32 | |
| 33 | #include <QtWidgets/qaccessiblewidget.h> |
| 34 | #include <QtWidgets/qpushbutton.h> |
| 35 | |
| 36 | class QtTestAccessibleWidget: public QWidget |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | public: |
| 40 | QtTestAccessibleWidget(QWidget *parent, const char *name): QWidget(parent) |
| 41 | { |
| 42 | setObjectName(name); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | class QtTestAccessibleWidgetIface: public QAccessibleWidget |
| 47 | { |
| 48 | public: |
| 49 | QtTestAccessibleWidgetIface(QtTestAccessibleWidget *w): QAccessibleWidget(w) {} |
| 50 | QString text(QAccessible::Text t) const override |
| 51 | { |
| 52 | if (t == QAccessible::Help) |
| 53 | return QString::fromLatin1(str: "Help yourself" ); |
| 54 | return QAccessibleWidget::text(t); |
| 55 | } |
| 56 | static QAccessibleInterface *ifaceFactory(const QString &key, QObject *o) |
| 57 | { |
| 58 | if (key == "QtTestAccessibleWidget" ) |
| 59 | return new QtTestAccessibleWidgetIface(static_cast<QtTestAccessibleWidget*>(o)); |
| 60 | return 0; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | class QtTestAccessibleWidgetSubclass: public QtTestAccessibleWidget |
| 65 | { |
| 66 | Q_OBJECT |
| 67 | public: |
| 68 | QtTestAccessibleWidgetSubclass(QWidget *parent, const char *name): QtTestAccessibleWidget(parent, name) |
| 69 | {} |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | class KFooButton: public QPushButton |
| 74 | { |
| 75 | Q_OBJECT |
| 76 | public: |
| 77 | KFooButton(const QString &text, QWidget* parent = 0) |
| 78 | : QPushButton(text, parent) |
| 79 | {} |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | class CustomTextWidget : public QWidget |
| 84 | { |
| 85 | Q_OBJECT |
| 86 | public: |
| 87 | int cursorPosition; |
| 88 | QString text; |
| 89 | }; |
| 90 | |
| 91 | class CustomTextWidgetIface: public QAccessibleWidget, public QAccessibleTextInterface |
| 92 | { |
| 93 | public: |
| 94 | static QAccessibleInterface *ifaceFactory(const QString &key, QObject *o) |
| 95 | { |
| 96 | if (key == "CustomTextWidget" ) |
| 97 | return new CustomTextWidgetIface(static_cast<CustomTextWidget*>(o)); |
| 98 | return 0; |
| 99 | } |
| 100 | CustomTextWidgetIface(CustomTextWidget *w): QAccessibleWidget(w) {} |
| 101 | void *interface_cast(QAccessible::InterfaceType t) override |
| 102 | { |
| 103 | if (t == QAccessible::TextInterface) |
| 104 | return static_cast<QAccessibleTextInterface*>(this); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | // this is mostly to test the base implementation for textBefore/At/After |
| 109 | QString text(QAccessible::Text t) const override |
| 110 | { |
| 111 | if (t == QAccessible::Value) |
| 112 | return textWidget()->text; |
| 113 | return QAccessibleWidget::text(t); |
| 114 | } |
| 115 | |
| 116 | QString textBeforeOffset(int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset) const override |
| 117 | { |
| 118 | if (offset == -2) |
| 119 | offset = textWidget()->cursorPosition; |
| 120 | return QAccessibleTextInterface::textBeforeOffset(offset, boundaryType, startOffset, endOffset); |
| 121 | } |
| 122 | QString textAtOffset(int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset) const override |
| 123 | { |
| 124 | if (offset == -2) |
| 125 | offset = textWidget()->cursorPosition; |
| 126 | return QAccessibleTextInterface::textAtOffset(offset, boundaryType, startOffset, endOffset); |
| 127 | } |
| 128 | QString textAfterOffset(int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset) const override |
| 129 | { |
| 130 | if (offset == -2) |
| 131 | offset = textWidget()->cursorPosition; |
| 132 | return QAccessibleTextInterface::textAfterOffset(offset, boundaryType, startOffset, endOffset); |
| 133 | } |
| 134 | |
| 135 | void selection(int, int *startOffset, int *endOffset) const override |
| 136 | { *startOffset = *endOffset = -1; } |
| 137 | int selectionCount() const override { return 0; } |
| 138 | void addSelection(int, int) override {} |
| 139 | void removeSelection(int) override {} |
| 140 | void setSelection(int, int, int) override {} |
| 141 | int cursorPosition() const override { return textWidget()->cursorPosition; } |
| 142 | void setCursorPosition(int position) override { textWidget()->cursorPosition = position; } |
| 143 | QString text(int startOffset, int endOffset) const override { return textWidget()->text.mid(position: startOffset, n: endOffset); } |
| 144 | int characterCount() const override { return textWidget()->text.length(); } |
| 145 | QRect characterRect(int) const override { return QRect(); } |
| 146 | int offsetAtPoint(const QPoint &) const override { return 0; } |
| 147 | void scrollToSubstring(int, int) override {} |
| 148 | QString attributes(int, int *, int *) const override |
| 149 | { return QString(); } |
| 150 | |
| 151 | private: |
| 152 | CustomTextWidget *textWidget() const { return qobject_cast<CustomTextWidget *>(object: widget()); } |
| 153 | }; |
| 154 | |
| 155 | #endif // ACCESSIBLEWIDGETS_H |
| 156 | |