]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistviewaccessible.cpp
Fix selection rect after porting from QFontMetrics::width()
[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 <QGraphicsScene>
31 #include <QGraphicsView>
32
33 KItemListView* KItemListViewAccessible::view() const
34 {
35 return qobject_cast<KItemListView*>(object());
36 }
37
38 KItemListViewAccessible::KItemListViewAccessible(KItemListView* view_) :
39 QAccessibleObject(view_)
40 {
41 Q_ASSERT(view());
42 m_cells.resize(childCount());
43 }
44
45 KItemListViewAccessible::~KItemListViewAccessible()
46 {
47 foreach (AccessibleIdWrapper idWrapper, m_cells) {
48 if (idWrapper.isValid) {
49 QAccessible::deleteAccessibleInterface(idWrapper.id);
50 }
51 }
52 }
53
54 void* KItemListViewAccessible::interface_cast(QAccessible::InterfaceType type)
55 {
56 if (type == QAccessible::TableInterface) {
57 return static_cast<QAccessibleTableInterface*>(this);
58 }
59 return nullptr;
60 }
61
62 void KItemListViewAccessible::modelReset()
63 {
64 }
65
66 QAccessibleInterface* KItemListViewAccessible::cell(int index) const
67 {
68 if (index < 0 || index >= view()->model()->count()) {
69 return nullptr;
70 }
71
72 if (m_cells.size() <= index) {
73 m_cells.resize(childCount());
74 }
75 Q_ASSERT(index < m_cells.size());
76
77 AccessibleIdWrapper idWrapper = m_cells.at(index);
78 if (!idWrapper.isValid) {
79 idWrapper.id = QAccessible::registerAccessibleInterface(new KItemListAccessibleCell(view(), index));
80 idWrapper.isValid = true;
81 m_cells.insert(index, idWrapper);
82 }
83 return QAccessible::accessibleInterface(idWrapper.id);
84 }
85
86 QAccessibleInterface* KItemListViewAccessible::cellAt(int row, int column) const
87 {
88 return cell(columnCount() * row + column);
89 }
90
91 QAccessibleInterface* KItemListViewAccessible::caption() const
92 {
93 return nullptr;
94 }
95
96 QString KItemListViewAccessible::columnDescription(int) const
97 {
98 return QString();
99 }
100
101 int KItemListViewAccessible::columnCount() const
102 {
103 return view()->m_layouter->columnCount();
104 }
105
106 int KItemListViewAccessible::rowCount() const
107 {
108 if (columnCount() <= 0) {
109 return 0;
110 }
111
112 int itemCount = view()->model()->count();
113 int rowCount = itemCount / columnCount();
114
115 if (rowCount <= 0) {
116 return 0;
117 }
118
119 if (itemCount % columnCount()) {
120 ++rowCount;
121 }
122 return rowCount;
123 }
124
125 int KItemListViewAccessible::selectedCellCount() const
126 {
127 return view()->controller()->selectionManager()->selectedItems().count();
128 }
129
130 int KItemListViewAccessible::selectedColumnCount() const
131 {
132 return 0;
133 }
134
135 int KItemListViewAccessible::selectedRowCount() const
136 {
137 return 0;
138 }
139
140 QString KItemListViewAccessible::rowDescription(int) const
141 {
142 return QString();
143 }
144
145 QList<QAccessibleInterface*> KItemListViewAccessible::selectedCells() const
146 {
147 QList<QAccessibleInterface*> cells;
148 const auto items = view()->controller()->selectionManager()->selectedItems();
149 cells.reserve(items.count());
150 for (int index : items) {
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 nullptr;
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 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().at(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 nullptr;
266 }
267
268 KItemListViewAccessible::AccessibleIdWrapper::AccessibleIdWrapper() :
269 isValid(false),
270 id(0)
271 {
272 }
273
274 // Table Cell
275
276 KItemListAccessibleCell::KItemListAccessibleCell(KItemListView* view, int index) :
277 m_view(view),
278 m_index(index)
279 {
280 Q_ASSERT(index >= 0 && index < view->model()->count());
281 }
282
283 void* KItemListAccessibleCell::interface_cast(QAccessible::InterfaceType type)
284 {
285 if (type == QAccessible::TableCellInterface) {
286 return static_cast<QAccessibleTableCellInterface*>(this);
287 }
288 return nullptr;
289 }
290
291 int KItemListAccessibleCell::columnExtent() const
292 {
293 return 1;
294 }
295
296 int KItemListAccessibleCell::rowExtent() const
297 {
298 return 1;
299 }
300
301 QList<QAccessibleInterface*> KItemListAccessibleCell::rowHeaderCells() const
302 {
303 return QList<QAccessibleInterface*>();
304 }
305
306 QList<QAccessibleInterface*> KItemListAccessibleCell::columnHeaderCells() const
307 {
308 return QList<QAccessibleInterface*>();
309 }
310
311 int KItemListAccessibleCell::columnIndex() const
312 {
313 return m_view->m_layouter->itemColumn(m_index);
314 }
315
316 int KItemListAccessibleCell::rowIndex() const
317 {
318 return m_view->m_layouter->itemRow(m_index);
319 }
320
321 bool KItemListAccessibleCell::isSelected() const
322 {
323 return m_view->controller()->selectionManager()->isSelected(m_index);
324 }
325
326 QAccessibleInterface* KItemListAccessibleCell::table() const
327 {
328 return QAccessible::queryAccessibleInterface(m_view);
329 }
330
331 QAccessible::Role KItemListAccessibleCell::role() const
332 {
333 return QAccessible::Cell;
334 }
335
336 QAccessible::State KItemListAccessibleCell::state() const
337 {
338 QAccessible::State state;
339
340 state.selectable = true;
341 if (isSelected()) {
342 state.selected = true;
343 }
344
345 state.focusable = true;
346 if (m_view->controller()->selectionManager()->currentItem() == m_index) {
347 state.focused = true;
348 }
349
350 if (m_view->controller()->selectionBehavior() == KItemListController::MultiSelection) {
351 state.multiSelectable = true;
352 }
353
354 if (m_view->model()->isExpandable(m_index)) {
355 if (m_view->model()->isExpanded(m_index)) {
356 state.expanded = true;
357 } else {
358 state.collapsed = true;
359 }
360 }
361
362 return state;
363 }
364
365 bool KItemListAccessibleCell::isExpandable() const
366 {
367 return m_view->model()->isExpandable(m_index);
368 }
369
370 QRect KItemListAccessibleCell::rect() const
371 {
372 QRect rect = m_view->itemRect(m_index).toRect();
373
374 if (rect.isNull()) {
375 return QRect();
376 }
377
378 rect.translate(m_view->mapToScene(QPointF(0.0, 0.0)).toPoint());
379 rect.translate(m_view->scene()->views()[0]->mapToGlobal(QPoint(0, 0)));
380 return rect;
381 }
382
383 QString KItemListAccessibleCell::text(QAccessible::Text t) const
384 {
385 switch (t) {
386 case QAccessible::Name: {
387 const QHash<QByteArray, QVariant> data = m_view->model()->data(m_index);
388 return data["text"].toString();
389 }
390
391 default:
392 break;
393 }
394
395 return QString();
396 }
397
398 void KItemListAccessibleCell::setText(QAccessible::Text, const QString&)
399 {
400 }
401
402 QAccessibleInterface* KItemListAccessibleCell::child(int) const
403 {
404 return nullptr;
405 }
406
407 bool KItemListAccessibleCell::isValid() const
408 {
409 return m_view && (m_index >= 0) && (m_index < m_view->model()->count());
410 }
411
412 QAccessibleInterface* KItemListAccessibleCell::childAt(int, int) const
413 {
414 return nullptr;
415 }
416
417 int KItemListAccessibleCell::childCount() const
418 {
419 return 0;
420 }
421
422 int KItemListAccessibleCell::indexOfChild(const QAccessibleInterface* child) const
423 {
424 Q_UNUSED(child)
425 return -1;
426 }
427
428 QAccessibleInterface* KItemListAccessibleCell::parent() const
429 {
430 return QAccessible::queryAccessibleInterface(m_view);
431 }
432
433 int KItemListAccessibleCell::index() const
434 {
435 return m_index;
436 }
437
438 QObject* KItemListAccessibleCell::object() const
439 {
440 return nullptr;
441 }
442
443 // Container Interface
444 KItemListContainerAccessible::KItemListContainerAccessible(KItemListContainer* container) :
445 QAccessibleWidget(container)
446 {
447 }
448
449 KItemListContainerAccessible::~KItemListContainerAccessible()
450 {
451 }
452
453 int KItemListContainerAccessible::childCount() const
454 {
455 return 1;
456 }
457
458 int KItemListContainerAccessible::indexOfChild(const QAccessibleInterface* child) const
459 {
460 if (child->object() == container()->controller()->view()) {
461 return 0;
462 }
463 return -1;
464 }
465
466 QAccessibleInterface* KItemListContainerAccessible::child(int index) const
467 {
468 if (index == 0) {
469 return QAccessible::queryAccessibleInterface(container()->controller()->view());
470 }
471 return nullptr;
472 }
473
474 const KItemListContainer* KItemListContainerAccessible::container() const
475 {
476 return qobject_cast<KItemListContainer*>(object());
477 }
478
479 #endif // QT_NO_ACCESSIBILITY