Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qaccessiblehelper.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtGui/qtextcursor.h>
7
9
10using namespace Qt::StringLiterals;
11
12/* This function will return the offset of the '&' in the text that would be
13 preceding the accelerator character.
14 If this text does not have an accelerator, -1 will be returned. */
15static qsizetype qt_accAmpIndex(const QString &text)
16{
17#ifndef QT_NO_SHORTCUT
18 if (text.isEmpty())
19 return -1;
20
21 qsizetype fa = 0;
22 while ((fa = text.indexOf(u'&', fa)) != -1) {
23 ++fa;
24 if (fa < text.size()) {
25 // ignore "&&"
26 if (text.at(fa) == u'&') {
27 ++fa;
28 continue;
29 } else {
30 return fa - 1;
31 break;
32 }
33 }
34 }
35
36 return -1;
37#else
38 Q_UNUSED(text);
39 return -1;
40#endif
41}
42
43QString qt_accStripAmp(const QString &text)
44{
45 QString newText(text);
46 qsizetype ampIndex = qt_accAmpIndex(newText);
47 if (ampIndex != -1)
48 newText.remove(ampIndex, 1);
49
50 return newText.replace("&&"_L1, "&"_L1);
51}
52
53QString qt_accTextBeforeOffsetHelper(const QAccessibleTextInterface &textInterface,
54 const QTextCursor &textCursor, int offset,
55 QAccessible::TextBoundaryType boundaryType, int *startOffset,
56 int *endOffset)
57{
58 Q_ASSERT(startOffset);
59 Q_ASSERT(endOffset);
60 *startOffset = *endOffset = -1;
61
62 QTextCursor cursor = textCursor;
63 cursor.setPosition(offset);
64 std::pair<int, int> boundaries =
65 QAccessible::qAccessibleTextBoundaryHelper(cursor, boundaryType);
66 if (boundaries.second > offset) {
67 cursor.setPosition(boundaries.first);
68 while (boundaries.second > offset) {
69 if (!cursor.movePosition(QTextCursor::PreviousCharacter))
70 return QString();
71 boundaries = QAccessible::qAccessibleTextBoundaryHelper(cursor, boundaryType);
72 }
73 }
74
75 *startOffset = boundaries.first;
76 *endOffset = boundaries.second;
77
78 return textInterface.text(boundaries.first, boundaries.second);
79}
80
81QString qt_accTextAfterOffsetHelper(const QAccessibleTextInterface &textInterface,
82 const QTextCursor &textCursor, int offset,
83 QAccessible::TextBoundaryType boundaryType, int *startOffset,
84 int *endOffset)
85{
86 Q_ASSERT(startOffset);
87 Q_ASSERT(endOffset);
88 *startOffset = *endOffset = -1;
89
90 QTextCursor cursor = textCursor;
91 cursor.setPosition(offset);
92 std::pair<int, int> boundaries =
93 QAccessible::qAccessibleTextBoundaryHelper(cursor, boundaryType);
94 if (boundaries.first <= offset) {
95 cursor.setPosition(boundaries.second);
96 while (boundaries.first <= offset) {
97 if (!cursor.movePosition(QTextCursor::NextCharacter))
98 return QString();
99 boundaries = QAccessible::qAccessibleTextBoundaryHelper(cursor, boundaryType);
100 }
101 }
102
103 *startOffset = boundaries.first;
104 *endOffset = boundaries.second;
105
106 return textInterface.text(boundaries.first, boundaries.second);
107}
108
109QString qt_accTextAtOffsetHelper(const QAccessibleTextInterface &textInterface,
110 const QTextCursor &textCursor, int offset,
111 QAccessible::TextBoundaryType boundaryType, int *startOffset,
112 int *endOffset)
113{
114 Q_ASSERT(startOffset);
115 Q_ASSERT(endOffset);
116
117 QTextCursor cursor = textCursor;
118 cursor.setPosition(offset);
119 std::pair<int, int> boundaries =
120 QAccessible::qAccessibleTextBoundaryHelper(cursor, boundaryType);
121
122 *startOffset = boundaries.first;
123 *endOffset = boundaries.second;
124
125 return textInterface.text(boundaries.first, boundaries.second);
126}
127
128QT_END_NAMESPACE
Combined button and popup list for selecting options.
QString qt_accStripAmp(const QString &text)
QString qt_accTextAfterOffsetHelper(const QAccessibleTextInterface &textInterface, const QTextCursor &textCursor, int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset)
QString qt_accTextAtOffsetHelper(const QAccessibleTextInterface &textInterface, const QTextCursor &textCursor, int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset)
QString qt_accTextBeforeOffsetHelper(const QAccessibleTextInterface &textInterface, const QTextCursor &textCursor, int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset)
static qsizetype qt_accAmpIndex(const QString &text)