qaccessiblequickitem.cpp 9.34 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/
5
**
6
** This file is part of the QtQml module of the Qt Toolkit.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
37
**
38 39 40 41 42 43 44 45 46 47 48 49
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qaccessiblequickitem.h"
#include "QtQuick/private/qquickitem_p.h"
#include "QtQuick/private/qquicktext_p.h"
#include "QtQuick/private/qquickaccessibleattached_p.h"

QT_BEGIN_NAMESPACE

QAccessibleQuickItem::QAccessibleQuickItem(QQuickItem *item)
50
    : QQmlAccessible(item)
51 52 53 54 55 56 57 58 59 60
{
}

int QAccessibleQuickItem::childCount() const
{
    return childItems().count();
}

QRect QAccessibleQuickItem::rect() const
{
61
    const QRect r = itemScreenRect(item());
62 63

    if (!r.isValid()) {
64
        qWarning() << item()->metaObject()->className() << item()->property("accessibleText") << r;
65 66 67 68 69 70 71
    }
    return r;
}

QRect QAccessibleQuickItem::viewRect() const
{
    // ### no canvas in some cases.
72
    if (!item()->canvas()) {
73 74 75
        return QRect();
    }

76
    QQuickCanvas *canvas = item()->canvas();
77 78 79 80 81 82 83
    QPoint screenPos = canvas->mapToGlobal(QPoint(0,0));
    return QRect(screenPos, canvas->size());
}


bool QAccessibleQuickItem::clipsChildren() const
{
84
    return static_cast<QQuickItem *>(item())->clip();
85 86 87 88 89
}


QAccessibleInterface *QAccessibleQuickItem::parent() const
{
90
    QQuickItem *parent = item()->parentItem();
91
    if (parent) {
92
        QQuickCanvas *canvas = item()->canvas();
93 94 95 96 97
        // Jump out to the scene widget if the parent is the root item.
        // There are two root items, QQuickCanvas::rootItem and
        // QQuickView::declarativeRoot. The former is the true root item,
        // but is not a part of the accessibility tree. Check if we hit
        // it here and return an interface for the scene instead.
98
        if (canvas && (parent == canvas->rootItem())) {
99 100
            return QAccessible::queryAccessibleInterface(canvas);
        } else {
101
            return QAccessible::queryAccessibleInterface(parent);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        }
    }
    return 0;
}

QAccessibleInterface *QAccessibleQuickItem::child(int index) const
{
    QList<QQuickItem *> children = childItems();

    if (index < 0 || index >= children.count())
        return 0;

    QQuickItem *child = children.at(index);
    if (!child) // FIXME can this happen?
        return 0;

118
    return QAccessible::queryAccessibleInterface(child);
119 120 121 122 123
}

int QAccessibleQuickItem::indexOfChild(const QAccessibleInterface *iface) const
{
    QList<QQuickItem*> kids = childItems();
124
    return kids.indexOf(static_cast<QQuickItem*>(iface->object()));
125 126 127 128
}

QList<QQuickItem *> QAccessibleQuickItem::childItems() const
{
129 130 131 132 133 134 135 136
    if (    role() == QAccessible::Button ||
            role() == QAccessible::CheckBox ||
            role() == QAccessible::RadioButton ||
            role() == QAccessible::SpinBox ||
            role() == QAccessible::EditableText ||
            role() == QAccessible::Slider ||
            role() == QAccessible::PageTab ||
            role() == QAccessible::ProgressBar)
137
        return QList<QQuickItem *>();
138 139 140 141 142 143 144 145

    QList<QQuickItem *> items;
    Q_FOREACH (QQuickItem *child, item()->childItems()) {
        QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(child);
        if (itemPrivate->isAccessible)
            items.append(child);
    }
    return items;
146 147
}

148
QAccessible::State QAccessibleQuickItem::state() const
149
{
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    QAccessible::State state;

    if (item()->hasActiveFocus())
        state.focused = true;

    QAccessible::Role r = role();
    switch (r) {
    case QAccessible::Button: {
        QVariant checkable = item()->property("checkable");
        if (!checkable.toBool())
            break;
        // fall through
    }
    case QAccessible::CheckBox:
    case QAccessible::RadioButton: {
        // FIXME when states are extended: state.checkable = true;
        state.checked = item()->property("checked").toBool();
        break;
    }
    default:
        break;
171
    }
172

173 174 175 176 177 178 179 180
    return state;
}

