]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistviewaccessible.cpp
Fix includes
[dolphin.git] / src / kitemviews / kitemlistviewaccessible.cpp
index 66bf6fe229f9b0ecbadd903129adf37adf60a0cd..8efa60fff64c3b8d8d0c7f4ae229c140970f8e5c 100644 (file)
@@ -1,53 +1,93 @@
+/***************************************************************************
+ *   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            *
+ ***************************************************************************/
+
+#ifndef QT_NO_ACCESSIBILITY
+
 #include "kitemlistviewaccessible.h"
+
+#include "kitemlistcontainer.h"
 #include "kitemlistcontroller.h"
 #include "kitemlistselectionmanager.h"
+#include "kitemlistview.h"
 #include "private/kitemlistviewlayouter.h"
 
-#include <QtGui/qaccessible2.h>
+#include <QtGui/qaccessible.h>
 #include <qgraphicsscene.h>
 #include <qgraphicsview.h>
 
 #include <KDebug>
 #include <QHash>
 
-#ifndef QT_NO_ACCESSIBILITY
-
 KItemListView* KItemListViewAccessible::view() const
 {
     return qobject_cast<KItemListView*>(object());
 }
 
 KItemListViewAccessible::KItemListViewAccessible(KItemListView* view_) :
-    QAccessibleObjectEx(view_)
+    QAccessibleObject(view_)
 {
     Q_ASSERT(view());
+    m_cells.resize(childCount());
 }
 
-void KItemListViewAccessible::modelReset()
-{}
+KItemListViewAccessible::~KItemListViewAccessible()
+{
+    foreach (QAccessibleInterface* child, m_cells) {
+        if (child) {
+            QAccessible::Id childId = QAccessible::uniqueId(child);
+            QAccessible::deleteAccessibleInterface(childId);
+        }
+    }
+}
 
-QAccessible::Role KItemListViewAccessible::cellRole() const
+void* KItemListViewAccessible::interface_cast(QAccessible::InterfaceType type)
 {
-    return QAccessible::Cell;
+    if (type == QAccessible::TableInterface) {
+        return static_cast<QAccessibleTableInterface*>(this);
+    }
+    return Q_NULLPTR;
 }
 
-QAccessibleTable2CellInterface* KItemListViewAccessible::cell(int index) const
+void KItemListViewAccessible::modelReset()
 {
-    Q_ASSERT(index >= 0 && index < view()->model()->count());
-    if (index < 0 || index >= view()->model()->count())
-        return 0;
-    return new KItemListAccessibleCell(view(), index);
 }
 
-QVariant KItemListViewAccessible::invokeMethodEx(Method, int, const QVariantList &)
+QAccessibleInterface* KItemListViewAccessible::cell(int index) const
 {
-    return QVariant();
+    if (index < 0 || index >= view()->model()->count()) {
+        return 0;
+    }
+
+    if (m_cells.size() < index - 1)
+        m_cells.resize(childCount());
+
+    QAccessibleInterface* child = m_cells.at(index);
+    if (!child) {
+        child = new KItemListAccessibleCell(view(), index);
+        QAccessible::registerAccessibleInterface(child);
+    }
+    return child;
 }
 
-QAccessibleTable2CellInterface* KItemListViewAccessible::cellAt(int row, int column) const
+QAccessibleInterface* KItemListViewAccessible::cellAt(int row, int column) const
 {
-    qDebug() << "cellAt: " << row << column << " is: " << column*row + column;
-    return cell(columnCount()*row + column);
+    return cell(columnCount() * row + column);
 }
 
 QAccessibleInterface* KItemListViewAccessible::caption() const
