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