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