@@ -62,19 +102,22 @@ QString KItemListViewAccessible::columnDescription(int) const
 
 int KItemListViewAccessible::columnCount() const
 {
-    return view()->layouter()->columnCount();
+    return view()->m_layouter->columnCount();
 }
 
 int KItemListViewAccessible::rowCount() const
 {
-    if(columnCount()<=0) {
+    if (columnCount() <= 0) {
         return 0;
     }
+
     int itemCount = view()->model()->count();
     int rowCount = itemCount / columnCount();
-    if(rowCount <= 0){
+
+    if (rowCount <= 0) {
         return 0;
     }
+
     if (itemCount % columnCount()) {
         ++rowCount;
     }
@@ -83,7 +126,7 @@ int KItemListViewAccessible::rowCount() const
 
 int KItemListViewAccessible::selectedCellCount() const
 {
-    return view()->controller()->selectionManager()->selectedItems().size();
+    return view()->controller()->selectionManager()->selectedItems().count();
 }
 
 int KItemListViewAccessible::selectedColumnCount() const
@@ -101,9 +144,9 @@ QString KItemListViewAccessible::rowDescription(int) const
     return QString();
 }
 
-QList<QAccessibleTable2CellInterface*> KItemListViewAccessible::selectedCells() const
+QList<QAccessibleInterface*> KItemListViewAccessible::selectedCells() const
 {
-    QList<QAccessibleTable2CellInterface*> cells;
+    QList<QAccessibleInterface*> cells;
     Q_FOREACH (int index, view()->controller()->selectionManager()->selectedItems()) {
         cells.append(cell(index));
     }
@@ -155,37 +198,31 @@ bool KItemListViewAccessible::unselectColumn(int)
     return true;
 }
 
-QAccessible2::TableModelChange KItemListViewAccessible::modelChange() const
+void KItemListViewAccessible::modelChange(QAccessibleTableModelChangeEvent* /*event*/)
+{}
+
+QAccessible::Role KItemListViewAccessible::role() const
 {
-    QAccessible2::TableModelChange change;
-    return change;
+    return QAccessible::Table;
 }
 
-QAccessible::Role KItemListViewAccessible::role(int child) const
+QAccessible::State KItemListViewAccessible::state() const
 {
-    Q_ASSERT(child >= 0);
-    if (child > 0) {
-        return QAccessible::Cell;
-    }
-    return QAccessible::Table;
+    QAccessible::State s;
+    return s;
 }
 
-QAccessible::State KItemListViewAccessible::state(int child) const
+QAccessibleInterface* KItemListViewAccessible::childAt(int x, int y) const
 {
-    if (child) {
-        QAccessibleInterface* iface = 0;
-        navigate(Child, child, &iface);
-        if (iface) {
-            return iface->state(0);
-        }
-    }
-    return QAccessible::Normal | QAccessible::HasInvokeExtension;
+    const QPointF point = QPointF(x, y);
+    int itemIndex = view()->itemAt(view()->mapFromScene(point));
+    return child(itemIndex);
 }
 
-int KItemListViewAccessible::childAt(int x, int y) const
+QAccessibleInterface* KItemListViewAccessible::parent() const
 {
-    QPointF point = QPointF(x,y);
-    return view()->itemAt(view()->mapFromScene(point));
+    // FIXME: return KItemListContainerAccessible here
+    return Q_NULLPTR;
 }
 
 int KItemListViewAccessible::childCount() const
@@ -193,71 +230,41 @@ int KItemListViewAccessible::childCount() const
     return view()->model()->count();
 }
 
-int KItemListViewAccessible::indexOfChild(const QAccessibleInterface* iface) const
+int KItemListViewAccessible::indexOfChild(const QAccessibleInterface* interface) const
 {
-    const KItemListAccessibleCell* widget = static_cast<const KItemListAccessibleCell*>(iface);
-    return widget->index() + 1;
+    const KItemListAccessibleCell* widget = static_cast<const KItemListAccessibleCell*>(interface);
+    return widget->index();
 }
 
-QString KItemListViewAccessible::text(Text , int child) const
+QString KItemListViewAccessible::text(QAccessible::Text) const
 {
-    Q_ASSERT(child == 0);
     return QString();
 }
 
-QRect KItemListViewAccessible::rect(int child) const
+QRect KItemListViewAccessible::rect() const
 {
-    Q_UNUSED(child)
     if (!view()->isVisible()) {
         return QRect();
     }
-    QPoint origin = view()->scene()->views()[0]->mapToGlobal(QPoint(0, 0));
-    QRect viewRect = view()->geometry().toRect();
-    return viewRect.translated(origin);
-}
-
-int KItemListViewAccessible::navigate(RelationFlag relation, int index, QAccessibleInterface* *iface) const
-{
-    *iface = 0;
-    switch (relation) {
-        case QAccessible::Child: {
-            Q_ASSERT(index > 0);
-            *iface = cell(index - 1);
-            if (*iface) {
-                return 0;
-            }
-            break;
-        }
-        default:
-            break;
-    }
-    return -1;
-}
-
-QAccessible::Relation KItemListViewAccessible::relationTo(int, const QAccessibleInterface* , int) const
-{
-    return QAccessible::Unrelated;
-}
-
-#ifndef QT_NO_ACTION
-
-int KItemListViewAccessible::userActionCount(int) const
-{
-    return 0;
-}
 
-QString KItemListViewAccessible::actionText(int, Text, int) const
-{
-    return QString();
+    const QGraphicsScene* scene = view()->scene();
+    if (scene) {
+        const QPoint origin = scene->views()[0]->mapToGlobal(QPoint(0, 0));
+        const QRect viewRect = view()->geometry().toRect();
+        return viewRect.translated(origin);
+    } else {
+        return QRect();
+    }
 }
 
-bool KItemListViewAccessible::doAction(int, int, const QVariantList &)
+QAccessibleInterface* KItemListViewAccessible::child(int index) const
 {
-    return false;
+    if (index >= 0 && index < childCount()) {
+        return cell(index);
+    }
+    return Q_NULLPTR;
 }
 
-#endif
-
 // Table Cell
 
 KItemListAccessibleCell::KItemListAccessibleCell(KItemListView* view, int index) :
@@ -267,6 +274,14 @@ KItemListAccessibleCell::KItemListAccessibleCell(KItemListView* view, int index)
     Q_ASSERT(index >= 0 && index < view->model()->count());
 }
 
