]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistviewaccessible.cpp
Fix includes
[dolphin.git] / src / kitemviews / kitemlistviewaccessible.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Amandeep Singh <aman.dedman@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #ifndef QT_NO_ACCESSIBILITY
21
22 #include "kitemlistviewaccessible.h"
23
24 #include "kitemlistcontainer.h"
25 #include "kitemlistcontroller.h"
26 #include "kitemlistselectionmanager.h"
27 #include "kitemlistview.h"
28 #include "private/kitemlistviewlayouter.h"
29
30 #include <QtGui/qaccessible.h>
31 #include <qgraphicsscene.h>
32 #include <qgraphicsview.h>
33
34 #include <KDebug>
35 #include <QHash>
36
37 KItemListView* KItemListViewAccessible::view() const
38 {
39 return qobject_cast<KItemListView*>(object());
40 }
41
42 KItemListViewAccessible::KItemListViewAccessible(KItemListView* view_) :
43 QAccessibleObject(view_)
44 {
45 Q_ASSERT(view());
46 m_cells.resize(childCount());
47 }
48
49 KItemListViewAccessible::~KItemListViewAccessible()
50 {
51 foreach (QAccessibleInterface* child, m_cells) {
52 if (child) {
53 QAccessible::Id childId = QAccessible::uniqueId(child);
54 QAccessible::deleteAccessibleInterface(childId);
55 }
56 }
57 }
58
59 void* KItemListViewAccessible::interface_cast(QAccessible::InterfaceType type)
60 {
61 if (type == QAccessible::TableInterface) {
62 return static_cast<QAccessibleTableInterface*>(this);
63 }
64 return Q_NULLPTR;
65 }
66
67 void KItemListViewAccessible::modelReset()
68 {
69 }
70
71 QAccessibleInterface* KItemListViewAccessible::cell(int index) const
72 {
73 if (index < 0 || index >= view()->model()->count()) {
74 return 0;
75 }
76
77 if (m_cells.size() < index - 1)
78 m_cells.resize(childCount());
79
80 QAccessibleInterface* child = m_cells.at(index);
81 if (!child) {
82 child = new KItemListAccessibleCell(view(), index);
83 QAccessible::registerAccessibleInterface(child);
84 }
85 return child;
86 }
87
88 QAccessibleInterface* KItemListViewAccessible::cellAt(int row, int column) const
89 {
90 return cell(columnCount() * row + column);
91 }
92
93 QAccessibleInterface* KItemListViewAccessible::caption() const
94 {
95 return 0;
96 }
97
98 QString KItemListViewAccessible::columnDescription(int) const
99 {
100 return QString();
101 }
102
103 int KItemListViewAccessible::columnCount() const
104 {
105 return view()->m_layouter->columnCount();
106 }
107
108 int KItemListViewAccessible::rowCount() const
109 {
110 if (columnCount() <= 0) {
111 return 0;
112 }
113
114 int itemCount = view()->model()->count();
115 int rowCount = itemCount / columnCount();
116
117 if (rowCount <= 0) {
118 return 0;
119 }
120
121 if (itemCount % columnCount()) {
122 ++rowCount;
123 }
124 return rowCount;
125 }
126
127 int KItemListViewAccessible::selectedCellCount() const
128 {
129 return view()->controller()->selectionManager()->selectedItems().count();
130 }
131
132 int KItemListViewAccessible::selectedColumnCount() const
133 {
134 return 0;
135 }
136
137 int KItemListViewAccessible::selectedRowCount() const
138 {
139 return 0;
140 }
141
142 QString KItemListViewAccessible::rowDescription(int) const
143 {
144 return QString();
145 }
146
147 QList<QAccessibleInterface*> KItemListViewAccessible::selectedCells() const
148 {
149 QList<QAccessibleInterface*> cells;
150 Q_FOREACH (int index, view()->controller()->selectionManager()->selectedItems()) {
151 cells.append(cell(index));
152 }
153 return cells;
154 }
155
156 QList<int> KItemListViewAccessible::selectedColumns() const
157 {
158 return QList<int>();
159 }
160
161 QList<int> KItemListViewAccessible::selectedRows() const
162 {
163 return QList<int>();
164 }
165
166 QAccessibleInterface* KItemListViewAccessible::summary() const
167 {
168 return 0;
169 }
170
171 bool KItemListViewAccessible::isColumnSelected(int) const
172 {
173 return false;
174 }
175
176 bool KItemListViewAccessible::isRowSelected(int) const
177 {
178 return false;
179 }
180
181 bool KItemListViewAccessible::selectRow(int)
182 {
183 return true;
184 }
185
186 bool KItemListViewAccessible::selectColumn(int)
187 {
188 return true;
189 }
190
191 bool KItemListViewAccessible::unselectRow(int)
192 {
193 return true;
194 }
195
196 bool KItemListViewAccessible::unselectColumn(int)
197 {
198 return true;
199 }
200
201 void KItemListViewAccessible::modelChange(QAccessibleTableModelChangeEvent* /*event*/)
202 {}
203
204 QAccessible::Role KItemListViewAccessible::role() const
205 {
206 return QAccessible::Table;
207 }
208
209 QAccessible::State KItemListViewAccessible::state() const
210 {
211 QAccessible::State s;
212 return s;
213 }
214
215 QAccessibleInterface* KItemListViewAccessible::childAt(int x, int y) const
216 {
217 const QPointF point = QPointF(x, y);
218 int itemIndex = view()->itemAt(view()->mapFromScene(point));
219 return child(itemIndex);
220 }
221
222 QAccessibleInterface* KItemListViewAccessible::parent() const
223 {
224 // FIXME: return KItemListContainerAccessible here
225 return Q_NULLPTR;
226 }
227
228 int KItemListViewAccessible::childCount() const
229 {
230 return view()->model()->count();
231 }
232
233 int KItemListViewAccessible::indexOfChild(const QAccessibleInterface* interface) const
234 {
235 const KItemListAccessibleCell* widget = static_cast<const KItemListAccessibleCell*>(interface);
236 return widget->index();
237 }
238
239 QString KItemListViewAccessible::text(QAccessible::Text) const
240 {
241 return QString();
242 }
243
244 QRect KItemListViewAccessible::rect() const
245 {
246 if (!view()->isVisible()) {
247 return QRect();
248 }
249
250 const QGraphicsScene* scene = view()->scene();
251 if (scene) {
252 const QPoint origin = scene->views()[0]->mapToGlobal(QPoint(0, 0));
253 const QRect viewRect = view()->geometry().toRect();
254 return viewRect.translated(origin);
255 } else {
256 return QRect();
257 }
258 }
259
260 QAccessibleInterface* KItemListViewAccessible::child(int index) const
261 {
262 if (index >= 0 && index < childCount()) {
263 return cell(index);
264 }
265 return Q_NULLPTR;
266 }
267
268 // Table Cell
269
270 KItemListAccessibleCell::KItemListAccessibleCell(KItemListView* view, int index) :
271 m_view(view),
272 m_index(index)
273 {
274 Q_ASSERT(index >= 0 && index < view->model()->count());
275 }
276
277 void* KItemListAccessibleCell::interface_cast(QAccessible::InterfaceType type)
278 {
279 if (type == QAccessible::TableCellInterface) {
280 return static_cast<QAccessibleTableCellInterface*>(this);
281 }
282 return Q_NULLPTR;
283 }
284
285 int KItemListAccessibleCell::columnExtent() const
286 {
287 return 1;
288 }
289
290 int KItemListAccessibleCell::rowExtent() const
291 {
292 return 1;
293 }
294
295 QList<QAccessibleInterface*> KItemListAccessibleCell::rowHeaderCells() const
296 {
297 return QList<QAccessibleInterface*>();
298 }
299
300 QList<QAccessibleInterface*> KItemListAccessibleCell::columnHeaderCells() const
301 {
302 return QList<QAccessibleInterface*>();
303 }
304
305 int KItemListAccessibleCell::columnIndex() const
306 {
307 return m_view->m_layouter->itemColumn(m_index);
308 }
309
310 int KItemListAccessibleCell::rowIndex() const
311 {
312 return m_view->m_layouter->itemRow(m_index);
313 }
314
315 bool KItemListAccessibleCell::isSelected() const
316 {
317 return m_view->controller()->selectionManager()->isSelected(m_index);
318 }
319
320 QAccessibleInterface* KItemListAccessibleCell::table() const
321 {
322 return QAccessible::queryAccessibleInterface(m_view);
323 }
324
325 QAccessible::Role KItemListAccessibleCell::role() const
326 {
327 return QAccessible::Cell;
328 }
329
330 QAccessible::State KItemListAccessibleCell::state() const
331 {
332 QAccessible::State state;
333
334 state.selectable = true;
335 if (isSelected()) {
336 state.selected = true;
337 }
338
339 state.focusable = true;
340 if (m_view->controller()->selectionManager()->currentItem() == m_index) {
341 state.focused = true;
342 }
343
344 if (m_view->controller()->selectionBehavior() == KItemListController::MultiSelection) {
345 state.multiSelectable = true;
346 }
347
348 if (m_view->model()->isExpandable(m_index)) {
349 if (m_view->model()->isExpanded(m_index)) {
350 state.expanded = true;
351 } else {
352 state.collapsed = true;
353 }
354 }
355
356 return state;
357 }
358
359 bool KItemListAccessibleCell::isExpandable() const
360 {
361 return m_view->model()->isExpandable(m_index);
362 }
363
364 QRect KItemListAccessibleCell::rect() const
365 {
366 QRect rect = m_view->itemRect(m_index).toRect();
367
368 if (rect.isNull()) {
369 return QRect();
370 }
371
372 rect.translate(m_view->mapToScene(QPointF(0.0, 0.0)).toPoint());
373 rect.translate(m_view->scene()->views()[0]->mapToGlobal(QPoint(0, 0)));
374 return rect;
375 }
376
377 QString KItemListAccessibleCell::text(QAccessible::Text t) const
378 {
379 switch (t) {
380 case QAccessible::Name: {
381 const QHash<QByteArray, QVariant> data = m_view->model()->data(m_index);
382 return data["text"].toString();
383 }
384
385 default:
386 break;
387 }
388
389 return QString();
390 }
391
392 void KItemListAccessibleCell::setText(QAccessible::Text, const QString&)
393 {
394 }
395
396 QAccessibleInterface* KItemListAccessibleCell::child(int) const
397 {
398 return Q_NULLPTR;
399 }
400
401 bool KItemListAccessibleCell::isValid() const
402 {
403 return m_view && (m_index >= 0) && (m_index < m_view->model()->count());
404 }
405
406 QAccessibleInterface* KItemListAccessibleCell::childAt(int, int) const
407 {
408 return Q_NULLPTR;
409 }
410
411 int KItemListAccessibleCell::childCount() const
412 {
413 return 0;
414 }
415
416 int KItemListAccessibleCell::indexOfChild(const QAccessibleInterface* child) const
417 {
418 Q_UNUSED(child);
419 return -1;
420 }
421
422 QAccessibleInterface* KItemListAccessibleCell::parent() const
423 {
424 return QAccessible::queryAccessibleInterface(m_view);
425 }
426
427 int KItemListAccessibleCell::index() const
428 {
429 return m_index;
430 }
431
432 QObject* KItemListAccessibleCell::object() const
433 {
434 return 0;
435 }
436
437 // Container Interface
438 KItemListContainerAccessible::KItemListContainerAccessible(KItemListContainer* container) :
439 QAccessibleWidget(container)
440 {
441 }
442
443 KItemListContainerAccessible::~KItemListContainerAccessible()
444 {
445 }
446
447 int KItemListContainerAccessible::childCount() const
448 {
449 return 1;
450 }
451
452 int KItemListContainerAccessible::indexOfChild(const QAccessibleInterface* child) const
453 {
454 if (child->object() == container()->controller()->view()) {
455 return 0;
456 }
457 return -1;
458 }
459
460 QAccessibleInterface* KItemListContainerAccessible::child(int index) const
461 {
462 if (index == 0) {
463 return QAccessible::queryAccessibleInterface(container()->controller()->view());
464 }
465 return Q_NULLPTR;
466 }
467
468 const KItemListContainer* KItemListContainerAccessible::container() const
469 {
470 return qobject_cast<KItemListContainer*>(object());
471 }
472
473 #endif // QT_NO_ACCESSIBILITY