Change copyrights from Nokia to Digia
[profile/ivi/qtdeclarative.git] / src / plugins / accessible / shared / qqmlaccessible.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qnamespace.h>
43 #include "qqmlaccessible.h"
44
45 #ifndef QT_NO_ACCESSIBILITY
46
47 QT_BEGIN_NAMESPACE
48
49
50 QString Q_GUI_EXPORT qTextBeforeOffsetFromString(int offset, QAccessible2::BoundaryType boundaryType,
51         int *startOffset, int *endOffset, const QString& text);
52 QString Q_GUI_EXPORT qTextAtOffsetFromString(int offset, QAccessible2::BoundaryType boundaryType,
53         int *startOffset, int *endOffset, const QString& text);
54 QString Q_GUI_EXPORT qTextAfterOffsetFromString(int offset, QAccessible2::BoundaryType boundaryType,
55         int *startOffset, int *endOffset, const QString& text);
56
57 QQmlAccessible::QQmlAccessible(QObject *object)
58     :QAccessibleObject(object)
59 {
60 }
61
62 void *QQmlAccessible::interface_cast(QAccessible::InterfaceType t)
63 {
64     if (t == QAccessible::ActionInterface)
65         return static_cast<QAccessibleActionInterface*>(this);
66     return QAccessibleObject::interface_cast(t);
67 }
68
69 QQmlAccessible::~QQmlAccessible()
70 {
71 }
72
73 QAccessibleInterface *QQmlAccessible::childAt(int x, int y) const
74 {
75     // Note that this function will disregard stacking order.
76     // (QAccessibleQuickView::childAt() does this correctly and more efficient)
77
78     // If the item clips its children, we can return early if the coordinate is outside its rect
79     if (clipsChildren()) {
80         if (!rect().contains(x, y))
81             return 0;
82     }
83
84     for (int i = childCount() - 1; i >= 0; --i) {
85         QAccessibleInterface *childIface = child(i);
86         if (childIface && !childIface->state().invisible) {
87             if (childIface->rect().contains(x, y))
88                 return childIface;
89         }
90         delete childIface;
91     }
92     return 0;
93 }
94
95 QAccessible::State QQmlAccessible::state() const
96 {
97     QAccessible::State state;
98
99     //QRect viewRect(QPoint(0, 0), m_implementation->size());
100     //QRect itemRect(m_item->scenePos().toPoint(), m_item->boundingRect().size().toSize());
101
102     QRect viewRect_ = viewRect();
103     QRect itemRect = rect();
104
105    // qDebug() << "viewRect" << viewRect << "itemRect" << itemRect;
106     // error case:
107     if (viewRect_.isNull() || itemRect.isNull()) {
108         state.invisible = true;
109     }
110
111     if (!viewRect_.intersects(itemRect)) {
112         state.offscreen = true;
113         // state.invisible = true; // no set at this point to ease development
114     }
115
116     if (!object()->property("visible").toBool() || qFuzzyIsNull(object()->property("opacity").toDouble())) {
117         state.invisible = true;
118     }
119
120     if ((role() == QAccessible::CheckBox || role() == QAccessible::RadioButton) && object()->property("checked").toBool()) {
121         state.checked = true;
122     }
123
124     if (role() == QAccessible::EditableText)
125         state.focusable = true;
126
127     //qDebug() << "state?" << m_item->property("state").toString() << m_item->property("status").toString() << m_item->property("visible").toString();
128
129     return state;
130 }
131
132 QStringList QQmlAccessible::actionNames() const
133 {
134     QStringList actions;
135     switch (role()) {
136     case QAccessible::PushButton:
137         actions << QAccessibleActionInterface::pressAction();
138         break;
139     case QAccessible::RadioButton:
140     case QAccessible::CheckBox:
141         actions << QAccessibleActionInterface::toggleAction();
142         break;
143     case QAccessible::Slider:
144     case QAccessible::SpinBox:
145     case QAccessible::ScrollBar:
146         actions << QAccessibleActionInterface::increaseAction()
147                 << QAccessibleActionInterface::decreaseAction();
148         break;
149     default:
150         break;
151     }
152     return actions;
153 }
154
155 void QQmlAccessible::doAction(const QString &actionName)
156 {
157     // Look for and call the accessible[actionName]Action() function on the item.
158     // This allows for overriding the default action handling.
159     const QByteArray functionName = "accessible" + actionName.toLatin1() + "Action";
160     if (object()->metaObject()->indexOfMethod(functionName + "()") != -1) {
161         QMetaObject::invokeMethod(object(), functionName);
162         return;
163     }
164
165     // Role-specific default action handling follows. Items are excepted to provide
166     // properties according to role conventions. These will then be read and/or updated
167     // by the accessibility system.
168     //   Checkable roles   : checked
169     //   Value-based roles : (via the value interface: value, minimumValue, maximumValue), stepSize
170     switch (role()) {
171     case QAccessible::RadioButton:
172     case QAccessible::CheckBox: {
173         QVariant checked = object()->property("checked");
174         if (checked.isValid()) {
175             if (actionName == QAccessibleActionInterface::toggleAction()) {
176                 object()->setProperty("checked",  QVariant(!checked.toBool()));
177             }
178         }
179         break;
180     }
181     case QAccessible::Slider:
182     case QAccessible::SpinBox:
183     case QAccessible::Dial:
184     case QAccessible::ScrollBar: {
185         if (actionName != QAccessibleActionInterface::increaseAction() &&
186             actionName != QAccessibleActionInterface::decreaseAction())
187             break;
188
189         // Update the value using QAccessibleValueInterface, respecting
190         // the minimum and maximum value (if set). Also check for and
191         // use the "stepSize" property on the item
192         if (QAccessibleValueInterface *valueIface = valueInterface()) {
193             QVariant valueV = valueIface->currentValue();
194             qreal newValue = valueV.toInt();
195
196             QVariant stepSizeV = object()->property("stepSize");
197             qreal stepSize = stepSizeV.isValid() ? stepSizeV.toReal() : qreal(1.0);
198             if (actionName == QAccessibleActionInterface::increaseAction()) {
199                 newValue += stepSize;
200             } else {
201                 newValue -= stepSize;
202             }
203
204             QVariant minimumValueV = valueIface->minimumValue();
205             if (minimumValueV.isValid()) {
206                 newValue = qMax(newValue, minimumValueV.toReal());
207             }
208             QVariant maximumValueV = valueIface->maximumValue();
209             if (maximumValueV.isValid()) {
210                 newValue = qMin(newValue, maximumValueV.toReal());
211             }
212
213             valueIface->setCurrentValue(QVariant(newValue));
214         }
215         break;
216     }
217     default:
218         break;
219     }
220 }
221
222 QStringList QQmlAccessible::keyBindingsForAction(const QString &actionName) const
223 {
224     Q_UNUSED(actionName)
225     return QStringList();
226 }
227
228 QT_END_NAMESPACE
229
230 #endif // QT_NO_ACCESSIBILITY