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