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