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 QtGui module 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 | #include <qmenubar.h>
|
---|
43 |
|
---|
44 | #include <qstyle.h>
|
---|
45 | #include <qlayout.h>
|
---|
46 | #include <qapplication.h>
|
---|
47 | #include <qdesktopwidget.h>
|
---|
48 | #ifndef QT_NO_ACCESSIBILITY
|
---|
49 | # include <qaccessible.h>
|
---|
50 | #endif
|
---|
51 | #include <qpainter.h>
|
---|
52 | #include <qstylepainter.h>
|
---|
53 | #include <qevent.h>
|
---|
54 | #include <qmainwindow.h>
|
---|
55 | #include <qtoolbar.h>
|
---|
56 | #include <qtoolbutton.h>
|
---|
57 | #include <qwhatsthis.h>
|
---|
58 |
|
---|
59 | #ifndef QT_NO_MENUBAR
|
---|
60 |
|
---|
61 | #ifdef QT3_SUPPORT
|
---|
62 | #include <private/qaction_p.h>
|
---|
63 | #include <qmenudata.h>
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | #include "qmenu_p.h"
|
---|
67 | #include "qmenubar_p.h"
|
---|
68 | #include "qdebug.h"
|
---|
69 |
|
---|
70 | #ifdef Q_OS_WINCE
|
---|
71 | extern bool qt_wince_is_mobile(); //defined in qguifunctions_wce.cpp
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | QT_BEGIN_NAMESPACE
|
---|
75 |
|
---|
76 | class QMenuBarExtension : public QToolButton
|
---|
77 | {
|
---|
78 | public:
|
---|
79 | explicit QMenuBarExtension(QWidget *parent);
|
---|
80 |
|
---|
81 | QSize sizeHint() const;
|
---|
82 | void paintEvent(QPaintEvent *);
|
---|
83 | };
|
---|
84 |
|
---|
85 | QMenuBarExtension::QMenuBarExtension(QWidget *parent)
|
---|
86 | : QToolButton(parent)
|
---|
87 | {
|
---|
88 | setObjectName(QLatin1String("qt_menubar_ext_button"));
|
---|
89 | setAutoRaise(true);
|
---|
90 | #ifndef QT_NO_MENU
|
---|
91 | setPopupMode(QToolButton::InstantPopup);
|
---|
92 | #endif
|
---|
93 | setIcon(style()->standardIcon(QStyle::SP_ToolBarHorizontalExtensionButton, 0, parentWidget()));
|
---|
94 | }
|
---|
95 |
|
---|
96 | void QMenuBarExtension::paintEvent(QPaintEvent *)
|
---|
97 | {
|
---|
98 | QStylePainter p(this);
|
---|
99 | QStyleOptionToolButton opt;
|
---|
100 | initStyleOption(&opt);
|
---|
101 | // We do not need to draw both extention arrows
|
---|
102 | opt.features &= ~QStyleOptionToolButton::HasMenu;
|
---|
103 | p.drawComplexControl(QStyle::CC_ToolButton, opt);
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | QSize QMenuBarExtension::sizeHint() const
|
---|
108 | {
|
---|
109 | int ext = style()->pixelMetric(QStyle::PM_ToolBarExtensionExtent, 0, parentWidget());
|
---|
110 | return QSize(ext, ext);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /*!
|
---|
115 | \internal
|
---|
116 | */
|
---|
117 | QAction *QMenuBarPrivate::actionAt(QPoint p) const
|
---|
118 | {
|
---|
119 | Q_Q(const QMenuBar);
|
---|
120 | QList<QAction*> items = q->actions();
|
---|
121 | for(int i = 0; i < items.size(); ++i) {
|
---|
122 | if(actionRect(items.at(i)).contains(p))
|
---|
123 | return items.at(i);
|
---|
124 | }
|
---|
125 | return 0;
|
---|
126 | }
|
---|
127 |
|
---|
128 | QRect QMenuBarPrivate::menuRect(bool extVisible) const
|
---|
129 | {
|
---|
130 | Q_Q(const QMenuBar);
|
---|
131 |
|
---|
132 | int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
|
---|
133 | QRect result = q->rect();
|
---|
134 | result.adjust(hmargin, 0, -hmargin, 0);
|
---|
135 |
|
---|
136 | if (extVisible) {
|
---|
137 | if (q->layoutDirection() == Qt::RightToLeft)
|
---|
138 | result.setLeft(result.left() + extension->sizeHint().width());
|
---|
139 | else
|
---|
140 | result.setWidth(result.width() - extension->sizeHint().width());
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (leftWidget && leftWidget->isVisible()) {
|
---|
144 | QSize sz = leftWidget->sizeHint();
|
---|
145 | if (q->layoutDirection() == Qt::RightToLeft)
|
---|
146 | result.setRight(result.right() - sz.width());
|
---|
147 | else
|
---|
148 | result.setLeft(result.left() + sz.width());
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (rightWidget && rightWidget->isVisible()) {
|
---|
152 | QSize sz = rightWidget->sizeHint();
|
---|
153 | if (q->layoutDirection() == Qt::RightToLeft)
|
---|
154 | result.setLeft(result.left() + sz.width());
|
---|
155 | else
|
---|
156 | result.setRight(result.right() - sz.width());
|
---|
157 | }
|
---|
158 |
|
---|
159 | return result;
|
---|
160 | }
|
---|
161 |
|
---|
162 | bool QMenuBarPrivate::isVisible(QAction *action)
|
---|
163 | {
|
---|
164 | return !hiddenActions.contains(action);
|
---|
165 | }
|
---|
166 |
|
---|
167 | void QMenuBarPrivate::updateGeometries()
|
---|
168 | {
|
---|
169 | Q_Q(QMenuBar);
|
---|
170 | if(!itemsDirty)
|
---|
171 | return;
|
---|
172 | int q_width = q->width()-(q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q)*2);
|
---|
173 | int q_start = -1;
|
---|
174 | if(leftWidget || rightWidget) {
|
---|
175 | int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q)
|
---|
176 | + q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
|
---|
177 | int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, q)
|
---|
178 | + q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
|
---|
179 | if (leftWidget && leftWidget->isVisible()) {
|
---|
180 | QSize sz = leftWidget->sizeHint();
|
---|
181 | q_width -= sz.width();
|
---|
182 | q_start = sz.width();
|
---|
183 | QPoint pos(hmargin, (q->height() - leftWidget->height()) / 2);
|
---|
184 | QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));
|
---|
185 | leftWidget->setGeometry(vRect);
|
---|
186 | }
|
---|
187 | if (rightWidget && rightWidget->isVisible()) {
|
---|
188 | QSize sz = rightWidget->sizeHint();
|
---|
189 | q_width -= sz.width();
|
---|
190 | QPoint pos(q->width() - sz.width() - hmargin, vmargin);
|
---|
191 | QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));
|
---|
192 | rightWidget->setGeometry(vRect);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | #ifdef Q_WS_MAC
|
---|
197 | if(mac_menubar) {//nothing to see here folks, move along..
|
---|
198 | itemsDirty = false;
|
---|
199 | return;
|
---|
200 | }
|
---|
201 | #endif
|
---|
202 | calcActionRects(q_width, q_start, actionRects, actionList);
|
---|
203 | itemsWidth = q_width;
|
---|
204 | itemsStart = q_start;
|
---|
205 | currentAction = 0;
|
---|
206 | #ifndef QT_NO_SHORTCUT
|
---|
207 | if(itemsDirty) {
|
---|
208 | for(int j = 0; j < shortcutIndexMap.size(); ++j)
|
---|
209 | q->releaseShortcut(shortcutIndexMap.value(j));
|
---|
210 | shortcutIndexMap.resize(0); // faster than clear
|
---|
211 | for(int i = 0; i < actionList.count(); i++)
|
---|
212 | shortcutIndexMap.append(q->grabShortcut(QKeySequence::mnemonic(actionList.at(i)->text())));
|
---|
213 | }
|
---|
214 | #endif
|
---|
215 | itemsDirty = false;
|
---|
216 |
|
---|
217 | hiddenActions.clear();
|
---|
218 | //this is the menu rectangle without any extension
|
---|
219 | QRect menuRect = this->menuRect(false);
|
---|
220 |
|
---|
221 | //we try to see if the actions will fit there
|
---|
222 | bool hasHiddenActions = false;
|
---|
223 | foreach(QAction *action, actionList) {
|
---|
224 | if (!menuRect.contains(actionRect(action))) {
|
---|
225 | hasHiddenActions = true;
|
---|
226 | break;
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | //...and if not, determine the ones that fit on the menu with the extension visible
|
---|
231 | if (hasHiddenActions) {
|
---|
232 | menuRect = this->menuRect(true);
|
---|
233 | foreach(QAction *action, actionList) {
|
---|
234 | if (!menuRect.contains(actionRect(action))) {
|
---|
235 | hiddenActions.append(action);
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (hiddenActions.count() > 0) {
|
---|
241 | QMenu *pop = extension->menu();
|
---|
242 | if (!pop) {
|
---|
243 | pop = new QMenu(q);
|
---|
244 | extension->setMenu(pop);
|
---|
245 | }
|
---|
246 | pop->clear();
|
---|
247 | pop->addActions(hiddenActions);
|
---|
248 |
|
---|
249 | int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q);
|
---|
250 | int x = q->layoutDirection() == Qt::RightToLeft
|
---|
251 | ? menuRect.left() - extension->sizeHint().width() + 1
|
---|
252 | : menuRect.right();
|
---|
253 | extension->setGeometry(x, vmargin, extension->sizeHint().width(), menuRect.height() - vmargin*2);
|
---|
254 | extension->show();
|
---|
255 | } else {
|
---|
256 | extension->hide();
|
---|
257 | }
|
---|
258 | q->updateGeometry();
|
---|
259 | #ifdef QT3_SUPPORT
|
---|
260 | if (q->parentWidget() != 0) {
|
---|
261 | QMenubarUpdatedEvent menubarUpdated(q);
|
---|
262 | QApplication::sendEvent(q->parentWidget(), &menubarUpdated);
|
---|
263 | }
|
---|
264 | #endif
|
---|
265 | }
|
---|
266 |
|
---|
267 | QRect QMenuBarPrivate::actionRect(QAction *act) const
|
---|
268 | {
|
---|
269 | Q_Q(const QMenuBar);
|
---|
270 | QRect ret = actionRects.value(act);
|
---|
271 | const int fw = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
|
---|
272 | ret.translate(fw, fw);
|
---|
273 | return QStyle::visualRect(q->layoutDirection(), q->rect(), ret);
|
---|
274 | }
|
---|
275 |
|
---|
276 | void QMenuBarPrivate::setKeyboardMode(bool b)
|
---|
277 | {
|
---|
278 | Q_Q(QMenuBar);
|
---|
279 | if (b && !q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, q)) {
|
---|
280 | setCurrentAction(0);
|
---|
281 | return;
|
---|
282 | }
|
---|
283 | keyboardState = b;
|
---|
284 | if(b) {
|
---|
285 | QWidget *fw = qApp->focusWidget();
|
---|
286 | if (fw != q)
|
---|
287 | keyboardFocusWidget = fw;
|
---|
288 | if(!currentAction && !actionList.isEmpty())
|
---|
289 | setCurrentAction(actionList.first());
|
---|
290 | q->setFocus(Qt::MenuBarFocusReason);
|
---|
291 | } else {
|
---|
292 | if(!popupState)
|
---|
293 | setCurrentAction(0);
|
---|
294 | if(keyboardFocusWidget) {
|
---|
295 | if (qApp->focusWidget() == q)
|
---|
296 | keyboardFocusWidget->setFocus(Qt::MenuBarFocusReason);
|
---|
297 | keyboardFocusWidget = 0;
|
---|
298 | }
|
---|
299 | }
|
---|
300 | q->update();
|
---|
301 | }
|
---|
302 |
|
---|
303 | void QMenuBarPrivate::popupAction(QAction *action, bool activateFirst)
|
---|
304 | {
|
---|
305 | Q_Q(QMenuBar);
|
---|
306 | if(!action || !action->menu() || closePopupMode)
|
---|
307 | return;
|
---|
308 | popupState = true;
|
---|
309 | if (action->isEnabled() && action->menu()->isEnabled()) {
|
---|
310 | closePopupMode = 0;
|
---|
311 | activeMenu = action->menu();
|
---|
312 | activeMenu->d_func()->causedPopup.widget = q;
|
---|
313 | activeMenu->d_func()->causedPopup.action = action;
|
---|
314 |
|
---|
315 | QRect adjustedActionRect = actionRect(action);
|
---|
316 | QPoint pos(q->mapToGlobal(QPoint(adjustedActionRect.left(), adjustedActionRect.bottom() + 1)));
|
---|
317 | QSize popup_size = activeMenu->sizeHint();
|
---|
318 |
|
---|
319 | QRect screenRect = QApplication::desktop()->screenGeometry(pos);
|
---|
320 |
|
---|
321 | const bool fitUp = (q->mapToGlobal(adjustedActionRect.topLeft()).y() >= popup_size.height());
|
---|
322 | const bool fitDown = (pos.y() + popup_size.height() <= screenRect.bottom());
|
---|
323 | const int actionWidth = adjustedActionRect.width();
|
---|
324 |
|
---|
325 | if (!fitUp && !fitDown) { //we should shift the menu
|
---|
326 | bool shouldShiftToRight = !q->isRightToLeft();
|
---|
327 | if (q->isRightToLeft() && popup_size.width() > pos.x())
|
---|
328 | shouldShiftToRight = true;
|
---|
329 | else if (actionWidth + popup_size.width() + pos.x() > screenRect.right())
|
---|
330 | shouldShiftToRight = false;
|
---|
331 |
|
---|
332 | if (shouldShiftToRight)
|
---|
333 | pos.rx() += actionWidth;
|
---|
334 | else
|
---|
335 | pos.rx() -= popup_size.width();
|
---|
336 | } else if (q->isRightToLeft()) {
|
---|
337 | pos.setX(pos.x()-(popup_size.width() - actionWidth));
|
---|
338 | }
|
---|
339 |
|
---|
340 | if(pos.x() < screenRect.x()) {
|
---|
341 | pos.setX(screenRect.x());
|
---|
342 | } else {
|
---|
343 | const int off = pos.x()+popup_size.width() - screenRect.right();
|
---|
344 | if(off > 0)
|
---|
345 | pos.setX(qMax(screenRect.x(), pos.x()-off));
|
---|
346 |
|
---|
347 | }
|
---|
348 |
|
---|
349 | if(!defaultPopDown || (fitUp && !fitDown))
|
---|
350 | pos.setY(qMax(screenRect.y(), q->mapToGlobal(QPoint(0, adjustedActionRect.top()-popup_size.height())).y()));
|
---|
351 | activeMenu->popup(pos);
|
---|
352 | if(activateFirst)
|
---|
353 | activeMenu->d_func()->setFirstActionActive();
|
---|
354 | }
|
---|
355 | q->update(actionRect(action));
|
---|
356 | }
|
---|
357 |
|
---|
358 | void QMenuBarPrivate::setCurrentAction(QAction *action, bool popup, bool activateFirst)
|
---|
359 | {
|
---|
360 | if(currentAction == action && popup == popupState)
|
---|
361 | return;
|
---|
362 |
|
---|
363 | autoReleaseTimer.stop();
|
---|
364 |
|
---|
365 | doChildEffects = (popup && !activeMenu);
|
---|
366 | Q_Q(QMenuBar);
|
---|
367 | QWidget *fw = 0;
|
---|
368 | if(QMenu *menu = activeMenu) {
|
---|
369 | activeMenu = 0;
|
---|
370 | if (popup) {
|
---|
371 | fw = q->window()->focusWidget();
|
---|
372 | q->setFocus(Qt::NoFocusReason);
|
---|
373 | }
|
---|
374 | menu->hide();
|
---|
375 | }
|
---|
376 |
|
---|
377 | if(currentAction)
|
---|
378 | q->update(actionRect(currentAction));
|
---|
379 |
|
---|
380 | popupState = popup;
|
---|
381 | #ifndef QT_NO_STATUSTIP
|
---|
382 | QAction *previousAction = currentAction;
|
---|
383 | #endif
|
---|
384 | currentAction = action;
|
---|
385 | if (action) {
|
---|
386 | activateAction(action, QAction::Hover);
|
---|
387 | if(popup)
|
---|
388 | popupAction(action, activateFirst);
|
---|
389 | q->update(actionRect(action));
|
---|
390 | #ifndef QT_NO_STATUSTIP
|
---|
391 | } else if (previousAction) {
|
---|
392 | QString empty;
|
---|
393 | QStatusTipEvent tip(empty);
|
---|
394 | QApplication::sendEvent(q, &tip);
|
---|
395 | #endif
|
---|
396 | }
|
---|
397 | if (fw)
|
---|
398 | fw->setFocus(Qt::NoFocusReason);
|
---|
399 | }
|
---|
400 |
|
---|
401 | void QMenuBarPrivate::calcActionRects(int max_width, int start, QMap<QAction*, QRect> &actionRects, QList<QAction*> &actionList) const
|
---|
402 | {
|
---|
403 | Q_Q(const QMenuBar);
|
---|
404 |
|
---|
405 | if(!itemsDirty && itemsWidth == max_width && itemsStart == start) {
|
---|
406 | actionRects = actionRects;
|
---|
407 | actionList = actionList;
|
---|
408 | return;
|
---|
409 | }
|
---|
410 | actionRects.clear();
|
---|
411 | actionList.clear();
|
---|
412 | const int itemSpacing = q->style()->pixelMetric(QStyle::PM_MenuBarItemSpacing, 0, q);
|
---|
413 | int max_item_height = 0, separator = -1, separator_start = 0, separator_len = 0;
|
---|
414 | QList<QAction*> items = q->actions();
|
---|
415 |
|
---|
416 | //calculate size
|
---|
417 | const QFontMetrics fm = q->fontMetrics();
|
---|
418 | const int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, q),
|
---|
419 | vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q),
|
---|
420 | icone = q->style()->pixelMetric(QStyle::PM_SmallIconSize, 0, q);
|
---|
421 | for(int i = 0; i < items.count(); i++) {
|
---|
422 | QAction *action = items.at(i);
|
---|
423 | if(!action->isVisible())
|
---|
424 | continue;
|
---|
425 |
|
---|
426 | QSize sz;
|
---|
427 |
|
---|
428 | //calc what I think the size is..
|
---|
429 | if(action->isSeparator()) {
|
---|
430 | if (q->style()->styleHint(QStyle::SH_DrawMenuBarSeparator, 0, q))
|
---|
431 | separator = actionRects.count();
|
---|
432 | continue; //we don't really position these!
|
---|
433 | } else {
|
---|
434 | QString s = action->text();
|
---|
435 | if(!s.isEmpty()) {
|
---|
436 | int w = fm.width(s);
|
---|
437 | w -= s.count(QLatin1Char('&')) * fm.width(QLatin1Char('&'));
|
---|
438 | w += s.count(QLatin1String("&&")) * fm.width(QLatin1Char('&'));
|
---|
439 | sz = QSize(w, fm.height());
|
---|
440 | }
|
---|
441 |
|
---|
442 | QIcon is = action->icon();
|
---|
443 | if (!is.isNull()) {
|
---|
444 | QSize is_sz = QSize(icone, icone);
|
---|
445 | if (is_sz.height() > sz.height())
|
---|
446 | sz.setHeight(is_sz.height());
|
---|
447 | if (is_sz.width() > sz.width())
|
---|
448 | sz.setWidth(is_sz.width());
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | //let the style modify the above size..
|
---|
453 | QStyleOptionMenuItem opt;
|
---|
454 | q->initStyleOption(&opt, action);
|
---|
455 | sz = q->style()->sizeFromContents(QStyle::CT_MenuBarItem, &opt, sz, q);
|
---|
456 |
|
---|
457 | if(!sz.isEmpty()) {
|
---|
458 | { //update the separator state
|
---|
459 | int iWidth = sz.width();
|
---|
460 | iWidth += itemSpacing;
|
---|
461 | if(separator == -1)
|
---|
462 | separator_start += iWidth;
|
---|
463 | else
|
---|
464 | separator_len += iWidth;
|
---|
465 | }
|
---|
466 | //maximum height
|
---|
467 | max_item_height = qMax(max_item_height, sz.height());
|
---|
468 | //append
|
---|
469 | actionRects.insert(action, QRect(0, 0, sz.width(), sz.height()));
|
---|
470 | actionList.append(action);
|
---|
471 | }
|
---|
472 | }
|
---|
473 |
|
---|
474 | //calculate position
|
---|
475 | int x = ((start == -1) ? hmargin : start) + itemSpacing;
|
---|
476 | int y = vmargin;
|
---|
477 | for(int i = 0; i < actionList.count(); i++) {
|
---|
478 | QAction *action = actionList.at(i);
|
---|
479 | QRect &rect = actionRects[action];
|
---|
480 | //resize
|
---|
481 | rect.setHeight(max_item_height);
|
---|
482 |
|
---|
483 | //move
|
---|
484 | if(separator != -1 && i >= separator) { //after the separator
|
---|
485 | int left = (max_width - separator_len - hmargin - itemSpacing) + (x - separator_start - hmargin);
|
---|
486 | if(left < separator_start) { //wrap
|
---|
487 | separator_start = x = hmargin;
|
---|
488 | y += max_item_height;
|
---|
489 | }
|
---|
490 | rect.moveLeft(left);
|
---|
491 | } else {
|
---|
492 | rect.moveLeft(x);
|
---|
493 | }
|
---|
494 | rect.moveTop(y);
|
---|
495 |
|
---|
496 | //keep moving along..
|
---|
497 | x += rect.width() + itemSpacing;
|
---|
498 | }
|
---|
499 | }
|
---|
500 |
|
---|
501 | void QMenuBarPrivate::activateAction(QAction *action, QAction::ActionEvent action_e)
|
---|
502 | {
|
---|
503 | Q_Q(QMenuBar);
|
---|
504 | if (!action || !action->isEnabled())
|
---|
505 | return;
|
---|
506 | action->activate(action_e);
|
---|
507 | if (action_e == QAction::Hover)
|
---|
508 | action->showStatusText(q);
|
---|
509 |
|
---|
510 | // if(action_e == QAction::Trigger)
|
---|
511 | // emit q->activated(action);
|
---|
512 | // else if(action_e == QAction::Hover)
|
---|
513 | // emit q->highlighted(action);
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | void QMenuBarPrivate::_q_actionTriggered()
|
---|
518 | {
|
---|
519 | Q_Q(QMenuBar);
|
---|
520 | if (QAction *action = qobject_cast<QAction *>(q->sender())) {
|
---|
521 | emit q->triggered(action);
|
---|
522 | #ifdef QT3_SUPPORT
|
---|
523 | emit q->activated(q->findIdForAction(action));
|
---|
524 | #endif
|
---|
525 | }
|
---|
526 | }
|
---|
527 |
|
---|
528 | void QMenuBarPrivate::_q_actionHovered()
|
---|
529 | {
|
---|
530 | Q_Q(QMenuBar);
|
---|
531 | if (QAction *action = qobject_cast<QAction *>(q->sender())) {
|
---|
532 | emit q->hovered(action);
|
---|
533 | #ifndef QT_NO_ACCESSIBILITY
|
---|
534 | if (QAccessible::isActive()) {
|
---|
535 | QList<QAction*> actions = q->actions();
|
---|
536 | int actionIndex = actions.indexOf(action);
|
---|
537 | ++actionIndex;
|
---|
538 | QAccessible::updateAccessibility(q, actionIndex, QAccessible::Focus);
|
---|
539 | QAccessible::updateAccessibility(q, actionIndex, QAccessible::Selection);
|
---|
540 | }
|
---|
541 | #endif //QT_NO_ACCESSIBILITY
|
---|
542 | #ifdef QT3_SUPPORT
|
---|
543 | emit q->highlighted(q->findIdForAction(action));
|
---|
544 | #endif
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | /*!
|
---|
549 | Initialize \a option with the values from the menu bar and information from \a action. This method
|
---|
550 | is useful for subclasses when they need a QStyleOptionMenuItem, but don't want
|
---|
551 | to fill in all the information themselves.
|
---|
552 |
|
---|
553 | \sa QStyleOption::initFrom() QMenu::initStyleOption()
|
---|
554 | */
|
---|
555 | void QMenuBar::initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const
|
---|
556 | {
|
---|
557 | if (!option || !action)
|
---|
558 | return;
|
---|
559 | Q_D(const QMenuBar);
|
---|
560 | option->palette = palette();
|
---|
561 | option->state = QStyle::State_None;
|
---|
562 | if (isEnabled() && action->isEnabled())
|
---|
563 | option->state |= QStyle::State_Enabled;
|
---|
564 | else
|
---|
565 | option->palette.setCurrentColorGroup(QPalette::Disabled);
|
---|
566 | option->fontMetrics = fontMetrics();
|
---|
567 | if (d->currentAction && d->currentAction == action) {
|
---|
568 | option->state |= QStyle::State_Selected;
|
---|
569 | if (d->popupState && !d->closePopupMode)
|
---|
570 | option->state |= QStyle::State_Sunken;
|
---|
571 | }
|
---|
572 | if (hasFocus() || d->currentAction)
|
---|
573 | option->state |= QStyle::State_HasFocus;
|
---|
574 | option->menuRect = rect();
|
---|
575 | option->menuItemType = QStyleOptionMenuItem::Normal;
|
---|
576 | option->checkType = QStyleOptionMenuItem::NotCheckable;
|
---|
577 | option->text = action->text();
|
---|
578 | option->icon = action->icon();
|
---|
579 | }
|
---|
580 |
|
---|
581 | /*!
|
---|
582 | \class QMenuBar
|
---|
583 | \brief The QMenuBar class provides a horizontal menu bar.
|
---|
584 |
|
---|
585 | \ingroup application
|
---|
586 | \mainclass
|
---|
587 |
|
---|
588 | A menu bar consists of a list of pull-down menu items. You add
|
---|
589 | menu items with addMenu(). For example, asuming that \c menubar
|
---|
590 | is a pointer to a QMenuBar and \c fileMenu is a pointer to a
|
---|
591 | QMenu, the following statement inserts the menu into the menu bar:
|
---|
592 | \snippet doc/src/snippets/code/src_gui_widgets_qmenubar.cpp 0
|
---|
593 |
|
---|
594 | The ampersand in the menu item's text sets Alt+F as a shortcut for
|
---|
595 | this menu. (You can use "\&\&" to get a real ampersand in the menu
|
---|
596 | bar.)
|
---|
597 |
|
---|
598 | There is no need to lay out a menu bar. It automatically sets its
|
---|
599 | own geometry to the top of the parent widget and changes it
|
---|
600 | appropriately whenever the parent is resized.
|
---|
601 |
|
---|
602 | \section1 Usage
|
---|
603 |
|
---|
604 | In most main window style applications you would use the
|
---|
605 | \l{QMainWindow::}{menuBar()} function provided in QMainWindow,
|
---|
606 | adding \l{QMenu}s to the menu bar and adding \l{QAction}s to the
|
---|
607 | pop-up menus.
|
---|
608 |
|
---|
609 | Example (from the \l{mainwindows/menus}{Menus} example):
|
---|
610 |
|
---|
611 | \snippet examples/mainwindows/menus/mainwindow.cpp 9
|
---|
612 |
|
---|
613 | Menu items may be removed with removeAction().
|
---|
614 |
|
---|
615 | Widgets can be added to menus by using instances of the QWidgetAction
|
---|
616 | class to hold them. These actions can then be inserted into menus
|
---|
617 | in the usual way; see the QMenu documentation for more details.
|
---|
618 |
|
---|
619 | \section1 Platform Dependent Look and Feel
|
---|
620 |
|
---|
621 | Different platforms have different requirements for the appearance
|
---|
622 | of menu bars and their behavior when the user interacts with them.
|
---|
623 | For example, Windows systems are often configured so that the
|
---|
624 | underlined character mnemonics that indicate keyboard shortcuts
|
---|
625 | for items in the menu bar are only shown when the \gui{Alt} key is
|
---|
626 | pressed.
|
---|
627 |
|
---|
628 | \table
|
---|
629 |
|
---|
630 | \row \o \inlineimage plastique-menubar.png A menu bar shown in the
|
---|
631 | Plastique widget style.
|
---|
632 |
|
---|
633 | \o The \l{QPlastiqueStyle}{Plastique widget style}, like most
|
---|
634 | other styles, handles the \gui{Help} menu in the same way as it
|
---|
635 | handles any other menu.
|
---|
636 |
|
---|
637 | \row \o \inlineimage motif-menubar.png A menu bar shown in the
|
---|
638 | Motif widget style.
|
---|
639 |
|
---|
640 | \o The \l{QMotifStyle}{Motif widget style} treats \gui{Help} menus
|
---|
641 | in a special way, placing them at right-hand end of the menu bar.
|
---|
642 |
|
---|
643 | \endtable
|
---|
644 |
|
---|
645 | \section1 QMenuBar on Mac OS X
|
---|
646 |
|
---|
647 | QMenuBar on Mac OS X is a wrapper for using the system-wide menu bar.
|
---|
648 | If you have multiple menu bars in one dialog the outermost menu bar
|
---|
649 | (normally inside a widget with widget flag Qt::Window) will
|
---|
650 | be used for the system-wide menu bar.
|
---|
651 |
|
---|
652 | Qt for Mac OS X also provides a menu bar merging feature to make
|
---|
653 | QMenuBar conform more closely to accepted Mac OS X menu bar layout.
|
---|
654 | The merging functionality is based on string matching the title of
|
---|
655 | a QMenu entry. These strings are translated (using QObject::tr())
|
---|
656 | in the "QMenuBar" context. If an entry is moved its slots will still
|
---|
657 | fire as if it was in the original place. The table below outlines
|
---|
658 | the strings looked for and where the entry is placed if matched:
|
---|
659 |
|
---|
660 | \table
|
---|
661 | \header \i String matches \i Placement \i Notes
|
---|
662 | \row \i about.*
|
---|
663 | \i Application Menu | About <application name>
|
---|
664 | \i If this entry is not found no About item will appear in
|
---|
665 | the Application Menu
|
---|
666 | \row \i config, options, setup, settings or preferences
|
---|
667 | \i Application Menu | Preferences
|
---|
668 | \i If this entry is not found the Settings item will be disabled
|
---|
669 | \row \i quit or exit
|
---|
670 | \i Application Menu | Quit <application name>
|
---|
671 | \i If this entry is not found a default Quit item will be
|
---|
672 | created to call QApplication::quit()
|
---|
673 | \endtable
|
---|
674 |
|
---|
675 | You can override this behavior by using the QAction::menuRole()
|
---|
676 | property.
|
---|
677 |
|
---|
678 | If you want all windows in a Mac application to share one menu
|
---|
679 | bar, you must create a menu bar that does not have a parent.
|
---|
680 | Create a parent-less menu bar this way:
|
---|
681 |
|
---|
682 | \snippet doc/src/snippets/code/src_gui_widgets_qmenubar.cpp 1
|
---|
683 |
|
---|
684 | \bold{Note:} Do \e{not} call QMainWindow::menuBar() to create the
|
---|
685 | shared menu bar, because that menu bar will have the QMainWindow
|
---|
686 | as its parent. That menu bar would only be displayed for the
|
---|
687 | parent QMainWindow.
|
---|
688 |
|
---|
689 | \bold{Note:} The text used for the application name in the menu
|
---|
690 | bar is obtained from the value set in the \c{Info.plist} file in
|
---|
691 | the application's bundle. See \l{Deploying an Application on
|
---|
692 | Mac OS X} for more information.
|
---|
693 |
|
---|
694 | \section1 QMenuBar on Windows CE
|
---|
695 |
|
---|
696 | QMenuBar on Windows CE is a wrapper for using the system-wide menu bar,
|
---|
697 | similar to the Mac. This feature is activated for Windows Mobile
|
---|
698 | and integrates QMenuBar with the native soft keys. The left soft
|
---|
699 | key can be controlled with QMenuBar::setDefaultAction() and the
|
---|
700 | right soft key can be used to access the menu bar.
|
---|
701 |
|
---|
702 | The hovered() signal is not supported for the native menu
|
---|
703 | integration. Also, it is not possible to display an icon in a
|
---|
704 | native menu on Windows Mobile.
|
---|
705 |
|
---|
706 | \section1 Examples
|
---|
707 |
|
---|
708 | The \l{mainwindows/menus}{Menus} example shows how to use QMenuBar
|
---|
709 | and QMenu. The other \l{Qt Examples#Main Windows}{main window
|
---|
710 | application examples} also provide menus using these classes.
|
---|
711 |
|
---|
712 | \sa QMenu, QShortcut, QAction,
|
---|
713 | {http://developer.apple.com/documentation/UserExperience/Conceptual/OSXHIGuidelines/index.html}{Introduction to Apple Human Interface Guidelines},
|
---|
714 | {fowler}{GUI Design Handbook: Menu Bar}, {Menus Example}
|
---|
715 | */
|
---|
716 |
|
---|
717 |
|
---|
718 | void QMenuBarPrivate::init()
|
---|
719 | {
|
---|
720 | Q_Q(QMenuBar);
|
---|
721 | q->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
|
---|
722 | q->setAttribute(Qt::WA_CustomWhatsThis);
|
---|
723 | #ifdef Q_WS_MAC
|
---|
724 | macCreateMenuBar(q->parentWidget());
|
---|
725 | if(mac_menubar)
|
---|
726 | q->hide();
|
---|
727 | #endif
|
---|
728 | #ifdef Q_OS_WINCE
|
---|
729 | if (qt_wince_is_mobile()) {
|
---|
730 | wceCreateMenuBar(q->parentWidget());
|
---|
731 | if(wce_menubar)
|
---|
732 | q->hide();
|
---|
733 | }
|
---|
734 | #endif
|
---|
735 | q->setBackgroundRole(QPalette::Button);
|
---|
736 | oldWindow = oldParent = 0;
|
---|
737 | #ifdef QT3_SUPPORT
|
---|
738 | doAutoResize = false;
|
---|
739 | #endif
|
---|
740 | handleReparent();
|
---|
741 | q->setMouseTracking(q->style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, q));
|
---|
742 |
|
---|
743 | extension = new QMenuBarExtension(q);
|
---|
744 | extension->setFocusPolicy(Qt::NoFocus);
|
---|
745 | extension->hide();
|
---|
746 | }
|
---|
747 |
|
---|
748 | /*!
|
---|
749 | Constructs a menu bar with parent \a parent.
|
---|
750 | */
|
---|
751 | QMenuBar::QMenuBar(QWidget *parent) : QWidget(*new QMenuBarPrivate, parent, 0)
|
---|
752 | {
|
---|
753 | Q_D(QMenuBar);
|
---|
754 | d->init();
|
---|
755 | }
|
---|
756 |
|
---|
757 | #ifdef QT3_SUPPORT
|
---|
758 | /*!
|
---|
759 | Use one of the constructors that doesn't take the \a name
|
---|
760 | argument and then use setObjectName() instead.
|
---|
761 | */
|
---|
762 | QMenuBar::QMenuBar(QWidget *parent, const char *name) : QWidget(*new QMenuBarPrivate, parent, 0)
|
---|
763 | {
|
---|
764 | Q_D(QMenuBar);
|
---|
765 | d->init();
|
---|
766 | setObjectName(QString::fromAscii(name));
|
---|
767 | }
|
---|
768 | #endif
|
---|
769 |
|
---|
770 | /*!
|
---|
771 | Destroys the menu bar.
|
---|
772 | */
|
---|
773 | QMenuBar::~QMenuBar()
|
---|
774 | {
|
---|
775 | #ifdef Q_WS_MAC
|
---|
776 | Q_D(QMenuBar);
|
---|
777 | d->macDestroyMenuBar();
|
---|
778 | #endif
|
---|
779 | #ifdef Q_OS_WINCE
|
---|
780 | Q_D(QMenuBar);
|
---|
781 | if (qt_wince_is_mobile())
|
---|
782 | d->wceDestroyMenuBar();
|
---|
783 | #endif
|
---|
784 | }
|
---|
785 |
|
---|
786 | /*!
|
---|
787 | \overload
|
---|
788 |
|
---|
789 | This convenience function creates a new action with \a text.
|
---|
790 | The function adds the newly created action to the menu's
|
---|
791 | list of actions, and returns it.
|
---|
792 |
|
---|
793 | \sa QWidget::addAction(), QWidget::actions()
|
---|
794 | */
|
---|
795 | QAction *QMenuBar::addAction(const QString &text)
|
---|
796 | {
|
---|
797 | QAction *ret = new QAction(text, this);
|
---|
798 | addAction(ret);
|
---|
799 | return ret;
|
---|
800 | }
|
---|
801 |
|
---|
802 | /*!
|
---|
803 | \overload
|
---|
804 |
|
---|
805 | This convenience function creates a new action with the given \a
|
---|
806 | text. The action's triggered() signal is connected to the \a
|
---|
807 | receiver's \a member slot. The function adds the newly created
|
---|
808 | action to the menu's list of actions and returns it.
|
---|
809 |
|
---|
810 | \sa QWidget::addAction(), QWidget::actions()
|
---|
811 | */
|
---|
812 | QAction *QMenuBar::addAction(const QString &text, const QObject *receiver, const char* member)
|
---|
813 | {
|
---|
814 | QAction *ret = new QAction(text, this);
|
---|
815 | QObject::connect(ret, SIGNAL(triggered(bool)), receiver, member);
|
---|
816 | addAction(ret);
|
---|
817 | return ret;
|
---|
818 | }
|
---|
819 |
|
---|
820 | /*!
|
---|
821 | Appends a new QMenu with \a title to the menu bar. The menu bar
|
---|
822 | takes ownership of the menu. Returns the new menu.
|
---|
823 |
|
---|
824 | \sa QWidget::addAction() QMenu::menuAction()
|
---|
825 | */
|
---|
826 | QMenu *QMenuBar::addMenu(const QString &title)
|
---|
827 | {
|
---|
828 | QMenu *menu = new QMenu(title, this);
|
---|
829 | addAction(menu->menuAction());
|
---|
830 | return menu;
|
---|
831 | }
|
---|
832 |
|
---|
833 | /*!
|
---|
834 | Appends a new QMenu with \a icon and \a title to the menu bar. The menu bar
|
---|
835 | takes ownership of the menu. Returns the new menu.
|
---|
836 |
|
---|
837 | \sa QWidget::addAction() QMenu::menuAction()
|
---|
838 | */
|
---|
839 | QMenu *QMenuBar::addMenu(const QIcon &icon, const QString &title)
|
---|
840 | {
|
---|
841 | QMenu *menu = new QMenu(title, this);
|
---|
842 | menu->setIcon(icon);
|
---|
843 | addAction(menu->menuAction());
|
---|
844 | return menu;
|
---|
845 | }
|
---|
846 |
|
---|
847 | /*!
|
---|
848 | Appends \a menu to the menu bar. Returns the menu's menuAction().
|
---|
849 |
|
---|
850 | \note The returned QAction object can be used to hide the corresponding
|
---|
851 | menu.
|
---|
852 |
|
---|
853 | \sa QWidget::addAction() QMenu::menuAction()
|
---|
854 | */
|
---|
855 | QAction *QMenuBar::addMenu(QMenu *menu)
|
---|
856 | {
|
---|
857 | QAction *action = menu->menuAction();
|
---|
858 | addAction(action);
|
---|
859 | return action;
|
---|
860 | }
|
---|
861 |
|
---|
862 | /*!
|
---|
863 | Appends a separator to the menu.
|
---|
864 | */
|
---|
865 | QAction *QMenuBar::addSeparator()
|
---|
866 | {
|
---|
867 | QAction *ret = new QAction(this);
|
---|
868 | ret->setSeparator(true);
|
---|
869 | addAction(ret);
|
---|
870 | return ret;
|
---|
871 | }
|
---|
872 |
|
---|
873 | /*!
|
---|
874 | This convenience function creates a new separator action, i.e. an
|
---|
875 | action with QAction::isSeparator() returning true. The function inserts
|
---|
876 | the newly created action into this menu bar's list of actions before
|
---|
877 | action \a before and returns it.
|
---|
878 |
|
---|
879 | \sa QWidget::insertAction(), addSeparator()
|
---|
880 | */
|
---|
881 | QAction *QMenuBar::insertSeparator(QAction *before)
|
---|
882 | {
|
---|
883 | QAction *action = new QAction(this);
|
---|
884 | action->setSeparator(true);
|
---|
885 | insertAction(before, action);
|
---|
886 | return action;
|
---|
887 | }
|
---|
888 |
|
---|
889 | /*!
|
---|
890 | This convenience function inserts \a menu before action \a before
|
---|
891 | and returns the menus menuAction().
|
---|
892 |
|
---|
893 | \sa QWidget::insertAction() addMenu()
|
---|
894 | */
|
---|
895 | QAction *QMenuBar::insertMenu(QAction *before, QMenu *menu)
|
---|
896 | {
|
---|
897 | QAction *action = menu->menuAction();
|
---|
898 | insertAction(before, action);
|
---|
899 | return action;
|
---|
900 | }
|
---|
901 |
|
---|
902 | /*!
|
---|
903 | Returns the QAction that is currently highlighted. A null pointer
|
---|
904 | will be returned if no action is currently selected.
|
---|
905 | */
|
---|
906 | QAction *QMenuBar::activeAction() const
|
---|
907 | {
|
---|
908 | Q_D(const QMenuBar);
|
---|
909 | return d->currentAction;
|
---|
910 | }
|
---|
911 |
|
---|
912 | /*!
|
---|
913 | \since 4.1
|
---|
914 |
|
---|
915 | Sets the currently highlighted action to \a act.
|
---|
916 | */
|
---|
917 | void QMenuBar::setActiveAction(QAction *act)
|
---|
918 | {
|
---|
919 | Q_D(QMenuBar);
|
---|
920 | d->setCurrentAction(act, true, false);
|
---|
921 | }
|
---|
922 |
|
---|
923 |
|
---|
924 | /*!
|
---|
925 | Removes all the actions from the menu bar.
|
---|
926 |
|
---|
927 | \sa removeAction()
|
---|
928 | */
|
---|
929 | void QMenuBar::clear()
|
---|
930 | {
|
---|
931 | QList<QAction*> acts = actions();
|
---|
932 | for(int i = 0; i < acts.size(); i++)
|
---|
933 | removeAction(acts[i]);
|
---|
934 | }
|
---|
935 |
|
---|
936 | /*!
|
---|
937 | \property QMenuBar::defaultUp
|
---|
938 | \brief the popup orientation
|
---|
939 |
|
---|
940 | The default popup orientation. By default, menus pop "down" the
|
---|
941 | screen. By setting the property to true, the menu will pop "up".
|
---|
942 | You might call this for menus that are \e below the document to
|
---|
943 | which they refer.
|
---|
944 |
|
---|
945 | If the menu would not fit on the screen, the other direction is
|
---|
946 | used automatically.
|
---|
947 | */
|
---|
948 | void QMenuBar::setDefaultUp(bool b)
|
---|
949 | {
|
---|
950 | Q_D(QMenuBar);
|
---|
951 | d->defaultPopDown = !b;
|
---|
952 | }
|
---|
953 |
|
---|
954 | bool QMenuBar::isDefaultUp() const
|
---|
955 | {
|
---|
956 | Q_D(const QMenuBar);
|
---|
957 | return !d->defaultPopDown;
|
---|
958 | }
|
---|
959 |
|
---|
960 | /*!
|
---|
961 | \reimp
|
---|
962 | */
|
---|
963 | void QMenuBar::resizeEvent(QResizeEvent *)
|
---|
964 | {
|
---|
965 | Q_D(QMenuBar);
|
---|
966 | d->itemsDirty = true;
|
---|
967 | d->updateGeometries();
|
---|
968 | }
|
---|
969 |
|
---|
970 | /*!
|
---|
971 | \reimp
|
---|
972 | */
|
---|
973 | void QMenuBar::paintEvent(QPaintEvent *e)
|
---|
974 | {
|
---|
975 | Q_D(QMenuBar);
|
---|
976 | QPainter p(this);
|
---|
977 | QRegion emptyArea(rect());
|
---|
978 |
|
---|
979 | //draw the items
|
---|
980 | for (int i = 0; i < d->actionList.count(); ++i) {
|
---|
981 | QAction *action = d->actionList.at(i);
|
---|
982 | QRect adjustedActionRect = d->actionRect(action);
|
---|
983 | if (adjustedActionRect.isEmpty() || !d->isVisible(action))
|
---|
984 | continue;
|
---|
985 | if(!e->rect().intersects(adjustedActionRect))
|
---|
986 | continue;
|
---|
987 |
|
---|
988 | emptyArea -= adjustedActionRect;
|
---|
989 | QStyleOptionMenuItem opt;
|
---|
990 | initStyleOption(&opt, action);
|
---|
991 | opt.rect = adjustedActionRect;
|
---|
992 | p.setClipRect(adjustedActionRect);
|
---|
993 | style()->drawControl(QStyle::CE_MenuBarItem, &opt, &p, this);
|
---|
994 | }
|
---|
995 | //draw border
|
---|
996 | if(int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this)) {
|
---|
997 | QRegion borderReg;
|
---|
998 | borderReg += QRect(0, 0, fw, height()); //left
|
---|
999 | borderReg += QRect(width()-fw, 0, fw, height()); //right
|
---|
1000 | borderReg += QRect(0, 0, width(), fw); //top
|
---|
1001 | borderReg += QRect(0, height()-fw, width(), fw); //bottom
|
---|
1002 | p.setClipRegion(borderReg);
|
---|
1003 | emptyArea -= borderReg;
|
---|
1004 | QStyleOptionFrame frame;
|
---|
1005 | frame.rect = rect();
|
---|
1006 | frame.palette = palette();
|
---|
1007 | frame.state = QStyle::State_None;
|
---|
1008 | frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth);
|
---|
1009 | frame.midLineWidth = 0;
|
---|
1010 | style()->drawPrimitive(QStyle::PE_PanelMenuBar, &frame, &p, this);
|
---|
1011 | }
|
---|
1012 | p.setClipRegion(emptyArea);
|
---|
1013 | QStyleOptionMenuItem menuOpt;
|
---|
1014 | menuOpt.palette = palette();
|
---|
1015 | menuOpt.state = QStyle::State_None;
|
---|
1016 | menuOpt.menuItemType = QStyleOptionMenuItem::EmptyArea;
|
---|
1017 | menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
|
---|
1018 | menuOpt.rect = rect();
|
---|
1019 | menuOpt.menuRect = rect();
|
---|
1020 | style()->drawControl(QStyle::CE_MenuBarEmptyArea, &menuOpt, &p, this);
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | /*!
|
---|
1024 | \reimp
|
---|
1025 | */
|
---|
1026 | void QMenuBar::setVisible(bool visible)
|
---|
1027 | {
|
---|
1028 | #ifdef Q_WS_MAC
|
---|
1029 | Q_D(QMenuBar);
|
---|
1030 | if(d->mac_menubar)
|
---|
1031 | return;
|
---|
1032 | #endif
|
---|
1033 | #ifdef Q_OS_WINCE
|
---|
1034 | Q_D(QMenuBar);
|
---|
1035 | if(d->wce_menubar)
|
---|
1036 | return;
|
---|
1037 | #endif
|
---|
1038 | QWidget::setVisible(visible);
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | /*!
|
---|
1042 | \reimp
|
---|
1043 | */
|
---|
1044 | void QMenuBar::mousePressEvent(QMouseEvent *e)
|
---|
1045 | {
|
---|
1046 | Q_D(QMenuBar);
|
---|
1047 | if(e->button() != Qt::LeftButton)
|
---|
1048 | return;
|
---|
1049 |
|
---|
1050 | QAction *action = d->actionAt(e->pos());
|
---|
1051 | if (!action || !d->isVisible(action)) {
|
---|
1052 | d->setCurrentAction(0);
|
---|
1053 | #ifndef QT_NO_WHATSTHIS
|
---|
1054 | if (QWhatsThis::inWhatsThisMode())
|
---|
1055 | QWhatsThis::showText(e->globalPos(), d->whatsThis, this);
|
---|
1056 | #endif
|
---|
1057 | return;
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | d->mouseDown = true;
|
---|
1061 |
|
---|
1062 | if(d->currentAction == action && d->popupState) {
|
---|
1063 | if(QMenu *menu = d->activeMenu) {
|
---|
1064 | d->activeMenu = 0;
|
---|
1065 | menu->hide();
|
---|
1066 | }
|
---|
1067 | #ifdef Q_WS_WIN
|
---|
1068 | if((d->closePopupMode = style()->styleHint(QStyle::SH_MenuBar_DismissOnSecondClick)))
|
---|
1069 | update(d->actionRect(action));
|
---|
1070 | #endif
|
---|
1071 | } else {
|
---|
1072 | d->setCurrentAction(action, true);
|
---|
1073 | }
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | /*!
|
---|
1077 | \reimp
|
---|
1078 | */
|
---|
1079 | void QMenuBar::mouseReleaseEvent(QMouseEvent *e)
|
---|
1080 | {
|
---|
1081 | Q_D(QMenuBar);
|
---|
1082 | if(e->button() != Qt::LeftButton || !d->mouseDown)
|
---|
1083 | return;
|
---|
1084 |
|
---|
1085 | d->mouseDown = false;
|
---|
1086 | QAction *action = d->actionAt(e->pos());
|
---|
1087 | if((d->closePopupMode && action == d->currentAction) || !action || !action->menu()) {
|
---|
1088 | //we set the current action before activating
|
---|
1089 | //so that we let the leave event set the current back to 0
|
---|
1090 | d->setCurrentAction(action, false);
|
---|
1091 | if(action)
|
---|
1092 | d->activateAction(action, QAction::Trigger);
|
---|
1093 | }
|
---|
1094 | d->closePopupMode = 0;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | /*!
|
---|
1098 | \reimp
|
---|
1099 | */
|
---|
1100 | void QMenuBar::keyPressEvent(QKeyEvent *e)
|
---|
1101 | {
|
---|
1102 | Q_D(QMenuBar);
|
---|
1103 | int key = e->key();
|
---|
1104 | if(isRightToLeft()) { // in reverse mode open/close key for submenues are reversed
|
---|
1105 | if(key == Qt::Key_Left)
|
---|
1106 | key = Qt::Key_Right;
|
---|
1107 | else if(key == Qt::Key_Right)
|
---|
1108 | key = Qt::Key_Left;
|
---|
1109 | }
|
---|
1110 | if(key == Qt::Key_Tab) //means right
|
---|
1111 | key = Qt::Key_Right;
|
---|
1112 | else if(key == Qt::Key_Backtab) //means left
|
---|
1113 | key = Qt::Key_Left;
|
---|
1114 |
|
---|
1115 | bool key_consumed = false;
|
---|
1116 | switch(key) {
|
---|
1117 | case Qt::Key_Up:
|
---|
1118 | case Qt::Key_Down:
|
---|
1119 | case Qt::Key_Enter:
|
---|
1120 | case Qt::Key_Space:
|
---|
1121 | case Qt::Key_Return: {
|
---|
1122 | if(!style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this) || !d->currentAction)
|
---|
1123 | break;
|
---|
1124 | if(d->currentAction->menu()) {
|
---|
1125 | d->popupAction(d->currentAction, true);
|
---|
1126 | } else if(key == Qt::Key_Enter || key == Qt::Key_Return || key == Qt::Key_Space) {
|
---|
1127 | d->activateAction(d->currentAction, QAction::Trigger);
|
---|
1128 | d->setCurrentAction(d->currentAction, false);
|
---|
1129 | d->setKeyboardMode(false);
|
---|
1130 | }
|
---|
1131 | key_consumed = true;
|
---|
1132 | break; }
|
---|
1133 |
|
---|
1134 | case Qt::Key_Right:
|
---|
1135 | case Qt::Key_Left: {
|
---|
1136 | if(d->currentAction) {
|
---|
1137 | QAction *nextAction = 0;
|
---|
1138 | bool allowActiveAndDisabled =
|
---|
1139 | style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, this);
|
---|
1140 |
|
---|
1141 | for(int i=0; i<(int)d->actionList.count(); i++) {
|
---|
1142 | if(d->actionList.at(i) == (QAction*)d->currentAction) {
|
---|
1143 | if (key == Qt::Key_Left) {
|
---|
1144 | while (i > 0) {
|
---|
1145 | i--;
|
---|
1146 | if (allowActiveAndDisabled || d->actionList[i]->isEnabled()) {
|
---|
1147 | nextAction = d->actionList.at(i);
|
---|
1148 | break;
|
---|
1149 | }
|
---|
1150 | }
|
---|
1151 | } else {
|
---|
1152 | while (i < d->actionList.count()-1) {
|
---|
1153 | i++;
|
---|
1154 | if (allowActiveAndDisabled || d->actionList[i]->isEnabled()) {
|
---|
1155 | nextAction = d->actionList.at(i);
|
---|
1156 | break;
|
---|
1157 | }
|
---|
1158 | }
|
---|
1159 | }
|
---|
1160 | break;
|
---|
1161 |
|
---|
1162 | }
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | if(!nextAction) {
|
---|
1166 | if (key == Qt::Key_Left) {
|
---|
1167 | for (int i = d->actionList.size() - 1 ; i >= 0 ; --i) {
|
---|
1168 | if (allowActiveAndDisabled || d->actionList[i]->isEnabled()) {
|
---|
1169 | nextAction = d->actionList.at(i);
|
---|
1170 | i--;
|
---|
1171 | break;
|
---|
1172 | }
|
---|
1173 | }
|
---|
1174 | } else {
|
---|
1175 | for (int i = 0 ; i < d->actionList.count() ; ++i) {
|
---|
1176 | if (allowActiveAndDisabled || d->actionList[i]->isEnabled()) {
|
---|
1177 | nextAction = d->actionList.at(i);
|
---|
1178 | i++;
|
---|
1179 | break;
|
---|
1180 | }
|
---|
1181 | }
|
---|
1182 | }
|
---|
1183 | }
|
---|
1184 | if(nextAction) {
|
---|
1185 | d->setCurrentAction(nextAction, d->popupState, true);
|
---|
1186 | key_consumed = true;
|
---|
1187 | }
|
---|
1188 | }
|
---|
1189 | break; }
|
---|
1190 |
|
---|
1191 | case Qt::Key_Escape:
|
---|
1192 | d->setCurrentAction(0);
|
---|
1193 | d->setKeyboardMode(false);
|
---|
1194 | key_consumed = true;
|
---|
1195 | break;
|
---|
1196 |
|
---|
1197 | default:
|
---|
1198 | key_consumed = false;
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | if(!key_consumed &&
|
---|
1202 | (!e->modifiers() ||
|
---|
1203 | (e->modifiers()&(Qt::MetaModifier|Qt::AltModifier))) && e->text().length()==1 && !d->popupState) {
|
---|
1204 | int clashCount = 0;
|
---|
1205 | QAction *first = 0, *currentSelected = 0, *firstAfterCurrent = 0;
|
---|
1206 | {
|
---|
1207 | QChar c = e->text()[0].toUpper();
|
---|
1208 | for(int i = 0; i < d->actionList.size(); ++i) {
|
---|
1209 | register QAction *act = d->actionList.at(i);
|
---|
1210 | QString s = act->text();
|
---|
1211 | if(!s.isEmpty()) {
|
---|
1212 | int ampersand = s.indexOf(QLatin1Char('&'));
|
---|
1213 | if(ampersand >= 0) {
|
---|
1214 | if(s[ampersand+1].toUpper() == c) {
|
---|
1215 | clashCount++;
|
---|
1216 | if(!first)
|
---|
1217 | first = act;
|
---|
1218 | if(act == d->currentAction)
|
---|
1219 | currentSelected = act;
|
---|
1220 | else if (!firstAfterCurrent && currentSelected)
|
---|
1221 | firstAfterCurrent = act;
|
---|
1222 | }
|
---|
1223 | }
|
---|
1224 | }
|
---|
1225 | }
|
---|
1226 | }
|
---|
1227 | QAction *next_action = 0;
|
---|
1228 | if(clashCount >= 1) {
|
---|
1229 | if(clashCount == 1 || !d->currentAction || (currentSelected && !firstAfterCurrent))
|
---|
1230 | next_action = first;
|
---|
1231 | else
|
---|
1232 | next_action = firstAfterCurrent;
|
---|
1233 | }
|
---|
1234 | if(next_action) {
|
---|
1235 | key_consumed = true;
|
---|
1236 | d->setCurrentAction(next_action, true, true);
|
---|
1237 | }
|
---|
1238 | }
|
---|
1239 | if(key_consumed)
|
---|
1240 | e->accept();
|
---|
1241 | else
|
---|
1242 | e->ignore();
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | /*!
|
---|
1246 | \reimp
|
---|
1247 | */
|
---|
1248 | void QMenuBar::mouseMoveEvent(QMouseEvent *e)
|
---|
1249 | {
|
---|
1250 | Q_D(QMenuBar);
|
---|
1251 | d->mouseDown = e->buttons() & Qt::LeftButton;
|
---|
1252 | QAction *action = d->actionAt(e->pos());
|
---|
1253 | bool popupState = d->popupState || d->mouseDown;
|
---|
1254 | if ((action && d->isVisible(action)) || !popupState)
|
---|
1255 | d->setCurrentAction(action, popupState);
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | /*!
|
---|
1259 | \reimp
|
---|
1260 | */
|
---|
1261 | void QMenuBar::leaveEvent(QEvent *)
|
---|
1262 | {
|
---|
1263 | Q_D(QMenuBar);
|
---|
1264 | if(!hasFocus() && !d->popupState)
|
---|
1265 | d->setCurrentAction(0);
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | /*!
|
---|
1269 | \reimp
|
---|
1270 | */
|
---|
1271 | void QMenuBar::actionEvent(QActionEvent *e)
|
---|
1272 | {
|
---|
1273 | Q_D(QMenuBar);
|
---|
1274 | d->itemsDirty = true;
|
---|
1275 | #ifdef Q_WS_MAC
|
---|
1276 | if(d->mac_menubar) {
|
---|
1277 | if(e->type() == QEvent::ActionAdded)
|
---|
1278 | d->mac_menubar->addAction(e->action(), d->mac_menubar->findAction(e->before()));
|
---|
1279 | else if(e->type() == QEvent::ActionRemoved)
|
---|
1280 | d->mac_menubar->removeAction(e->action());
|
---|
1281 | else if(e->type() == QEvent::ActionChanged)
|
---|
1282 | d->mac_menubar->syncAction(e->action());
|
---|
1283 | }
|
---|
1284 | #endif
|
---|
1285 | #ifdef Q_OS_WINCE
|
---|
1286 | if(d->wce_menubar) {
|
---|
1287 | if(e->type() == QEvent::ActionAdded)
|
---|
1288 | d->wce_menubar->addAction(e->action(), d->wce_menubar->findAction(e->before()));
|
---|
1289 | else if(e->type() == QEvent::ActionRemoved)
|
---|
1290 | d->wce_menubar->removeAction(e->action());
|
---|
1291 | else if(e->type() == QEvent::ActionChanged)
|
---|
1292 | d->wce_menubar->syncAction(e->action());
|
---|
1293 | }
|
---|
1294 | #endif
|
---|
1295 | if(e->type() == QEvent::ActionAdded) {
|
---|
1296 | connect(e->action(), SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
|
---|
1297 | connect(e->action(), SIGNAL(hovered()), this, SLOT(_q_actionHovered()));
|
---|
1298 | } else if(e->type() == QEvent::ActionRemoved) {
|
---|
1299 | e->action()->disconnect(this);
|
---|
1300 | }
|
---|
1301 | if (isVisible()) {
|
---|
1302 | d->updateGeometries();
|
---|
1303 | update();
|
---|
1304 | }
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | /*!
|
---|
1308 | \reimp
|
---|
1309 | */
|
---|
1310 | void QMenuBar::focusInEvent(QFocusEvent *)
|
---|
1311 | {
|
---|
1312 | Q_D(QMenuBar);
|
---|
1313 | if(d->keyboardState && !d->currentAction && !d->actionList.isEmpty())
|
---|
1314 | d->setCurrentAction(d->actionList.first());
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | /*!
|
---|
1318 | \reimp
|
---|
1319 | */
|
---|
1320 | void QMenuBar::focusOutEvent(QFocusEvent *)
|
---|
1321 | {
|
---|
1322 | Q_D(QMenuBar);
|
---|
1323 | if(!d->popupState) {
|
---|
1324 | d->setCurrentAction(0);
|
---|
1325 | d->setKeyboardMode(false);
|
---|
1326 | }
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | /*!
|
---|
1330 | \reimp
|
---|
1331 | */
|
---|
1332 | void QMenuBar::timerEvent (QTimerEvent *e)
|
---|
1333 | {
|
---|
1334 | Q_D(QMenuBar);
|
---|
1335 | if (e->timerId() == d->autoReleaseTimer.timerId()) {
|
---|
1336 | d->autoReleaseTimer.stop();
|
---|
1337 | d->setCurrentAction(0);
|
---|
1338 | }
|
---|
1339 | QWidget::timerEvent(e);
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 |
|
---|
1343 | void QMenuBarPrivate::handleReparent()
|
---|
1344 | {
|
---|
1345 | Q_Q(QMenuBar);
|
---|
1346 | QWidget *newParent = q->parentWidget();
|
---|
1347 | //Note: if parent is reparented, then window may change even if parent doesn't
|
---|
1348 |
|
---|
1349 | // we need to install an event filter on parent, and remove the old one
|
---|
1350 |
|
---|
1351 | if (oldParent != newParent) {
|
---|
1352 | if (oldParent)
|
---|
1353 | oldParent->removeEventFilter(q);
|
---|
1354 | if (newParent)
|
---|
1355 | newParent->installEventFilter(q);
|
---|
1356 | }
|
---|
1357 |
|
---|
1358 | //we also need event filter on top-level (for shortcuts)
|
---|
1359 | QWidget *newWindow = newParent ? newParent->window() : 0;
|
---|
1360 |
|
---|
1361 | if (oldWindow != newWindow) {
|
---|
1362 | if (oldParent && oldParent != oldWindow)
|
---|
1363 | oldWindow->removeEventFilter(q);
|
---|
1364 |
|
---|
1365 | if (newParent && newParent != newWindow)
|
---|
1366 | newWindow->installEventFilter(q);
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | oldParent = newParent;
|
---|
1370 | oldWindow = newWindow;
|
---|
1371 |
|
---|
1372 | #ifdef Q_WS_MAC
|
---|
1373 | macDestroyMenuBar();
|
---|
1374 | macCreateMenuBar(newParent);
|
---|
1375 | #endif
|
---|
1376 |
|
---|
1377 | #ifdef Q_OS_WINCE
|
---|
1378 | if (qt_wince_is_mobile() && wce_menubar)
|
---|
1379 | wce_menubar->rebuild();
|
---|
1380 | #endif
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | #ifdef QT3_SUPPORT
|
---|
1384 | /*!
|
---|
1385 | Sets whether the menu bar should automatically resize itself
|
---|
1386 | when its parent widget is resized.
|
---|
1387 |
|
---|
1388 | This feature is provided to help porting to Qt 4. We recommend
|
---|
1389 | against using it in new code.
|
---|
1390 |
|
---|
1391 | \sa autoGeometry()
|
---|
1392 | */
|
---|
1393 | void QMenuBar::setAutoGeometry(bool b)
|
---|
1394 | {
|
---|
1395 | Q_D(QMenuBar);
|
---|
1396 | d->doAutoResize = b;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | /*!
|
---|
1400 | Returns true if the menu bar automatically resizes itself
|
---|
1401 | when its parent widget is resized; otherwise returns false.
|
---|
1402 |
|
---|
1403 | This feature is provided to help porting to Qt 4. We recommend
|
---|
1404 | against using it in new code.
|
---|
1405 |
|
---|
1406 | \sa setAutoGeometry()
|
---|
1407 | */
|
---|
1408 | bool QMenuBar::autoGeometry() const
|
---|
1409 | {
|
---|
1410 | Q_D(const QMenuBar);
|
---|
1411 | return d->doAutoResize;
|
---|
1412 | }
|
---|
1413 | #endif
|
---|
1414 |
|
---|
1415 | /*!
|
---|
1416 | \reimp
|
---|
1417 | */
|
---|
1418 | void QMenuBar::changeEvent(QEvent *e)
|
---|
1419 | {
|
---|
1420 | Q_D(QMenuBar);
|
---|
1421 | if(e->type() == QEvent::StyleChange) {
|
---|
1422 | d->itemsDirty = true;
|
---|
1423 | setMouseTracking(style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, this));
|
---|
1424 | if(parentWidget())
|
---|
1425 | resize(parentWidget()->width(), heightForWidth(parentWidget()->width()));
|
---|
1426 | d->updateGeometries();
|
---|
1427 | } else if (e->type() == QEvent::ParentChange) {
|
---|
1428 | d->handleReparent();
|
---|
1429 | } else if (e->type() == QEvent::FontChange
|
---|
1430 | || e->type() == QEvent::ApplicationFontChange) {
|
---|
1431 | d->itemsDirty = true;
|
---|
1432 | d->updateGeometries();
|
---|
1433 | }
|
---|
1434 | QWidget::changeEvent(e);
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | /*!
|
---|
1438 | \reimp
|
---|
1439 | */
|
---|
1440 | bool QMenuBar::event(QEvent *e)
|
---|
1441 | {
|
---|
1442 | Q_D(QMenuBar);
|
---|
1443 | switch (e->type()) {
|
---|
1444 | case QEvent::KeyPress: {
|
---|
1445 | QKeyEvent *ke = (QKeyEvent*)e;
|
---|
1446 | #if 0
|
---|
1447 | if(!d->keyboardState) { //all keypresses..
|
---|
1448 | d->setCurrentAction(0);
|
---|
1449 | return ;
|
---|
1450 | }
|
---|
1451 | #endif
|
---|
1452 | if(ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
|
---|
1453 | keyPressEvent(ke);
|
---|
1454 | return true;
|
---|
1455 | }
|
---|
1456 |
|
---|
1457 | } break;
|
---|
1458 | #ifndef QT_NO_SHORTCUT
|
---|
1459 | case QEvent::Shortcut: {
|
---|
1460 | QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
|
---|
1461 | int shortcutId = se->shortcutId();
|
---|
1462 | for(int j = 0; j < d->shortcutIndexMap.size(); ++j) {
|
---|
1463 | if (shortcutId == d->shortcutIndexMap.value(j))
|
---|
1464 | d->_q_internalShortcutActivated(j);
|
---|
1465 | }
|
---|
1466 | } break;
|
---|
1467 | #endif
|
---|
1468 | case QEvent::Show:
|
---|
1469 | #ifdef QT3_SUPPORT
|
---|
1470 | if(QWidget *p = parentWidget()) {
|
---|
1471 | // If itemsDirty == true, updateGeometries sends the MenubarUpdated event.
|
---|
1472 | if (!d->itemsDirty) {
|
---|
1473 | QMenubarUpdatedEvent menubarUpdated(this);
|
---|
1474 | QApplication::sendEvent(p, &menubarUpdated);
|
---|
1475 | }
|
---|
1476 | }
|
---|
1477 | #endif
|
---|
1478 | d->_q_updateLayout();
|
---|
1479 | break;
|
---|
1480 | case QEvent::ShortcutOverride: {
|
---|
1481 | QKeyEvent *kev = static_cast<QKeyEvent*>(e);
|
---|
1482 | if (kev->key() == Qt::Key_Escape) {
|
---|
1483 | e->accept();
|
---|
1484 | return true;
|
---|
1485 | }
|
---|
1486 | }
|
---|
1487 | break;
|
---|
1488 |
|
---|
1489 | #ifdef QT3_SUPPORT
|
---|
1490 | case QEvent::Hide: {
|
---|
1491 | if(QWidget *p = parentWidget()) {
|
---|
1492 | QMenubarUpdatedEvent menubarUpdated(this);
|
---|
1493 | QApplication::sendEvent(p, &menubarUpdated);
|
---|
1494 | }
|
---|
1495 | } break;
|
---|
1496 | #endif
|
---|
1497 |
|
---|
1498 | #ifndef QT_NO_WHATSTHIS
|
---|
1499 | case QEvent::QueryWhatsThis:
|
---|
1500 | e->setAccepted(d->whatsThis.size());
|
---|
1501 | if (QAction *action = d->actionAt(static_cast<QHelpEvent*>(e)->pos())) {
|
---|
1502 | if (action->whatsThis().size() || action->menu())
|
---|
1503 | e->accept();
|
---|
1504 | }
|
---|
1505 | return true;
|
---|
1506 | #endif
|
---|
1507 | case QEvent::LayoutDirectionChange:
|
---|
1508 | d->_q_updateLayout();
|
---|
1509 | break;
|
---|
1510 | default:
|
---|
1511 | break;
|
---|
1512 | }
|
---|
1513 | return QWidget::event(e);
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | /*!
|
---|
1517 | \reimp
|
---|
1518 | */
|
---|
1519 | bool QMenuBar::eventFilter(QObject *object, QEvent *event)
|
---|
1520 | {
|
---|
1521 | Q_D(QMenuBar);
|
---|
1522 | if (object == parent() && object) {
|
---|
1523 | #ifdef QT3_SUPPORT
|
---|
1524 | if (d->doAutoResize && event->type() == QEvent::Resize) {
|
---|
1525 | QResizeEvent *e = (QResizeEvent *)event;
|
---|
1526 | int w = e->size().width();
|
---|
1527 | setGeometry(0, y(), w, heightForWidth(w));
|
---|
1528 | return false;
|
---|
1529 | }
|
---|
1530 | #endif
|
---|
1531 | if (event->type() == QEvent::ParentChange) //GrandparentChange
|
---|
1532 | d->handleReparent();
|
---|
1533 | }
|
---|
1534 | if (object == d->leftWidget || object == d->rightWidget) {
|
---|
1535 | switch (event->type()) {
|
---|
1536 | case QEvent::ShowToParent:
|
---|
1537 | case QEvent::HideToParent:
|
---|
1538 | d->_q_updateLayout();
|
---|
1539 | break;
|
---|
1540 | default:
|
---|
1541 | break;
|
---|
1542 | }
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 | if (style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this)) {
|
---|
1546 | if (d->altPressed) {
|
---|
1547 | switch (event->type()) {
|
---|
1548 | case QEvent::KeyPress:
|
---|
1549 | case QEvent::KeyRelease:
|
---|
1550 | {
|
---|
1551 | QKeyEvent *kev = static_cast<QKeyEvent*>(event);
|
---|
1552 | if (kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta) {
|
---|
1553 | if (event->type() == QEvent::KeyPress) // Alt-press does not interest us, we have the shortcut-override event
|
---|
1554 | break;
|
---|
1555 | d->setKeyboardMode(!d->keyboardState);
|
---|
1556 | }
|
---|
1557 | }
|
---|
1558 | // fall through
|
---|
1559 | case QEvent::MouseButtonPress:
|
---|
1560 | case QEvent::MouseButtonRelease:
|
---|
1561 | case QEvent::MouseMove:
|
---|
1562 | case QEvent::FocusIn:
|
---|
1563 | case QEvent::FocusOut:
|
---|
1564 | case QEvent::ActivationChange:
|
---|
1565 | d->altPressed = false;
|
---|
1566 | qApp->removeEventFilter(this);
|
---|
1567 | break;
|
---|
1568 | default:
|
---|
1569 | break;
|
---|
1570 | }
|
---|
1571 | } else if (isVisible()) {
|
---|
1572 | if (event->type() == QEvent::ShortcutOverride) {
|
---|
1573 | QKeyEvent *kev = static_cast<QKeyEvent*>(event);
|
---|
1574 | if ((kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta)
|
---|
1575 | && kev->modifiers() == Qt::AltModifier) {
|
---|
1576 | d->altPressed = true;
|
---|
1577 | qApp->installEventFilter(this);
|
---|
1578 | }
|
---|
1579 | }
|
---|
1580 | }
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 | return false;
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 | /*!
|
---|
1587 | \internal
|
---|
1588 |
|
---|
1589 | Return the item at \a pt, or 0 if there is no item there or if it is
|
---|
1590 | a separator item.
|
---|
1591 | */
|
---|
1592 | QAction *QMenuBar::actionAt(const QPoint &pt) const
|
---|
1593 | {
|
---|
1594 | Q_D(const QMenuBar);
|
---|
1595 | return d->actionAt(pt);
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 | /*!
|
---|
1599 | \internal
|
---|
1600 |
|
---|
1601 | Returns the geometry of action \a act.
|
---|
1602 | */
|
---|
1603 | QRect QMenuBar::actionGeometry(QAction *act) const
|
---|
1604 | {
|
---|
1605 | Q_D(const QMenuBar);
|
---|
1606 | return d->actionRect(act);
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | /*!
|
---|
1610 | \reimp
|
---|
1611 | */
|
---|
1612 | QSize QMenuBar::minimumSizeHint() const
|
---|
1613 | {
|
---|
1614 | Q_D(const QMenuBar);
|
---|
1615 | #ifdef Q_WS_MAC
|
---|
1616 | const bool as_gui_menubar = !d->mac_menubar;
|
---|
1617 | #elif defined (Q_OS_WINCE)
|
---|
1618 | const bool as_gui_menubar = !d->wce_menubar;
|
---|
1619 | #else
|
---|
1620 | const bool as_gui_menubar = true;
|
---|
1621 | #endif
|
---|
1622 |
|
---|
1623 | ensurePolished();
|
---|
1624 | QSize ret(0, 0);
|
---|
1625 | const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this);
|
---|
1626 | const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
|
---|
1627 | int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
|
---|
1628 | int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
|
---|
1629 | if(as_gui_menubar) {
|
---|
1630 | QMap<QAction*, QRect> actionRects;
|
---|
1631 | QList<QAction*> actionList;
|
---|
1632 | int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width();
|
---|
1633 | d->calcActionRects(w - (2 * fw), 0, actionRects, actionList);
|
---|
1634 | if (d->actionList.count() > 0) {
|
---|
1635 | ret = d->actionRect(d->actionList.at(0)).size();
|
---|
1636 | if (!d->extension->isHidden())
|
---|
1637 | ret += QSize(d->extension->sizeHint().width(), 0);
|
---|
1638 | }
|
---|
1639 | ret += QSize(2*fw + hmargin, 2*fw + vmargin);
|
---|
1640 | }
|
---|
1641 | int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
|
---|
1642 | if(d->leftWidget) {
|
---|
1643 | QSize sz = d->leftWidget->minimumSizeHint();
|
---|
1644 | ret.setWidth(ret.width() + sz.width());
|
---|
1645 | if(sz.height() + margin > ret.height())
|
---|
1646 | ret.setHeight(sz.height() + margin);
|
---|
1647 | }
|
---|
1648 | if(d->rightWidget) {
|
---|
1649 | QSize sz = d->rightWidget->minimumSizeHint();
|
---|
1650 | ret.setWidth(ret.width() + sz.width());
|
---|
1651 | if(sz.height() + margin > ret.height())
|
---|
1652 | ret.setHeight(sz.height() + margin);
|
---|
1653 | }
|
---|
1654 | if(as_gui_menubar) {
|
---|
1655 | QStyleOptionMenuItem opt;
|
---|
1656 | opt.rect = rect();
|
---|
1657 | opt.menuRect = rect();
|
---|
1658 | opt.state = QStyle::State_None;
|
---|
1659 | opt.menuItemType = QStyleOptionMenuItem::Normal;
|
---|
1660 | opt.checkType = QStyleOptionMenuItem::NotCheckable;
|
---|
1661 | opt.palette = palette();
|
---|
1662 | return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt,
|
---|
1663 | ret.expandedTo(QApplication::globalStrut()),
|
---|
1664 | this));
|
---|
1665 | }
|
---|
1666 | return ret;
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | /*!
|
---|
1670 | \reimp
|
---|
1671 | */
|
---|
1672 | QSize QMenuBar::sizeHint() const
|
---|
1673 | {
|
---|
1674 | Q_D(const QMenuBar);
|
---|
1675 | #ifdef Q_WS_MAC
|
---|
1676 | const bool as_gui_menubar = !d->mac_menubar;
|
---|
1677 | #elif defined (Q_OS_WINCE)
|
---|
1678 | const bool as_gui_menubar = !d->wce_menubar;
|
---|
1679 | #else
|
---|
1680 | const bool as_gui_menubar = true;
|
---|
1681 | #endif
|
---|
1682 |
|
---|
1683 | ensurePolished();
|
---|
1684 | QSize ret(0, 0);
|
---|
1685 | const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this);
|
---|
1686 | const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
|
---|
1687 | int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
|
---|
1688 | int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
|
---|
1689 | if(as_gui_menubar) {
|
---|
1690 | QMap<QAction*, QRect> actionRects;
|
---|
1691 | QList<QAction*> actionList;
|
---|
1692 | const int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width();
|
---|
1693 | d->calcActionRects(w - (2 * fw), 0, actionRects, actionList);
|
---|
1694 | for (QMap<QAction*, QRect>::const_iterator i = actionRects.constBegin();
|
---|
1695 | i != actionRects.constEnd(); ++i) {
|
---|
1696 | QRect actionRect(i.value());
|
---|
1697 | if(actionRect.x() + actionRect.width() > ret.width())
|
---|
1698 | ret.setWidth(actionRect.x() + actionRect.width());
|
---|
1699 | if(actionRect.y() + actionRect.height() > ret.height())
|
---|
1700 | ret.setHeight(actionRect.y() + actionRect.height());
|
---|
1701 | }
|
---|
1702 | ret += QSize(2*fw + 2*hmargin, 2*fw + 2*vmargin);
|
---|
1703 | }
|
---|
1704 | int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
|
---|
1705 | if(d->leftWidget) {
|
---|
1706 | QSize sz = d->leftWidget->sizeHint();
|
---|
1707 | ret.setWidth(ret.width() + sz.width());
|
---|
1708 | if(sz.height() + margin > ret.height())
|
---|
1709 | ret.setHeight(sz.height() + margin);
|
---|
1710 | }
|
---|
1711 | if(d->rightWidget) {
|
---|
1712 | QSize sz = d->rightWidget->sizeHint();
|
---|
1713 | ret.setWidth(ret.width() + sz.width());
|
---|
1714 | if(sz.height() + margin > ret.height())
|
---|
1715 | ret.setHeight(sz.height() + margin);
|
---|
1716 | }
|
---|
1717 | if(as_gui_menubar) {
|
---|
1718 | QStyleOptionMenuItem opt;
|
---|
1719 | opt.rect = rect();
|
---|
1720 | opt.menuRect = rect();
|
---|
1721 | opt.state = QStyle::State_None;
|
---|
1722 | opt.menuItemType = QStyleOptionMenuItem::Normal;
|
---|
1723 | opt.checkType = QStyleOptionMenuItem::NotCheckable;
|
---|
1724 | opt.palette = palette();
|
---|
1725 | return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt,
|
---|
1726 | ret.expandedTo(QApplication::globalStrut()),
|
---|
1727 | this));
|
---|
1728 | }
|
---|
1729 | return ret;
|
---|
1730 | }
|
---|
1731 |
|
---|
1732 | /*!
|
---|
1733 | \reimp
|
---|
1734 | */
|
---|
1735 | int QMenuBar::heightForWidth(int) const
|
---|
1736 | {
|
---|
1737 | Q_D(const QMenuBar);
|
---|
1738 | #ifdef Q_WS_MAC
|
---|
1739 | const bool as_gui_menubar = !d->mac_menubar;
|
---|
1740 | #elif defined (Q_OS_WINCE)
|
---|
1741 | const bool as_gui_menubar = !d->wce_menubar;
|
---|
1742 | #else
|
---|
1743 | const bool as_gui_menubar = true;
|
---|
1744 | #endif
|
---|
1745 | int height = 0;
|
---|
1746 | const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
|
---|
1747 | int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
|
---|
1748 | int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
|
---|
1749 | if(as_gui_menubar) {
|
---|
1750 | if (d->actionList.count()) {
|
---|
1751 | // assume all actionrects have the same height
|
---|
1752 | height = d->actionRect(d->actionList.first()).height();
|
---|
1753 | height += spaceBelowMenuBar;
|
---|
1754 | }
|
---|
1755 | height += 2*fw;
|
---|
1756 | height += 2*vmargin;
|
---|
1757 | }
|
---|
1758 | int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
|
---|
1759 | if(d->leftWidget)
|
---|
1760 | height = qMax(d->leftWidget->sizeHint().height() + margin, height);
|
---|
1761 | if(d->rightWidget)
|
---|
1762 | height = qMax(d->rightWidget->sizeHint().height() + margin, height);
|
---|
1763 | if(as_gui_menubar) {
|
---|
1764 | QStyleOptionMenuItem opt;
|
---|
1765 | opt.init(this);
|
---|
1766 | opt.menuRect = rect();
|
---|
1767 | opt.state = QStyle::State_None;
|
---|
1768 | opt.menuItemType = QStyleOptionMenuItem::Normal;
|
---|
1769 | opt.checkType = QStyleOptionMenuItem::NotCheckable;
|
---|
1770 | return style()->sizeFromContents(QStyle::CT_MenuBar, &opt, QSize(0, height), this).height(); //not pretty..
|
---|
1771 | }
|
---|
1772 | return height;
|
---|
1773 | }
|
---|
1774 |
|
---|
1775 | /*!
|
---|
1776 | \internal
|
---|
1777 | */
|
---|
1778 | void QMenuBarPrivate::_q_internalShortcutActivated(int id)
|
---|
1779 | {
|
---|
1780 | Q_Q(QMenuBar);
|
---|
1781 | QAction *act = actionList.at(id);
|
---|
1782 | setCurrentAction(act, true, true);
|
---|
1783 | if (act && !act->menu()) {
|
---|
1784 | activateAction(act, QAction::Trigger);
|
---|
1785 | //100 is the same as the default value in QPushButton::animateClick
|
---|
1786 | autoReleaseTimer.start(100, q);
|
---|
1787 | }
|
---|
1788 | }
|
---|
1789 |
|
---|
1790 | void QMenuBarPrivate::_q_updateLayout()
|
---|
1791 | {
|
---|
1792 | Q_Q(QMenuBar);
|
---|
1793 | itemsDirty = true;
|
---|
1794 | if (q->isVisible()) {
|
---|
1795 | updateGeometries();
|
---|
1796 | q->update();
|
---|
1797 | }
|
---|
1798 | }
|
---|
1799 |
|
---|
1800 | /*!
|
---|
1801 | \internal
|
---|
1802 |
|
---|
1803 | This sets widget \a w to be shown directly on the left of the first or
|
---|
1804 | the right of the last menu item, depending on \a corner.
|
---|
1805 | */
|
---|
1806 | void QMenuBar::setCornerWidget(QWidget *w, Qt::Corner corner)
|
---|
1807 | {
|
---|
1808 | Q_D(QMenuBar);
|
---|
1809 | switch (corner) {
|
---|
1810 | case Qt::TopLeftCorner:
|
---|
1811 | if (d->leftWidget)
|
---|
1812 | d->leftWidget->removeEventFilter(this);
|
---|
1813 | d->leftWidget = w;
|
---|
1814 | break;
|
---|
1815 | case Qt::TopRightCorner:
|
---|
1816 | if (d->rightWidget)
|
---|
1817 | d->rightWidget->removeEventFilter(this);
|
---|
1818 | d->rightWidget = w;
|
---|
1819 | break;
|
---|
1820 | default:
|
---|
1821 | qWarning("QMenuBar::setCornerWidget: Only TopLeftCorner and TopRightCorner are supported");
|
---|
1822 | return;
|
---|
1823 | }
|
---|
1824 |
|
---|
1825 | if (w) {
|
---|
1826 | w->setParent(this);
|
---|
1827 | w->installEventFilter(this);
|
---|
1828 | }
|
---|
1829 |
|
---|
1830 | d->_q_updateLayout();
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | /*!
|
---|
1834 | \internal
|
---|
1835 |
|
---|
1836 | Returns the widget in the left of the first or the right of the last menu
|
---|
1837 | item, depending on \a corner.
|
---|
1838 | */
|
---|
1839 | QWidget *QMenuBar::cornerWidget(Qt::Corner corner) const
|
---|
1840 | {
|
---|
1841 | Q_D(const QMenuBar);
|
---|
1842 | QWidget *w = 0;
|
---|
1843 | switch(corner) {
|
---|
1844 | case Qt::TopLeftCorner:
|
---|
1845 | w = d->leftWidget;
|
---|
1846 | break;
|
---|
1847 | case Qt::TopRightCorner:
|
---|
1848 | w = d->rightWidget;
|
---|
1849 | break;
|
---|
1850 | default:
|
---|
1851 | qWarning("QMenuBar::cornerWidget: Only TopLeftCorner and TopRightCorner are supported");
|
---|
1852 | break;
|
---|
1853 | }
|
---|
1854 |
|
---|
1855 | return w;
|
---|
1856 | }
|
---|
1857 |
|
---|
1858 | /*!
|
---|
1859 | \since 4.4
|
---|
1860 |
|
---|
1861 | Sets the default action to \a act.
|
---|
1862 |
|
---|
1863 | The default action is assigned to the left soft key. The menu is assigned
|
---|
1864 | to the right soft key.
|
---|
1865 |
|
---|
1866 | Currently there is only support for the default action on Windows
|
---|
1867 | Mobile. All other platforms ignore the default action.
|
---|
1868 |
|
---|
1869 | \sa defaultAction()
|
---|
1870 | */
|
---|
1871 |
|
---|
1872 | #ifdef Q_OS_WINCE
|
---|
1873 | void QMenuBar::setDefaultAction(QAction *act)
|
---|
1874 | {
|
---|
1875 | Q_D(QMenuBar);
|
---|
1876 | if (d->defaultAction == act)
|
---|
1877 | return;
|
---|
1878 | #ifdef Q_OS_WINCE
|
---|
1879 | if (qt_wince_is_mobile())
|
---|
1880 | if (d->defaultAction) {
|
---|
1881 | disconnect(d->defaultAction, SIGNAL(changed()), this, SLOT(_q_updateDefaultAction()));
|
---|
1882 | disconnect(d->defaultAction, SIGNAL(destroyed ()), this, SLOT(_q_updateDefaultAction()));
|
---|
1883 | }
|
---|
1884 | #endif
|
---|
1885 | d->defaultAction = act;
|
---|
1886 | #ifdef Q_OS_WINCE
|
---|
1887 | if (qt_wince_is_mobile())
|
---|
1888 | if (d->defaultAction) {
|
---|
1889 | connect(d->defaultAction, SIGNAL(changed()), this, SLOT(_q_updateDefaultAction()));
|
---|
1890 | connect(d->defaultAction, SIGNAL(destroyed()), this, SLOT(_q_updateDefaultAction()));
|
---|
1891 | }
|
---|
1892 | if (d->wce_menubar) {
|
---|
1893 | d->wce_menubar->rebuild();
|
---|
1894 | }
|
---|
1895 | #endif
|
---|
1896 | }
|
---|
1897 |
|
---|
1898 | /*!
|
---|
1899 | \since 4.4
|
---|
1900 |
|
---|
1901 | Returns the current default action.
|
---|
1902 |
|
---|
1903 | \sa setDefaultAction()
|
---|
1904 | */
|
---|
1905 | QAction *QMenuBar::defaultAction() const
|
---|
1906 | {
|
---|
1907 | return d_func()->defaultAction;
|
---|
1908 | }
|
---|
1909 | #endif
|
---|
1910 |
|
---|
1911 | /*!
|
---|
1912 | \fn void QMenuBar::triggered(QAction *action)
|
---|
1913 |
|
---|
1914 | This signal is emitted when an action in a menu belonging to this menubar
|
---|
1915 | is triggered as a result of a mouse click; \a action is the action that
|
---|
1916 | caused the signal to be emitted.
|
---|
1917 |
|
---|
1918 | Normally, you connect each menu action to a single slot using
|
---|
1919 | QAction::triggered(), but sometimes you will want to connect
|
---|
1920 | several items to a single slot (most often if the user selects
|
---|
1921 | from an array). This signal is useful in such cases.
|
---|
1922 |
|
---|
1923 | \sa hovered(), QAction::triggered()
|
---|
1924 | */
|
---|
1925 |
|
---|
1926 | /*!
|
---|
1927 | \fn void QMenuBar::hovered(QAction *action)
|
---|
1928 |
|
---|
1929 | This signal is emitted when a menu action is highlighted; \a action
|
---|
1930 | is the action that caused the event to be sent.
|
---|
1931 |
|
---|
1932 | Often this is used to update status information.
|
---|
1933 |
|
---|
1934 | \sa triggered(), QAction::hovered()
|
---|
1935 | */
|
---|
1936 |
|
---|
1937 |
|
---|
1938 | #ifdef QT3_SUPPORT
|
---|
1939 | /*!
|
---|
1940 | Use style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, this)
|
---|
1941 | instead.
|
---|
1942 | */
|
---|
1943 | int QMenuBar::frameWidth() const
|
---|
1944 | {
|
---|
1945 | return style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
|
---|
1946 | }
|
---|
1947 |
|
---|
1948 | int QMenuBar::insertAny(const QIcon *icon, const QString *text, const QObject *receiver, const char *member,
|
---|
1949 | const QKeySequence *shortcut, const QMenu *popup, int id, int index)
|
---|
1950 | {
|
---|
1951 | QAction *act = popup ? popup->menuAction() : new QAction(this);
|
---|
1952 | if(id != -1)
|
---|
1953 | static_cast<QMenuItem*>(act)->setId(id);
|
---|
1954 | if(icon)
|
---|
1955 | act->setIcon(*icon);
|
---|
1956 | if(text)
|
---|
1957 | act->setText(*text);
|
---|
1958 | if(shortcut)
|
---|
1959 | act->setShortcut(*shortcut);
|
---|
1960 | if(receiver && member)
|
---|
1961 | QObject::connect(act, SIGNAL(triggered(bool)), receiver, member);
|
---|
1962 | if(index == -1 || index >= actions().count())
|
---|
1963 | addAction(act);
|
---|
1964 | else
|
---|
1965 | insertAction(actions().value(index), act);
|
---|
1966 | return findIdForAction(act);
|
---|
1967 | }
|
---|
1968 |
|
---|
1969 | /*!
|
---|
1970 | \since 4.2
|
---|
1971 |
|
---|
1972 | Use addSeparator() or insertAction() instead.
|
---|
1973 |
|
---|
1974 | \oldcode
|
---|
1975 | menuBar->insertSeparator();
|
---|
1976 | \newcode
|
---|
1977 | menuBar->addSeparator();
|
---|
1978 | \endcode
|
---|
1979 | */
|
---|
1980 | int QMenuBar::insertSeparator(int index)
|
---|
1981 | {
|
---|
1982 | QAction *act = new QAction(this);
|
---|
1983 | act->setSeparator(true);
|
---|
1984 | if(index == -1 || index >= actions().count())
|
---|
1985 | addAction(act);
|
---|
1986 | else
|
---|
1987 | insertAction(actions().value(index), act);
|
---|
1988 | return findIdForAction(act);
|
---|
1989 | }
|
---|
1990 |
|
---|
1991 | /*!
|
---|
1992 | Use QAction::setData() instead.
|
---|
1993 | */
|
---|
1994 | bool QMenuBar::setItemParameter(int id, int param)
|
---|
1995 | {
|
---|
1996 | if(QAction *act = findActionForId(id)) {
|
---|
1997 | act->d_func()->param = param;
|
---|
1998 | return true;
|
---|
1999 | }
|
---|
2000 | return false;
|
---|
2001 | }
|
---|
2002 |
|
---|
2003 | /*!
|
---|
2004 | Use QAction::data() instead.
|
---|
2005 | */
|
---|
2006 | int QMenuBar::itemParameter(int id) const
|
---|
2007 | {
|
---|
2008 | if(QAction *act = findActionForId(id))
|
---|
2009 | return act->d_func()->param;
|
---|
2010 | return id;
|
---|
2011 | }
|
---|
2012 |
|
---|
2013 | QAction *QMenuBar::findActionForId(int id) const
|
---|
2014 | {
|
---|
2015 | QList<QAction *> list = actions();
|
---|
2016 | for (int i = 0; i < list.size(); ++i) {
|
---|
2017 | QAction *act = list.at(i);
|
---|
2018 | if (findIdForAction(act) == id)
|
---|
2019 | return act;
|
---|
2020 | }
|
---|
2021 | return 0;
|
---|
2022 | }
|
---|
2023 |
|
---|
2024 | int QMenuBar::findIdForAction(QAction *act) const
|
---|
2025 | {
|
---|
2026 | Q_ASSERT(act);
|
---|
2027 | return act->d_func()->id;
|
---|
2028 | }
|
---|
2029 | #endif
|
---|
2030 |
|
---|
2031 | /*!
|
---|
2032 | \enum QMenuBar::Separator
|
---|
2033 |
|
---|
2034 | \compat
|
---|
2035 |
|
---|
2036 | \value Never
|
---|
2037 | \value InWindowsStyle
|
---|
2038 |
|
---|
2039 | */
|
---|
2040 |
|
---|
2041 | /*!
|
---|
2042 | \fn void QMenuBar::addAction(QAction *action)
|
---|
2043 | \overload
|
---|
2044 |
|
---|
2045 | Appends the action \a action to the menu bar's list of actions.
|
---|
2046 |
|
---|
2047 | \sa QMenu::addAction(), QWidget::addAction(), QWidget::actions()
|
---|
2048 | */
|
---|
2049 |
|
---|
2050 | /*!
|
---|
2051 | \fn uint QMenuBar::count() const
|
---|
2052 |
|
---|
2053 | Use actions().count() instead.
|
---|
2054 | */
|
---|
2055 |
|
---|
2056 | /*!
|
---|
2057 | \fn int QMenuBar::insertItem(const QString &text, const QObject *receiver, const char* member, const QKeySequence& shortcut, int id, int index)
|
---|
2058 |
|
---|
2059 | Use one of the insertAction() or addAction() overloads instead.
|
---|
2060 | */
|
---|
2061 |
|
---|
2062 | /*!
|
---|
2063 | \fn int QMenuBar::insertItem(const QIcon& icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence& shortcut, int id, int index)
|
---|
2064 |
|
---|
2065 | Use one of the insertAction() or addAction() overloads instead.
|
---|
2066 | */
|
---|
2067 |
|
---|
2068 | /*!
|
---|
2069 | \fn int QMenuBar::insertItem(const QPixmap &pixmap, const QObject *receiver, const char* member, const QKeySequence& shortcut, int id, int index)
|
---|
2070 |
|
---|
2071 | Use one of the insertAction(), addAction(), insertMenu(), or
|
---|
2072 | addMenu() overloads instead.
|
---|
2073 | */
|
---|
2074 |
|
---|
2075 | /*!
|
---|
2076 | \fn int QMenuBar::insertItem(const QString &text, int id, int index)
|
---|
2077 |
|
---|
2078 | Use one of the insertAction() or addAction() overloads instead.
|
---|
2079 | */
|
---|
2080 |
|
---|
2081 | /*!
|
---|
2082 | \fn int QMenuBar::insertItem(const QIcon& icon, const QString &text, int id, int index)
|
---|
2083 |
|
---|
2084 | Use one of the insertAction(), addAction(), insertMenu(), or
|
---|
2085 | addMenu() overloads instead.
|
---|
2086 | */
|
---|
2087 |
|
---|
2088 | /*!
|
---|
2089 | \fn int QMenuBar::insertItem(const QString &text, QMenu *popup, int id, int index)
|
---|
2090 |
|
---|
2091 | Use one of the insertMenu(), or addMenu() overloads instead.
|
---|
2092 | */
|
---|
2093 |
|
---|
2094 | /*!
|
---|
2095 | \fn int QMenuBar::insertItem(const QIcon& icon, const QString &text, QMenu *popup, int id, int index)
|
---|
2096 |
|
---|
2097 | Use one of the insertMenu(), or addMenu() overloads instead.
|
---|
2098 | */
|
---|
2099 |
|
---|
2100 | /*!
|
---|
2101 | \fn int QMenuBar::insertItem(const QPixmap &pixmap, int id, int index)
|
---|
2102 |
|
---|
2103 | Use one of the insertAction(), addAction(), insertMenu(), or
|
---|
2104 | addMenu() overloads instead.
|
---|
2105 | */
|
---|
2106 |
|
---|
2107 | /*!
|
---|
2108 | \fn int QMenuBar::insertItem(const QPixmap &pixmap, QMenu *popup, int id, int index)
|
---|
2109 |
|
---|
2110 | Use one of the insertMenu(), or addMenu() overloads instead.
|
---|
2111 | */
|
---|
2112 |
|
---|
2113 | /*!
|
---|
2114 | \fn void QMenuBar::removeItem(int id)
|
---|
2115 |
|
---|
2116 | Use removeAction() instead.
|
---|
2117 | */
|
---|
2118 |
|
---|
2119 | /*!
|
---|
2120 | \fn void QMenuBar::removeItemAt(int index)
|
---|
2121 |
|
---|
2122 | Use removeAction() instead.
|
---|
2123 | */
|
---|
2124 |
|
---|
2125 | /*!
|
---|
2126 | \fn QKeySequence QMenuBar::accel(int id) const
|
---|
2127 |
|
---|
2128 | Use shortcut() on the relevant QAction instead.
|
---|
2129 | */
|
---|
2130 |
|
---|
2131 | /*!
|
---|
2132 | \fn void QMenuBar::setAccel(const QKeySequence& key, int id)
|
---|
2133 |
|
---|
2134 | Use setShortcut() on the relevant QAction instead.
|
---|
2135 | */
|
---|
2136 |
|
---|
2137 | /*!
|
---|
2138 | \fn QIcon QMenuBar::iconSet(int id) const
|
---|
2139 |
|
---|
2140 | Use icon() on the relevant QAction instead.
|
---|
2141 | */
|
---|
2142 |
|
---|
2143 | /*!
|
---|
2144 | \fn QString QMenuBar::text(int id) const
|
---|
2145 |
|
---|
2146 | Use text() on the relevant QAction instead.
|
---|
2147 | */
|
---|
2148 |
|
---|
2149 | /*!
|
---|
2150 | \fn QPixmap QMenuBar::pixmap(int id) const
|
---|
2151 |
|
---|
2152 | Use QPixmap(icon()) on the relevant QAction instead.
|
---|
2153 | */
|
---|
2154 |
|
---|
2155 | /*!
|
---|
2156 | \fn void QMenuBar::setWhatsThis(int id, const QString &w)
|
---|
2157 |
|
---|
2158 | Use setWhatsThis() on the relevant QAction instead.
|
---|
2159 | */
|
---|
2160 |
|
---|
2161 | /*!
|
---|
2162 | \fn QString QMenuBar::whatsThis(int id) const
|
---|
2163 |
|
---|
2164 | Use whatsThis() on the relevant QAction instead.
|
---|
2165 | */
|
---|
2166 |
|
---|
2167 | /*!
|
---|
2168 | \fn void QMenuBar::changeItem(int id, const QString &text)
|
---|
2169 |
|
---|
2170 | Use setText() on the relevant QAction instead.
|
---|
2171 | */
|
---|
2172 |
|
---|
2173 | /*!
|
---|
2174 | \fn void QMenuBar::changeItem(int id, const QPixmap &pixmap)
|
---|
2175 |
|
---|
2176 | Use setText() on the relevant QAction instead.
|
---|
2177 | */
|
---|
2178 |
|
---|
2179 | /*!
|
---|
2180 | \fn void QMenuBar::changeItem(int id, const QIcon &icon, const QString &text)
|
---|
2181 |
|
---|
2182 | Use setIcon() and setText() on the relevant QAction instead.
|
---|
2183 | */
|
---|
2184 |
|
---|
2185 | /*!
|
---|
2186 | \fn bool QMenuBar::isItemActive(int id) const
|
---|
2187 |
|
---|
2188 | Use activeAction() instead.
|
---|
2189 | */
|
---|
2190 |
|
---|
2191 | /*!
|
---|
2192 | \fn bool QMenuBar::isItemEnabled(int id) const
|
---|
2193 |
|
---|
2194 | Use isEnabled() on the relevant QAction instead.
|
---|
2195 | */
|
---|
2196 |
|
---|
2197 | /*!
|
---|
2198 | \fn void QMenuBar::setItemEnabled(int id, bool enable)
|
---|
2199 |
|
---|
2200 | Use setEnabled() on the relevant QAction instead.
|
---|
2201 | */
|
---|
2202 |
|
---|
2203 | /*!
|
---|
2204 | \fn bool QMenuBar::isItemChecked(int id) const
|
---|
2205 |
|
---|
2206 | Use isChecked() on the relevant QAction instead.
|
---|
2207 | */
|
---|
2208 |
|
---|
2209 | /*!
|
---|
2210 | \fn void QMenuBar::setItemChecked(int id, bool check)
|
---|
2211 |
|
---|
2212 | Use setChecked() on the relevant QAction instead.
|
---|
2213 | */
|
---|
2214 |
|
---|
2215 | /*!
|
---|
2216 | \fn bool QMenuBar::isItemVisible(int id) const
|
---|
2217 |
|
---|
2218 | Use isVisible() on the relevant QAction instead.
|
---|
2219 | */
|
---|
2220 |
|
---|
2221 | /*!
|
---|
2222 | \fn void QMenuBar::setItemVisible(int id, bool visible)
|
---|
2223 |
|
---|
2224 | Use setVisible() on the relevant QAction instead.
|
---|
2225 | */
|
---|
2226 |
|
---|
2227 | /*!
|
---|
2228 | \fn int QMenuBar::indexOf(int id) const
|
---|
2229 |
|
---|
2230 | Use actions().indexOf(action) on the relevant QAction instead.
|
---|
2231 | */
|
---|
2232 |
|
---|
2233 | /*!
|
---|
2234 | \fn int QMenuBar::idAt(int index) const
|
---|
2235 |
|
---|
2236 | Use actions instead.
|
---|
2237 | */
|
---|
2238 |
|
---|
2239 | /*!
|
---|
2240 | \fn void QMenuBar::activateItemAt(int index)
|
---|
2241 |
|
---|
2242 | Use activate() on the relevant QAction instead.
|
---|
2243 | */
|
---|
2244 |
|
---|
2245 | /*!
|
---|
2246 | \fn bool QMenuBar::connectItem(int id, const QObject *receiver, const char* member)
|
---|
2247 |
|
---|
2248 | Use connect() on the relevant QAction instead.
|
---|
2249 | */
|
---|
2250 |
|
---|
2251 | /*!
|
---|
2252 | \fn bool QMenuBar::disconnectItem(int id,const QObject *receiver, const char* member)
|
---|
2253 |
|
---|
2254 | Use disconnect() on the relevant QAction instead.
|
---|
2255 | */
|
---|
2256 |
|
---|
2257 | /*!
|
---|
2258 | \fn QMenuItem *QMenuBar::findItem(int id) const
|
---|
2259 |
|
---|
2260 | Use actions instead.
|
---|
2261 | */
|
---|
2262 |
|
---|
2263 | /*!
|
---|
2264 | \fn Separator QMenuBar::separator() const
|
---|
2265 |
|
---|
2266 | This function is provided only to make old code compile.
|
---|
2267 | */
|
---|
2268 |
|
---|
2269 | /*!
|
---|
2270 | \fn void QMenuBar::setSeparator(Separator sep)
|
---|
2271 |
|
---|
2272 | This function is provided only to make old code compile.
|
---|
2273 | */
|
---|
2274 |
|
---|
2275 | /*!
|
---|
2276 | \fn QRect QMenuBar::itemRect(int index)
|
---|
2277 |
|
---|
2278 | Use actionGeometry() on the relevant QAction instead.
|
---|
2279 | */
|
---|
2280 |
|
---|
2281 | /*!
|
---|
2282 | \fn int QMenuBar::itemAtPos(const QPoint &p)
|
---|
2283 |
|
---|
2284 | There is no equivalent way to achieve this in Qt 4.
|
---|
2285 | */
|
---|
2286 |
|
---|
2287 | /*!
|
---|
2288 | \fn void QMenuBar::activated(int itemId);
|
---|
2289 |
|
---|
2290 | Use triggered() instead.
|
---|
2291 | */
|
---|
2292 |
|
---|
2293 | /*!
|
---|
2294 | \fn void QMenuBar::highlighted(int itemId);
|
---|
2295 |
|
---|
2296 | Use hovered() instead.
|
---|
2297 | */
|
---|
2298 |
|
---|
2299 | /*!
|
---|
2300 | \fn void QMenuBar::setFrameRect(QRect)
|
---|
2301 | \internal
|
---|
2302 | */
|
---|
2303 |
|
---|
2304 | /*!
|
---|
2305 | \fn QRect QMenuBar::frameRect() const
|
---|
2306 | \internal
|
---|
2307 | */
|
---|
2308 | /*!
|
---|
2309 | \enum QMenuBar::DummyFrame
|
---|
2310 | \internal
|
---|
2311 |
|
---|
2312 | \value Box
|
---|
2313 | \value Sunken
|
---|
2314 | \value Plain
|
---|
2315 | \value Raised
|
---|
2316 | \value MShadow
|
---|
2317 | \value NoFrame
|
---|
2318 | \value Panel
|
---|
2319 | \value StyledPanel
|
---|
2320 | \value HLine
|
---|
2321 | \value VLine
|
---|
2322 | \value GroupBoxPanel
|
---|
2323 | \value WinPanel
|
---|
2324 | \value ToolBarPanel
|
---|
2325 | \value MenuBarPanel
|
---|
2326 | \value PopupPanel
|
---|
2327 | \value LineEditPanel
|
---|
2328 | \value TabWidgetPanel
|
---|
2329 | \value MShape
|
---|
2330 | */
|
---|
2331 |
|
---|
2332 | /*!
|
---|
2333 | \fn void QMenuBar::setFrameShadow(DummyFrame)
|
---|
2334 | \internal
|
---|
2335 | */
|
---|
2336 |
|
---|
2337 | /*!
|
---|
2338 | \fn DummyFrame QMenuBar::frameShadow() const
|
---|
2339 | \internal
|
---|
2340 | */
|
---|
2341 |
|
---|
2342 | /*!
|
---|
2343 | \fn void QMenuBar::setFrameShape(DummyFrame)
|
---|
2344 | \internal
|
---|
2345 | */
|
---|
2346 |
|
---|
2347 | /*!
|
---|
2348 | \fn DummyFrame QMenuBar::frameShape() const
|
---|
2349 | \internal
|
---|
2350 | */
|
---|
2351 |
|
---|
2352 | /*!
|
---|
2353 | \fn void QMenuBar::setFrameStyle(int)
|
---|
2354 | \internal
|
---|
2355 | */
|
---|
2356 |
|
---|
2357 | /*!
|
---|
2358 | \fn int QMenuBar::frameStyle() const
|
---|
2359 | \internal
|
---|
2360 | */
|
---|
2361 |
|
---|
2362 | /*!
|
---|
2363 | \fn void QMenuBar::setLineWidth(int)
|
---|
2364 | \internal
|
---|
2365 | */
|
---|
2366 |
|
---|
2367 | /*!
|
---|
2368 | \fn int QMenuBar::lineWidth() const
|
---|
2369 | \internal
|
---|
2370 | */
|
---|
2371 |
|
---|
2372 | /*!
|
---|
2373 | \fn void QMenuBar::setMargin(int margin)
|
---|
2374 | Sets the width of the margin around the contents of the widget to \a margin.
|
---|
2375 |
|
---|
2376 | Use QWidget::setContentsMargins() instead.
|
---|
2377 | \sa margin(), QWidget::setContentsMargins()
|
---|
2378 | */
|
---|
2379 |
|
---|
2380 | /*!
|
---|
2381 | \fn int QMenuBar::margin() const
|
---|
2382 | Returns the with of the the margin around the contents of the widget.
|
---|
2383 |
|
---|
2384 | Use QWidget::getContentsMargins() instead.
|
---|
2385 | \sa setMargin(), QWidget::getContentsMargins()
|
---|
2386 | */
|
---|
2387 |
|
---|
2388 | /*!
|
---|
2389 | \fn void QMenuBar::setMidLineWidth(int)
|
---|
2390 | \internal
|
---|
2391 | */
|
---|
2392 |
|
---|
2393 | /*!
|
---|
2394 | \fn int QMenuBar::midLineWidth() const
|
---|
2395 | \internal
|
---|
2396 | */
|
---|
2397 |
|
---|
2398 | // for private slots
|
---|
2399 |
|
---|
2400 |
|
---|
2401 | QT_END_NAMESPACE
|
---|
2402 |
|
---|
2403 | #include <moc_qmenubar.cpp>
|
---|
2404 |
|
---|
2405 | #endif // QT_NO_MENUBAR
|
---|