]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistviewaccessible.cpp
Remove some dubious code for now (crash less)
[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 // FIXME: I don't think this is needed, but if at all it needs i18n
260 if (t == QAccessible::Description)
261 return "List of files present in the current directory";
262 return "File List";
263 }
264
265 QRect KItemListViewAccessible::rect(int child) const
266 {
267 Q_UNUSED(child)
268 if (!view()->isVisible())
269 return QRect();
270
271 // FIXME: map to global
272 return view()->geometry().toRect();
273 }
274
275 int KItemListViewAccessible::navigate(RelationFlag relation, int index, QAccessibleInterface **iface) const
276 {
277 *iface = 0;
278 switch (relation) {
279 /*case Ancestor: {
280 if (index == 1 && view()->parent()) {
281 *iface = QAccessible::queryAccessibleInterface(view()->parent());
282 if (*iface)
283 return 0;
284 }
285 break;
286 }*/
287 case QAccessible::Child: {
288 Q_ASSERT(index > 0);
289 *iface = cell(index);
290 if (*iface) {
291 return 0;
292 }
293 break;
294 }
295 default:
296 break;
297 }
298 return -1;
299 }
300
301 QAccessible::Relation KItemListViewAccessible::relationTo(int, const QAccessibleInterface *, int) const
302 {
303 return QAccessible::Unrelated;
304 }
305
306 #ifndef QT_NO_ACTION
307 int KItemListViewAccessible::userActionCount(int) const
308 {
309 return 0;
310 }
311 QString KItemListViewAccessible::actionText(int, Text, int) const
312 {
313 return QString();
314 }
315 bool KItemListViewAccessible::doAction(int, int, const QVariantList &)
316 {
317 return false;
318 }
319 #endif
320
321 // TABLE CELL
322
323 KItemListWidgetAccessible::KItemListWidgetAccessible(KItemListView *view_, int index_)
324 : /* QAccessibleSimpleEditableTextInterface(this), */ view(view_), index(index_)
325 {
326 Q_ASSERT(index_>0);
327 }
328
329 int KItemListWidgetAccessible::columnExtent() const { return 1; }
330 int KItemListWidgetAccessible::rowExtent() const { return 1; }
331
332 QList<QAccessibleInterface*> KItemListWidgetAccessible::rowHeaderCells() const
333 {
334 QList<QAccessibleInterface*> headerCell;
335 /*if (verticalHeader()) {
336 headerCell.append(new QAccessibleTable2HeaderCell(view, m_index.row(), Qt::Vertical));
337 }*/
338 return headerCell;
339 }
340
341 QList<QAccessibleInterface*> KItemListWidgetAccessible::columnHeaderCells() const
342 {
343 QList<QAccessibleInterface*> headerCell;
344 /*if (horizontalHeader()) {
345 headerCell.append(new QAccessibleTable2HeaderCell(view, m_index.column(), Qt::Horizontal));
346 }*/
347 return headerCell;
348 }
349
350 int KItemListWidgetAccessible::columnIndex() const
351 {
352 return view->layouter()->itemColumn(index);
353 }
354
355 int KItemListWidgetAccessible::rowIndex() const
356 {
357 /*if (role(0) == QAccessible::TreeItem) {
358 const QTreeView *treeView = qobject_cast<const QTreeView*>(view);
359 Q_ASSERT(treeView);
360 int row = treeView->d_func()->viewIndex(m_index);
361 return row;
362 }*/
363 return view->layouter()->itemRow(index);
364 }
365
366 //Done
367 bool KItemListWidgetAccessible::isSelected() const
368 {
369 //return widget->isSelected();
370 // FIXME
371 return false;
372 }
373
374 void KItemListWidgetAccessible::rowColumnExtents(int *row, int *column, int *rowExtents, int *columnExtents, bool *selected) const
375 {
376 KItemListViewLayouter* layouter = view->layouter();
377 *row = layouter->itemRow(index);
378 *column = layouter->itemColumn(index);
379 *rowExtents = 1;
380 *columnExtents = 1;
381 *selected = isSelected();
382 }
383
384 QAccessibleTable2Interface* KItemListWidgetAccessible::table() const
385 {
386 return QAccessible::queryAccessibleInterface(view)->table2Interface();
387 }
388
389 QAccessible::Role KItemListWidgetAccessible::role(int child) const
390 {
391 Q_ASSERT(child == 0);
392 return QAccessible::Cell;
393 }
394
395 QAccessible::State KItemListWidgetAccessible::state(int child) const
396 {
397 Q_ASSERT(child == 0);
398 QAccessible::State st = Normal;
399
400 //QRect globalRect = view->rect();
401 //globalRect.translate(view->mapToGlobal(QPoint(0,0)));
402 //if (!globalRect.intersects(rect(0)))
403 // st |= Invisible;
404
405 // if (widget->isSelected())
406 // st |= Selected;
407 if (view->controller()->selectionManager()->currentItem() == index)
408 st |= Focused;
409
410 //if (m_index.model()->data(m_index, Qt::CheckStateRole).toInt() == Qt::Checked)
411 // st |= Checked;
412 //if (flags & Qt::ItemIsSelectable) {
413 st |= Selectable;
414 st |= Focusable;
415 if (view->controller()->selectionBehavior() == KItemListController::MultiSelection)
416 st |= MultiSelectable;
417
418 //if (view->selectionMode() == QAbstractItemView::ExtendedSelection)
419 //st |= ExtSelectable;
420 //}
421 //if (m_role == QAccessible::TreeItem) {
422 // const QTreeView *treeView = qobject_cast<const QTreeView*>(view);
423 // if (treeView->isExpanded(m_index))
424 // st |= Expanded;
425 //}
426
427 st |= HasInvokeExtension;
428 return st;
429 }
430
431 //Done
432 bool KItemListWidgetAccessible::isExpandable() const
433 {
434 return false; //view->model()->hasChildren(m_index);
435 }
436
437 //Done
438 QRect KItemListWidgetAccessible::rect(int child) const
439 {
440 // Q_ASSERT(child == 0);
441 return QRect();
442
443 // FIXME
444
445 //QRect r;
446 //r = view->visualRect(m_index);
447
448 //if (!r.isNull())
449 // r.translate(view->viewport()->mapTo(view, QPoint(0,0)));
450 // r.translate(view->mapToGlobal(QPoint(0, 0)));
451 //return widget->textRect().toRect();
452 }
453
454 //Done
455 QString KItemListWidgetAccessible::text(Text t, int child) const
456 {
457 Q_ASSERT(child == 0);
458
459 // switch (t) {
460 // case QAccessible::Value:
461 // case QAccessible::Name:
462 // //return data["text"].toString();
463 // case QAccessible::Description:
464 // //return data["text"].toString() + " : " + data["group"].toString();
465 // default:
466 // break;
467 // }
468 return QString();
469 }
470
471 //Done
472 void KItemListWidgetAccessible::setText(QAccessible::Text /*t*/, int child, const QString &text)
473 {
474 Q_ASSERT(child == 0);
475 // FIXME - is this even allowed on the KItemListWidget?
476 }
477
478 //Done
479 bool KItemListWidgetAccessible::isValid() const
480 {
481 if (index <= 0) {
482 kDebug() << "Interface is not valid";
483 }
484
485 return index > 0;
486 }
487
488 int KItemListWidgetAccessible::navigate(RelationFlag relation, int index, QAccessibleInterface **iface) const
489 {
490 if (relation == Ancestor && index == 1) {
491 //if (m_role == QAccessible::TreeItem) {
492 // *iface = new QAccessibleTree(view);
493 //} else {
494 *iface = new KItemListViewAccessible(view);
495 return 0;
496 }
497
498 *iface = 0;
499 if (!view)
500 return -1;
501
502 switch (relation) {
503
504 case Child: {
505 return -1;
506 }
507 case Sibling:
508 if (index > 0) {
509 QAccessibleInterface *parent = queryAccessibleInterface(view);
510 int ret = parent->navigate(QAccessible::Child, index, iface);
511 delete parent;
512 if (*iface)
513 return ret;
514 }
515 return -1;
516
517 // From table1 implementation:
518 // case Up:
519 // case Down:
520 // case Left:
521 // case Right: {
522 // // This is in the "not so nice" category. In order to find out which item
523 // // is geometrically around, we have to set the current index, navigate
524 // // and restore the index as well as the old selection
525 // view()->setUpdatesEnabled(false);
526 // const QModelIndex oldIdx = view()->currentIndex();
527 // QList<QModelIndex> kids = children();
528 // const QModelIndex currentIndex = index ? kids.at(index - 1) : QModelIndex(row);
529 // const QItemSelection oldSelection = view()->selectionModel()->selection();
530 // view()->setCurrentIndex(currentIndex);
531 // const QModelIndex idx = view()->moveCursor(toCursorAction(relation), Qt::NoModifier);
532 // view()->setCurrentIndex(oldIdx);
533 // view()->selectionModel()->select(oldSelection, QItemSelectionModel::ClearAndSelect);
534 // view()->setUpdatesEnabled(true);
535 // if (!idx.isValid())
536 // return -1;
537
538 // if (idx.parent() != row.parent() || idx.row() != row.row())
539 // *iface = cell(idx);
540 // return index ? kids.indexOf(idx) + 1 : 0; }
541 default:
542 break;
543 }
544
545 return -1;
546 }
547
548 QAccessible::Relation KItemListWidgetAccessible::relationTo(int child, const QAccessibleInterface *, int otherChild) const
549 {
550 Q_ASSERT(child == 0);
551 Q_ASSERT(otherChild == 0);
552 /* we only check for parent-child relationships in trees
553 if (m_role == QAccessible::TreeItem && other->role(0) == QAccessible::TreeItem) {
554 QModelIndex otherIndex = static_cast<const QAccessibleTable2Cell*>(other)->m_index;
555 // is the other our parent?
556 if (otherIndex.parent() == m_index)
557 return QAccessible::Ancestor;
558 // are we the other's child?
559 if (m_index.parent() == otherIndex)
560 return QAccessible::Child;
561 }*/
562 return QAccessible::Unrelated;
563 }
564
565 #ifndef QT_NO_ACTION
566 int KItemListWidgetAccessible::userActionCount(int) const
567 {
568 return 0;
569 }
570
571 QString KItemListWidgetAccessible::actionText(int, Text, int) const
572 {
573 return QString();
574 }
575
576 bool KItemListWidgetAccessible::doAction(int, int, const QVariantList &)
577 {
578 return false;
579 }
580
581 #endif
582
583 KItemListContainerAccessible::KItemListContainerAccessible(KItemListContainer *container)
584 : QAccessibleWidgetEx(container)
585 {}
586
587 KItemListContainerAccessible::~KItemListContainerAccessible ()
588 {}
589
590 int KItemListContainerAccessible::childCount () const
591 {
592 return 1;
593 }
594
595 int KItemListContainerAccessible::indexOfChild ( const QAccessibleInterface * child ) const
596 {
597 if(child == QAccessible::queryAccessibleInterface(container()->controller()->view()))
598 return 1;
599 return -1;
600 }
601
602 int KItemListContainerAccessible::navigate ( QAccessible::RelationFlag relation, int index, QAccessibleInterface ** target ) const
603 {
604 if (relation == QAccessible::Child) {
605 *target = new KItemListViewAccessible(container()->controller()->view());
606 return 0;
607 }
608 return QAccessibleWidgetEx::navigate(relation, index, target);
609 }
610
611 #endif // QT_NO_ITEMVIEWS
612
613 #endif // QT_NO_ACCESSIBILITY