+void* KItemListAccessibleCell::interface_cast(QAccessible::InterfaceType type)
+{
+    if (type == QAccessible::TableCellInterface) {
+        return static_cast<QAccessibleTableCellInterface*>(this);
+    }
+    return Q_NULLPTR;
+}
+
 int KItemListAccessibleCell::columnExtent() const
 {
     return 1;
@@ -289,12 +304,12 @@ QList<QAccessibleInterface*> KItemListAccessibleCell::columnHeaderCells() const
 
 int KItemListAccessibleCell::columnIndex() const
 {
-    return m_view->layouter()->itemColumn(m_index);
+    return m_view->m_layouter->itemColumn(m_index);
 }
 
 int KItemListAccessibleCell::rowIndex() const
 {
-    return m_view->layouter()->itemRow(m_index);
+    return m_view->m_layouter->itemRow(m_index);
 }
 
 bool KItemListAccessibleCell::isSelected() const
@@ -302,44 +317,40 @@ bool KItemListAccessibleCell::isSelected() const
     return m_view->controller()->selectionManager()->isSelected(m_index);
 }
 
-void KItemListAccessibleCell::rowColumnExtents(int* row, int* column, int* rowExtents, int* columnExtents, bool* selected) const
+QAccessibleInterface* KItemListAccessibleCell::table() const
 {
-    KItemListViewLayouter* layouter = m_view->layouter();
-    *row = layouter->itemRow(m_index);
-    *column = layouter->itemColumn(m_index);
-    *rowExtents = 1;
-    *columnExtents = 1;
-    *selected = isSelected();
+    return QAccessible::queryAccessibleInterface(m_view);
 }
 
-QAccessibleTable2Interface* KItemListAccessibleCell::table() const
+QAccessible::Role KItemListAccessibleCell::role() const
 {
-    return QAccessible::queryAccessibleInterface(m_view)->table2Interface();
-}
-
-QAccessible::Role KItemListAccessibleCell::role(int child) const
-{
-    Q_ASSERT(child == 0);
     return QAccessible::Cell;
 }
 
