+/***************************************************************************
+ * 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 <qgraphicsscene.h>
-#include <qgraphicsview.h>
-
-#include <KDebug>
-#include <QHash>
-
-#ifndef QT_NO_ACCESSIBILITY
+#include <QGraphicsScene>
+#include <QGraphicsView>
KItemListView* KItemListViewAccessible::view() const
{
}
KItemListViewAccessible::KItemListViewAccessible(KItemListView* view_) :
- QAccessibleObjectEx(view_)
+ QAccessibleObject(view_)
{
Q_ASSERT(view());
+ m_cells.resize(childCount());
}
-void KItemListViewAccessible::modelReset()
-{}
-
-QAccessible::Role KItemListViewAccessible::cellRole() const
+KItemListViewAccessible::~KItemListViewAccessible()
{
- return QAccessible::Cell;
+ foreach (AccessibleIdWrapper idWrapper, m_cells) {
+ if (idWrapper.isValid) {
+ QAccessible::deleteAccessibleInterface(idWrapper.id);
+ }
+ }
}
-QAccessibleTable2CellInterface* KItemListViewAccessible::cell(int index) const
+void* KItemListViewAccessible::interface_cast(QAccessible::InterfaceType type)
{
- if (index > 0) {
- return new KItemListAccessibleCell(view(), index);
+ if (type == QAccessible::TableInterface) {
+ return static_cast<QAccessibleTableInterface*>(this);
}
- return 0;
+ return nullptr;
+}
+
+void KItemListViewAccessible::modelReset()
+{
}
-QVariant KItemListViewAccessible::invokeMethodEx(Method, int, const QVariantList &)
+QAccessibleInterface* KItemListViewAccessible::cell(int index) const
{
- return QVariant();
+ if (index < 0 || index >= view()->model()->count()) {
+ return nullptr;
+ }
+
+ if (m_cells.size() <= index) {
+ m_cells.resize(childCount());
+ }
+ Q_ASSERT(index < m_cells.size());
+
+ 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 QAccessible::accessibleInterface(idWrapper.id);
}
-QAccessibleTable2CellInterface* KItemListViewAccessible::cellAt(int row, int column) const
+QAccessibleInterface* KItemListViewAccessible::cellAt(int row, int column) const
{
- return cell(column* (row - 1) + column) ;
+ return cell(columnCount() * row + column);
}
QAccessibleInterface* KItemListViewAccessible::caption() const
{
- return 0;
+ return nullptr;
}
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) {
+ 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;
int KItemListViewAccessible::selectedCellCount() const
{
- return view()->controller()->selectionManager()->selectedItems().size();
+ return view()->controller()->selectionManager()->selectedItems().count();
}
int KItemListViewAccessible::selectedColumnCount() const
return QString();
}
-QList<QAccessibleTable2CellInterface*> KItemListViewAccessible::selectedCells() const
+QList<QAccessibleInterface*> KItemListViewAccessible::selectedCells() const
{
- QList<QAccessibleTable2CellInterface*> cells;
- Q_FOREACH (int index, view()->controller()->selectionManager()->selectedItems()) {
+ QList<QAccessibleInterface*> cells;
+ const auto items = view()->controller()->selectionManager()->selectedItems();
+ cells.reserve(items.count());
+ for (int index : items) {
cells.append(cell(index));
}
return cells;
QAccessibleInterface* KItemListViewAccessible::summary() const
{
- return 0;
+ return nullptr;
}
bool KItemListViewAccessible::isColumnSelected(int) const
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;
- 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 nullptr;
}
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);
+ 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);
- 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;
+ const QGraphicsScene* scene = view()->scene();
+ if (scene) {
+ const QPoint origin = scene->views().at(0)->mapToGlobal(QPoint(0, 0));
+ const QRect viewRect = view()->geometry().toRect();
+ return viewRect.translated(origin);
+ } else {
+ return QRect();
+ }
}
-QString KItemListViewAccessible::actionText(int, Text, int) const
+QAccessibleInterface* KItemListViewAccessible::child(int index) const
{
- return QString();
+ if (index >= 0 && index < childCount()) {
+ return cell(index);
+ }
+ return nullptr;
}
-bool KItemListViewAccessible::doAction(int, int, const QVariantList &)
+KItemListViewAccessible::AccessibleIdWrapper::AccessibleIdWrapper() :
+ isValid(false),
+ id(0)
{
- return false;
}
-#endif
-
// Table Cell
KItemListAccessibleCell::KItemListAccessibleCell(KItemListView* view, int index) :
m_view(view),
m_index(index)
{
- Q_ASSERT(index > 0);
+ 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 nullptr;
}
int KItemListAccessibleCell::columnExtent() 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
{
- return m_view->controller()->selectionManager()->isSelected(m_index-1);
+ 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;
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-1).toRect();
+ 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-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, const QString&)
+{
+}
+
+QAccessibleInterface* KItemListAccessibleCell::child(int) const
{
- Q_ASSERT(child == 0);
+ return nullptr;
}
bool KItemListAccessibleCell::isValid() const
{
- return m_view && (m_index > 0);
+ 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 nullptr;
}
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
-{
- 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;
-}
-
-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 &)
+QAccessibleInterface* KItemListAccessibleCell::parent() const
{
- return false;
+ return QAccessible::queryAccessibleInterface(m_view);
}
-#endif
-
int KItemListAccessibleCell::index() const
{
return m_index;
QObject* KItemListAccessibleCell::object() const
{
- return 0;
+ return nullptr;
}
// 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 nullptr;
+}
+
+const KItemListContainer* KItemListContainerAccessible::container() const
+{
+ return qobject_cast<KItemListContainer*>(object());
}
#endif // QT_NO_ACCESSIBILITY