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