~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/accessible/qaccessibleobject.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the accessibility module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qaccessibleobject.h"
 
30
 
 
31
#ifndef QT_NO_ACCESSIBILITY
 
32
 
 
33
#include "qapplication.h"
 
34
#include "qwidget.h"
 
35
#include "qpointer.h"
 
36
#include "qmetaobject.h"
 
37
#include "qvarlengtharray.h"
 
38
 
 
39
class QAccessibleObjectPrivate
 
40
{
 
41
public:
 
42
    QPointer<QObject> object;
 
43
 
 
44
    QList<QByteArray> actionList() const;
 
45
};
 
46
 
 
47
QList<QByteArray> QAccessibleObjectPrivate::actionList() const
 
48
{
 
49
    QList<QByteArray> actionList;
 
50
 
 
51
    if (!object)
 
52
        return actionList;
 
53
 
 
54
    const QMetaObject *mo = object->metaObject();
 
55
    Q_ASSERT(mo);
 
56
 
 
57
    QByteArray defaultAction = QMetaObject::normalizedSignature(
 
58
        mo->classInfo(mo->indexOfClassInfo("DefaultSlot")).value());
 
59
 
 
60
    for (int i = 0; i < mo->methodCount(); ++i) {
 
61
        const QMetaMethod member = mo->method(i);
 
62
        if (member.methodType() != QMetaMethod::Slot && member.access() != QMetaMethod::Public)
 
63
            continue;
 
64
 
 
65
        if (!qstrcmp(member.tag(), "QACCESSIBLE_SLOT")) {
 
66
            if (member.signature() == defaultAction)
 
67
                actionList.prepend(defaultAction);
 
68
            else
 
69
                actionList << member.signature();
 
70
        }
 
71
    }
 
72
 
 
73
    return actionList;
 
74
}
 
75
 
 
76
/*!
 
77
    \class QAccessibleObject
 
78
    \brief The QAccessibleObject class implements parts of the
 
79
    QAccessibleInterface for QObjects.
 
80
 
 
81
    \ingroup accessibility
 
82
 
 
83
    This class is mainly provided for convenience. All subclasses of
 
84
    the QAccessibleInterface that provide implementations of non-widget objects
 
85
    should use this class as their base class.
 
86
 
 
87
    \sa QAccessible, QAccessibleWidget
 
88
*/
 
89
 
 
90
/*!
 
91
    Creates a QAccessibleObject for \a object.
 
92
*/
 
93
QAccessibleObject::QAccessibleObject(QObject *object)
 
94
{
 
95
    d = new QAccessibleObjectPrivate;
 
96
    d->object = object;
 
97
}
 
98
 
 
99
/*!
 
100
    Destroys the QAccessibleObject.
 
101
 
 
102
    This only happens when a call to release() decrements the internal
 
103
    reference counter to zero.
 
104
*/
 
105
QAccessibleObject::~QAccessibleObject()
 
106
{
 
107
    delete d;
 
108
}
 
109
 
 
110
/*!
 
111
    \reimp
 
112
*/
 
113
QObject *QAccessibleObject::object() const
 
114
{
 
115
#ifndef QT_NO_DEBUG
 
116
    if (!isValid())
 
117
        qWarning("QAccessibleInterface is invalid. Crash pending...");
 
118
#endif
 
119
    return d->object;
 
120
}
 
121
 
 
122
/*!
 
123
    \reimp
 
124
*/
 
125
bool QAccessibleObject::isValid() const
 
126
{
 
127
    return !d->object.isNull();
 
128
}
 
129
 
 
130
/*! \reimp */
 
131
QRect QAccessibleObject::rect(int) const
 
132
{
 
133
    return QRect();
 
134
}
 
135
 
 
136
/*! \reimp */
 
137
void QAccessibleObject::setText(Text, int, const QString &)
 
138
{
 
139
}
 
140
 
 
141
/*! \reimp */
 
142
int QAccessibleObject::userActionCount(int) const
 
143
{
 
144
    return 0;
 
145
}
 
146
 
 
147
/*! \reimp */
 
