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