1// Copyright (C) 2016 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
4#ifndef ACCESSIBLE_ITEMVIEWS_H
5#define ACCESSIBLE_ITEMVIEWS_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtWidgets/private/qtwidgetsglobal_p.h>
19#include "QtCore/qpointer.h"
20#include <QtGui/qaccessible.h>
21#include <QtWidgets/qaccessiblewidget.h>
22#include <QtWidgets/qabstractitemview.h>
23#include <QtWidgets/qheaderview.h>
24
25QT_REQUIRE_CONFIG(itemviews);
26
27QT_BEGIN_NAMESPACE
28
29#if QT_CONFIG(accessibility)
30
31class QAccessibleTableCell;
32class QAccessibleTableHeaderCell;
33
34class QAccessibleTable :public QAccessibleTableInterface, public QAccessibleSelectionInterface, public QAccessibleObject
35{
36public:
37 explicit QAccessibleTable(QWidget *w);
38 bool isValid() const override;
39
40 QAccessible::Role role() const override;
41 QAccessible::State state() const override;
42 QString text(QAccessible::Text t) const override;
43 QRect rect() const override;
44
45 QAccessibleInterface *childAt(int x, int y) const override;
46 QAccessibleInterface *focusChild() const override;
47 int childCount() const override;
48 int indexOfChild(const QAccessibleInterface *) const override;
49
50 QAccessibleInterface *parent() const override;
51 QAccessibleInterface *child(int index) const override;
52
53 void *interface_cast(QAccessible::InterfaceType t) override;
54
55 // table interface
56 virtual QAccessibleInterface *cellAt(int row, int column) const override;
57 virtual QAccessibleInterface *caption() const override;
58 virtual QAccessibleInterface *summary() const override;
59 virtual QString columnDescription(int column) const override;
60 virtual QString rowDescription(int row) const override;
61 virtual int columnCount() const override;
62 virtual int rowCount() const override;
63
64 // selection
65 virtual int selectedCellCount() const override;
66 virtual int selectedColumnCount() const override;
67 virtual int selectedRowCount() const override;
68 virtual QList<QAccessibleInterface*> selectedCells() const override;
69 virtual QList<int> selectedColumns() const override;
70 virtual QList<int> selectedRows() const override;
71 virtual bool isColumnSelected(int column) const override;
72 virtual bool isRowSelected(int row) const override;
73 virtual bool selectRow(int row) override;
74 virtual bool selectColumn(int column) override;
75 virtual bool unselectRow(int row) override;
76 virtual bool unselectColumn(int column) override;
77
78 // QAccessibleSelectionInterface
79 virtual int selectedItemCount() const override;
80 virtual QList<QAccessibleInterface*> selectedItems() const override;
81 virtual bool isSelected(QAccessibleInterface *childCell) const override;
82 virtual bool select(QAccessibleInterface *childCell) override;
83 virtual bool unselect(QAccessibleInterface *childCell) override;
84 virtual bool selectAll() override;
85 virtual bool clear() override;
86
87 QAbstractItemView *view() const;
88
89 void modelChange(QAccessibleTableModelChangeEvent *event) override;
90
91protected:
92 inline QAccessible::Role cellRole() const {
93 switch (m_role) {
94 case QAccessible::List:
95 return QAccessible::ListItem;
96 case QAccessible::Table:
97 return QAccessible::Cell;
98 case QAccessible::Tree:
99 return QAccessible::TreeItem;
100 default:
101 Q_ASSERT(0);
102 }
103 return QAccessible::NoRole;
104 }
105
106 QHeaderView *horizontalHeader() const;
107 QHeaderView *verticalHeader() const;
108
109 // maybe vector
110 typedef QHash<int, QAccessible::Id> ChildCache;
111 mutable ChildCache childToId;
112
113 virtual ~QAccessibleTable();
114
115private:
116 // the child index for a model index
117 inline int logicalIndex(const QModelIndex &index) const;
118 QAccessible::Role m_role;
119};
120
121#if QT_CONFIG(treeview)
122class QAccessibleTree :public QAccessibleTable
123{
124public:
125 explicit QAccessibleTree(QWidget *w)
126 : QAccessibleTable(w)
127 {}
128
129
130 QAccessibleInterface *childAt(int x, int y) const override;
131 QAccessibleInterface *focusChild() const override;
132 int childCount() const override;
133 QAccessibleInterface *child(int index) const override;
134
135 int indexOfChild(const QAccessibleInterface *) const override;
136
137 int rowCount() const override;
138
139 // table interface
140 QAccessibleInterface *cellAt(int row, int column) const override;
141 QString rowDescription(int row) const override;
142 bool isRowSelected(int row) const override;
143 bool selectRow(int row) override;
144
145private:
146 QModelIndex indexFromLogical(int row, int column = 0) const;
147
148 inline int logicalIndex(const QModelIndex &index) const;
149};
150#endif
151
152class QAccessibleTableCell: public QAccessibleInterface, public QAccessibleTableCellInterface, public QAccessibleActionInterface
153{
154public:
155 QAccessibleTableCell(QAbstractItemView *view, const QModelIndex &m_index, QAccessible::Role role);
156
157 void *interface_cast(QAccessible::InterfaceType t) override;
158 QObject *object() const override { return nullptr; }
159 QAccessible::Role role() const override;
160 QAccessible::State state() const override;
161 QRect rect() const override;
162 bool isValid() const override;
163
164 QAccessibleInterface *childAt(int, int) const override { return nullptr; }
165 int childCount() const override { return 0; }
166 int indexOfChild(const QAccessibleInterface *) const override { return -1; }
167
168 QString text(QAccessible::Text t) const override;
169 void setText(QAccessible::Text t, const QString &text) override;
170
171 QAccessibleInterface *parent() const override;
172 QAccessibleInterface *child(int) const override;
173
174 // cell interface
175 virtual int columnExtent() const override;
176 virtual QList<QAccessibleInterface*> columnHeaderCells() const override;
177 virtual int columnIndex() const override;
178 virtual int rowExtent() const override;
179 virtual QList<QAccessibleInterface*> rowHeaderCells() const override;
180 virtual int rowIndex() const override;
181 virtual bool isSelected() const override;
182 virtual QAccessibleInterface* table() const override;
183
184 //action interface
185 virtual QStringList actionNames() const override;
186 virtual void doAction(const QString &actionName) override;
187 virtual QStringList keyBindingsForAction(const QString &actionName) const override;
188
189private:
190 QHeaderView *verticalHeader() const;
191 QHeaderView *horizontalHeader() const;
192 QPointer<QAbstractItemView > view;
193 QPersistentModelIndex m_index;
194 QAccessible::Role m_role;
195
196 void selectCell();
197 void unselectCell();
198
199friend class QAccessibleTable;
200#if QT_CONFIG(treeview)
201friend class QAccessibleTree;
202#endif
203};
204
205
206class QAccessibleTableHeaderCell: public QAccessibleInterface
207{
208public:
209 // For header cells, pass the header view in addition
210 QAccessibleTableHeaderCell(QAbstractItemView *view, int index, Qt::Orientation orientation);
211
212 QObject *object() const override { return nullptr; }
213 QAccessible::Role role() const override;
214 QAccessible::State state() const override;
215 QRect rect() const override;
216 bool isValid() const override;
217
218 QAccessibleInterface *childAt(int, int) const override { return nullptr; }
219 int childCount() const override { return 0; }
220 int indexOfChild(const QAccessibleInterface *) const override { return -1; }
221
222 QString text(QAccessible::Text t) const override;
223 void setText(QAccessible::Text t, const QString &text) override;
224
225 QAccessibleInterface *parent() const override;
226 QAccessibleInterface *child(int index) const override;
227
228private:
229 QHeaderView *headerView() const;
230
231 QPointer<QAbstractItemView> view;
232 int index;
233 Qt::Orientation orientation;
234
235friend class QAccessibleTable;
236#if QT_CONFIG(treeview)
237friend class QAccessibleTree;
238#endif
239};
240
241// This is the corner button on the top left of a table.
242// It can be used to select all cells or it is not active at all.
243// For now it is ignored.
244class QAccessibleTableCornerButton: public QAccessibleInterface
245{
246public:
247 QAccessibleTableCornerButton(QAbstractItemView *view_)
248 :view(view_)
249 {}
250
251 QObject *object() const override { return nullptr; }
252 QAccessible::Role role() const override { return QAccessible::Pane; }
253 QAccessible::State state() const override { return QAccessible::State(); }
254 QRect rect() const override { return QRect(); }
255 bool isValid() const override { return true; }
256
257 QAccessibleInterface *childAt(int, int) const override { return nullptr; }
258 int childCount() const override { return 0; }
259 int indexOfChild(const QAccessibleInterface *) const override { return -1; }
260
261 QString text(QAccessible::Text) const override { return QString(); }
262 void setText(QAccessible::Text, const QString &) override {}
263
264 QAccessibleInterface *parent() const override {
265 return QAccessible::queryAccessibleInterface(view);
266 }
267 QAccessibleInterface *child(int) const override {
268 return nullptr;
269 }
270
271private:
272 QPointer<QAbstractItemView> view;
273};
274
275
276#endif // QT_CONFIG(accessibility)
277
278QT_END_NAMESPACE
279
280#endif // ACCESSIBLE_ITEMVIEWS_H
281

source code of qtbase/src/widgets/accessible/itemviews_p.h