]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistviewaccessible.cpp
Improving Accessibility implementation
[dolphin.git] / src / kitemviews / kitemlistviewaccessible.cpp
1 #include "kitemlistviewaccessible.h"
2 #include "kitemlistcontroller.h"
3 #include "kitemlistselectionmanager.h"
4 #include "private/kitemlistviewlayouter.h"
5
6 #include <QtGui/qtableview.h>
7 #include <QtGui/qaccessible2.h>
8 #include <qgraphicsscene.h>
9 #include <qgraphicsview.h>
10
11 #include <KDebug>
12 #include <QHash>
13
14 #ifndef QT_NO_ACCESSIBILITY
15
16 #ifndef QT_NO_ITEMVIEWS
17
18 KItemListView *KItemListViewAccessible::view() const
19 {
20 return qobject_cast<KItemListView*>(object());
21 }
22
23 KItemListViewAccessible::KItemListViewAccessible(KItemListView *view_)
24 : QAccessibleObjectEx(view_)
25 {
26 Q_ASSERT(view());
27
28 /*if (qobject_cast<const QTableView*>(view())) {
29 m_role = QAccessible::Table;
30 } else if (qobject_cast<const QTreeView*>(view())) {
31 m_role = QAccessible::Tree;
32 } else if (qobject_cast<const QListView*>(view())) {
33 m_role = QAccessible::List;
34 } else {
35 // is this our best guess?
36 m_role = QAccessible::Table;
37 }*/
38 }
39
40 KItemListViewAccessible::~KItemListViewAccessible()
41 {
42 }
43
44 void KItemListViewAccessible::modelReset()
45 {}
46
47 QAccessibleTable2CellInterface *KItemListViewAccessible::cell(int index) const
48 {
49 if (index > 0)
50 return new KItemListAccessibleCell(view(), index);
51 return 0;
52 }
53
54 QAccessibleTable2CellInterface *KItemListViewAccessible::cellAt(int row, int column) const
55 {
56 /*Q_ASSERT(role(0) != QAccessible::Tree);
57 QModelIndex index = view()->model()->index(row, column);
58 //Q_ASSERT(index.isValid());
59 if (!index.isValid()) {
60 qWarning() << "QAccessibleTable2::cellAt: invalid index: " << index << " for " << view();
61 return 0;
62 }
63 return cell(index);*/
64 return column * (row - 1) + column ;
65 }
66
67 QAccessibleInterface *KItemListViewAccessible::caption() const
68 {
69 return 0;
70 }
71
72 QString KItemListViewAccessible::columnDescription(int) const
73 {
74 return QObject::tr("No Column Description");
75 }
76
77 int KItemListViewAccessible::columnCount() const
78 {
79 return view()->layouter()->columnCount();
80 }
81
82 int KItemListViewAccessible::rowCount() const
83 {
84 int itemCount = view()->model()->count();
85 int rowCount = itemCount / columnCount();
86 if (itemCount % rowCount)
87 ++rowCount;
88 return rowCount;
89 }
90
91 int KItemListViewAccessible::selectedCellCount() const
92 {
93 return view()->controller()->selectionManager()->selectedItems().size();
94 }
95
96 int KItemListViewAccessible::selectedColumnCount() const
97 {
98 return 0;
99 }
100
101 int KItemListViewAccessible::selectedRowCount() const
102 {
103 return 0;
104 }
105
106 QString KItemListViewAccessible::rowDescription(int) const
107 {
108 return "No Row Description";
109 }
110
111 QList<QAccessibleTable2CellInterface*> KItemListViewAccessible::selectedCells() const
112 {
113 QList<QAccessibleTable2CellInterface*> cells;
114 Q_FOREACH (int index, view()->controller()->selectionManager()->selectedItems()) {
115 cells.append(cell(index));
116 }
117 return cells;
118 }
119
120 QList<int> KItemListViewAccessible::selectedColumns() const
121 {
122 QList<int> columns;
123 /*Q_FOREACH (const QModelIndex &index, view()->selectionModel()->selectedColumns()) {
124 columns.append(index.column());
125 }*/
126 return columns;
127 }
128
129 QList<int> KItemListViewAccessible::selectedRows() const
130 {
131 QList<int> rows;
132 /*Q_FOREACH (const QModelIndex &index, view()->selectionModel()->selectedRows()) {
133 rows.append(index.row());
134 }*/
135 return rows;
136 }
137
138 QAccessibleInterface *KItemListViewAccessible::summary() const
139 {
140 return 0;
141 }
142
143 bool KItemListViewAccessible::isColumnSelected(int) const
144 {
145 return false;
146 }
147
148 bool KItemListViewAccessible::isRowSelected(int) const
149 {
150 return false;
151 }
152
153 bool KItemListViewAccessible::selectRow(int row)
154 {
155 return true;
156 }
157
158 bool KItemListViewAccessible::selectColumn(int)
159 {
160 return true;
161 }
162
163 bool KItemListViewAccessible::unselectRow(int)
164 {
165 return true;
166 }
167
168 bool KItemListViewAccessible::unselectColumn(int)
169 {
170 return true;
171 }
172
173 QAccessible2::TableModelChange KItemListViewAccessible::modelChange() const
174 {
175 QAccessible2::TableModelChange change;
176 // FIXME
177 return change;
178 }
179
180 QAccessible::Role KItemListViewAccessible::role(int child) const
181 {
182 Q_ASSERT(child >= 0);
183 if (child > 0)
184 return QAccessible::Cell;
185 return QAccessible::Table;
186 }
187
188 QAccessible::State KItemListViewAccessible::state(int child) const
189 {
190 return QAccessible::Normal | QAccessible::HasInvokeExtension;
191 }
192
193 int KItemListViewAccessible::childAt(int x, int y) const
194 {
195 QPointF point = QPointF(x,y);
196 return view()->itemAt(view()->mapFromScene(point));
197 }
198
199 int KItemListViewAccessible::childCount() const
200 {
201 return rowCount() * columnCount();
202 }
203
204 int KItemListViewAccessible::indexOfChild(const QAccessibleInterface *iface) const
205 {
206 /*Q_ASSERT(iface->role(0) != QAccessible::TreeItem); // should be handled by tree class
207 if (iface->role(0) == QAccessible::Cell || iface->role(0) == QAccessible::ListItem) {
208 const QAccessibleTable2Cell* cell = static_cast<const QAccessibleTable2Cell*>(iface);
209 return logicalIndex(cell->m_index);
210 } else if (iface->role(0) == QAccessible::ColumnHeader){
211 const QAccessibleTable2HeaderCell* cell = static_cast<const QAccessibleTable2HeaderCell*>(iface);
212 return cell->index + (verticalHeader() ? 1 : 0) + 1;
213 } else if (iface->role(0) == QAccessible::RowHeader){
214 const QAccessibleTable2HeaderCell* cell = static_cast<const QAccessibleTable2HeaderCell*>(iface);
215 return (cell->index+1) * (view()->model()->rowCount()+1) + 1;
216 } else if (iface->role(0) == QAccessible::Pane) {
217 return 1; // corner button
218 } else {
219 qWarning() << "WARNING QAccessibleTable2::indexOfChild Fix my children..."
220 << iface->role(0) << iface->text(QAccessible::Name, 0);
221 }
222 // FIXME: we are in denial of our children. this should stop.
223 return -1;*/
224
225 const KItemListAccessibleCell *widget = static_cast<const KItemListAccessibleCell*>(iface);
226 return widget->getIndex();
227 }
228
229 QString KItemListViewAccessible::text(Text t, int child) const
230 {
231 Q_ASSERT(child == 0);
232 // FIXME: I don't think this is needed, but if at all it needs i18n
233 if (t == QAccessible::Description)
234 return QObject::tr("List of files present in the current directory");
235 return QObject::tr("File List");
236 }
237
238 QRect KItemListViewAccessible::rect(int child) const
239 {
240 Q_UNUSED(child)
241 if (!view()->isVisible())
242 return QRect();
243 QPoint origin = view()->scene()->views()[0]->mapToGlobal(QPoint(0, 0));
244 QRect viewRect = view()->geometry().toRect();
245 return viewRect.translated(origin);
246 }
247
248 int KItemListViewAccessible::navigate(RelationFlag relation, int index, QAccessibleInterface **iface) const
249 {
250 *iface = 0;
251 switch (relation) {
252 /*case Ancestor: {
253 if (index == 1 && view()->parent()) {
254 *iface = QAccessible::queryAccessibleInterface(view()->parent());
255 if (*iface)
256 return 0;
257 }
258 break;
259 }*/
260 case QAccessible::Child: {
261 Q_ASSERT(index > 0);
262 *iface = cell(index);
263 if (*iface) {
264 return 0;
265 }
266 break;
267 }
268 default:
269 break;
270 }
271 return -1;
272 }
273
274 QAccessible::Relation KItemListViewAccessible::relationTo(int, const QAccessibleInterface *, int) const
275 {
276 return QAccessible::Unrelated;
277 }
278
279 #ifndef QT_NO_ACTION
280 int KItemListViewAccessible::userActionCount(int) const
281 {
282 return 0;
283 }
284 QString KItemListViewAccessible::actionText(int, Text, int) const
285 {
286 return QString();
287 }
288 bool KItemListViewAccessible::doAction(int, int, const QVariantList &)
289 {
290 return false;
291 }
292 #endif
293
294 // TABLE CELL
295
296 KItemListAccessibleCell::KItemListAccessibleCell(KItemListView *view_, int index_)
297 : /* QAccessibleSimpleEditableTextInterface(this), */ view(view_)
298 , index(index_)
299 {
300 Q_ASSERT(index_ > 0);
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 QList<QAccessibleInterface*> headerCell;
316 /*if (verticalHeader()) {
317 headerCell.append(new QAccessibleTable2HeaderCell(view, m_index.row(), Qt::Vertical));
318 }*/
319 return headerCell;
320 }
321
322 QList<QAccessibleInterface*> KItemListAccessibleCell::columnHeaderCells() const
323 {
324 QList<QAccessibleInterface*> headerCell;
325 /*if (horizontalHeader()) {
326 headerCell.append(new QAccessibleTable2HeaderCell(view, m_index.column(), Qt::Horizontal));
327 }*/
328 return headerCell;
329 }
330
331 int KItemListAccessibleCell::columnIndex() const
332 {
333 return view->layouter()->itemColumn(index);
334 }
335
336 int KItemListAccessibleCell::rowIndex() const
337 {
338 /*if (role(0) == QAccessible::TreeItem) {
339 const QTreeView *treeView = qobject_cast<const QTreeView*>(view);
340 Q_ASSERT(treeView);
341 int row = treeView->d_func()->viewIndex(m_index);
342 return row;
343 }*/
344 return view->layouter()->itemRow(index);
345 }
346
347 bool KItemListAccessibleCell::isSelected() const
348 {
349 return view->controller()->selectionManager()->isSelected(index-1);
350 }
351
352 void KItemListAccessibleCell::rowColumnExtents(int *row, int *column, int *rowExtents, int *columnExtents, bool *selected) const
353 {
354 KItemListViewLayouter* layouter = view->layouter();
355 *row = layouter->itemRow(index);
356 *column = layouter->itemColumn(index);
357 *rowExtents = 1;
358 *columnExtents = 1;
359 *selected = isSelected();
360 }
361
362 QAccessibleTable2Interface* KItemListAccessibleCell::table() const
363 {
364 return QAccessible::queryAccessibleInterface(view)->table2Interface();
365 }
366
367 QAccessible::Role KItemListAccessibleCell::role(int child) const
368 {
369 Q_ASSERT(child == 0);
370 return QAccessible::Cell;
371 }
372
373 QAccessible::State KItemListAccessibleCell::state(int child) const
374 {
375 Q_ASSERT(child == 0);
376 QAccessible::State st = Normal;
377
378 //QRect globalRect = view->rect();
379 //globalRect.translate(view->mapToGlobal(QPoint(0,0)));
380 //if (!globalRect.intersects(rect(0)))
381 // st |= Invisible;
382
383 if (view->controller()->selectionManager()->isSelected(index-1))
384 st |= Selected;
385 if (view->controller()->selectionManager()->currentItem() == index)
386 st |= Focused;
387
388 //if (m_index.model()->data(m_index, Qt::CheckStateRole).toInt() == Qt::Checked)
389 // st |= Checked;
390 //if (flags & Qt::ItemIsSelectable) {
391 st |= Selectable;
392 st |= Focusable;
393 if (view->controller()->selectionBehavior() == KItemListController::MultiSelection)
394 st |= MultiSelectable;
395
396 //if (view->selectionMode() == QAbstractItemView::ExtendedSelection)
397 //st |= ExtSelectable;
398 //}
399 //if (m_role == QAccessible::TreeItem) {
400 // const QTreeView *treeView = qobject_cast<const QTreeView*>(view);
401 // if (treeView->isExpanded(m_index))
402 // st |= Expanded;
403 //}
404
405 return st;
406 }
407
408 //Done
409 bool KItemListAccessibleCell::isExpandable() const
410 {
411 return false; //view->model()->hasChildren(m_index);
412 }
413
414 //Done
415 QRect KItemListAccessibleCell::rect(int) const
416 {
417 QRect r = view->itemRect(index).toRect();
418 if (r.isNull())
419 return QRect();
420 r.translate(view->mapToScene(QPointF(0.0, 0.0)).toPoint());
421 r.translate(view->scene()->views()[0]->mapToGlobal(QPoint(0, 0)));
422 return r;
423 }
424
425 QString KItemListAccessibleCell::text(QAccessible::Text t, int child) const
426 {
427 Q_ASSERT(child == 0);
428 Q_UNUSED(child)
429 const QHash<QByteArray, QVariant> data = view->model()->data(index-1);
430 switch (t) {
431 case QAccessible::Value:
432 case QAccessible::Name:
433 return data["text"].toString();
434 case QAccessible::Description:
435 return data["text"].toString() + " : " + data["group"].toString();
436 default:
437 break;
438 }
439 return QString();
440 }
441
442 void KItemListAccessibleCell::setText(QAccessible::Text /*t*/, int child, const QString &text)
443 {
444 Q_ASSERT(child == 0);
445 // FIXME - is this even allowed on the KItemListWidget?
446 }
447
448 bool KItemListAccessibleCell::isValid() const
449 {
450 return view && (index > 0);
451 }
452
453 int KItemListAccessibleCell::navigate(RelationFlag relation, int index, QAccessibleInterface **iface) const
454 {
455 if (relation == Ancestor && index == 1) {
456 //if (m_role == QAccessible::TreeItem) {
457 // *iface = new QAccessibleTree(view);
458 //} else {
459 *iface = new KItemListViewAccessible(view);
460 return 0;
461 }
462
463 *iface = 0;
464 if (!view)
465 return -1;
466
467 switch (relation) {
468
469 case Child: {
470 return -1;
471 }
472 case Sibling:
473 if (index > 0) {
474 QAccessibleInterface *parent = queryAccessibleInterface(view);
475 int ret = parent->navigate(QAccessible::Child, index, iface);
476 delete parent;
477 if (*iface)
478 return ret;
479 }
480 return -1;
481 default:
482 break;
483 }
484
485 return -1;
486 }
487
488 QAccessible::Relation KItemListAccessibleCell::relationTo(int child, const QAccessibleInterface *, int otherChild) const
489 {
490 Q_ASSERT(child == 0);
491 Q_ASSERT(otherChild == 0);
492 /* we only check for parent-child relationships in trees
493 if (m_role == QAccessible::TreeItem && other->role(0) == QAccessible::TreeItem) {
494 QModelIndex otherIndex = static_cast<const QAccessibleTable2Cell*>(other)->m_index;
495 // is the other our parent?
496 if (otherIndex.parent() == m_index)
497 return QAccessible::Ancestor;
498 // are we the other's child?
499 if (m_index.parent() == otherIndex)
500 return QAccessible::Child;
501 }*/
502 return QAccessible::Unrelated;
503 }
504
505 #ifndef QT_NO_ACTION
506 int KItemListAccessibleCell::userActionCount(int) const
507 {
508 return 0;
509 }
510
511 QString KItemListAccessibleCell::actionText(int, Text, int) const
512 {
513 return QString();
514 }
515
516 bool KItemListAccessibleCell::doAction(int, int, const QVariantList &)
517 {
518 return false;
519 }
520
521 #endif
522
523 KItemListContainerAccessible::KItemListContainerAccessible(KItemListContainer *container)
524 : QAccessibleWidgetEx(container)
525 {}
526
527 KItemListContainerAccessible::~KItemListContainerAccessible ()
528 {}
529
530 int KItemListContainerAccessible::childCount () const
531 {
532 return 1;
533 }
534
535 int KItemListContainerAccessible::indexOfChild ( const QAccessibleInterface * child ) const
536 {
537 if(child->object() == container()->controller()->view())
538 return 1;
539 return -1;
540 }
541
542 int KItemListContainerAccessible::navigate ( QAccessible::RelationFlag relation, int index, QAccessibleInterface ** target ) const
543 {
544 if (relation == QAccessible::Child) {
545 *target = new KItemListViewAccessible(container()->controller()->view());
546 return 0;
547 }
548 return QAccessibleWidgetEx::navigate(relation, index, target);
549 }
550
551 #endif // QT_NO_ITEMVIEWS
552
553 #endif // QT_NO_ACCESSIBILITY