| 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 | |
| 25 | QT_REQUIRE_CONFIG(itemviews); |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | #if QT_CONFIG(accessibility) |
| 30 | |
| 31 | class QAccessibleTableCell; |
| 32 | class QAccessibleTableHeaderCell; |
| 33 | |
| 34 | class QAccessibleTable :public QAccessibleTableInterface, public QAccessibleSelectionInterface, public QAccessibleObject |
| 35 | { |
| 36 | public: |
| 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 | |
| 91 | protected: |
| 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 *() const; |
| 107 | QHeaderView *() const; |
| 108 | |
| 109 | // maybe vector |
| 110 | typedef QHash<int, QAccessible::Id> ChildCache; |
| 111 | mutable ChildCache childToId; |
| 112 | |
| 113 | virtual ~QAccessibleTable(); |
| 114 | |
| 115 | private: |
| 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) |
| 122 | class QAccessibleTree :public QAccessibleTable |
| 123 | { |
| 124 | public: |
| 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 | |
| 145 | private: |
| 146 | QModelIndex indexFromLogical(int row, int column = 0) const; |
| 147 | }; |
| 148 | #endif |
| 149 | |
| 150 | #if QT_CONFIG(listview) |
| 151 | class QAccessibleList :public QAccessibleTable |
| 152 | { |
| 153 | public: |
| 154 | explicit QAccessibleList(QWidget *w) |
| 155 | : QAccessibleTable(w) |
| 156 | {} |
| 157 | |
| 158 | QAccessibleInterface *child(int index) const override; |
| 159 | |
| 160 | // table interface |
| 161 | QAccessibleInterface *cellAt(int row, int column) const override; |
| 162 | |
| 163 | // selection |
| 164 | int selectedCellCount() const override; |
| 165 | QList<QAccessibleInterface*> selectedCells() const override; |
| 166 | }; |
| 167 | #endif |
| 168 | |
| 169 | class QAccessibleTableCell: public QAccessibleInterface, public QAccessibleTableCellInterface, public QAccessibleActionInterface |
| 170 | { |
| 171 | public: |
| 172 | QAccessibleTableCell(QAbstractItemView *view, const QModelIndex &m_index, QAccessible::Role role); |
| 173 | |
| 174 | void *interface_cast(QAccessible::InterfaceType t) override; |
| 175 | QObject *object() const override { return nullptr; } |
| 176 | QAccessible::Role role() const override; |
| 177 | QAccessible::State state() const override; |
| 178 | QRect rect() const override; |
| 179 | bool isValid() const override; |
| 180 | |
| 181 | QAccessibleInterface *childAt(int, int) const override { return nullptr; } |
| 182 | int childCount() const override { return 0; } |
| 183 | int indexOfChild(const QAccessibleInterface *) const override { return -1; } |
| 184 | |
| 185 | QString text(QAccessible::Text t) const override; |
| 186 | void setText(QAccessible::Text t, const QString &text) override; |
| 187 | |
| 188 | QAccessibleInterface *parent() const override; |
| 189 | QAccessibleInterface *child(int) const override; |
| 190 | |
| 191 | // cell interface |
| 192 | virtual int columnExtent() const override; |
| 193 | virtual QList<QAccessibleInterface*> columnHeaderCells() const override; |
| 194 | virtual int columnIndex() const override; |
| 195 | virtual int rowExtent() const override; |
| 196 | virtual QList<QAccessibleInterface*> () const override; |
| 197 | virtual int rowIndex() const override; |
| 198 | virtual bool isSelected() const override; |
| 199 | virtual QAccessibleInterface* table() const override; |
| 200 | |
| 201 | //action interface |
| 202 | virtual QStringList actionNames() const override; |
| 203 | virtual void doAction(const QString &actionName) override; |
| 204 | virtual QStringList keyBindingsForAction(const QString &actionName) const override; |
| 205 | |
| 206 | private: |
| 207 | QHeaderView *() const; |
| 208 | QHeaderView *() const; |
| 209 | QPointer<QAbstractItemView > view; |
| 210 | QPersistentModelIndex m_index; |
| 211 | QAccessible::Role m_role; |
| 212 | |
| 213 | void selectCell(); |
| 214 | void unselectCell(); |
| 215 | |
| 216 | friend class QAccessibleTable; |
| 217 | #if QT_CONFIG(treeview) |
| 218 | friend class QAccessibleTree; |
| 219 | #endif |
| 220 | #if QT_CONFIG(listview) |
| 221 | friend class QAccessibleList; |
| 222 | #endif |
| 223 | }; |
| 224 | |
| 225 | |
| 226 | class : public QAccessibleInterface |
| 227 | { |
| 228 | public: |
| 229 | // For header cells, pass the header view in addition |
| 230 | (QAbstractItemView *view, int index, Qt::Orientation orientation); |
| 231 | |
| 232 | QObject *() const override { return nullptr; } |
| 233 | QAccessible::Role () const override; |
| 234 | QAccessible::State () const override; |
| 235 | QRect () const override; |
| 236 | bool () const override; |
| 237 | |
| 238 | QAccessibleInterface *(int, int) const override { return nullptr; } |
| 239 | int () const override { return 0; } |
| 240 | int (const QAccessibleInterface *) const override { return -1; } |
| 241 | |
| 242 | QString (QAccessible::Text t) const override; |
| 243 | void (QAccessible::Text t, const QString &text) override; |
| 244 | |
| 245 | QAccessibleInterface *() const override; |
| 246 | QAccessibleInterface *(int index) const override; |
| 247 | |
| 248 | private: |
| 249 | QHeaderView *() const; |
| 250 | |
| 251 | QPointer<QAbstractItemView> ; |
| 252 | int ; |
| 253 | Qt::Orientation ; |
| 254 | |
| 255 | friend class QAccessibleTable; |
| 256 | #if QT_CONFIG(treeview) |
| 257 | friend class QAccessibleTree; |
| 258 | #endif |
| 259 | #if QT_CONFIG(listview) |
| 260 | friend class QAccessibleList; |
| 261 | #endif |
| 262 | }; |
| 263 | |
| 264 | // This is the corner button on the top left of a table. |
| 265 | // It can be used to select all cells or it is not active at all. |
| 266 | // For now it is ignored. |
| 267 | class QAccessibleTableCornerButton: public QAccessibleInterface |
| 268 | { |
| 269 | public: |
| 270 | QAccessibleTableCornerButton(QAbstractItemView *view_) |
| 271 | :view(view_) |
| 272 | {} |
| 273 | |
| 274 | QObject *object() const override { return nullptr; } |
| 275 | QAccessible::Role role() const override { return QAccessible::Pane; } |
| 276 | QAccessible::State state() const override { return QAccessible::State(); } |
| 277 | QRect rect() const override { return QRect(); } |
| 278 | bool isValid() const override { return true; } |
| 279 | |
| 280 | QAccessibleInterface *childAt(int, int) const override { return nullptr; } |
| 281 | int childCount() const override { return 0; } |
| 282 | int indexOfChild(const QAccessibleInterface *) const override { return -1; } |
| 283 | |
| 284 | QString text(QAccessible::Text) const override { return QString(); } |
| 285 | void setText(QAccessible::Text, const QString &) override {} |
| 286 | |
| 287 | QAccessibleInterface *parent() const override { |
| 288 | return QAccessible::queryAccessibleInterface(view); |
| 289 | } |
| 290 | QAccessibleInterface *child(int) const override { |
| 291 | return nullptr; |
| 292 | } |
| 293 | |
| 294 | private: |
| 295 | QPointer<QAbstractItemView> view; |
| 296 | }; |
| 297 | |
| 298 | |
| 299 | #endif // QT_CONFIG(accessibility) |
| 300 | |
| 301 | QT_END_NAMESPACE |
| 302 | |
| 303 | #endif // ACCESSIBLE_ITEMVIEWS_H |
| 304 | |