]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistviewaccessible.cpp
kitemlistview: Port KItemListView::itemAt to std::optional
[dolphin.git] / src / kitemviews / kitemlistviewaccessible.cpp
index 7b1605a0c8451184b9095643d18a5f7f8383cabb..a1afadff4bd93fc234628e8a1fea1c97f84c800a 100644 (file)
@@ -1,24 +1,10 @@
-/***************************************************************************
- *   Copyright (C) 2012 by Amandeep Singh <aman.dedman@gmail.com>          *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2012 Amandeep Singh <aman.dedman@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
 
 #ifndef QT_NO_ACCESSIBILITY
-
 #include "kitemlistviewaccessible.h"
 
 #include "kitemlistcontainer.h"
 #include "kitemlistview.h"
 #include "private/kitemlistviewlayouter.h"
 
-#include <qaccessible.h>
-#include <qgraphicsscene.h>
-#include <qgraphicsview.h>
-
-#include <QHash>
+#include <QGraphicsScene>
+#include <QGraphicsView>
 
 KItemListView* KItemListViewAccessible::view() const
 {
@@ -47,10 +30,9 @@ KItemListViewAccessible::KItemListViewAccessible(KItemListView* view_) :
 
 KItemListViewAccessible::~KItemListViewAccessible()
 {
-    foreach (QAccessibleInterface* child, m_cells) {
-        if (child) {
-            QAccessible::Id childId = QAccessible::uniqueId(child);
-            QAccessible::deleteAccessibleInterface(childId);
+    for (AccessibleIdWrapper idWrapper : qAsConst(m_cells)) {
+        if (idWrapper.isValid) {
+            QAccessible::deleteAccessibleInterface(idWrapper.id);
         }
     }
 }
@@ -60,7 +42,7 @@ void* KItemListViewAccessible::interface_cast(QAccessible::InterfaceType type)
     if (type == QAccessible::TableInterface) {
         return static_cast<QAccessibleTableInterface*>(this);
     }
-    return Q_NULLPTR;
+    return nullptr;
 }
 
 void KItemListViewAccessible::modelReset()
@@ -70,18 +52,21 @@ void KItemListViewAccessible::modelReset()
 QAccessibleInterface* KItemListViewAccessible::cell(int index) const
 {
     if (index < 0 || index >= view()->model()->count()) {
-        return 0;
+        return nullptr;
     }
 
-    if (m_cells.size() < index - 1)
+    if (m_cells.size() <= index) {
         m_cells.resize(childCount());
+    }
+    Q_ASSERT(index < m_cells.size());
 
-    QAccessibleInterface* child = m_cells.at(index);
-    if (!child) {
-        child = new KItemListAccessibleCell(view(), index);
-        QAccessible::registerAccessibleInterface(child);
+    AccessibleIdWrapper idWrapper = m_cells.at(index);
+    if (!idWrapper.isValid) {
+        idWrapper.id = QAccessible::registerAccessibleInterface(new KItemListAccessibleCell(view(), index));
+        idWrapper.isValid = true;
+        m_cells.insert(index, idWrapper);
     }
-    return child;
+    return QAccessible::accessibleInterface(idWrapper.id);
 }
 
 QAccessibleInterface* KItemListViewAccessible::cellAt(int row, int column) const
@@ -91,7 +76,7 @@ QAccessibleInterface* KItemListViewAccessible::cellAt(int row, int column) const
 
 QAccessibleInterface* KItemListViewAccessible::caption() const
 {
-    return 0;
+    return nullptr;
 }
 
 QString KItemListViewAccessible::columnDescription(int) const
@@ -146,7 +131,9 @@ QString KItemListViewAccessible::rowDescription(int) const
 QList<QAccessibleInterface*> KItemListViewAccessible::selectedCells() const
 {
     QList<QAccessibleInterface*> cells;
-    Q_FOREACH (int index, view()->controller()->selectionManager()->selectedItems()) {
+    const auto items = view()->controller()->selectionManager()->selectedItems();
+    cells.reserve(items.count());
+    for (int index : items) {
         cells.append(cell(index));
     }
     return cells;
@@ -164,7 +151,7 @@ QList<int> KItemListViewAccessible::selectedRows() const
 
 QAccessibleInterface* KItemListViewAccessible::summary() const
 {
-    return 0;
+    return nullptr;
 }
 
 bool KItemListViewAccessible::isColumnSelected(int) const
@@ -214,14 +201,14 @@ QAccessible::State KItemListViewAccessible::state() const
 QAccessibleInterface* KItemListViewAccessible::childAt(int x, int y) const
 {
     const QPointF point = QPointF(x, y);
-    int itemIndex = view()->itemAt(view()->mapFromScene(point));
-    return child(itemIndex);
+    const std::optional<int> itemIndex = view()->itemAt(view()->mapFromScene(point));
+    return child(itemIndex.value_or(-1));
 }
 
 QAccessibleInterface* KItemListViewAccessible::parent() const
 {
     // FIXME: return KItemListContainerAccessible here
-    return Q_NULLPTR;
+    return nullptr;
 }
 
 int KItemListViewAccessible::childCount() const
@@ -248,7 +235,7 @@ QRect KItemListViewAccessible::rect() const
 
     const QGraphicsScene* scene = view()->scene();
     if (scene) {
-        const QPoint origin = scene->views()[0]->mapToGlobal(QPoint(0, 0));
+        const QPoint origin = scene->views().at(0)->mapToGlobal(QPoint(0, 0));
         const QRect viewRect = view()->geometry().toRect();
         return viewRect.translated(origin);
     } else {
@@ -261,7 +248,13 @@ QAccessibleInterface* KItemListViewAccessible::child(int index) const
     if (index >= 0 && index < childCount()) {
         return cell(index);
     }
-    return Q_NULLPTR;
+    return nullptr;
+}
+
+KItemListViewAccessible::AccessibleIdWrapper::AccessibleIdWrapper() :
+    isValid(false),
+    id(0)
+{
 }
 
 // Table Cell
@@ -278,7 +271,7 @@ void* KItemListAccessibleCell::interface_cast(QAccessible::InterfaceType type)
     if (type == QAccessible::TableCellInterface) {
         return static_cast<QAccessibleTableCellInterface*>(this);
     }
-    return Q_NULLPTR;
+    return nullptr;
 }
 
 int KItemListAccessibleCell::columnExtent() const
@@ -394,7 +387,7 @@ void KItemListAccessibleCell::setText(QAccessible::Text, const QString&)
 
 QAccessibleInterface* KItemListAccessibleCell::child(int) const
 {
-    return Q_NULLPTR;
+    return nullptr;
 }
 
 bool KItemListAccessibleCell::isValid() const
@@ -404,7 +397,7 @@ bool KItemListAccessibleCell::isValid() const
 
 QAccessibleInterface* KItemListAccessibleCell::childAt(int, int) const
 {
-    return Q_NULLPTR;
+    return nullptr;
 }
 
 int KItemListAccessibleCell::childCount() const
@@ -414,7 +407,7 @@ int KItemListAccessibleCell::childCount() const
 
 int KItemListAccessibleCell::indexOfChild(const QAccessibleInterface* child) const
 {
-    Q_UNUSED(child);
+    Q_UNUSED(child)
     return -1;
 }
 
@@ -430,7 +423,7 @@ int KItemListAccessibleCell::index() const
 
 QObject* KItemListAccessibleCell::object() const
 {
-    return 0;
+    return nullptr;
 }
 
 // Container Interface
@@ -461,7 +454,7 @@ QAccessibleInterface* KItemListContainerAccessible::child(int index) const
     if (index == 0) {
         return QAccessible::queryAccessibleInterface(container()->controller()->view());
     }
-    return Q_NULLPTR;
+    return nullptr;
 }
 
 const KItemListContainer* KItemListContainerAccessible::container() const