-QAccessible::State KItemListAccessibleCell::state(int child) const
+QAccessible::State KItemListAccessibleCell::state() const
 {
-    Q_ASSERT(child == 0);
-    QAccessible::State state = Normal;
+    QAccessible::State state;
 
+    state.selectable = true;
     if (isSelected()) {
-         state |= Selected;
+        state.selected = true;
     }
+
+    state.focusable = true;
     if (m_view->controller()->selectionManager()->currentItem() == m_index) {
-        state |= Focused;
+        state.focused = true;
     }
 
-    state |= Selectable;
-    state |= Focusable;
+    if (m_view->controller()->selectionBehavior() == KItemListController::MultiSelection) {
+        state.multiSelectable = true;
+    }
 
-    if (m_view->controller()->selectionBehavior() == KItemListController::MultiSelection){
-        state |= MultiSelectable;
+    if (m_view->model()->isExpandable(m_index)) {
+        if (m_view->model()->isExpanded(m_index)) {
+            state.expanded = true;
+        } else {
+            state.collapsed = true;
+        }
     }
 
     return state;
@@ -347,38 +358,44 @@ QAccessible::State KItemListAccessibleCell::state(int child) const
 
 bool KItemListAccessibleCell::isExpandable() const
 {
-    return false;
+    return m_view->model()->isExpandable(m_index);
 }
 
-QRect KItemListAccessibleCell::rect(int) const
+QRect KItemListAccessibleCell::rect() const
 {
     QRect rect = m_view->itemRect(m_index).toRect();
+
     if (rect.isNull()) {
         return QRect();
     }
+
     rect.translate(m_view->mapToScene(QPointF(0.0, 0.0)).toPoint());
     rect.translate(m_view->scene()->views()[0]->mapToGlobal(QPoint(0, 0)));
     return rect;
 }
 
-QString KItemListAccessibleCell::text(QAccessible::Text t, int child) const
+QString KItemListAccessibleCell::text(QAccessible::Text t) const
 {
-    Q_ASSERT(child == 0);
-    Q_UNUSED(child)
-    const QHash<QByteArray, QVariant> data = m_view->model()->data(m_index);
     switch (t) {
-    case QAccessible::Value:
-    case QAccessible::Name:
+    case QAccessible::Name: {
+        const QHash<QByteArray, QVariant> data = m_view->model()->data(m_index);
         return data["text"].toString();
+    }
+
     default:
         break;
     }
+
     return QString();
 }
 
-void KItemListAccessibleCell::setText(QAccessible::Text /*t*/, int child, const QString &/*text*/)
+void KItemListAccessibleCell::setText(QAccessible::Text, const QString&)
 {
-    Q_ASSERT(child == 0);
+}
+
+QAccessibleInterface* KItemListAccessibleCell::child(int) const
+{
+    return Q_NULLPTR;
 }
 
 bool KItemListAccessibleCell::isValid() const
@@ -386,9 +403,9 @@ bool KItemListAccessibleCell::isValid() const
     return m_view && (m_index >= 0) && (m_index < m_view->model()->count());
 }
 
-int KItemListAccessibleCell::childAt(int, int) const
+QAccessibleInterface* KItemListAccessibleCell::childAt(int, int) const
 {
-    return 0;
+    return Q_NULLPTR;
 }
 
 int KItemListAccessibleCell::childCount() const
@@ -398,69 +415,15 @@ int KItemListAccessibleCell::childCount() const
 
 int KItemListAccessibleCell::indexOfChild(const QAccessibleInterface* child) const
 {
+    Q_UNUSED(child);
     return -1;
 }
 
-int KItemListAccessibleCell::navigate(RelationFlag relation, int index, QAccessibleInterface* *iface) const
+QAccessibleInterface* KItemListAccessibleCell::parent() const
 {
-    if (relation == Ancestor && index == 1) {
-        *iface = new KItemListViewAccessible(m_view);
-        return 0;
-    }
-
-    *iface = 0;
-    if (!m_view) {
-        return -1;
-    }
-
-    switch (relation) {
-
-    case Child: {
-        return -1;
-    }
-    case Sibling:
-        if (index > 0) {
-            QAccessibleInterface* parent = queryAccessibleInterface(m_view);
-            int ret = parent->navigate(QAccessible::Child, index, iface);
-            delete parent;
-            if (*iface) {
-                return ret;
-            }
-        }
-        return -1;
-    default:
-        break;
-    }
-
-    return -1;
+    return QAccessible::queryAccessibleInterface(m_view);
 }
 
-QAccessible::Relation KItemListAccessibleCell::relationTo(int child, const QAccessibleInterface* , int otherChild) const
-{
-    Q_ASSERT(child == 0);
-    Q_ASSERT(otherChild == 0);
-    return QAccessible::Unrelated;
-}
-
-#ifndef QT_NO_ACTION
-
-int KItemListAccessibleCell::userActionCount(int) const
-{
-    return 0;
-}
-
-QString KItemListAccessibleCell::actionText(int, Text, int) const
-{
-    return QString();
-}
-
-bool KItemListAccessibleCell::doAction(int, int, const QVariantList &)
-{
-    return false;
-}
-
-#endif
-
 int KItemListAccessibleCell::index() const
 {
     return m_index;
@@ -473,32 +436,38 @@ QObject* KItemListAccessibleCell::object() const
 
 // Container Interface
 KItemListContainerAccessible::KItemListContainerAccessible(KItemListContainer* container) :
-    QAccessibleWidgetEx(container)
-{}
+    QAccessibleWidget(container)
+{
+}
 
-KItemListContainerAccessible::~KItemListContainerAccessible ()
-{}
+KItemListContainerAccessible::~KItemListContainerAccessible()
+{
+}
 
-int KItemListContainerAccessible::childCount () const
+int KItemListContainerAccessible::childCount() const
 {
     return 1;
 }
 
-int KItemListContainerAccessible::indexOfChild ( const QAccessibleInterface*  child ) const
+int KItemListContainerAccessible::indexOfChild(const QAccessibleInterface* child) const
 {
     if (child->object() == container()->controller()->view()) {
-        return 1;
+        return 0;
     }
     return -1;
 }
 
-int KItemListContainerAccessible::navigate ( QAccessible::RelationFlag relation, int index, QAccessibleInterface** target ) const
+QAccessibleInterface* KItemListContainerAccessible::child(int index) const
 {
-    if (relation == QAccessible::Child) {
-        *target = new KItemListViewAccessible(container()->controller()->view());
-        return 0;
+    if (index == 0) {
+        return QAccessible::queryAccessibleInterface(container()->controller()->view());
     }
-    return QAccessibleWidgetEx::navigate(relation, index, target);
+    return Q_NULLPTR;
+}
+
+const KItemListContainer* KItemListContainerAccessible::container() const
+{
+    return qobject_cast<KItemListContainer*>(object());
 }
 
 #endif // QT_NO_ACCESSIBILITY