source: trunk/plugins/src/accessible/widgets/qaccessiblemenu.cpp

Last change on this file was 204, checked in by rudi, 14 years ago

Added plugin source code

File size: 7.7 KB
Line 
1#include "qaccessiblemenu.h"
2
3#include <qpopupmenu.h>
4#include <qmenubar.h>
5#include <qstyle.h>
6
7class MyPopupMenu : public QPopupMenu
8{
9 friend class QAccessiblePopup;
10};
11
12QAccessiblePopup::QAccessiblePopup( QObject *o )
13: QAccessibleWidget( o )
14{
15 Q_ASSERT( o->inherits("QPopupMenu") );
16}
17
18QPopupMenu *QAccessiblePopup::popupMenu() const
19{
20 return (QPopupMenu*)object();
21}
22
23int QAccessiblePopup::childCount() const
24{
25 return popupMenu()->count();
26}
27
28QRESULT QAccessiblePopup::queryChild( int /*control*/, QAccessibleInterface **iface ) const
29{
30 *iface = 0;
31 return QS_FALSE;
32}
33
34QRect QAccessiblePopup::rect( int control ) const
35{
36 if ( !control )
37 return QAccessibleWidget::rect( control );
38
39 QRect r = popupMenu()->itemGeometry( control - 1 );
40 QPoint tlp = popupMenu()->mapToGlobal( QPoint( 0,0 ) );
41
42 return QRect( tlp.x() + r.x(), tlp.y() + r.y(), r.width(), r.height() );
43}
44
45int QAccessiblePopup::controlAt( int x, int y ) const
46{
47 QPoint p = popupMenu()->mapFromGlobal( QPoint( x,y ) );
48 MyPopupMenu *mp = (MyPopupMenu*)popupMenu();
49 return mp->itemAtPos( p, FALSE ) + 1;
50}
51
52int QAccessiblePopup::navigate( NavDirection direction, int startControl ) const
53{
54 if ( direction != NavFirstChild && direction != NavLastChild && direction != NavFocusChild && !startControl )
55 return QAccessibleWidget::navigate( direction, startControl );
56
57 switch ( direction ) {
58 case NavFirstChild:
59 return 1;
60 case NavLastChild:
61 return childCount();
62 case NavNext:
63 case NavDown:
64 return (startControl + 1) > childCount() ? -1 : startControl + 1;
65 case NavPrevious:
66 case NavUp:
67 return (startControl - 1) < 1 ? -1 : startControl - 1;
68 default:
69 break;
70 }
71 return -1;
72}
73
74QString QAccessiblePopup::text( Text t, int control ) const
75{
76 QString tx = QAccessibleWidget::text( t, control );
77 if ( !!tx )
78 return tx;
79
80 int id;
81 QMenuItem *item = 0;
82 if ( control ) {
83 id = popupMenu()->idAt( control - 1 );
84 item = popupMenu()->findItem( id );
85 }
86
87 switch ( t ) {
88 case Name:
89 if ( !control )
90 return popupMenu()->caption();
91 return stripAmp( popupMenu()->text( id ) );
92 case Help:
93 return popupMenu()->whatsThis( id );
94 case Accelerator:
95 return hotKey( popupMenu()->text( id ) );
96 case DefaultAction:
97 if ( !item || item->isSeparator() || !item->isEnabled() )
98 break;
99 if ( item->popup() )
100 return QPopupMenu::tr("Open");
101 return QPopupMenu::tr("Execute");
102 default:
103 break;
104 }
105 return tx;
106}
107
108QAccessible::Role QAccessiblePopup::role( int control ) const
109{
110 if ( !control )
111 return PopupMenu;
112
113 QMenuItem *item = popupMenu()->findItem( popupMenu()->idAt( control -1 ) );
114 if ( item && item->isSeparator() )
115 return Separator;
116 return MenuItem;
117}
118
119QAccessible::State QAccessiblePopup::state( int control ) const
120{
121 int s = QAccessibleWidget::state( control );
122 if ( !control )
123 return (State)s;
124
125 int id = popupMenu()->idAt( control -1 );
126 QMenuItem *item = popupMenu()->findItem( id );
127 if ( !item )
128 return (State)s;
129
130 if ( popupMenu()->style().styleHint( QStyle::SH_PopupMenu_MouseTracking ) )
131 s |= HotTracked;
132 if ( item->isSeparator() || !item->isEnabled() )
133 s |= Unavailable;
134 if ( popupMenu()->isCheckable() && item->isChecked() )
135 s |= Checked;
136 if ( popupMenu()->isItemActive( id ) )
137 s |= Focused;
138
139 return (State)s;
140}
141
142bool QAccessiblePopup::doDefaultAction( int control )
143{
144 if ( !control )
145 return FALSE;
146
147 int id = popupMenu()->idAt( control -1 );
148 QMenuItem *item = popupMenu()->findItem( id );
149 if ( !item || !item->isEnabled() )
150 return FALSE;
151
152 popupMenu()->activateItemAt( control - 1);
153 return TRUE;
154}
155
156bool QAccessiblePopup::setFocus( int control )
157{
158 if ( !control )
159 return FALSE;
160
161 int id = popupMenu()->idAt( control -1 );
162 QMenuItem *item = popupMenu()->findItem( id );
163 if ( !item || !item->isEnabled() )
164 return FALSE;
165
166 popupMenu()->setActiveItem( control - 1 );
167 return TRUE;
168}
169
170
171class MyMenuBar : public QMenuBar
172{
173 friend class QAccessibleMenuBar;
174};
175
176QAccessibleMenuBar::QAccessibleMenuBar( QObject *o )
177: QAccessibleWidget( o )
178{
179 Q_ASSERT( o->inherits( "QMenuBar" ) );
180}
181
182QMenuBar *QAccessibleMenuBar::menuBar() const
183{
184 return (QMenuBar*)object();
185}
186
187int QAccessibleMenuBar::childCount() const
188{
189 return menuBar()->count();
190}
191
192QRESULT QAccessibleMenuBar::queryChild( int /*control*/, QAccessibleInterface **iface ) const
193{
194 *iface = 0;
195 return QS_FALSE;
196}
197
198QRect QAccessibleMenuBar::rect( int control ) const
199{
200 if ( !control )
201 return QAccessibleWidget::rect( control );
202
203 MyMenuBar *mb = (MyMenuBar*)menuBar();
204 QRect r = mb->itemRect( control - 1 );
205 QPoint tlp = mb->mapToGlobal( QPoint( 0,0 ) );
206
207 return QRect( tlp.x() + r.x(), tlp.y() + r.y(), r.width(), r.height() );
208}
209
210int QAccessibleMenuBar::controlAt( int x, int y ) const
211{
212 MyMenuBar *mb = (MyMenuBar*)menuBar();
213 QPoint p = mb->mapFromGlobal( QPoint( x,y ) );
214 return mb->itemAtPos( p ) + 1;
215}
216
217int QAccessibleMenuBar::navigate( NavDirection direction, int startControl ) const
218{
219 if ( direction != NavFirstChild && direction != NavLastChild && direction != NavFocusChild && !startControl )
220 return QAccessibleWidget::navigate( direction, startControl );
221
222 switch ( direction ) {
223 case NavFirstChild:
224 return 1;
225 case NavLastChild:
226 return childCount();
227 case NavNext:
228 case NavRight:
229 return (startControl + 1) > childCount() ? -1 : startControl + 1;
230 case NavPrevious:
231 case NavLeft:
232 return (startControl - 1) < 1 ? -1 : startControl - 1;
233 default:
234 break;
235 }
236
237 return -1;
238}
239
240QString QAccessibleMenuBar::text( Text t, int control ) const
241{
242 QString tx = QAccessibleWidget::text( t, control );
243 if ( !!tx )
244 return tx;
245 if ( !control )
246 return tx;
247
248 int id = menuBar()->idAt( control - 1 );
249 switch ( t ) {
250 case Name:
251 return stripAmp( menuBar()->text( id ) );
252 case Accelerator:
253 tx = hotKey( menuBar()->text( id ) );
254 if ( !!tx )
255 return "Alt + "+tx;
256 break;
257 case DefaultAction:
258 return QMenuBar::tr("Open");
259 default:
260 break;
261 }
262
263 return tx;
264}
265
266QAccessible::Role QAccessibleMenuBar::role( int control ) const
267{
268 if ( !control )
269 return MenuBar;
270
271 QMenuItem *item = menuBar()->findItem( menuBar()->idAt( control -1 ) );
272 if ( item && item->isSeparator() )
273 return Separator;
274 return MenuItem;
275}
276
277QAccessible::State QAccessibleMenuBar::state( int control ) const
278{
279 int s = QAccessibleWidget::state( control );
280 if ( !control )
281 return (State)s;
282
283 int id = menuBar()->idAt( control -1 );
284 QMenuItem *item = menuBar()->findItem( id );
285 if ( !item )
286 return (State)s;
287
288 if ( menuBar()->style().styleHint( QStyle::SH_PopupMenu_MouseTracking ) )
289 s |= HotTracked;
290 if ( item->isSeparator() || !item->isEnabled() )
291 s |= Unavailable;
292 if ( menuBar()->isItemActive( id ) )
293 s |= Focused;
294
295 return (State)s;
296}
297
298bool QAccessibleMenuBar::doDefaultAction( int control )
299{
300 if ( !control )
301 return FALSE;
302
303 int id = menuBar()->idAt( control -1 );
304 QMenuItem *item = menuBar()->findItem( id );
305 if ( !item || !item->isEnabled() )
306 return FALSE;
307
308 menuBar()->activateItemAt( control - 1);
309 return TRUE;
310}
311
312bool QAccessibleMenuBar::setFocus( int control )
313{
314 if ( !control )
315 return FALSE;
316
317 int id = menuBar()->idAt( control -1 );
318 QMenuItem *item = menuBar()->findItem( id );
319 if ( !item || !item->isEnabled() )
320 return FALSE;
321
322 return TRUE;
323}
Note: See TracBrowser for help on using the repository browser.