]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistviewaccessible.cpp
Merge branch 'master' into frameworks
[dolphin.git] / src / kitemviews / kitemlistviewaccessible.cpp
index 980c310640c105715a7c857d0714a75201197d5a..d9ddd58f84045b9831b56ee74a601013250220b4 100644 (file)
@@ -1,9 +1,32 @@
+/***************************************************************************
+ *   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/qtableview.h>
 #include <QtGui/qaccessible2.h>
 #include <qgraphicsscene.h>
 #include <qgraphicsview.h>
 #include <KDebug>
 #include <QHash>
 
-#ifndef QT_NO_ACCESSIBILITY
-
-#ifndef QT_NO_ITEMVIEWS
-
-KItemListView *KItemListViewAccessible::view() const
+KItemListView* KItemListViewAccessible::view() const
 {
     return qobject_cast<KItemListView*>(object());
 }
 
-KItemListViewAccessible::KItemListViewAccessible(KItemListView *view_)
-    QAccessibleObjectEx(view_)
+KItemListViewAccessible::KItemListViewAccessible(KItemListView* view_) :
+    QAccessibleObjectEx(view_)
 {
     Q_ASSERT(view());
 }
 
-KItemListViewAccessible::~KItemListViewAccessible()
+void KItemListViewAccessible::modelReset()
 {
 }
 
-void KItemListViewAccessible::modelReset()
-{}
+QAccessible::Role KItemListViewAccessible::cellRole() const
+{
+    return QAccessible::Cell;
+}
 
-QAccessibleTable2CellInterface *KItemListViewAccessible::cell(int index) const
+QAccessibleTable2CellInterfaceKItemListViewAccessible::cell(int index) const
 {
-    if (index > 0) {
+    if (index < 0 || index >= view()->model()->count()) {
+        return 0;
+    } else {
         return new KItemListAccessibleCell(view(), index);
     }
-    return 0;
 }
 
-QAccessibleTable2CellInterface *KItemListViewAccessible::cellAt(int row, int column) const
+QVariant KItemListViewAccessible::invokeMethodEx(Method, int, const QVariantList&)
+{
+    return QVariant();
+}
+
+QAccessibleTable2CellInterface* KItemListViewAccessible::cellAt(int row, int column) const
 {
-    return cell(column * (row - 1) + column) ;
+    return cell(columnCount() * row + column);
 }
 
-QAccessibleInterface *KItemListViewAccessible::caption() const
+QAccessibleInterfaceKItemListViewAccessible::caption() const
 {
     return 0;
 }
 
 QString KItemListViewAccessible::columnDescription(int) const
 {
-    return "";
+    return QString();
 }
 
 int KItemListViewAccessible::columnCount() const
 {
-    return view()->layouter()->columnCount();
+    return view()->m_layouter->columnCount();
 }
 
 int KItemListViewAccessible::rowCount() const
 {
+    if (columnCount() <= 0) {
+        return 0;
+    }
+
     int itemCount = view()->model()->count();
     int rowCount = itemCount / columnCount();
-    if (itemCount % rowCount) {
+
+    if (rowCount <= 0) {
+        return 0;
+    }
+
+    if (itemCount % columnCount()) {
         ++rowCount;
     }
     return rowCount;
@@ -73,7 +109,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
@@ -88,7 +124,7 @@ int KItemListViewAccessible::selectedRowCount() const
 
 QString KItemListViewAccessible::rowDescription(int) const
 {
-    return "";
+    return QString();
 }
 
 QList<QAccessibleTable2CellInterface*> KItemListViewAccessible::selectedCells() const
@@ -110,7 +146,7 @@ QList<int> KItemListViewAccessible::selectedRows() const
     return QList<int>();
 }
 
-QAccessibleInterface *KItemListViewAccessible::summary() const
+QAccessibleInterfaceKItemListViewAccessible::summary() const
 {
     return 0;
 }
@@ -125,7 +161,7 @@ bool KItemListViewAccessible::isRowSelected(int) const
     return false;
 }
 
-bool KItemListViewAccessible::selectRow(int row)
+bool KItemListViewAccessible::selectRow(int)
 {
     return true;
 }
@@ -148,55 +184,56 @@ bool KItemListViewAccessible::unselectColumn(int)
 QAccessible2::TableModelChange KItemListViewAccessible::modelChange() const
 {
     QAccessible2::TableModelChange change;
-    // FIXME
+    change.lastRow = rowCount();
+    change.lastColumn = columnCount();
     return change;
 }
 
 QAccessible::Role KItemListViewAccessible::role(int child) const
 {
     Q_ASSERT(child >= 0);
+
     if (child > 0) {
         return QAccessible::Cell;
+    } else {
+        return QAccessible::Table;
     }
-    return QAccessible::Table;
 }
 
 QAccessible::State KItemListViewAccessible::state(int child) const
 {
     if (child) {
-        QAccessibleInterface *iface;
-        navigate(Child,child,&iface);
-        if (iface) {
-            return iface->state(0);
+        QAccessibleInterface* interface = 0;
+        navigate(Child, child, &interface);
+        if (interface) {
+            return interface->state(0);
         }
     }
+
     return QAccessible::Normal | QAccessible::HasInvokeExtension;
 }
 
 int KItemListViewAccessible::childAt(int x, int y) const
 {
-    QPointF point = QPointF(x,y);
+    const QPointF point = QPointF(x,y);
     return view()->itemAt(view()->mapFromScene(point));
 }
 
 int KItemListViewAccessible::childCount() const
 {
-    return rowCount() * columnCount();
+    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->getIndex();
+    const KItemListAccessibleCell* widget = static_cast<const KItemListAccessibleCell*>(interface);
+    return widget->index() + 1;
 }
 
-QString KItemListViewAccessible::text(Text t, int child) const
+QString KItemListViewAccessible::text(Text, int child) const
 {
     Q_ASSERT(child == 0);
-    if (t == QAccessible::Description) {
-        return QObject::tr("List of files present in the current directory");
-    }
-    return QObject::tr("File List");
+    return QString();
 }
 
 QRect KItemListViewAccessible::rect(int child) const
@@ -205,56 +242,68 @@ QRect KItemListViewAccessible::rect(int child) const
     if (!view()->isVisible()) {
         return QRect();
     }
-    QPoint origin = view()->scene()->views()[0]->mapToGlobal(QPoint(0, 0));
-    QRect viewRect = view()->geometry().toRect();
-    return viewRect.translated(origin);
+
+    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();
+    }
 }
 
-int KItemListViewAccessible::navigate(RelationFlag relation, int index, QAccessibleInterface **iface) const
+int KItemListViewAccessible::navigate(RelationFlag relation, int index, QAccessibleInterface** interface) const
 {
-    *iface = 0;
+    *interface = 0;
+
     switch (relation) {
-        case QAccessible::Child: {
-            Q_ASSERT(index > 0);
-            *iface = cell(index);
-            if (*iface) {
-                return 0;
-            }
-            break;
+    case QAccessible::Child:
+        Q_ASSERT(index > 0);
+        *interface = cell(index - 1);
+        if (*interface) {
+            return 0;
         }
-        default:
-            break;
+        break;
+
+    default:
+        break;
     }
+
     return -1;
 }
 
-QAccessible::Relation KItemListViewAccessible::relationTo(int, const QAccessibleInterface *, int) const
+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();
 }
-bool KItemListViewAccessible::doAction(int, int, const QVariantList &)
+
+bool KItemListViewAccessible::doAction(int, int, const QVariantList&)
 {
     return false;
 }
+
 #endif
 
-// TABLE CELL
+// Table Cell
 
-KItemListAccessibleCell::KItemListAccessibleCell(KItemListView *view_, int index_)
-    : view(view_)
-    , index(index_)
+KItemListAccessibleCell::KItemListAccessibleCell(KItemListView* view, int index) :
+    m_view(view),
+    m_index(index)
 {
-    Q_ASSERT(index_ > 0);
+    Q_ASSERT(index >= 0 && index < view->model()->count());
 }
 
 int KItemListAccessibleCell::columnExtent() const
@@ -279,24 +328,24 @@ QList<QAccessibleInterface*> KItemListAccessibleCell::columnHeaderCells() const
 
 int KItemListAccessibleCell::columnIndex() const
 {
-    return view->layouter()->itemColumn(index);
+    return m_view->m_layouter->itemColumn(m_index);
 }
 
 int KItemListAccessibleCell::rowIndex() const
 {
-    return view->layouter()->itemRow(index);
+    return m_view->m_layouter->itemRow(m_index);
 }
 
 bool KItemListAccessibleCell::isSelected() const
 {
-    return view->controller()->selectionManager()->isSelected(index-1);
+    return m_view->controller()->selectionManager()->isSelected(m_index);
 }
 
-void KItemListAccessibleCell::rowColumnExtents(int *row, int *column, int *rowExtents, int *columnExtents, bool *selected) const
+void KItemListAccessibleCell::rowColumnExtents(int* row, int* column, int* rowExtents, int* columnExtents, bool* selected) const
 {
-    KItemListViewLayouter* layouter = view->layouter();
-    *row = layouter->itemRow(index);
-    *column = layouter->itemColumn(index);
+    const KItemListViewLayouter* layouter = m_view->m_layouter;
+    *row = layouter->itemRow(m_index);
+    *column = layouter->itemColumn(m_index);
     *rowExtents = 1;
     *columnExtents = 1;
     *selected = isSelected();
@@ -304,7 +353,7 @@ void KItemListAccessibleCell::rowColumnExtents(int *row, int *column, int *rowEx
 
 QAccessibleTable2Interface* KItemListAccessibleCell::table() const
 {
-    return QAccessible::queryAccessibleInterface(view)->table2Interface();
+    return QAccessible::queryAccessibleInterface(m_view)->table2Interface();
 }
 
 QAccessible::Role KItemListAccessibleCell::role(int child) const
@@ -316,102 +365,109 @@ QAccessible::Role KItemListAccessibleCell::role(int child) const
 QAccessible::State KItemListAccessibleCell::state(int child) const
 {
     Q_ASSERT(child == 0);
-    QAccessible::State st = Normal;
+    QAccessible::State state = Normal;
 
     if (isSelected()) {
-         st |= Selected;
+        state |= Selected;
     }
-    if (view->controller()->selectionManager()->currentItem() == index) {
-        st |= Focused;
+
+    if (m_view->controller()->selectionManager()->currentItem() == m_index) {
+        state |= Focused;
     }
 
-    st |= Selectable;
-    st |= Focusable;
+    state |= Selectable;
+    state |= Focusable;
 
-    if (view->controller()->selectionBehavior() == KItemListController::MultiSelection){
-        st |= MultiSelectable;
+    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;
+        } else {
+            state |= Collapsed;
+        }
     }
 
-    return st;
+    return state;
 }
 
 bool KItemListAccessibleCell::isExpandable() const
 {
-    return false;
+    return m_view->model()->isExpandable(m_index);
 }
 
 QRect KItemListAccessibleCell::rect(int) const
 {
-    QRect r = view->itemRect(index-1).toRect();
-    if (r.isNull()) {
+    QRect rect = m_view->itemRect(m_index).toRect();
+
+    if (rect.isNull()) {
         return QRect();
     }
-    r.translate(view->mapToScene(QPointF(0.0, 0.0)).toPoint());
-    r.translate(view->scene()->views()[0]->mapToGlobal(QPoint(0, 0)));
-    return r;
+
+    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
 {
     Q_ASSERT(child == 0);
     Q_UNUSED(child)
-    const QHash<QByteArray, QVariant> data = view->model()->data(index-1);
+
     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, int child, const QString&)
 {
     Q_ASSERT(child == 0);
-    // FIXME - is this even allowed on the KItemListWidget?
 }
 
 bool KItemListAccessibleCell::isValid() const
 {
-    return view && (index > 0);
+    return m_view && (m_index >= 0) && (m_index < m_view->model()->count());
 }
 
-int KItemListAccessibleCell::navigate(RelationFlag relation, int index, QAccessibleInterface **iface) const
+int KItemListAccessibleCell::childAt(int, int) const
 {
-    if (relation == Ancestor && index == 1) {
-        *iface = new KItemListViewAccessible(view);
-        return 0;
-    }
+    return 0;
+}
 
-    *iface = 0;
-    if (!view) {
-        return -1;
-    }
+int KItemListAccessibleCell::childCount() const
+{
+    return 0;
+}
 
-    switch (relation) {
+int KItemListAccessibleCell::indexOfChild(const QAccessibleInterface* child) const
+{
+    Q_UNUSED(child);
+    return -1;
+}
 
-    case Child: {
-        return -1;
-    }
-    case Sibling:
-        if (index > 0) {
-            QAccessibleInterface *parent = queryAccessibleInterface(view);
-            int ret = parent->navigate(QAccessible::Child, index, iface);
-            delete parent;
-            if (*iface) {
-                return ret;
-            }
-        }
-        return -1;
-    default:
-        break;
+int KItemListAccessibleCell::navigate(RelationFlag relation, int index, QAccessibleInterface** interface) const
+{
+    if (relation == Ancestor && index == 1) {
+        *interface = new KItemListViewAccessible(m_view);
+        return 0;
     }
 
+    *interface = 0;
     return -1;
 }
 
-QAccessible::Relation KItemListAccessibleCell::relationTo(int child, const QAccessibleInterface *, int otherChild) const
+QAccessible::Relation KItemListAccessibleCell::relationTo(int child, const QAccessibleInterface, int otherChild) const
 {
     Q_ASSERT(child == 0);
     Q_ASSERT(otherChild == 0);
@@ -419,6 +475,7 @@ QAccessible::Relation KItemListAccessibleCell::relationTo(int child, const QAcce
 }
 
 #ifndef QT_NO_ACTION
+
 int KItemListAccessibleCell::userActionCount(int) const
 {
     return 0;
@@ -429,42 +486,60 @@ QString KItemListAccessibleCell::actionText(int, Text, int) const
     return QString();
 }
 
-bool KItemListAccessibleCell::doAction(int, int, const QVariantList &)
+bool KItemListAccessibleCell::doAction(int, int, const QVariantList&)
 {
     return false;
 }
 
 #endif
 
-KItemListContainerAccessible::KItemListContainerAccessible(KItemListContainer *container)
-    : QAccessibleWidgetEx(container)
-{}
+int KItemListAccessibleCell::index() const
+{
+    return m_index;
+}
 
-KItemListContainerAccessible::~KItemListContainerAccessible ()
-{}
+QObject* KItemListAccessibleCell::object() const
+{
+    return 0;
+}
+
+// Container Interface
+KItemListContainerAccessible::KItemListContainerAccessible(KItemListContainer* container) :
+    QAccessibleWidgetEx(container)
+{
+}
 
-int KItemListContainerAccessible::childCount () const
+KItemListContainerAccessible::~KItemListContainerAccessible()
+{
+}
+
+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;
+    } else {
+        return -1;
     }
-    return -1;
 }
 
-int KItemListContainerAccessible::navigate ( QAccessible::RelationFlag relation, int index, QAccessibleInterface ** target ) const
+int KItemListContainerAccessible::navigate(QAccessible::RelationFlag relation, int index, QAccessibleInterface** target) const
 {
     if (relation == QAccessible::Child) {
         *target = new KItemListViewAccessible(container()->controller()->view());
         return 0;
+    } else {
+        return QAccessibleWidgetEx::navigate(relation, index, target);
     }
-    return QAccessibleWidgetEx::navigate(relation, index, target);
 }
 
-#endif // QT_NO_ITEMVIEWS
+const KItemListContainer* KItemListContainerAccessible::container() const
+{
+    return qobject_cast<KItemListContainer*>(object());
+}
 
 #endif // QT_NO_ACCESSIBILITY