]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistviewaccessible.cpp
Minor fixes to accessible interfaces
[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 cell(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 "";
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 "";
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 if(child){
191 QAccessibleInterface *iface;
192 navigate(Child,child,&iface);
193 if(iface)
194 return iface->state(0);
195 }
196 return QAccessible::Normal | QAccessible::HasInvokeExtension;
197 }
198
199 int KItemListViewAccessible::childAt(int x, int y) const
200 {
201 QPointF point = QPointF(x,y);
202 return view()->itemAt(view()->mapFromScene(point));
203 }
204
205 int KItemListViewAccessible::childCount() const
206 {
207 return rowCount() * columnCount();
208 }
209
210 int KItemListViewAccessible::indexOfChild(const QAccessibleInterface *iface) const
211 {
212 /*Q_ASSERT(iface->role(0) != QAccessible::TreeItem); // should be handled by tree class
213 if (iface->role(0) == QAccessible::Cell || iface->role(0) == QAccessible::ListItem) {
214 const QAccessibleTable2Cell* cell = static_cast<const QAccessibleTable2Cell*>(iface);
215 return logicalIndex(cell->m_index);
216 } else if (iface->role(0) == QAccessible::ColumnHeader){
217 const QAccessibleTable2HeaderCell* cell = static_cast<const QAccessibleTable2HeaderCell*>(iface);
218 return cell->index + (verticalHeader() ? 1 : 0) + 1;
219 } else if (iface->role(0) == QAccessible::RowHeader){
220 const QAccessibleTable2HeaderCell* cell = static_cast<const QAccessibleTable2HeaderCell*>(iface);
221 return (cell->index+1) * (view()->model()->rowCount()+1) + 1;
222 } else if (iface->role(0) == QAccessible::Pane) {
223 return 1; // corner button
224 } else {
225 qWarning() << "WARNING QAccessibleTable2::indexOfChild Fix my children..."
226 << iface->role(0) << iface->text(QAccessible::Name, 0);
227 }
228 // FIXME: we are in denial of our children. this should stop.
229 return -1;*/
230
231 const KItemListAccessibleCell *widget = static_cast<const KItemListAccessibleCell*>(iface);
232 return widget->getIndex();
233 }
234
235 QString KItemListViewAccessible::text(Text t, int child) const
236 {
237 Q_ASSERT(child == 0);
238 // FIXME: I don't think this is needed, but if at all it needs i18n
239 if (t == QAccessible::Description)
240 return QObject::tr("List of files present in the current directory");
241 return QObject::tr("File List");
242 }
243
244 QRect KItemListViewAccessible::rect(int child) const
245 {
246 Q_UNUSED(child)
247 if (!view()->isVisible())
248 return QRect();
249 QPoint origin = view()->scene()->views()[0]->mapToGlobal(QPoint(0, 0));
250 QRect viewRect = view()->geometry().toRect();
251 return viewRect.translated(origin);
252 }
253
254 int KItemListViewAccessible::navigate(RelationFlag relation, int index, QAccessibleInterface **iface) const
255 {
256 *iface = 0;
257 switch (relation) {
258 /*case Ancestor: {
259 if (index == 1 && view()->parent()) {
260 *iface = QAccessible::queryAccessibleInterface(view()->parent());
261 if (*iface)
262 return 0;
263 }
264 break;
265 }*/
266 case QAccessible::Child: {
267 Q_ASSERT(index > 0);
268 *iface = cell(index);
269 if (*iface) {
270 return 0;
271 }
272 break;
273 }
274 default:
275 break;
276 }
277 return -1;
278 }
279
280 QAccessible::Relation KItemListViewAccessible::relationTo(int, const QAccessibleInterface *, int) const
281 {
282 return QAccessible::Unrelated;
283 }
284
285 #ifndef QT_NO_ACTION
286 int KItemListViewAccessible::userActionCount(int) const
287 {
288 return 0;
289 }
290 QString KItemListViewAccessible::actionText(int, Text, int) const
291 {
292 return QString();
293 }
294 bool KItemListViewAccessible::doAction(int, int, const QVariantList &)
295 {
296 return false;
297 }
298 #endif
299
300 // TABLE CELL
301
302 KItemListAccessibleCell::KItemListAccessibleCell(KItemListView *view_, int index_)
303 : /* QAccessibleSimpleEditableTextInterface(this), */ view(view_)
304 , index(index_)
305 {
306 Q_ASSERT(index_ > 0);
307 }
308
309 int KItemListAccessibleCell::columnExtent() const
310 {
311 return 1;
312 }
313
314 int KItemListAccessibleCell::rowExtent() const
315 {
316 return 1;
317 }
318
319 QList<QAccessibleInterface*> KItemListAccessibleCell::rowHeaderCells() const
320 {
321 QList<QAccessibleInterface*> headerCell;
322 /*if (verticalHeader()) {
323 headerCell.append(new QAccessibleTable2HeaderCell(view, m_index.row(), Qt::Vertical));
324 }*/
325 return headerCell;
326 }
327
328 QList<QAccessibleInterface*> KItemListAccessibleCell::columnHeaderCells() const
329 {
330 QList<QAccessibleInterface*> headerCell;
331 /*if (horizontalHeader()) {
332 headerCell.append(new QAccessibleTable2HeaderCell(view, m_index.column(), Qt::Horizontal));
333 }*/
334 return headerCell;
335 }
336
337 int KItemListAccessibleCell::columnIndex() const
338 {
339 return view->layouter()->itemColumn(index);
340 }
341
342 int KItemListAccessibleCell::rowIndex() const
343 {
344 /*if (role(0) == QAccessible::TreeItem) {
345 const QTreeView *treeView = qobject_cast<const QTreeView*>(view);
346 Q_ASSERT(treeView);
347 int row = treeView->d_func()->viewIndex(m_index);
348 return row;
349 }*/
350 return view->layouter()->itemRow(index);
351 }
352
353 bool KItemListAccessibleCell::isSelected() const
354 {
355 return view->controller()->selectionManager()->isSelected(index-1);
356 }
357
358 void KItemListAccessibleCell::rowColumnExtents(int *row, int *column, int *rowExtents, int *columnExtents, bool *selected) const
359 {
360 KItemListViewLayouter* layouter = view->layouter();
361 *row = layouter->itemRow(index);
362 *column = layouter->itemColumn(index);
363 *rowExtents = 1;
364 *columnExtents = 1;
365 *selected = isSelected();
366 }
367
368 QAccessibleTable2Interface* KItemListAccessibleCell::table() const
369 {
370 return QAccessible::queryAccessibleInterface(view)->table2Interface();
371 }
372
373 QAccessible::Role KItemListAccessibleCell::role(int child) const
374 {
375 Q_ASSERT(child == 0);
376 return QAccessible::Cell;
377 }
378
379 QAccessible::State KItemListAccessibleCell::state(int child) const
380 {
381 Q_ASSERT(child == 0);
382 QAccessible::State st = Normal;
383
384 //QRect globalRect = view->rect();
385 //globalRect.translate(view->mapToGlobal(QPoint(0,0)));
386 //if (!globalRect.intersects(rect(0)))
387 // st |= Invisible;
388
389 if (isSelected())
390 st |= Selected;
391 if (view->controller()->selectionManager()->currentItem() == index)
392 st |= Focused;
393
394 //if (m_index.model()->data(m_index, Qt::CheckStateRole).toInt() == Qt::Checked)
395 // st |= Checked;
396 //if (flags & Qt::ItemIsSelectable) {
397 st |= Selectable;
398 st |= Focusable;
399 if (view->controller()->selectionBehavior() == KItemListController::MultiSelection)
400 st |= MultiSelectable;
401
402 //if (view->selectionMode() == QAbstractItemView::ExtendedSelection)
403 //st |= ExtSelectable;
404 //}
405 //if (m_role == QAccessible::TreeItem) {
406 // const QTreeView *treeView = qobject_cast<const QTreeView*>(view);
407 // if (treeView->isExpanded(m_index))
408 // st |= Expanded;
409 //}
410
411 return st;
412 }
413
414 bool KItemListAccessibleCell::isExpandable() const
415 {
416 return false; //view->model()->hasChildren(m_index);
417 }
418
419 QRect KItemListAccessibleCell::rect(int) const
420 {
421 QRect r = view->itemRect(index-1).toRect();
422 if (r.isNull())
423 return QRect();
424 r.translate(view->mapToScene(QPointF(0.0, 0.0)).toPoint());
425 r.translate(view->scene()->views()[0]->mapToGlobal(QPoint(0, 0)));
426 return r;
427 }
428
429 QString KItemListAccessibleCell::text(QAccessible::Text t, int child) const
430 {
431 Q_ASSERT(child == 0);
432 Q_UNUSED(child)
433 const QHash<QByteArray, QVariant> data = view->model()->data(index-1);
434 switch (t) {
435 case QAccessible::Value:
436 case QAccessible::Name:
437 return data["text"].toString();
438 //case QAccessible::Description:
439 //return data["text"].toString() + " : " + data["group"].toString();
440 default:
441 break;
442 }
443 return QString();
444 }
445
446 void KItemListAccessibleCell::setText(QAccessible::Text /*t*/, int child, const QString &/*text*/)
447 {
448 Q_ASSERT(child == 0);
449 // FIXME - is this even allowed on the KItemListWidget?
450 }
451
452 bool KItemListAccessibleCell::isValid() const
453 {
454 return view && (index > 0);
455 }
456
457 int KItemListAccessibleCell::navigate(RelationFlag relation, int index, QAccessibleInterface **iface) const
458 {
459 if (relation == Ancestor && index == 1) {
460 //if (m_role == QAccessible::TreeItem) {
461 // *iface = new QAccessibleTree(view);
462 //} else {
463 *iface = new KItemListViewAccessible(view);
464 return 0;
465 }
466
467 *iface = 0;
468 if (!view)
469 return -1;
470
471 switch (relation) {
472
473 case Child: {
474 return -1;
475 }
476 case Sibling:
477 if (index > 0) {
478 QAccessibleInterface *parent = queryAccessibleInterface(view);
479 int ret = parent->navigate(QAccessible::Child, index, iface);
480 delete parent;
481 if (*iface)
482 return ret;
483 }
484 return -1;
485 default:
486 break;
487 }
488
489 return -1;
490 }
491
492 QAccessible::Relation KItemListAccessibleCell::relationTo(int child, const QAccessibleInterface *, int otherChild) const
493 {
494 Q_ASSERT(child == 0);
495 Q_ASSERT(otherChild == 0);
496 /* we only check for parent-child relationships in trees
497 if (m_role == QAccessible::TreeItem && other->role(0) == QAccessible::TreeItem) {
498 QModelIndex otherIndex = static_cast<const QAccessibleTable2Cell*>(other)->m_index;
499 // is the other our parent?
500 if (otherIndex.parent() == m_index)
501 return QAccessible::Ancestor;
502 // are we the other's child?
503 if (m_index.parent() == otherIndex)
504 return QAccessible::Child;
505 }*/
506 return QAccessible::Unrelated;
507 }
508
509 #ifndef QT_NO_ACTION
510 int KItemListAccessibleCell::userActionCount(int) const
511 {
512 return 0;
513 }
514
515 QString KItemListAccessibleCell::actionText(int, Text, int) const
516 {
517 return QString();
518 }
519
520 bool KItemListAccessibleCell::doAction(int, int, const QVariantList &)
521 {
522 return false;
523 }
524
525 #endif
526
527 KItemListContainerAccessible::KItemListContainerAccessible(KItemListContainer *container)
528 : QAccessibleWidgetEx(container)
529 {}
530
531 KItemListContainerAccessible::~KItemListContainerAccessible ()
532 {}
533
534 int KItemListContainerAccessible::childCount () const
535 {
536 return 1;
537 }
538
539 int KItemListContainerAccessible::indexOfChild ( const QAccessibleInterface * child ) const
540 {
541 if(child->object() == container()->controller()->view())
542 return 1;
543 return -1;
544 }
545
546 int KItemListContainerAccessible::navigate ( QAccessible::RelationFlag relation, int index, QAccessibleInterface ** target ) const
547 {
548 if (relation == QAccessible::Child) {
549 *target = new KItemListViewAccessible(container()->controller()->view());
550 return 0;
551 }
552 return QAccessibleWidgetEx::navigate(relation, index, target);
553 }
554
555 #endif // QT_NO_ITEMVIEWS
556
557 #endif // QT_NO_ACCESSIBILITY