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