148
bool QAccessibleObject::doAction(int, int, const QVariantList &)
 
149
{
 
150
    return false;
 
151
}
 
152
 
 
153
static const char * const action_text[][5] =
 
154
{
 
155
    // Name, Description, Value, Help, Accelerator
 
156
    { "Press", "", "", "", "Space" },
 
157
    { "SetFocus", "Passes focus to this widget", "", "", "" },
 
158
    { "Increase", "", "", "", "" },
 
159
    { "Decrease", "", "", "", "" },
 
160
    { "Accept", "", "", "", "" },
 
161
    { "Cancel", "", "", "", "" },
 
162
    { "Select", "", "", "", "" },
 
163
    { "ClearSelection", "", "", "", "" },
 
164
    { "RemoveSelection", "", "", "", "" },
 
165
    { "ExtendSelection", "", "", "", "" },
 
166
    { "AddToSelection", "", "", "", "" }
 
167
};
 
168
 
 
169
/*! \reimp */
 
170
QString QAccessibleObject::actionText(int action, Text t, int child) const
 
171
{
 
172
    if (child || action > FirstStandardAction || action < LastStandardAction || t > Accelerator)
 
173
        return QString();
 
174
 
 
175
    return QString(action_text[-(action - FirstStandardAction)][t]);
 
176
}
 
177
 
 
178
 
 
179
/*!
 
180
    \class QAccessibleApplication
 
181
    \brief The QAccessibleApplication class implements the QAccessibleInterface for QApplication.
 
182
 
 
183
    \internal
 
184
 
 
185
    \ingroup accessibility
 
186
*/
 
187
 
 
188
/*!
 
189
    Creates a QAccessibleApplication for the QApplication object referenced by qApp.
 
190
*/
 
191
QAccessibleApplication::QAccessibleApplication()
 
192
: QAccessibleObject(qApp)
 
193
{
 
194
}
 
195
 
 
196
// all toplevel widgets except popups and the desktop
 
197
static QWidgetList topLevelWidgets()
 
198
{
 
199
    QWidgetList list;
 
200
    const QWidgetList tlw(qApp->topLevelWidgets());
 
201
    for (int i = 0; i < tlw.count(); ++i) {
 
202
        QWidget *w = tlw.at(i);
 
203
        if (!(w->windowType() == Qt::Popup) && !(w->windowType() == Qt::Desktop))
 
204
            list.append(w);
 
205
    }
 
206
 
 
207
    return list;
 
208
}
 
209
 
 
210
/*! \reimp */
 
211
int QAccessibleApplication::childCount() const
 
212
{
 
213
    return topLevelWidgets().count();
 
214
}
 
215
 
 
216
/*! \reimp */
 
217
int QAccessibleApplication::indexOfChild(const QAccessibleInterface *child) const
 
218
{
 
219
    if (!child->object()->isWidgetType())
 
220
        return -1;
 
221
 
 
222
    const QWidgetList tlw(topLevelWidgets());
 
223
    int index = tlw.indexOf(static_cast<QWidget*>(child->object()));
 
224
    if (index != -1)
 
225
        ++index;
 
226
    return index;
 
227
}
 
228
 
 
229
/*! \reimp */
 
230
int QAccessibleApplication::childAt(int x, int y) const
 
231
{
 
232
    const QWidgetList tlw(topLevelWidgets());
 
233
    for (int i = 0; i < tlw.count(); ++i) {
 
234
        QWidget *w = tlw.at(i);
 
235
        if (w->frameGeometry().contains(x,y))
 
236
            return i+1;
 
237
    }
 
238
    return -1;
 
239
}
 
240
 
 
241
/*! \reimp */
 
242
QAccessible::Relation QAccessibleApplication::relationTo(int child, const
 
243
        QAccessibleInterface *other, int otherChild) const
 
