]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistviewaccessible.cpp
Merge branch 'Applications/18.08'
[dolphin.git] / src / kitemviews / kitemlistviewaccessible.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Amandeep Singh <aman.dedman@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #ifndef QT_NO_ACCESSIBILITY
21
22 #include "kitemlistviewaccessible.h"
23
24 #include "kitemlistcontainer.h"
25 #include "kitemlistcontroller.h"
26 #include "kitemlistselectionmanager.h"
27 #include "kitemlistview.h"
28 #include "private/kitemlistviewlayouter.h"
29
30 #include <QGraphicsScene>
31 #include <QGraphicsView>
32
33 KItemListView* KItemListViewAccessible::view() const
34 {
35 return qobject_cast<KItemListView*>(object());
36 }
37
38 KItemListViewAccessible::KItemListViewAccessible(KItemListView* view_) :
39 QAccessibleObject(view_)
40 {
41 Q_ASSERT(view());
42 m_cells.resize(childCount());
43 }
44
45 KItemListViewAccessible::~KItemListViewAccessible()
46 {
47 foreach (QAccessibleInterface* child, m_cells) {
48 if (child) {
49 QAccessible::Id childId = QAccessible::uniqueId(child);
50 QAccessible::deleteAccessibleInterface(childId);
51 }
52 }
53 }
54
55 void* KItemListViewAccessible::interface_cast(QAccessible::InterfaceType type)
56 {
57 if (type == QAccessible::TableInterface) {
58 return static_cast<QAccessibleTableInterface*>(this);
59 }
60 return nullptr;
61 }
62
63 void KItemListViewAccessible::modelReset()
64 {
65 }
66
67 QAccessibleInterface* KItemListViewAccessible::cell(int index) const
68 {
69 if (index < 0 || index >= view()->model()->count()) {
70 return nullptr;
71 }
72
73 if (m_cells.size() <= index) {
74 m_cells.resize(childCount());
75 }
76 Q_ASSERT(index < m_cells.size());
77
78 QAccessibleInterface* child = m_cells.at(index);
79 if (!child) {
80 child = new KItemListAccessibleCell(view(), index);
81 m_cells.insert(index, child);
82 QAccessible::registerAccessibleInterface(child);
83 }
84 return child;
85 }
86
87 QAccessibleInterface* KItemListViewAccessible::cellAt(int row, int column) const
88 {
89 return cell(columnCount() * row + column);
90 }
91
92 QAccessibleInterface* KItemListViewAccessible::caption() const
93 {
94 return nullptr;
95 }
96
97 QString KItemListViewAccessible::columnDescription(int) const
98 {
99 return QString();
100 }
101
102 int KItemListViewAccessible::columnCount() const
103 {
104 return view()->m_layouter->columnCount();
105 }
106
107 int KItemListViewAccessible::rowCount() const
108 {
109 if (columnCount() <= 0) {
110 return 0;
111 }
112
113 int itemCount = view()->model()->count();
114 int rowCount = itemCount / columnCount();
115
116 if (rowCount <= 0) {
117 return 0;
118 }
119
120 if (itemCount % columnCount()) {
121 ++rowCount;
122 }
123 return rowCount;
124 }
125
126 int KItemListViewAccessible::selectedCellCount() const
127 {
128 return view()->controller()->selectionManager()->selectedItems().count();
129 }
130
131 int KItemListViewAccessible::selectedColumnCount() const
132 {
133 return 0;
134 }
135
136 int KItemListViewAccessible::selectedRowCount() const
137 {
138 return 0;
139 }
140
141 QString KItemListViewAccessible::rowDescription(int) const
142 {
143 return QString();
144 }
145
146 QList<QAccessibleInterface*> KItemListViewAccessible::selectedCells() const
147 {
148 QList<QAccessibleInterface*> cells;
149 const auto items = view()->controller()->selectionManager()->selectedItems();
150 cells.reserve(items.count());
151 for (int index : items) {
152 cells.append(cell(index));
153 }
154 return cells;
155 }
156
157 QList<int> KItemListViewAccessible::selectedColumns() const
158 {
159 return QList<int>();
160 }
161
162 QList<int> KItemListViewAccessible::selectedRows() const
163 {
164 return QList<int>();
165 }
166
167 QAccessibleInterface* KItemListViewAccessible::summary() const
168 {
169 return nullptr;
170 }
171
172 bool KItemListViewAccessible::isColumnSelected(int) const
173 {
174 return false;
175 }
176
177 bool KItemListViewAccessible::isRowSelected(int) const
178 {
179 return false;
180 }
181
182 bool KItemListViewAccessible::selectRow(int)
183 {
184 return true;
185 }
186
187 bool KItemListViewAccessible::selectColumn(int)
188 {
189 return true;
190 }
191
192 bool KItemListViewAccessible::unselectRow(int)
193 {
194 return true;
195 }
196
197 bool KItemListViewAccessible::unselectColumn(int)
198 {
199 return true;
200 }
201
202 void KItemListViewAccessible::modelChange(QAccessibleTableModelChangeEvent* /*event*/)
203 {}
204
205 QAccessible::Role KItemListViewAccessible::role() const
206 {
207 return QAccessible::Table;
208 }
209
210 QAccessible::State KItemListViewAccessible::state() const
211 {
212 QAccessible::State s;
213 return s;
214 }
215
216 QAccessibleInterface* KItemListViewAccessible::childAt(int x, int y) const
217 {
218 const QPointF point = QPointF(x, y);
219 int itemIndex = view()->itemAt(view()->mapFromScene(point));
220 return child(itemIndex);
221 }
222
223 QAccessibleInterface* KItemListViewAccessible::parent() const
224 {
225 // FIXME: return KItemListContainerAccessible here
226 return nullptr;
227 }
228
229 int KItemListViewAccessible::childCount() const
230 {
231 return view()->model()->count();
232 }
233
234 int KItemListViewAccessible::indexOfChild(const QAccessibleInterface* interface) const
235 {
236 const KItemListAccessibleCell* widget = static_cast<const KItemListAccessibleCell*>(interface);
237 return widget->index();
238 }
239
240 QString KItemListViewAccessible::text(QAccessible::Text) const
241 {
242 return QString();
243 }
244
245 QRect KItemListViewAccessible::rect() const
246 {
247 if (!view()->isVisible()) {
248 return QRect();
249 }
250
251 const QGraphicsScene* scene = view()->scene();
252 if (scene) {
253 const QPoint origin = scene->views().at(0)->mapToGlobal(QPoint(0, 0));
254 const QRect viewRect = view()->geometry().toRect();
255 return viewRect.translated(origin);
256 } else {
257 return QRect();
258 }
259 }
260
261 QAccessibleInterface* KItemListViewAccessible::child(int index) const
262 {
263 if (index >= 0 && index < childCount()) {
264 return cell(index);
265 }
266 return nullptr;
267 }
268
269 // Table Cell
270
271 KItemListAccessibleCell::KItemListAccessibleCell(KItemListView* view, int index) :
272 m_view(view),
273 m_index(index)
274 {
275 Q_ASSERT(index >= 0 && index < view->model()->count());
276 }
277
278 void* KItemListAccessibleCell::interface_cast(QAccessible::InterfaceType type)
279 {
280 if (type == QAccessible::TableCellInterface) {
281 return static_cast<QAccessibleTableCellInterface*>(this);
282 }
283 return nullptr;
284 }
285
286 int KItemListAccessibleCell::columnExtent() const
287 {
288 return 1;
289 }
290
291 int KItemListAccessibleCell::rowExtent() const
292 {
293 return 1;
294 }
295
296 QList<QAccessibleInterface*> KItemListAccessibleCell::rowHeaderCells() const
297 {
298 return QList<QAccessibleInterface*>();
299 }
300
301 QList<QAccessibleInterface*> KItemListAccessibleCell::columnHeaderCells() const
302 {
303 return QList<QAccessibleInterface*>();
304 }
305
306 int KItemListAccessibleCell::columnIndex() const
307 {
308 return m_view->m_layouter->itemColumn(m_index);
309 }
310
311 int KItemListAccessibleCell::rowIndex() const
312 {
313 return m_view->m_layouter->itemRow(m_index);
314 }
315
316 bool KItemListAccessibleCell::isSelected() const
317 {
318 return m_view->controller()->selectionManager()->isSelected(m_index);
319 }
320
321 QAccessibleInterface* KItemListAccessibleCell::table() const
322 {
323 return QAccessible::queryAccessibleInterface(m_view);
324 }
325
326 QAccessible::Role KItemListAccessibleCell::role() const
327 {
328 return QAccessible::Cell;
329 }
330
331 QAccessible::State KItemListAccessibleCell::state() const
332 {
333 QAccessible::State state;
334
335 state.selectable = true;
336 if (isSelected()) {
337 state.selected = true;
338 }
339
340 state.focusable = true;
341 if (m_view->controller()->selectionManager()->currentItem() == m_index) {
342 state.focused = true;
343 }
344
345 if (m_view->controller()->selectionBehavior() == KItemListController::MultiSelection) {
346 state.multiSelectable = true;
347 }
348
349 if (m_view->model()->isExpandable(m_index)) {
350 if (m_view->model()->isExpanded(m_index)) {
351 state.expanded = true;
352 } else {
353 state.collapsed = true;
354 }
355 }
356
357 return state;
358 }
359
360 bool KItemListAccessibleCell::isExpandable() const
361 {
362 return m_view->model()->isExpandable(m_index);
363 }
364
365 QRect KItemListAccessibleCell::rect() const
366 {
367 QRect rect = m_view->itemRect(m_index).toRect();
368
369 if (rect.isNull()) {
370 return QRect();
371 }
372
373 rect.translate(m_view->mapToScene(QPointF(0.0, 0.0)).toPoint());
374 rect.translate(m_view->scene()->views()[0]->mapToGlobal(QPoint(0, 0)));
375 return rect;
376 }
377
378 QString KItemListAccessibleCell::text(QAccessible::Text t) const
379 {
380 switch (t) {
381 case QAccessible::Name: {
382 const QHash<QByteArray, QVariant> data = m_view->model()->data(m_index);
383 return data["text"].toString();
384 }
385
386 default:
387 break;
388 }
389
390 return QString();
391 }
392
393 void KItemListAccessibleCell::setText(QAccessible::Text, const QString&)
394 {
395 }
396
397 QAccessibleInterface* KItemListAccessibleCell::child(int) const
398 {
399 return nullptr;
400 }
401
402 bool KItemListAccessibleCell::isValid() const
403 {
404 return m_view && (m_index >= 0) && (m_index < m_view->model()->count());
405 }
406
407 QAccessibleInterface* KItemListAccessibleCell::childAt(int, int) const
408 {
409 return nullptr;
410 }
411
412 int KItemListAccessibleCell::childCount() const
413 {
414 return 0;
415 }
416
417 int KItemListAccessibleCell::indexOfChild(const QAccessibleInterface* child) const
418 {
419 Q_UNUSED(child);
420 return -1;
421 }
422
423 QAccessibleInterface* KItemListAccessibleCell::parent() const
424 {
425 return QAccessible::queryAccessibleInterface(m_view);
426 }
427
428 int KItemListAccessibleCell::index() const
429 {
430 return m_index;
431 }
432
433 QObject* KItemListAccessibleCell::object() const
434 {
435 return nullptr;
436 }
437
438 // Container Interface
439 KItemListContainerAccessible::KItemListContainerAccessible(KItemListContainer* container) :
440 QAccessibleWidget(container)
441 {
442 }
443
444 KItemListContainerAccessible::~KItemListContainerAccessible()
445 {
446 }
447
448 int KItemListContainerAccessible::childCount() const
449 {
450 return 1;
451 }
452
453 int KItemListContainerAccessible::indexOfChild(const QAccessibleInterface* child) const
454 {
455 if (child->object() == container()->controller()->view()) {
456 return 0;
457 }
458 return -1;
459 }
460
461 QAccessibleInterface* KItemListContainerAccessible::child(int index) const
462 {
463 if (index == 0) {
464 return QAccessible::queryAccessibleInterface(container()->controller()->view());
465 }
466 return nullptr;
467 }
468
469 const KItemListContainer* KItemListContainerAccessible::container() const
470 {
471 return qobject_cast<KItemListContainer*>(object());
472 }
473
474 #endif // QT_NO_ACCESSIBILITY