]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistviewaccessible.cpp
Removing layouter from public, adding TableModelChanged update
[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 || index >= view()->model()->count())
37 return 0;
38 return new KItemListAccessibleCell(view(), index);
39 }
40
41 QVariant KItemListViewAccessible::invokeMethodEx(Method, int, const QVariantList &)
42 {
43 return QVariant();
44 }
45
46 QAccessibleTable2CellInterface* KItemListViewAccessible::cellAt(int row, int column) const
47 {
48 qDebug() << "cellAt: " << row << column << " is: " << column*row + column;
49 return cell(columnCount()*row + column);
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()->m_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 % columnCount()) {
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 change.lastRow = rowCount();
161 change.lastColumn = columnCount();
162 return change;
163 }
164
165 QAccessible::Role KItemListViewAccessible::role(int child) const
166 {
167 Q_ASSERT(child >= 0);
168 if (child > 0) {
169 return QAccessible::Cell;
170 }
171 return QAccessible::Table;
172 }
173
174 QAccessible::State KItemListViewAccessible::state(int child) const
175 {
176 if (child) {
177 QAccessibleInterface* iface = 0;
178 navigate(Child, child, &iface);
179 if (iface) {
180 return iface->state(0);
181 }
182 }
183 return QAccessible::Normal | QAccessible::HasInvokeExtension;
184 }
185
186 int KItemListViewAccessible::childAt(int x, int y) const
187 {
188 QPointF point = QPointF(x,y);
189 return view()->itemAt(view()->mapFromScene(point));
190 }
191
192 int KItemListViewAccessible::childCount() const
193 {
194 return view()->model()->count();
195 }
196
197 int KItemListViewAccessible::indexOfChild(const QAccessibleInterface* iface) const
198 {
199 const KItemListAccessibleCell* widget = static_cast<const KItemListAccessibleCell*>(iface);
200 return widget->index() + 1;
201 }
202
203 QString KItemListViewAccessible::text(Text , int child) const
204 {
205 Q_ASSERT(child == 0);
206 return QString();
207 }
208
209 QRect KItemListViewAccessible::rect(int child) const
210 {
211 Q_UNUSED(child)
212 if (!view()->isVisible()) {
213 return QRect();
214 }
215 QPoint origin = view()->scene()->views()[0]->mapToGlobal(QPoint(0, 0));
216 QRect viewRect = view()->geometry().toRect();
217 return viewRect.translated(origin);
218 }
219
220 int KItemListViewAccessible::navigate(RelationFlag relation, int index, QAccessibleInterface* *iface) const
221 {
222 *iface = 0;
223 switch (relation) {
224 case QAccessible::Child: {
225 Q_ASSERT(index > 0);
226 *iface = cell(index - 1);
227 if (*iface) {
228 return 0;
229 }
230 break;
231 }
232 default:
233 break;
234 }
235 return -1;
236 }
237
238 QAccessible::Relation KItemListViewAccessible::relationTo(int, const QAccessibleInterface* , int) const
239 {
240 return QAccessible::Unrelated;
241 }
242
243 #ifndef QT_NO_ACTION
244
245 int KItemListViewAccessible::userActionCount(int) const
246 {
247 return 0;
248 }
249
250 QString KItemListViewAccessible::actionText(int, Text, int) const
251 {
252 return QString();
253 }
254
255 bool KItemListViewAccessible::doAction(int, int, const QVariantList &)
256 {
257 return false;
258 }
259
260 #endif
261
262 // Table Cell
263
264 KItemListAccessibleCell::KItemListAccessibleCell(KItemListView* view, int index) :
265 m_view(view),
266 m_index(index)
267 {
268 Q_ASSERT(index >= 0 && index < view->model()->count());
269 }
270
271 int KItemListAccessibleCell::columnExtent() const
272 {
273 return 1;
274 }
275
276 int KItemListAccessibleCell::rowExtent() const
277 {
278 return 1;
279 }
280
281 QList<QAccessibleInterface*> KItemListAccessibleCell::rowHeaderCells() const
282 {
283 return QList<QAccessibleInterface*>();
284 }
285
286 QList<QAccessibleInterface*> KItemListAccessibleCell::columnHeaderCells() const
287 {
288 return QList<QAccessibleInterface*>();
289 }
290
291 int KItemListAccessibleCell::columnIndex() const
292 {
293 return m_view->m_layouter->itemColumn(m_index);
294 }
295
296 int KItemListAccessibleCell::rowIndex() const
297 {
298 return m_view->m_layouter->itemRow(m_index);
299 }
300
301 bool KItemListAccessibleCell::isSelected() const
302 {
303 return m_view->controller()->selectionManager()->isSelected(m_index);
304 }
305
306 void KItemListAccessibleCell::rowColumnExtents(int* row, int* column, int* rowExtents, int* columnExtents, bool* selected) const
307 {
308 KItemListViewLayouter* layouter = m_view->m_layouter;
309 *row = layouter->itemRow(m_index);
310 *column = layouter->itemColumn(m_index);
311 *rowExtents = 1;
312 *columnExtents = 1;
313 *selected = isSelected();
314 }
315
316 QAccessibleTable2Interface* KItemListAccessibleCell::table() const
317 {
318 return QAccessible::queryAccessibleInterface(m_view)->table2Interface();
319 }
320
321 QAccessible::Role KItemListAccessibleCell::role(int child) const
322 {
323 Q_ASSERT(child == 0);
324 return QAccessible::Cell;
325 }
326
327 QAccessible::State KItemListAccessibleCell::state(int child) const
328 {
329 Q_ASSERT(child == 0);
330 QAccessible::State state = Normal;
331
332 if (isSelected()) {
333 state |= Selected;
334 }
335 if (m_view->controller()->selectionManager()->currentItem() == m_index) {
336 state |= Focused;
337 }
338
339 state |= Selectable;
340 state |= Focusable;
341
342 if (m_view->controller()->selectionBehavior() == KItemListController::MultiSelection){
343 state |= MultiSelectable;
344 }
345
346 if (m_view->model()->isExpandable(m_index)) {
347 if(m_view->model()->isExpanded(m_index)) {
348 state |= Expanded;
349 }
350 else {
351 state |= Collapsed;
352 }
353 }
354 return state;
355 }
356
357 bool KItemListAccessibleCell::isExpandable() const
358 {
359 return m_view->model()->isExpandable(m_index);
360 }
361
362 QRect KItemListAccessibleCell::rect(int) const
363 {
364 QRect rect = m_view->itemRect(m_index).toRect();
365 if (rect.isNull()) {
366 return QRect();
367 }
368 rect.translate(m_view->mapToScene(QPointF(0.0, 0.0)).toPoint());
369 rect.translate(m_view->scene()->views()[0]->mapToGlobal(QPoint(0, 0)));
370 return rect;
371 }
372
373 QString KItemListAccessibleCell::text(QAccessible::Text t, int child) const
374 {
375 Q_ASSERT(child == 0);
376 Q_UNUSED(child)
377 const QHash<QByteArray, QVariant> data = m_view->model()->data(m_index);
378 switch (t) {
379 case QAccessible::Value:
380 case QAccessible::Name:
381 return data["text"].toString();
382 default:
383 break;
384 }
385 return QString();
386 }
387
388 void KItemListAccessibleCell::setText(QAccessible::Text /*t*/, int child, const QString &/*text*/)
389 {
390 Q_ASSERT(child == 0);
391 }
392
393 bool KItemListAccessibleCell::isValid() const
394 {
395 return m_view && (m_index >= 0) && (m_index < m_view->model()->count());
396 }
397
398 int KItemListAccessibleCell::childAt(int, int) const
399 {
400 return 0;
401 }
402
403 int KItemListAccessibleCell::childCount() const
404 {
405 return 0;
406 }
407
408 int KItemListAccessibleCell::indexOfChild(const QAccessibleInterface* child) const
409 {
410 Q_UNUSED(child);
411 return -1;
412 }
413
414 int KItemListAccessibleCell::navigate(RelationFlag relation, int index, QAccessibleInterface* *iface) const
415 {
416 if (relation == Ancestor && index == 1) {
417 *iface = new KItemListViewAccessible(m_view);
418 return 0;
419 }
420
421 *iface = 0;
422 if (!m_view) {
423 return -1;
424 }
425
426 switch (relation) {
427
428 case Child: {
429 return -1;
430 }
431 default:
432 break;
433 }
434
435 return -1;
436 }
437
438 QAccessible::Relation KItemListAccessibleCell::relationTo(int child, const QAccessibleInterface* , int otherChild) const
439 {
440 Q_ASSERT(child == 0);
441 Q_ASSERT(otherChild == 0);
442 return QAccessible::Unrelated;
443 }
444
445 #ifndef QT_NO_ACTION
446
447 int KItemListAccessibleCell::userActionCount(int) const
448 {
449 return 0;
450 }
451
452 QString KItemListAccessibleCell::actionText(int, Text, int) const
453 {
454 return QString();
455 }
456
457 bool KItemListAccessibleCell::doAction(int, int, const QVariantList &)
458 {
459 return false;
460 }
461
462 #endif
463
464 int KItemListAccessibleCell::index() const
465 {
466 return m_index;
467 }
468
469 QObject* KItemListAccessibleCell::object() const
470 {
471 return 0;
472 }
473
474 // Container Interface
475 KItemListContainerAccessible::KItemListContainerAccessible(KItemListContainer* container) :
476 QAccessibleWidgetEx(container)
477 {}
478
479 KItemListContainerAccessible::~KItemListContainerAccessible ()
480 {}
481
482 int KItemListContainerAccessible::childCount () const
483 {
484 return 1;
485 }
486
487 int KItemListContainerAccessible::indexOfChild ( const QAccessibleInterface* child ) const
488 {
489 if (child->object() == container()->controller()->view()) {
490 return 1;
491 }
492 return -1;
493 }
494
495 int KItemListContainerAccessible::navigate ( QAccessible::RelationFlag relation, int index, QAccessibleInterface** target ) const
496 {
497 if (relation == QAccessible::Child) {
498 *target = new KItemListViewAccessible(container()->controller()->view());
499 return 0;
500 }
501 return QAccessibleWidgetEx::navigate(relation, index, target);
502 }
503
504 #endif // QT_NO_ACCESSIBILITY