244
{
 
245
    QObject *o = other ? other->object() : 0;
 
246
    if (!o)
 
247
        return Unrelated;
 
248
 
 
249
    if(o == object()) {
 
250
        if (child && !otherChild)
 
251
            return Child;
 
252
        if (!child && otherChild)
 
253
            return Ancestor;
 
254
        if (!child && !otherChild)
 
255
            return Self;
 
256
    }
 
257
 
 
258
    QWidgetList tlw(topLevelWidgets());
 
259
    if (tlw.contains(qobject_cast<QWidget*>(o)))
 
260
        return Ancestor;
 
261
 
 
262
    for (int i = 0; i < tlw.count(); ++i) {
 
263
        QWidget *w = tlw.at(i);
 
264
        QObjectList cl = qFindChildren<QObject *>(w, QString());
 
265
        if (cl.contains(o))
 
266
            return Ancestor;
 
267
    }
 
268
 
 
269
    return Unrelated;
 
270
}
 
271
 
 
272
/*! \reimp */
 
273
int QAccessibleApplication::navigate(RelationFlag relation, int entry,
 
274
                                     QAccessibleInterface **target) const
 
275
{
 
276
    if (!target)
 
277
        return -1;
 
278
 
 
279
    *target = 0;
 
280
    QObject *targetObject = 0;
 
281
 
 
282
    switch (relation) {
 
283
    case Self:
 
284
        targetObject = object();
 
285
        break;
 
286
    case Child:
 
287
        if (entry > 0 && entry <= childCount()) {
 
288
            const QWidgetList tlw(topLevelWidgets());
 
289
            if (tlw.count() >= entry)
 
290
                targetObject = tlw.at(entry-1);
 
291
        } else {
 
292
            return -1;
 
293
        }
 
294
        break;
 
295
    case FocusChild:
 
296
        targetObject = qApp->activeWindow();
 
297
        break;
 
298
    default:
 
299
        break;
 
300
    }
 
301
    *target = QAccessible::queryAccessibleInterface(targetObject);
 
302
    return *target ? 0 : -1;
 
303
}
 
304
 
 
305
/*! \reimp */
 
306
QString QAccessibleApplication::text(Text t, int) const
 
307
{
 
308
    switch (t) {
 
309
    case Name:
 
310
        if (qApp->activeWindow())
 
311
            return qApp->activeWindow()->windowTitle();
 
312
        break;
 
313
    case Description:
 
314
        return qApp->applicationFilePath();
 
315
    default:
 
316
        break;
 
317
    }
 
318
    return QString();
 
319
}
 
320
 
 
321
/*! \reimp */
 
322
QAccessible::Role QAccessibleApplication::role(int) const
 
323
{
 
324
    return Application;
 
325
}
 
326
 
 
327
/*! \reimp */
 
328
QAccessible::State QAccessibleApplication::state(int) const
 
329
{
 
330
    return qApp->activeWindow() ? Focused : Normal;
 
331
}
 
332
 
 
333
/*! \reimp */
 
334
int QAccessibleApplication::userActionCount(int) const
 
335
{
 
336
    return 1;
 
337
}
 
338
 
 
339
/*! \reimp */
 
340
bool QAccessibleApplication::doAction(int action, int child, const QVariantList &param)
 
341
{
 
342
    if (action == 0 || action == 1) {
 
343
        QWidget *w = 0;
 
344
        w = qApp->activeWindow();
 
345
        if (!w)
 
346
            w = topLevelWidgets().at(0);
 
347
        if (!w)
 
348
            return false;
 
349
        w->activateWindow();
 
350
        return true;
 
351
    }
 
352
    return QAccessibleObject::doAction(action, child, param);
 
353
}
 
354
 
 
355
/*! \reimp */
 
356
QString QAccessibleApplication::actionText(int action, Text text, int child) const
 
357
{
 
358
    QString str;
 
359
    if ((action == 0 || action == 1) && !child) switch (text) {
 
360
    case Name:
 
361
        return QApplication::tr("Activate");
 
362
    case Description:
 
363
        return QApplication::tr("Activates the application main widget");
 
364
    default:
 
365
        break;
 
366
    }
 
367
    return QAccessibleObject::actionText(action, text, child);
 
368
}
 
369
 
 
370
#endif //QT_NO_ACCESSIBILITY