]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistviewaccessible.cpp
Accessibility: Fix cellAt
[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/qaccessible2.h>
7 #include <qgraphicsscene.h>
8 #include <qgraphicsview.h>
9
10 #include <KDebug>
11 #include <QHash>
12
13 #ifndef QT_NO_ACCESSIBILITY
14
15 KItemListView* KItemListViewAccessible::view() const
16 {
17 return qobject_cast<KItemListView*>(object());
18 }
19
20 KItemListViewAccessible::KItemListViewAccessible(KItemListView* view_) :
21 QAccessibleObjectEx(view_)
22 {
23 Q_ASSERT(view());
24 }
25
26 void KItemListViewAccessible::modelReset()
27 {}
28
29 QAccessible::Role KItemListViewAccessible::cellRole() const
30 {
31 return QAccessible::Cell;
32 }
33
34 QAccessibleTable2CellInterface* KItemListViewAccessible::cell(int index) const
35 {
36 if (index > 0) {
37 return new KItemListAccessibleCell(view(), index);
38 }
39 return 0;
40 }
41
42 QVariant KItemListViewAccessible::invokeMethodEx(Method, int, const QVariantList &)
43 {
44 return QVariant();
45 }
46
47 QAccessibleTable2CellInterface* KItemListViewAccessible::cellAt(int row, int column) const
48 {
49 return cell(column*row + column + 1);
50 }
51
52 QAccessibleInterface* KItemListViewAccessible::caption() const
53 {
54 return 0;
55 }
56
57 QString KItemListViewAccessible::columnDescription(int) const
58 {
59 return QString();
60 }
61
62 int KItemListViewAccessible::columnCount() const
63 {
64 return view()->layouter()->columnCount();
65 }
66
67 int KItemListViewAccessible::rowCount() const
68 {
69 if(columnCount()<=0) {
70 return 0;
71 }
72 int itemCount = view()->model()->count();
73 int rowCount = itemCount / columnCount();
74 if(rowCount <= 0){
75 return 0;
76 }
77 if (itemCount % rowCount) {
78 ++rowCount;
79 }
80 return rowCount;
81 }
82
83 int KItemListViewAccessible::selectedCellCount() const
84 {
85 return view()->controller()->selectionManager()->selectedItems().size();
86 }
87
88 int KItemListViewAccessible::selectedColumnCount() const
89 {
90 return 0;
91 }
92
93 int KItemListViewAccessible::selectedRowCount() const
94 {
95 return 0;
96 }
97
98 QString KItemListViewAccessible::rowDescription(int) const
99 {
100 return QString();
101 }
102
103 QList<QAccessibleTable2CellInterface*> KItemListViewAccessible::selectedCells() const
104 {
105 QList<QAccessibleTable2CellInterface*> cells;
106 Q_FOREACH (int index, view()->controller()->selectionManager()->selectedItems()) {
107 cells.append(cell(index));
108 }
109 return cells;
110 }
111
112 QList<int> KItemListViewAccessible::selectedColumns() const
113 {
114 return QList<int>();
115 }
116
117 QList<int> KItemListViewAccessible::selectedRows() const
118 {
119 return QList<int>();
120 }
121
122 QAccessibleInterface* KItemListViewAccessible::summary() const
123 {
124 return 0;
125 }
126
127 bool KItemListViewAccessible::isColumnSelected(int) const
128 {
129 return false;
130 }
131
132 bool KItemListViewAccessible::isRowSelected(int) const
133 {
134 return false;
135 }
136
137 bool KItemListViewAccessible::selectRow(int)
138 {
139 return true;
140 }
141
142 bool KItemListViewAccessible::selectColumn(int)
143 {
144 return true;
145 }
146
147 bool KItemListViewAccessible::unselectRow(int)
148 {
149 return true;
150 }
151
152 bool KItemListViewAccessible::unselectColumn(int)
153 {
154 return true;
155 }
156
157 QAccessible2::TableModelChange KItemListViewAccessible::modelChange() const
158 {
159 QAccessible2::TableModelChange change;
160 return change;
161 }
162
163 QAccessible::Role KItemListViewAccessible::role(int child) const
164 {
165 Q_ASSERT(child >= 0);
166 if (child > 0) {
167 return QAccessible::Cell;
168 }
169 return QAccessible::Table;
170 }
171
172 QAccessible::State KItemListViewAccessible::state(int child) const
173 {
174 if (child) {
175 QAccessibleInterface* iface;
176 navigate(Child,child,&iface);
177 if (iface) {
178 return iface->state(0);
179 }
180 }
181 return QAccessible::Normal | QAccessible::HasInvokeExtension;
182 }
183
184 int KItemListViewAccessible::childAt(int x, int y) const
185 {
186 QPointF point = QPointF(x,y);
187 return view()->itemAt(view()->mapFromScene(point));
188 }
189
190 int KItemListViewAccessible::childCount() const
191 {
192 return view()->model()->count();
193 }
194
195 int KItemListViewAccessible::indexOfChild(const QAccessibleInterface* iface) const
196 {
197 const KItemListAccessibleCell* widget = static_cast<const KItemListAccessibleCell*>(iface);
198 return widget->index();
199 }
200
201 QString KItemListViewAccessible::text(Text , int child) const
202 {
203 Q_ASSERT(child == 0);
204 return QString();
205 }
206
207 QRect KItemListViewAccessible::rect(int child) const
208 {
209 Q_UNUSED(child)
210 if (!view()->isVisible()) {
211 return QRect();
212 }
213 QPoint origin = view()->scene()->views()[0]->mapToGlobal(QPoint(0, 0));
214 QRect viewRect = view()->geometry().toRect();
215 return viewRect.translated(origin);
216 }
217
218 int KItemListViewAccessible::navigate(RelationFlag relation, int index, QAccessibleInterface* *iface) const
219 {
220 *iface = 0;
221 switch (relation) {
222 case QAccessible::Child: {
223 Q_ASSERT(index > 0);
224 *iface = cell(index);
225 if (*iface) {
226 return 0;
227 }
228 break;
229 }
230 default:
231 break;
232 }
233 return -1;
234 }
235
236 QAccessible::Relation KItemListViewAccessible::relationTo(int, const QAccessibleInterface* , int) const
237 {
238 return QAccessible::Unrelated;
239 }
240
241 #ifndef QT_NO_ACTION
242
243 int KItemListViewAccessible::userActionCount(int) const
244 {
245 return 0;
246 }
247
248 QString KItemListViewAccessible::actionText(int, Text, int) const
249 {
250 return QString();
251 }
252
253 bool KItemListViewAccessible::doAction(int, int, const QVariantList &)
254 {
255 return false;
256 }
257
258 #endif
259
260 // Table Cell
261
262 KItemListAccessibleCell::KItemListAccessibleCell(KItemListView* view, int index) :
263 m_view(view),
264 m_index(index)
265 {
266 Q_ASSERT(index >= 0);
267 }
268
269 int KItemListAccessibleCell::columnExtent() const
270 {
271 return 1;
272 }
273
274 int KItemListAccessibleCell::rowExtent() const
275 {
276 return 1;
277 }
278
279 QList<QAccessibleInterface*> KItemListAccessibleCell::rowHeaderCells() const
280 {
281 return QList<QAccessibleInterface*>();
282 }
283
284 QList<QAccessibleInterface*> KItemListAccessibleCell::columnHeaderCells() const
285 {
286 return QList<QAccessibleInterface*>();
287 }
288
289 int KItemListAccessibleCell::columnIndex() const
290 {
291 return m_view->layouter()->itemColumn(m_index);
292 }
293
294 int KItemListAccessibleCell::rowIndex() const
295 {
296 return m_view->layouter()->itemRow(m_index);
297 }
298
299 bool KItemListAccessibleCell::isSelected() const
300 {
301 return m_view->controller()->selectionManager()->isSelected(m_index-1);
302 }
303
304 void KItemListAccessibleCell::rowColumnExtents(int* row, int* column, int* rowExtents, int* columnExtents, bool* selected) const
305 {
306 KItemListViewLayouter* layouter = m_view->layouter();
307 *row = layouter->itemRow(m_index);
308 *column = layouter->itemColumn(m_index);
309 *rowExtents = 1;
310 *columnExtents = 1;
311 *selected = isSelected();
312 }
313
314 QAccessibleTable2Interface* KItemListAccessibleCell::table() const
315 {
316 return QAccessible::queryAccessibleInterface(m_view)->table2Interface();
317 }
318
319 QAccessible::Role KItemListAccessibleCell::role(int child) const
320 {
321 Q_ASSERT(child == 0);
322 return QAccessible::Cell;
323 }
324
325 QAccessible::State KItemListAccessibleCell::state(int child) const
326 {
327 Q_ASSERT(child == 0);
328 QAccessible::State state = Normal;
329
330 if (isSelected()) {
331 state |= Selected;
332 }
333 if (m_view->controller()->selectionManager()->currentItem() == m_index) {
334 state |= Focused;
335 }
336
337 state |= Selectable;
338 state |= Focusable;
339
340 if (m_view->controller()->selectionBehavior() == KItemListController::MultiSelection){
341 state |= MultiSelectable;
342 }
343
344 return state;
345 }
346
347 bool KItemListAccessibleCell::isExpandable() const
348 {
349 return false;
350 }
351
352 QRect KItemListAccessibleCell::rect(int) const
353 {
354 QRect rect = m_view->itemRect(m_index-1).toRect();
355 if (rect.isNull()) {
356 return QRect();
357 }
358 rect.translate(m_view->mapToScene(QPointF(0.0, 0.0)).toPoint());
359 rect.translate(m_view->scene()->views()[0]->mapToGlobal(QPoint(0, 0)));
360 return rect;
361 }
362
363 QString KItemListAccessibleCell::text(QAccessible::Text t, int child) const
364 {
365 Q_ASSERT(child == 0);
366 Q_UNUSED(child)
367 const QHash<QByteArray, QVariant> data = m_view->model()->data(m_index-1);
368 switch (t) {
369 case QAccessible::Value:
370 case QAccessible::Name:
371 return data["text"].toString();
372 default:
373 break;
374 }
375 return QString();
376 }
377
378 void KItemListAccessibleCell::setText(QAccessible::Text /*t*/, int child, const QString &/*text*/)
379 {
380 Q_ASSERT(child == 0);
381 }
382
383 bool KItemListAccessibleCell::isValid() const
384 {
385 return m_view && (m_index > 0);
386 }
387
388 int KItemListAccessibleCell::childAt(int, int) const
389 {
390 return 0;
391 }
392
393 int KItemListAccessibleCell::childCount() const
394 {
395 return 0;
396 }
397
398 int KItemListAccessibleCell::indexOfChild(const QAccessibleInterface* child) const
399 {
400 return -1;
401 }
402
403 int KItemListAccessibleCell::navigate(RelationFlag relation, int index, QAccessibleInterface* *iface) const
404 {
405 if (relation == Ancestor && index == 1) {
406 *iface = new KItemListViewAccessible(m_view);
407 return 0;
408 }
409
410 *iface = 0;
411 if (!m_view) {
412 return -1;
413 }
414
415 switch (relation) {
416
417 case Child: {
418 return -1;
419 }
420 case Sibling:
421 if (index > 0) {
422 QAccessibleInterface* parent = queryAccessibleInterface(m_view);
423 int ret = parent->navigate(QAccessible::Child, index, iface);
424 delete parent;
425 if (*iface) {
426 return ret;
427 }
428 }
429 return -1;
430 default:
431 break;
432 }
433
434 return -1;
435 }
436
437 QAccessible::Relation KItemListAccessibleCell::relationTo(int child, const QAccessibleInterface* , int otherChild) const
438 {
439 Q_ASSERT(child == 0);
440 Q_ASSERT(otherChild == 0);
441 return QAccessible::Unrelated;
442 }
443
444 #ifndef QT_NO_ACTION
445
446 int KItemListAccessibleCell::userActionCount(int) const
447 {
448 return 0;
449 }
450
451 QString KItemListAccessibleCell::actionText(int, Text, int) const
452 {
453 return QString();
454 }
455
456 bool KItemListAccessibleCell::doAction(int, int, const QVariantList &)
457 {
458 return false;
459 }
460
461 #endif
462
463 int KItemListAccessibleCell::index() const
464 {
465 return m_index;
466 }
467
468 QObject* KItemListAccessibleCell::object() const
469 {
470 return 0;
471 }
472
473 // Container Interface
474 KItemListContainerAccessible::KItemListContainerAccessible(KItemListContainer* container) :
475 QAccessibleWidgetEx(container)
476 {}
477
478 KItemListContainerAccessible::~KItemListContainerAccessible ()
479 {}
480
481 int KItemListContainerAccessible::childCount () const
482 {
483 return 1;
484 }
485
486 int KItemListContainerAccessible::indexOfChild ( const QAccessibleInterface* child ) const
487 {
488 if (child->object() == container()->controller()->view()) {
489 return 1;
490 }
491 return -1;
492 }
493
494 int KItemListContainerAccessible::navigate ( QAccessible::RelationFlag relation, int index, QAccessibleInterface** target ) const
495 {
496 if (relation == QAccessible::Child) {
497 *target = new KItemListViewAccessible(container()->controller()->view());
498 return 0;
499 }
500 return QAccessibleWidgetEx::navigate(relation, index, target);
501 }
502
503 #endif // QT_NO_ACCESSIBILITY