source: trunk/doc/src/accessible.qdoc@ 551

Last change on this file since 551 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 26.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \page accessible.html
44 \title Accessibility
45 \ingroup accessibility
46
47 \tableofcontents
48
49 \section1 Introduction
50
51 Accessibility in computer software is making applications usable
52 for people with disabilities. This could be achieved by providing
53 keyboard shortcuts, a high-contrast user interface that uses
54 specially selected colors and fonts, or support for assistive tools
55 such as screen readers and braille displays.
56
57 An application does not usually communicate directly with
58 assistive tools but through an assistive technology, which is a
59 bridge for exchange of information between the applications and
60 the tools. Information about user interface elements, such
61 as buttons and scroll bars, is exposed to the assistive technologies.
62 Qt supports Microsoft Active Accessibility (MSAA) on Windows and
63 Mac OS X Accessibility on Mac OS X.
64 On Unix/X11, support is preliminary. The individual technologies
65 are abstracted from Qt, and there is only a single interface to
66 consider. We will use MSAA throughout this document when we need
67 to address technology related issues.
68
69 In this overview document, we will examine the overall Qt
70 accessibility architecture, and how to implement accessibility for
71 custom widgets and elements.
72
73 \section1 Architecture
74
75 Providing accessibility is a collaboration between accessibility
76 compliant applications, the assistive technology, and the
77 assistive tools.
78
79 \image accessibilityarchitecture.png
80
81 Accessibility compliant applications are called AT-Servers while
82 assistive tools are called AT-Clients. A Qt application will
83 typically be an AT-Server, but specialized programs might also
84 function like AT-Clients. We will refer to clients and servers
85 when talking about AT-Clients and AT-Servers in the rest of this
86 document.
87
88 We will from now on focus on the Qt accessibility interface and
89 how it is implemented to create Qt applications that support
90 accessibility.
91
92 \section2 Accessibility in Qt
93
94 When we communicate with the assistive technologies, we need to
95 describe Qt's user interface in a way that they can understand. Qt
96 applications use QAccessibleInterface to expose information about the
97 individual UI elements. Currently, Qt provides support for its widgets
98 and widget parts, e.g., slider handles, but the interface could
99 also be implemented for any QObject if necessary. QAccessible
100 contains enums that describe the UI. The description is mainly
101 based on MSAA and is independent of Qt. We will examine the enums
102 in the course of this document.
103
104 The structure of the UI is represented as a tree of
105 QAccessibleInterface subclasses. You can think of this as a
106 representation of a UI like the QObject tree built by Qt. Objects
107 can be widgets or widget parts (such as scroll bar handles). We
108 examine the tree in detail in the next section.
109
110 Servers notify clients through \l{QAccessible::}{updateAccessibility()}
111 about changes in objects by sending events, and the clients
112 register to receive the events. The available events are defined
113 by the QAccessible::Event enum. The clients may then query for
114 the object that generated the event through
115 QAccessible::queryAccessibleInterface().
116
117 Three of the enums in QAccessible help clients query and alter
118 accessible objects:
119
120 \list
121 \o \l{QAccessible::}{Role}: Describes the role the object
122 fills in the user interface, e.g., if it is a main
123 window, a text caret, or a cell in an item view.
124 \o \l{QAccessible::}{Action}: The actions that the
125 clients can perform on the objects, e.g., pushing a
126 button.
127 \o \l{QAccessible::}{Relation}: Describes the relationship
128 between objects in the object tree.
129 This is used for navigation.
130 \endlist
131
132 The clients also have some possibilities to get the content of
133 objects, e.g., a button's text; the object provides strings
134 defined by the QAccessible::Text enum, that give information
135 about content.
136
137 The objects can be in a number of different states as defined by
138 the \l{QAccessible::}{State} enum. Examples of states are whether
139 the object is disabled, if it has focus, or if it provides a pop-up
140 menu.
141
142 \section2 The Accessible Object Tree
143
144 As mentioned, a tree structure is built from the accessible
145 objects of an application. By navigating through the tree, the
146 clients can access all elements in the UI. Object relations give
147 clients information about the UI. For instance, a slider handle is
148 a child of the slider to which it belongs. QAccessible::Relation
149 describes the various relationships the clients can ask objects
150 for.
151
152 Note that there are no direct mapping between the Qt QObject tree
153 and the accessible object tree. For instance, scroll bar handles
154 are accessible objects but are not widgets or objects in Qt.
155
156 AT-Clients have access to the accessibility object tree through
157 the root object in the tree, which is the QApplication. They can
158 query other objects through QAccessible::navigate(), which fetches
159 objects based on \l{QAccessible::}{Relation}s. The children of any
160 node is 1-based numbered. The child numbered 0 is the object
161 itself. The children of all interfaces are numbered this way,
162 i.e., it is not a fixed numbering from the root node in the entire
163 tree.
164
165 Qt provides accessible interfaces for its widgets. Interfaces for
166 any QObject subclass can be requested through
167 QAccessible::queryInterface(). A default implementation is
168 provided if a more specialized interface is not defined. An
169 AT-Client cannot acquire an interface for accessible objects that
170 do not have an equivalent QObject, e.g., scroll bar handles, but
171 they appear as normal objects through interfaces of parent
172 accessible objects, e.g., you can query their relationships with
173 QAccessible::relationTo().
174
175 To illustrate, we present an image of an accessible object tree.
176 Beneath the tree is a table with examples of object relationships.
177
178 \image accessibleobjecttree.png
179
180 The labels in top-down order are: the QAccessibleInterface class
181 name, the widget for which an interface is provided, and the
182 \l{QAccessible::}{Role} of the object. The Position, PageLeft and
183 PageRight correspond to the slider handle, the slider groove left
184 and the slider groove right, respectively. These accessible objects
185 do not have an equivalent QObject.
186
187 \table 40%
188 \header
189 \o Source Object
190 \o Target Object
191 \o Relation
192 \row
193 \o Slider
194 \o Indicator
195 \o Controller
196 \row
197 \o Indicator
198 \o Slider
199 \o Controlled
200 \row
201 \o Slider
202 \o Application
203 \o Ancestor
204 \row
205 \o Application
206 \o Slider
207 \o Child
208 \row
209 \o PushButton
210 \o Indicator
211 \o Sibling
212 \endtable
213
214 \section2 The Static QAccessible Functions
215
216 The accessibility is managed by QAccessible's static functions,
217 which we will examine shortly. They produce QAccessible
218 interfaces, build the object tree, and initiate the connection
219 with MSAA or the other platform specific technologies. If you are
220 only interested in learning how to make your application
221 accessible, you can safely skip over this section to
222 \l{Implementing Accessibility}.
223
224 The communication between clients and the server is initiated when
225 \l{QAccessible::}{setRootObject()} is called. This is done when
226 the QApplication instance is instantiated and you should not have
227 to do this yourself.
228
229 When a QObject calls \l{QAccessible::}{updateAccessibility()},
230 clients that are listening to events are notified of the
231 change. The function is used to post events to the assistive
232 technology, and accessible \l{QAccessible::Event}{events} are
233 posted by \l{QAccessible::}{updateAccessibility()}.
234
235 \l{QAccessible::}{queryAccessibleInterface()} returns accessible
236 interfaces for \l{QObject}s. All widgets in Qt provide interfaces;
237 if you need interfaces to control the behavior of other \l{QObject}
238 subclasses, you must implement the interfaces yourself, although
239 the QAccessibleObject convenience class implements parts of the
240 functionality for you.
241
242 The factory that produces accessibility interfaces for QObjects is
243 a function of type QAccessible::InterfaceFactory. It is possible
244 to have several factories installed. The last factory installed
245 will be the first to be asked for interfaces.
246 \l{QAccessible::}{queryAccessibleInterface()} uses the factories
247 to create interfaces for \l{QObject}s. Normally, you need not be
248 concerned about factories because you can implement plugins that
249 produce interfaces. We will give examples of both approaches
250 later.
251
252 \section1 Implementing Accessibility
253
254 To provide accessibility support for a widget or other user
255 interface element, you need to implement the QAccessibleInterface
256 and distribute it in a QAccessiblePlugin. It is also possible to
257 compile the interface into the application and provide a
258 QAccessible::InterfaceFactory for it. The factory can be used if
259 you link statically or do not want the added complexity of
260 plugins. This can be an advantage if you, for instance, are
261 delivering a 3-rd party library.
262
263 All widgets and other user interface elements should have
264 interfaces and plugins. If you want your application to support
265 accessibility, you will need to consider the following:
266
267 \list
268 \o Qt already implements accessibility for its own widgets.
269 We therefore recommend that you use Qt widgets where possible.
270 \o A QAccessibleInterface needs to be implemented for each element
271 that you want to make available to accessibility clients.
272 \o You need to send accessibility events from the custom
273 user interface elements that you implement.
274 \endlist
275
276 In general, it is recommended that you are somewhat familiar with
277 MSAA, which Qt originally was built for. You should also study
278 the enum values of QAccessible, which describe the roles, actions,
279 relationships, and events that you need to consider.
280
281 Note that you can examine how Qt's widgets implement their
282 accessibility. One major problem with the MSAA standard is that
283 interfaces are often implemented in an inconsistent way. This
284 makes life difficult for clients and often leads to guesswork on
285 object functionality.
286
287 It is possible to implement interfaces by inheriting
288 QAccessibleInterface and implementing its pure virtual functions.
289 In practice, however, it is usually preferable to inherit
290 QAccessibleObject or QAccessibleWidget, which implement part of
291 the functionality for you. In the next section, we will see an
292 example of implementing accessibility for a widget by inheriting
293 the QAccessibleWidget class.
294
295 \section2 The QAccessibleObject and QAccessibleWidget Convenience Classes
296
297 When implementing an accessibility interface for widgets, one would
298 as a rule inherit QAccessibleWidget, which is a convenience class
299 for widgets. Another available convenience class, which is
300 inherited by QAccessibleWidget, is the QAccessibleObject, which
301 implements part of the interface for QObjects.
302
303 The QAccessibleWidget provides the following functionality:
304
305 \list
306 \o It handles the navigation of the tree and
307 hit testing of the objects.
308 \o It handles events, roles, and actions that are common for all
309 \l{QWidget}s.
310 \o It handles action and methods that can be performed on
311 all widgets.
312 \o It calculates bounding rectangles with
313 \l{QAccessibleInterface::}{rect()}.
314 \o It gives \l{QAccessibleInterface::}{text()} strings that are
315 appropriate for a generic widget.
316 \o It sets the \l{QAccessible::State}{states} that
317 are common for all widgets.
318 \endlist
319
320 \section2 QAccessibleWidget Example
321
322 Instead of creating a custom widget and implementing an interface
323 for it, we will show how accessibility can be implemented for one of
324 Qt's standard widgets: QSlider. Making this widget accessible
325 demonstrates many of the issues that need to be faced when making
326 a custom widget accessible.
327
328 The slider is a complex control that functions as a
329 \l{QAccessible::}{Controller} for its accessible children.
330 This relationship must be known by the interface (for
331 \l{QAccessibleInterface::}{relationTo()} and
332 \l{QAccessibleInterface::}{navigate()}). This can be done
333 using a controlling signal, which is a mechanism provided by
334 QAccessibleWidget. We do this in the constructor:
335
336 \snippet doc/src/snippets/accessibilityslidersnippet.cpp 0
337
338 The choice of signal shown is not important; the same principles
339 apply to all signals that are declared in this way. Note that we
340 use QLatin1String to ensure that the signal name is correctly
341 specified.
342
343 When an accessible object is changed in a way that users need
344 to know about, it notifies clients of the change by sending them
345 an event via the accessible interface. This is how QSlider calls
346 \l{QAccessibleInterface::}{updateAccessibility()} to indicate that
347 its value has changed:
348
349 \snippet doc/src/snippets/qabstractsliderisnippet.cpp 0
350 \dots
351 \snippet doc/src/snippets/qabstractsliderisnippet.cpp 1
352 \dots
353 \snippet doc/src/snippets/qabstractsliderisnippet.cpp 2
354
355 Note that the call is made after the value of the slider has
356 changed because clients may query the new value immediately after
357 receiving the event.
358
359 The interface must be able to calculate bounding rectangles of
360 itself and any children that do not provide an interface of their
361 own. The \c QAccessibleSlider has three such children identified by
362 the private enum, \c SliderElements, which has the following values:
363 \c PageLeft (the rectangle on the left hand side of the slider
364 handle), \c PageRight (the rectangle on the right hand side of the
365 handle), and \c Position (the slider handle). Here is the
366 implementation of \l{QAccessibleInterface::}{rect()}:
367
368 \snippet doc/src/snippets/accessibilityslidersnippet.cpp 1
369 \dots
370 \snippet doc/src/snippets/accessibilityslidersnippet.cpp 2
371 \dots
372
373 The first part of the function, which we have omitted, uses the
374 current \l{QStyle}{style} to calculate the slider handle's
375 bounding rectangle; it is stored in \c srect. Notice that child 0,
376 covered in the default case in the above code, is the slider itself,
377 so we can simply return the QSlider bounding rectangle obtained
378 from the superclass, which is effectively the value obtained from
379 QAccessibleWidget::rect().
380
381 \snippet doc/src/snippets/accessibilityslidersnippet.cpp 3
382
383 Before the rectangle is returned it must be mapped to screen
384 coordinates.
385
386 The QAccessibleSlider must reimplement
387 QAccessibleInterface::childCount() since it manages children
388 without interfaces.
389
390 The \l{QAccessibleInterface::}{text()} function returns the
391 QAccessible::Text strings for the slider:
392
393 \snippet doc/src/snippets/accessibilityslidersnippet.cpp 4
394
395 The \c slider() function returns a pointer to the interface's
396 QSlider. Some values are left for the superclass's implementation.
397 Not all values are appropriate for all accessible objects, as you
398 can see for QAccessible::Value case. You should just return an
399 empty string for those values where no relevant text can be
400 provided.
401
402 The implementation of the \l{QAccessibleInterface::}{role()}
403 function is straightforward:
404
405 \snippet doc/src/snippets/accessibilityslidersnippet.cpp 5
406
407 The role function should be reimplemented by all objects and
408 describes the role of themselves and the children that do not
409 provide accessible interfaces of their own.
410
411 Next, the accessible interface needs to return the
412 \l{QAccessible::State}{states} that the slider can be in. We look
413 at parts of the \c state() implementation to show how just a few
414 of the states are handled:
415
416 \snippet doc/src/snippets/accessibilityslidersnippet.cpp 6
417 \dots
418 \snippet doc/src/snippets/accessibilityslidersnippet.cpp 7
419
420 The superclass implementation of
421 \l{QAccessibleInterface::}{state()}, uses the
422 QAccessibleInterface::state() implementation. We simply need to
423 disable the buttons if the slider is at its minimum or maximum.
424
425 We have now exposed the information we have about the slider to
426 the clients. For the clients to be able to alter the slider - for
427 example, to change its value - we must provide information about
428 the actions that can be performed and perform them upon request.
429 We discuss this in the next section.
430
431 \section2 Handling Action Requests from Clients
432
433 QAccessible provides a number of \l{QAccessible::}{Action}s
434 that can be performed on request from clients. If an
435 accessible object supports actions, it should reimplement the
436 following functions from QAccessibleInterface:
437
438 \list
439 \o \l{QAccessibleInterface::}{actionText()} returns
440 strings that describe each action. The descriptions
441 to be made available are one for each
442 \l{QAccessible::}{Text} enum value.
443 \o \l{QAccessibleInterface::}{doAction()} executes requests
444 from clients to perform actions.
445 \endlist
446
447 Note that a client can request any action from an object. If
448 the object does not support the action, it returns false from
449 \l{QAccessibleInterface::}{doAction()}.
450
451 None of the standard actions take any parameters. It is possible
452 to provide user-defined actions that can take parameters.
453 The interface must then also reimplement
454 \l{QAccessibleInterface::}{userActionCount()}. Since this is not
455 defined in the MSAA specification, it is probably only useful to
456 use this if you know which specific AT-Clients will use the
457 application.
458
459 QAccessibleInterface gives another technique for clients to handle
460 accessible objects. It works basically the same way, but uses the
461 concept of methods in place of actions. The available methods are
462 defined by the QAccessible::Method enum. The following functions
463 need to be reimplemented from QAccessibleInterface if the
464 accessible object is to support methods:
465
466 \list
467 \o \l{QAccessibleInterface::}{supportedMethods()} returns
468 a QSet of \l{QAccessible::}{Method} values that are
469 supported by the object.
470 \o \l{QAccessibleInterface::}{invokeMethod()} executes
471 methods requested by clients.
472 \endlist
473
474 The action mechanism will probably be substituted by providing
475 methods in place of the standard actions.
476
477 To see examples on how to implement actions and methods, you
478 could examine the QAccessibleObject and QAccessibleWidget
479 implementations. You might also want to take a look at the
480 MSAA documentation.
481
482 \section2 Implementing Accessible Plugins
483
484 In this section we will explain the procedure of implementing
485 accessible plugins for your interfaces. A plugin is a class stored
486 in a shared library that can be loaded at run-time. It is
487 convenient to distribute interfaces as plugins since they will only
488 be loaded when required.
489
490 Creating an accessible plugin is achieved by inheriting
491 QAccessiblePlugin, reimplementing \l{QAccessiblePlugin::}{keys()}
492 and \l{QAccessiblePlugin::}{create()} from that class, and adding
493 one or two macros. The \c .pro file must be altered to use the
494 plugin template, and the library containing the plugin must be
495 placed on a path where Qt searches for accessible plugins.
496
497 We will go through the implementation of \c SliderPlugin, which is an
498 accessible plugin that produces interfaces for the
499 QAccessibleSlider we implemented in the \l{QAccessibleWidget Example}.
500 We start with the \c key() function:
501
502 \snippet doc/src/snippets/accessibilitypluginsnippet.cpp 0
503
504 We simply need to return the class name of the single interface
505 our plugin can create an accessible interface for. A plugin
506 can support any number of classes; just add more class names
507 to the string list. We move on to the \c create() function:
508
509 \snippet doc/src/snippets/accessibilitypluginsnippet.cpp 1
510
511 We check whether the interface requested is for the QSlider; if it
512 is, we create and return an interface for it. Note that \c object
513 will always be an instance of \c classname. You must return 0 if
514 you do not support the class.
515 \l{QAccessible::}{updateAccessibility()} checks with the
516 available accessibility plugins until it finds one that does not
517 return 0.
518
519 Finally, you need to include macros in the cpp file:
520
521 \snippet doc/src/snippets/accessibilitypluginsnippet.cpp 2
522
523 The Q_EXPORT_PLUGIN2 macro exports the plugin in the \c
524 SliderPlugin class into the \c acc_sliderplugin library. The first
525 argument is the name of the plugin library file, excluding the
526 file suffix, and the second is the class name. For more information
527 on plugins, consult the plugins \l{How to Create Qt
528 Plugins}{overview document}.
529
530 You can omit the the first macro unless you want the plugin
531 to be statically linked with the application.
532
533 \section2 Implementing Interface Factories
534
535 If you do not want to provide plugins for your accessibility
536 interfaces, you can use an interface factory
537 (QAccessible::InterfaceFactory), which is the recommended way to
538 provide accessible interfaces in a statically-linked application.
539
540 A factory is a function pointer for a function that takes the same
541 parameters as \l{QAccessiblePlugin}'s
542 \l{QAccessiblePlugin::}{create()} - a QString and a QObject. It
543 also works the same way. You install the factory with the
544 \l{QAccessible::}{installFactory()} function. We give an example
545 of how to create a factory for the \c SliderPlugin class:
546
547 \snippet doc/src/snippets/accessibilityfactorysnippet.cpp 0
548 \dots
549 \snippet doc/src/snippets/accessibilityfactorysnippet.cpp 1
550
551 \omit
552
553 \section1 Implementing Bridges for Other Assistive Technologies
554
555 An accessibility bridge provides the means for an assistive
556 technology to talk to Qt. On Windows and Mac, the built-in bridges
557 will be used. On UNIX, however, there are no built-in standard
558 assistive technology, and it might therefore be necessary to
559 implement an accessible bridge.
560
561 A bridge is implemented by inheriting QAccessibleBridge for the
562 technology to support. The class defines the interface that Qt
563 needs an assistive technology to support:
564
565 \list
566 \o A root object. This is the root in the accessible
567 object tree and is of type QAccessibleInterface.
568 \o Receive events from from accessible objects.
569 \endlist
570
571 The root object is set with the
572 \l{QAccessibleBridge::}{setRootObject()}. In the case of Qt, this
573 will always be an interface for the QApplication instance of the
574 application.
575
576 Event notification is sent through
577 \l{QAccessibleBridge::}{notifyAccessibilityUpdate()}. This
578 function is called by \l{QAccessible::}{updateAccessibility()}. Even
579 though the bridge needs only to implement these two functions, it
580 must be able to communicate the entire QAccessibleInterface to the
581 underlying technology. How this is achieved is, naturally, up to
582 the individual bridge and none of Qt's concern.
583
584 As with accessible interfaces, you distribute accessible bridges
585 in plugins. Accessible bridge plugins are subclasses of the
586 QAccessibleBridgePlugin class; the class defines the functions
587 \l{QAccessibleBridgePlugin::}{create()} and
588 \l{QAccessibleBridgePlugin::}{keys()}, which must me
589 reimplemented. If Qt finds a built-in bridge to use, it will
590 ignore any available plugins.
591
592 \endomit
593
594 \section1 Further Reading
595
596 The \l{Cross-Platform Accessibility Support in Qt 4} document contains a more
597 general overview of Qt's accessibility features and discusses how it is
598 used on each platform.
599 issues
600*/
Note: See TracBrowser for help on using the repository browser.