QAccessible::Role QAccessibleQuickItem::role() const
{
    // Workaround for setAccessibleRole() not working for
    // Text items. Text items are special since they are defined
    // entirely from C++ (setting the role from QML works.)
181
    if (qobject_cast<QQuickText*>(const_cast<QQuickItem *>(item())))
182 183
        return QAccessible::StaticText;

184
    QVariant v = QQuickAccessibleAttached::property(item(), "role");
185 186 187 188 189 190 191 192 193
    bool ok;
    QAccessible::Role role = (QAccessible::Role)v.toInt(&ok);
    if (!ok)    // Not sure if this check is needed.
        role = QAccessible::Pane;
    return role;
}

bool QAccessibleQuickItem::isAccessible() const
{
194
    return item()->d_func()->isAccessible;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
}

QString QAccessibleQuickItem::text(QAccessible::Text textType) const
{
    // handles generic behavior not specific to an item
    switch (textType) {
    case QAccessible::Name: {
        QVariant accessibleName = QQuickAccessibleAttached::property(object(), "name");
        if (!accessibleName.isNull())
            return accessibleName.toString();
        break;}
    case QAccessible::Description: {
        QVariant accessibleDecription = QQuickAccessibleAttached::property(object(), "description");
        if (!accessibleDecription.isNull())
            return accessibleDecription.toString();
        break;}
#ifdef Q_ACCESSIBLE_QUICK_ITEM_ENABLE_DEBUG_DESCRIPTION
    case QAccessible::DebugDescription: {
        QString debugString;
        debugString = QString::fromAscii(object()->metaObject()->className()) + QLatin1Char(' ');
        debugString += isAccessible() ? QLatin1String("enabled") : QLatin1String("disabled");
        return debugString;
        break; }
#endif
    case QAccessible::Value:
    case QAccessible::Help:
    case QAccessible::Accelerator:
    default:
        break;
    }

    // the following blocks handles item-specific behavior
    if (role() == QAccessible::EditableText) {
        if (textType == QAccessible::Value) {
            QVariant text = object()->property("text");
            return text.toString();
        } else if (textType == QAccessible::Name) {
            return object()->objectName();
        }
    } else {
        if (textType == QAccessible::Name) {
            QVariant text = object()->property("text");
            return text.toString();
        }
    }

    return QString();
}

244 245 246 247 248 249 250
void *QAccessibleQuickItemValueInterface::interface_cast(QAccessible::InterfaceType t)
{
    if (t == QAccessible::ValueInterface)
       return static_cast<QAccessibleValueInterface*>(this);
    return QAccessibleQuickItem::interface_cast(t);
}

251
QVariant QAccessibleQuickItemValueInterface::currentValue() const
252
{
253
    return item()->property("value");
254 255 256 257
}

void QAccessibleQuickItemValueInterface::setCurrentValue(const QVariant &value)
{
258
    item()->setProperty("value", value);
259 260
}

261
QVariant QAccessibleQuickItemValueInterface::maximumValue() const
262
{
263
    return item()->property("maximumValue");
264 265
}

266
QVariant QAccessibleQuickItemValueInterface::minimumValue() const
267
{
268
    return item()->property("minimumValue");
269 270
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
/*!
  \internal
  Shared between QAccessibleQuickItem and QAccessibleQuickView
*/
QRect itemScreenRect(QQuickItem *item)
{
    // ### no canvas in some cases.
    // ### Should we really check for 0 opacity?
    if (!item->canvas() ||!item->isVisible() || qFuzzyIsNull(item->opacity())) {
        return QRect();
    }

    QSize itemSize((int)item->width(), (int)item->height());
    // ### If the bounding rect fails, we first try the implicit size, then we go for the
    // parent size. WE MIGHT HAVE TO REVISIT THESE FALLBACKS.
    if (itemSize.isEmpty()) {
        itemSize = QSize((int)item->implicitWidth(), (int)item->implicitHeight());
288
        if (itemSize.isEmpty() && item->parentItem())
289 290 291 292 293 294 295 296 297 298
            // ### Seems that the above fallback is not enough, fallback to use the parent size...
            itemSize = QSize((int)item->parentItem()->width(), (int)item->parentItem()->height());
    }

    QPointF scenePoint = item->mapToScene(QPointF(0, 0));
    QPoint screenPos = item->canvas()->mapToGlobal(scenePoint.toPoint());
    return QRect(screenPos, itemSize);
}


299

300
QT_END_NAMESPACE