qmenubar.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qmenubar.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtWidgets module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#include <qmenubar.h>-
41-
42#include <qstyle.h>-
43#include <qlayout.h>-
44#include <qapplication.h>-
45#include <qdesktopwidget.h>-
46#ifndef QT_NO_ACCESSIBILITY-
47# include <qaccessible.h>-
48#endif-
49#include <qpainter.h>-
50#include <qstylepainter.h>-
51#include <qevent.h>-
52#include <qmainwindow.h>-
53#include <qtoolbar.h>-
54#include <qtoolbutton.h>-
55#include <qwhatsthis.h>-
56#include <qpa/qplatformtheme.h>-
57#include "private/qguiapplication_p.h"-
58#include "qpa/qplatformintegration.h"-
59-
60#ifndef QT_NO_MENUBAR-
61-
62-
63#include "qmenu_p.h"-
64#include "qmenubar_p.h"-
65#include "qdebug.h"-
66-
67#ifdef Q_OS_WINCE-
68extern bool qt_wince_is_mobile(); //defined in qguifunctions_wce.cpp-
69#endif-
70-
71QT_BEGIN_NAMESPACE-
72-
73class QMenuBarExtension : public QToolButton-
74{-
75public:-
76 explicit QMenuBarExtension(QWidget *parent);-
77-
78 QSize sizeHint() const Q_DECL_OVERRIDE;-
79 void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;-
80};-
81-
82QMenuBarExtension::QMenuBarExtension(QWidget *parent)-
83 : QToolButton(parent)-
84{-
85 setObjectName(QLatin1String("qt_menubar_ext_button"));-
86 setAutoRaise(true);-
87#ifndef QT_NO_MENU-
88 setPopupMode(QToolButton::InstantPopup);-
89#endif-
90 setIcon(style()->standardIcon(QStyle::SP_ToolBarHorizontalExtensionButton, 0, parentWidget()));-
91}-
92-
93void QMenuBarExtension::paintEvent(QPaintEvent *)-
94{-
95 QStylePainter p(this);-
96 QStyleOptionToolButton opt;-
97 initStyleOption(&opt);-
98 // We do not need to draw both extension arrows-
99 opt.features &= ~QStyleOptionToolButton::HasMenu;-
100 p.drawComplexControl(QStyle::CC_ToolButton, opt);-
101}-
102-
103-
104QSize QMenuBarExtension::sizeHint() const-
105{-
106 int ext = style()->pixelMetric(QStyle::PM_ToolBarExtensionExtent, 0, parentWidget());-
107 return QSize(ext, ext);-
108}-
109-
110-
111/*!-
112 \internal-
113*/-
114QAction *QMenuBarPrivate::actionAt(QPoint p) const-
115{-
116 for(int i = 0; i < actions.size(); ++i) {-
117 if(actionRect(actions.at(i)).contains(p))-
118 return actions.at(i);-
119 }-
120 return 0;-
121}-
122-
123QRect QMenuBarPrivate::menuRect(bool extVisible) const-
124{-
125 Q_Q(const QMenuBar);-
126-
127 int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);-
128 QRect result = q->rect();-
129 result.adjust(hmargin, 0, -hmargin, 0);-
130-
131 if (extVisible) {-
132 if (q->isRightToLeft())-
133 result.setLeft(result.left() + extension->sizeHint().width());-
134 else-
135 result.setWidth(result.width() - extension->sizeHint().width());-
136 }-
137-
138 if (leftWidget && leftWidget->isVisible()) {-
139 QSize sz = leftWidget->sizeHint();-
140 if (q->isRightToLeft())-
141 result.setRight(result.right() - sz.width());-
142 else-
143 result.setLeft(result.left() + sz.width());-
144 }-
145-
146 if (rightWidget && rightWidget->isVisible()) {-
147 QSize sz = rightWidget->sizeHint();-
148 if (q->isRightToLeft())-
149 result.setLeft(result.left() + sz.width());-
150 else-
151 result.setRight(result.right() - sz.width());-
152 }-
153-
154 return result;-
155}-
156-
157bool QMenuBarPrivate::isVisible(QAction *action)-
158{-
159 return !hiddenActions.contains(action);-
160}-
161-
162void QMenuBarPrivate::updateGeometries()-
163{-
164 Q_Q(QMenuBar);-
165 if(!itemsDirty)
!itemsDirtyDescription
TRUEnever evaluated
FALSEnever evaluated
0
166 return;
never executed: return;
0
167 int q_width = q->width()-(q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q)*2);-
168 int q_start = -1;-
169 if(leftWidget || rightWidget) {
leftWidgetDescription
TRUEnever evaluated
FALSEnever evaluated
rightWidgetDescription
TRUEnever evaluated
FALSEnever evaluated
0
170 int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q)-
171 + q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);-
172 int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, q)-
173 + q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);-
174 if (leftWidget && leftWidget->isVisible()) {
leftWidgetDescription
TRUEnever evaluated
FALSEnever evaluated
leftWidget->isVisible()Description
TRUEnever evaluated
FALSEnever evaluated
0
175 QSize sz = leftWidget->sizeHint();-
176 q_width -= sz.width();-
177 q_start = sz.width();-
178 QPoint pos(hmargin, (q->height() - leftWidget->height()) / 2);-
179 QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));-
180 leftWidget->setGeometry(vRect);-
181 }
never executed: end of block
0
182 if (rightWidget && rightWidget->isVisible()) {
rightWidgetDescription
TRUEnever evaluated
FALSEnever evaluated
rightWidget->isVisible()Description
TRUEnever evaluated
FALSEnever evaluated
0
183 QSize sz = rightWidget->sizeHint();-
184 q_width -= sz.width();-
185 QPoint pos(q->width() - sz.width() - hmargin, vmargin);-
186 QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));-
187 rightWidget->setGeometry(vRect);-
188 }
never executed: end of block
0
189 }
never executed: end of block
0
190-
191#ifdef Q_OS_MAC-
192 if(q->isNativeMenuBar()) {//nothing to see here folks, move along..-
193 itemsDirty = false;-
194 return;-
195 }-
196#endif-
197 calcActionRects(q_width, q_start);-
198 currentAction = 0;-
199#ifndef QT_NO_SHORTCUT-
200 if(itemsDirty) {
itemsDirtyDescription
TRUEnever evaluated
FALSEnever evaluated
0
201 for(int j = 0; j < shortcutIndexMap.size(); ++j)
j < shortcutIndexMap.size()Description
TRUEnever evaluated
FALSEnever evaluated
0
202 q->releaseShortcut(shortcutIndexMap.value(j));
never executed: q->releaseShortcut(shortcutIndexMap.value(j));
0
203 shortcutIndexMap.resize(0); clear();-
204 const int actionsCount = actions.count();-
205 shortcutIndexMap.reserve(actionsCount);-
206 for (int i = 0; i < actionsCount; i++)
i < actionsCountDescription
TRUEnever evaluated
FALSEnever evaluated
0
207 shortcutIndexMap.append(q->grabShortcut(QKeySequence::mnemonic(actions.at(i)->text())));
never executed: shortcutIndexMap.append(q->grabShortcut(QKeySequence::mnemonic(actions.at(i)->text())));
0
208 }
never executed: end of block
0
209#endif-
210 itemsDirty = false;-
211-
212 hiddenActions.clear();-
213 //this is the menu rectangle without any extension-
214 QRect menuRect = this->menuRect(false);-
215-
216 //we try to see if the actions will fit there-
217 bool hasHiddenActions = false;-
218 for (int i = 0; i < actions.count(); ++i) {
i < actions.count()Description
TRUEnever evaluated
FALSEnever evaluated
0
219 const QRect &rect = actionRects.at(i);-
220 if (rect.isValid() && !menuRect.contains(rect)) {
rect.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
!menuRect.contains(rect)Description
TRUEnever evaluated
FALSEnever evaluated
0
221 hasHiddenActions = true;-
222 break;
never executed: break;
0
223 }-
224 }
never executed: end of block
0
225-
226 //...and if not, determine the ones that fit on the menu with the extension visible-
227 if (hasHiddenActions) {
hasHiddenActionsDescription
TRUEnever evaluated
FALSEnever evaluated
0
228 menuRect = this->menuRect(true);-
229 for (int i = 0; i < actions.count(); ++i) {
i < actions.count()Description
TRUEnever evaluated
FALSEnever evaluated
0
230 const QRect &rect = actionRects.at(i);-
231 if (rect.isValid() && !menuRect.contains(rect)) {
rect.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
!menuRect.contains(rect)Description
TRUEnever evaluated
FALSEnever evaluated
0
232 hiddenActions.append(actions.at(i));-
233 }
never executed: end of block
0
234 }
never executed: end of block
0
235 }
never executed: end of block
0
236-
237 if (hiddenActions.count() > 0) {
hiddenActions.count() > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
238 QMenu *pop = extension->menu();-
239 if (!pop) {
!popDescription
TRUEnever evaluated
FALSEnever evaluated
0
240 pop = new QMenu(q);-
241 extension->setMenu(pop);-
242 }
never executed: end of block
0
243 pop->clear();-
244 pop->addActions(hiddenActions);-
245-
246 int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q);-
247 int x = q->isRightToLeft()
q->isRightToLeft()Description
TRUEnever evaluated
FALSEnever evaluated
0
248 ? menuRect.left() - extension->sizeHint().width() + 1-
249 : menuRect.right();-
250 extension->setGeometry(x, vmargin, extension->sizeHint().width(), menuRect.height() - vmargin*2);-
251 extension->show();-
252 } else {
never executed: end of block
0
253 extension->hide();-
254 }
never executed: end of block
0
255 q->updateGeometry();-
256}
never executed: end of block
0
257-
258QRect QMenuBarPrivate::actionRect(QAction *act) const-
259{-
260 const int index = actions.indexOf(act);-
261-
262 //makes sure the geometries are up-to-date-
263 const_cast<QMenuBarPrivate*>(this)->updateGeometries();-
264-
265 if (index < 0 || index >= actionRects.count())-
266 return QRect(); // that can happen in case of native menubar-
267-
268 return actionRects.at(index);-
269}-
270-
271void QMenuBarPrivate::focusFirstAction()-
272{-
273 if(!currentAction) {-
274 updateGeometries();-
275 int index = 0;-
276 while (index < actions.count() && actionRects.at(index).isNull()) ++index;-
277 if (index < actions.count())-
278 setCurrentAction(actions.at(index));-
279 }-
280}-
281-
282void QMenuBarPrivate::setKeyboardMode(bool b)-
283{-
284 Q_Q(QMenuBar);-
285 if (b && !q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, q)) {-
286 setCurrentAction(0);-
287 return;-
288 }-
289 keyboardState = b;-
290 if(b) {-
291 QWidget *fw = QApplication::focusWidget();-
292 if (fw != q)-
293 keyboardFocusWidget = fw;-
294 focusFirstAction();-
295 q->setFocus(Qt::MenuBarFocusReason);-
296 } else {-
297 if(!popupState)-
298 setCurrentAction(0);-
299 if(keyboardFocusWidget) {-
300 if (QApplication::focusWidget() == q)-
301 keyboardFocusWidget->setFocus(Qt::MenuBarFocusReason);-
302 keyboardFocusWidget = 0;-
303 }-
304 }-
305 q->update();-
306}-
307-
308void QMenuBarPrivate::popupAction(QAction *action, bool activateFirst)-
309{-
310 Q_Q(QMenuBar);-
311 if(!action || !action->menu() || closePopupMode)-
312 return;-
313 popupState = true;-
314 if (action->isEnabled() && action->menu()->isEnabled()) {-
315 closePopupMode = 0;-
316 activeMenu = action->menu();-
317 activeMenu->d_func()->causedPopup.widget = q;-
318 activeMenu->d_func()->causedPopup.action = action;-
319-
320 QRect adjustedActionRect = actionRect(action);-
321 QPoint pos(q->mapToGlobal(QPoint(adjustedActionRect.left(), adjustedActionRect.bottom() + 1)));-
322 QSize popup_size = activeMenu->sizeHint();-
323-
324 //we put the popup menu on the screen containing the bottom-center of the action rect-
325 QRect screenRect = QApplication::desktop()->screenGeometry(pos + QPoint(adjustedActionRect.width() / 2, 0));-
326 pos = QPoint(qMax(pos.x(), screenRect.x()), qMax(pos.y(), screenRect.y()));-
327-
328 const bool fitUp = (q->mapToGlobal(adjustedActionRect.topLeft()).y() >= popup_size.height());-
329 const bool fitDown = (pos.y() + popup_size.height() <= screenRect.bottom());-
330 const bool rtl = q->isRightToLeft();-
331 const int actionWidth = adjustedActionRect.width();-
332-
333 if (!fitUp && !fitDown) { //we should shift the menu-
334 bool shouldShiftToRight = !rtl;-
335 if (rtl && popup_size.width() > pos.x())-
336 shouldShiftToRight = true;-
337 else if (actionWidth + popup_size.width() + pos.x() > screenRect.right())-
338 shouldShiftToRight = false;-
339-
340 if (shouldShiftToRight) {-
341 pos.rx() += actionWidth + (rtl ? popup_size.width() : 0);-
342 } else {-
343 //shift to left-
344 if (!rtl)-
345 pos.rx() -= popup_size.width();-
346 }-
347 } else if (rtl) {-
348 pos.rx() += actionWidth;-
349 }-
350-
351 if(!defaultPopDown || (fitUp && !fitDown))-
352 pos.setY(qMax(screenRect.y(), q->mapToGlobal(QPoint(0, adjustedActionRect.top()-popup_size.height())).y()));-
353 activeMenu->popup(pos);-
354 if(activateFirst)-
355 activeMenu->d_func()->setFirstActionActive();-
356 }-
357 q->update(actionRect(action));-
358}-
359-
360void QMenuBarPrivate::setCurrentAction(QAction *action, bool popup, bool activateFirst)-
361{-
362 if(currentAction == action && popup == popupState)-
363 return;-
364-
365 autoReleaseTimer.stop();-
366-
367 doChildEffects = (popup && !activeMenu);-
368 Q_Q(QMenuBar);-
369 QWidget *fw = 0;-
370 if(QMenu *menu = activeMenu) {-
371 activeMenu = 0;-
372 if (popup) {-
373 fw = q->window()->focusWidget();-
374 q->setFocus(Qt::NoFocusReason);-
375 }-
376 menu->hide();-
377 }-
378-
379 if(currentAction)-
380 q->update(actionRect(currentAction));-
381-
382 popupState = popup;-
383#ifndef QT_NO_STATUSTIP-
384 QAction *previousAction = currentAction;-
385#endif-
386 currentAction = action;-
387 if (action && action->isEnabled()) {-
388 activateAction(action, QAction::Hover);-
389 if(popup)-
390 popupAction(action, activateFirst);-
391 q->update(actionRect(action));-
392#ifndef QT_NO_STATUSTIP-
393 } else if (previousAction) {-
394 QString empty;-
395 QStatusTipEvent tip(empty);-
396 QApplication::sendEvent(q, &tip);-
397#endif-
398 }-
399 if (fw)-
400 fw->setFocus(Qt::NoFocusReason);-
401}-
402-
403void QMenuBarPrivate::calcActionRects(int max_width, int start) const-
404{-
405 Q_Q(const QMenuBar);-
406-
407 if(!itemsDirty)-
408 return;-
409-
410 //let's reinitialize the buffer-
411 actionRects.resize(actions.count());-
412 actionRects.fill(QRect());-
413-
414 const QStyle *style = q->style();-
415-
416 const int itemSpacing = style->pixelMetric(QStyle::PM_MenuBarItemSpacing, 0, q);-
417 int max_item_height = 0, separator = -1, separator_start = 0, separator_len = 0;-
418-
419 //calculate size-
420 const QFontMetrics fm = q->fontMetrics();-
421 const int hmargin = style->pixelMetric(QStyle::PM_MenuBarHMargin, 0, q),-
422 vmargin = style->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q),-
423 icone = style->pixelMetric(QStyle::PM_SmallIconSize, 0, q);-
424 for(int i = 0; i < actions.count(); i++) {-
425 QAction *action = actions.at(i);-
426 if(!action->isVisible())-
427 continue;-
428-
429 QSize sz;-
430-
431 //calc what I think the size is..-
432 if(action->isSeparator()) {-
433 if (style->styleHint(QStyle::SH_DrawMenuBarSeparator, 0, q))-
434 separator = i;-
435 continue; //we don't really position these!-
436 } else {-
437 const QString s = action->text();-
438 QIcon is = action->icon();-
439 // If an icon is set, only the icon is visible-
440 if (!is.isNull())-
441 sz = sz.expandedTo(QSize(icone, icone));-
442 else if (!s.isEmpty())-
443 sz = fm.size(Qt::TextShowMnemonic, s);-
444 }-
445-
446 //let the style modify the above size..-
447 QStyleOptionMenuItem opt;-
448 q->initStyleOption(&opt, action);-
449 sz = q->style()->sizeFromContents(QStyle::CT_MenuBarItem, &opt, sz, q);-
450-
451 if(!sz.isEmpty()) {-
452 { //update the separator state-
453 int iWidth = sz.width() + itemSpacing;-
454 if(separator == -1)-
455 separator_start += iWidth;-
456 else-
457 separator_len += iWidth;-
458 }-
459 //maximum height-
460 max_item_height = qMax(max_item_height, sz.height());-
461 //append-
462 actionRects[i] = QRect(0, 0, sz.width(), sz.height());-
463 }-
464 }-
465-
466 //calculate position-
467 const int fw = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);-
468 int x = fw + ((start == -1) ? hmargin : start) + itemSpacing;-
469 int y = fw + vmargin;-
470 for(int i = 0; i < actions.count(); i++) {-
471 QRect &rect = actionRects[i];-
472 if (rect.isNull())-
473 continue;-
474-
475 //resize-
476 rect.setHeight(max_item_height);-
477-
478 //move-
479 if(separator != -1 && i >= separator) { //after the separator-
480 int left = (max_width - separator_len - hmargin - itemSpacing) + (x - separator_start - hmargin);-
481 if(left < separator_start) { //wrap-
482 separator_start = x = hmargin;-
483 y += max_item_height;-
484 }-
485 rect.moveLeft(left);-
486 } else {-
487 rect.moveLeft(x);-
488 }-
489 rect.moveTop(y);-
490-
491 //keep moving along..-
492 x += rect.width() + itemSpacing;-
493-
494 //make sure we follow the layout direction-
495 rect = QStyle::visualRect(q->layoutDirection(), q->rect(), rect);-
496 }-
497}-
498-
499void QMenuBarPrivate::activateAction(QAction *action, QAction::ActionEvent action_e)-
500{-
501 Q_Q(QMenuBar);-
502 if (!action || !action->isEnabled())-
503 return;-
504 action->activate(action_e);-
505 if (action_e == QAction::Hover)-
506 action->showStatusText(q);-
507-
508// if(action_e == QAction::Trigger)-
509// emit q->activated(action);-
510// else if(action_e == QAction::Hover)-
511// emit q->highlighted(action);-
512}-
513-
514-
515void QMenuBarPrivate::_q_actionTriggered()-
516{-
517 Q_Q(QMenuBar);-
518 if (QAction *action = qobject_cast<QAction *>(q->sender())) {-
519 emit q->triggered(action);-
520 }-
521}-
522-
523void QMenuBarPrivate::_q_actionHovered()-
524{-
525 Q_Q(QMenuBar);-
526 if (QAction *action = qobject_cast<QAction *>(q->sender())) {-
527 emit q->hovered(action);-
528#ifndef QT_NO_ACCESSIBILITY-
529 if (QAccessible::isActive()) {-
530 int actionIndex = actions.indexOf(action);-
531 QAccessibleEvent focusEvent(q, QAccessible::Focus);-
532 focusEvent.setChild(actionIndex);-
533 QAccessible::updateAccessibility(&focusEvent);-
534 }-
535#endif //QT_NO_ACCESSIBILITY-
536 }-
537}-
538-
539/*!-
540 Initialize \a option with the values from the menu bar and information from \a action. This method-
541 is useful for subclasses when they need a QStyleOptionMenuItem, but don't want-
542 to fill in all the information themselves.-
543-
544 \sa QStyleOption::initFrom(), QMenu::initStyleOption()-
545*/-
546void QMenuBar::initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const-
547{-
548 if (!option || !action)-
549 return;-
550 Q_D(const QMenuBar);-
551 option->palette = palette();-
552 option->state = QStyle::State_None;-
553 if (isEnabled() && action->isEnabled())-
554 option->state |= QStyle::State_Enabled;-
555 else-
556 option->palette.setCurrentColorGroup(QPalette::Disabled);-
557 option->fontMetrics = fontMetrics();-
558 if (d->currentAction && d->currentAction == action) {-
559 option->state |= QStyle::State_Selected;-
560 if (d->popupState && !d->closePopupMode)-
561 option->state |= QStyle::State_Sunken;-
562 }-
563 if (hasFocus() || d->currentAction)-
564 option->state |= QStyle::State_HasFocus;-
565 option->menuRect = rect();-
566 option->menuItemType = QStyleOptionMenuItem::Normal;-
567 option->checkType = QStyleOptionMenuItem::NotCheckable;-
568 option->text = action->text();-
569 option->icon = action->icon();-
570}-
571-
572/*!-
573 \class QMenuBar-
574 \brief The QMenuBar class provides a horizontal menu bar.-
575-
576 \ingroup mainwindow-classes-
577 \inmodule QtWidgets-
578-
579 A menu bar consists of a list of pull-down menu items. You add-
580 menu items with addMenu(). For example, asuming that \c menubar-
581 is a pointer to a QMenuBar and \c fileMenu is a pointer to a-
582 QMenu, the following statement inserts the menu into the menu bar:-
583 \snippet code/src_gui_widgets_qmenubar.cpp 0-
584-
585 The ampersand in the menu item's text sets Alt+F as a shortcut for-
586 this menu. (You can use "\&\&" to get a real ampersand in the menu-
587 bar.)-
588-
589 There is no need to lay out a menu bar. It automatically sets its-
590 own geometry to the top of the parent widget and changes it-
591 appropriately whenever the parent is resized.-
592-
593 \section1 Usage-
594-
595 In most main window style applications you would use the-
596 \l{QMainWindow::}{menuBar()} function provided in QMainWindow,-
597 adding \l{QMenu}s to the menu bar and adding \l{QAction}s to the-
598 pop-up menus.-
599-
600 Example (from the \l{mainwindows/menus}{Menus} example):-
601-
602 \snippet mainwindows/menus/mainwindow.cpp 9-
603-
604 Menu items may be removed with removeAction().-
605-
606 Widgets can be added to menus by using instances of the QWidgetAction-
607 class to hold them. These actions can then be inserted into menus-
608 in the usual way; see the QMenu documentation for more details.-
609-
610 \section1 Platform Dependent Look and Feel-
611-
612 Different platforms have different requirements for the appearance-
613 of menu bars and their behavior when the user interacts with them.-
614 For example, Windows systems are often configured so that the-
615 underlined character mnemonics that indicate keyboard shortcuts-
616 for items in the menu bar are only shown when the \uicontrol{Alt} key is-
617 pressed.-
618-
619 \section1 QMenuBar as a Global Menu Bar-
620-
621 On \macos and on certain Linux desktop environments such as-
622 Ubuntu Unity, QMenuBar is a wrapper for using the system-wide menu bar.-
623 If you have multiple menu bars in one dialog the outermost menu bar-
624 (normally inside a widget with widget flag Qt::Window) will-
625 be used for the system-wide menu bar.-
626-
627 Qt for \macos also provides a menu bar merging feature to make-
628 QMenuBar conform more closely to accepted \macos menu bar layout.-
629 The merging functionality is based on string matching the title of-
630 a QMenu entry. These strings are translated (using QObject::tr())-
631 in the "QMenuBar" context. If an entry is moved its slots will still-
632 fire as if it was in the original place. The table below outlines-
633 the strings looked for and where the entry is placed if matched:-
634-
635 \table-
636 \header \li String matches \li Placement \li Notes-
637 \row \li about.*-
638 \li Application Menu | About <application name>-
639 \li The application name is fetched from the \c {Info.plist} file-
640 (see note below). If this entry is not found no About item-
641 will appear in the Application Menu.-
642 \row \li config, options, setup, settings or preferences-
643 \li Application Menu | Preferences-
644 \li If this entry is not found the Settings item will be disabled-
645 \row \li quit or exit-
646 \li Application Menu | Quit <application name>-
647 \li If this entry is not found a default Quit item will be-
648 created to call QCoreApplication::quit()-
649 \endtable-
650-
651 You can override this behavior by using the QAction::menuRole()-
652 property.-
653-
654 If you want all windows in a Mac application to share one menu-
655 bar, you must create a menu bar that does not have a parent.-
656 Create a parent-less menu bar this way:-
657-
658 \snippet code/src_gui_widgets_qmenubar.cpp 1-
659-
660 \b{Note:} Do \e{not} call QMainWindow::menuBar() to create the-
661 shared menu bar, because that menu bar will have the QMainWindow-
662 as its parent. That menu bar would only be displayed for the-
663 parent QMainWindow.-
664-
665 \b{Note:} The text used for the application name in the \macos menu-
666 bar is obtained from the value set in the \c{Info.plist} file in-
667 the application's bundle. See \l{Qt for macOS - Deployment}-
668 for more information.-
669-
670 \b{Note:} On Linux, if the com.canonical.AppMenu.Registrar-
671 service is available on the D-Bus session bus, then Qt will-
672 communicate with it to install the application's menus into the-
673 global menu bar, as described.-
674-
675 \section1 Examples-
676-
677 The \l{mainwindows/menus}{Menus} example shows how to use QMenuBar-
678 and QMenu. The other \l{Main Window Examples}{main window-
679 application examples} also provide menus using these classes.-
680-
681 \sa QMenu, QShortcut, QAction,-
682 {http://developer.apple.com/documentation/UserExperience/Conceptual/AppleHIGuidelines/XHIGIntro/XHIGIntro.html}{Introduction to Apple Human Interface Guidelines},-
683 {fowler}{GUI Design Handbook: Menu Bar}, {Menus Example}-
684*/-
685-
686-
687void QMenuBarPrivate::init()-
688{-
689 Q_Q(QMenuBar);-
690 q->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);-
691 q->setAttribute(Qt::WA_CustomWhatsThis);-
692-
693 if (!QApplication::instance()->testAttribute(Qt::AA_DontUseNativeMenuBar))-
694 platformMenuBar = QGuiApplicationPrivate::platformTheme()->createPlatformMenuBar();-
695-
696 if (platformMenuBar)-
697 q->hide();-
698#ifdef Q_OS_WINCE-
699 if (qt_wince_is_mobile()) {-
700 wceCreateMenuBar(q->parentWidget());-
701 if(wce_menubar)-
702 q->hide();-
703 }-
704 else {-
705 QApplication::setAttribute(Qt::AA_DontUseNativeMenuBar, true);-
706 }-
707#endif-
708 q->setBackgroundRole(QPalette::Button);-
709 handleReparent();-
710 q->setMouseTracking(q->style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, q));-
711-
712 extension = new QMenuBarExtension(q);-
713 extension->setFocusPolicy(Qt::NoFocus);-
714 extension->hide();-
715}-
716-
717//Gets the next action for keyboard navigation-
718QAction *QMenuBarPrivate::getNextAction(const int _start, const int increment) const-
719{-
720 Q_Q(const QMenuBar);-
721 const_cast<QMenuBarPrivate*>(this)->updateGeometries();-
722 bool allowActiveAndDisabled = q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, q);-
723 const int start = (_start == -1 && increment == -1) ? actions.count() : _start;-
724 const int end = increment == -1 ? 0 : actions.count() - 1;-
725-
726 for (int i = start; i != end;) {-
727 i += increment;-
728 QAction *current = actions.at(i);-
729 if (!actionRects.at(i).isNull() && (allowActiveAndDisabled || current->isEnabled()))-
730 return current;-
731 }-
732-
733 if (_start != -1) //let's try from the beginning or the end-
734 return getNextAction(-1, increment);-
735-
736 return 0;-
737}-
738-
739/*!-
740 Constructs a menu bar with parent \a parent.-
741*/-
742QMenuBar::QMenuBar(QWidget *parent) : QWidget(*new QMenuBarPrivate, parent, 0)-
743{-
744 Q_D(QMenuBar);-
745 d->init();-
746}-
747-
748-
749/*!-
750 Destroys the menu bar.-
751*/-
752QMenuBar::~QMenuBar()-
753{-
754 Q_D(QMenuBar);-
755 delete d->platformMenuBar;-
756 d->platformMenuBar = 0;-
757-
758#ifdef Q_OS_WINCE-
759 if (qt_wince_is_mobile())-
760 d->wceDestroyMenuBar();-
761#endif-
762}-
763-
764/*!-
765 \overload-
766-
767 This convenience function creates a new action with \a text.-
768 The function adds the newly created action to the menu's-
769 list of actions, and returns it.-
770-
771 \sa QWidget::addAction(), QWidget::actions()-
772*/-
773QAction *QMenuBar::addAction(const QString &text)-
774{-
775 QAction *ret = new QAction(text, this);-
776 addAction(ret);-
777 return ret;-
778}-
779-
780/*!-
781 \overload-
782-
783 This convenience function creates a new action with the given \a-
784 text. The action's triggered() signal is connected to the \a-
785 receiver's \a member slot. The function adds the newly created-
786 action to the menu's list of actions and returns it.-
787-
788 \sa QWidget::addAction(), QWidget::actions()-
789*/-
790QAction *QMenuBar::addAction(const QString &text, const QObject *receiver, const char* member)-
791{-
792 QAction *ret = new QAction(text, this);-
793 QObject::connect(ret, SIGNAL(triggered(bool)), receiver, member);-
794 addAction(ret);-
795 return ret;-
796}-
797-
798/*!-
799 Appends a new QMenu with \a title to the menu bar. The menu bar-
800 takes ownership of the menu. Returns the new menu.-
801-
802 \sa QWidget::addAction(), QMenu::menuAction()-
803*/-
804QMenu *QMenuBar::addMenu(const QString &title)-
805{-
806 QMenu *menu = new QMenu(title, this);-
807 addAction(menu->menuAction());-
808 return menu;-
809}-
810-
811/*!-
812 Appends a new QMenu with \a icon and \a title to the menu bar. The menu bar-
813 takes ownership of the menu. Returns the new menu.-
814-
815 \sa QWidget::addAction(), QMenu::menuAction()-
816*/-
817QMenu *QMenuBar::addMenu(const QIcon &icon, const QString &title)-
818{-
819 QMenu *menu = new QMenu(title, this);-
820 menu->setIcon(icon);-
821 addAction(menu->menuAction());-
822 return menu;-
823}-
824-
825/*!-
826 Appends \a menu to the menu bar. Returns the menu's menuAction().-
827-
828 \note The returned QAction object can be used to hide the corresponding-
829 menu.-
830-
831 \sa QWidget::addAction(), QMenu::menuAction()-
832*/-
833QAction *QMenuBar::addMenu(QMenu *menu)-
834{-
835 QAction *action = menu->menuAction();-
836 addAction(action);-
837 return action;-
838}-
839-
840/*!-
841 Appends a separator to the menu.-
842*/-
843QAction *QMenuBar::addSeparator()-
844{-
845 QAction *ret = new QAction(this);-
846 ret->setSeparator(true);-
847 addAction(ret);-
848 return ret;-
849}-
850-
851/*!-
852 This convenience function creates a new separator action, i.e. an-
853 action with QAction::isSeparator() returning true. The function inserts-
854 the newly created action into this menu bar's list of actions before-
855 action \a before and returns it.-
856-
857 \sa QWidget::insertAction(), addSeparator()-
858*/-
859QAction *QMenuBar::insertSeparator(QAction *before)-
860{-
861 QAction *action = new QAction(this);-
862 action->setSeparator(true);-
863 insertAction(before, action);-
864 return action;-
865}-
866-
867/*!-
868 This convenience function inserts \a menu before action \a before-
869 and returns the menus menuAction().-
870-
871 \sa QWidget::insertAction(), addMenu()-
872*/-
873QAction *QMenuBar::insertMenu(QAction *before, QMenu *menu)-
874{-
875 QAction *action = menu->menuAction();-
876 insertAction(before, action);-
877 return action;-
878}-
879-
880/*!-
881 Returns the QAction that is currently highlighted. A null pointer-
882 will be returned if no action is currently selected.-
883*/-
884QAction *QMenuBar::activeAction() const-
885{-
886 Q_D(const QMenuBar);-
887 return d->currentAction;-
888}-
889-
890/*!-
891 \since 4.1-
892-
893 Sets the currently highlighted action to \a act.-
894*/-
895void QMenuBar::setActiveAction(QAction *act)-
896{-
897 Q_D(QMenuBar);-
898 d->setCurrentAction(act, true, false);-
899}-
900-
901-
902/*!-
903 Removes all the actions from the menu bar.-
904-
905 \note On \macos, menu items that have been merged to the system-
906 menu bar are not removed by this function. One way to handle this-
907 would be to remove the extra actions yourself. You can set the-
908 \l{QAction::MenuRole}{menu role} on the different menus, so that-
909 you know ahead of time which menu items get merged and which do-
910 not. Then decide what to recreate or remove yourself.-
911-
912 \sa removeAction()-
913*/-
914void QMenuBar::clear()-
915{-
916 QList<QAction*> acts = actions();-
917 for(int i = 0; i < acts.size(); i++)-
918 removeAction(acts[i]);-
919}-
920-
921/*!-
922 \property QMenuBar::defaultUp-
923 \brief the popup orientation-
924-
925 The default popup orientation. By default, menus pop "down" the-
926 screen. By setting the property to true, the menu will pop "up".-
927 You might call this for menus that are \e below the document to-
928 which they refer.-
929-
930 If the menu would not fit on the screen, the other direction is-
931 used automatically.-
932*/-
933void QMenuBar::setDefaultUp(bool b)-
934{-
935 Q_D(QMenuBar);-
936 d->defaultPopDown = !b;-
937}-
938-
939bool QMenuBar::isDefaultUp() const-
940{-
941 Q_D(const QMenuBar);-
942 return !d->defaultPopDown;-
943}-
944-
945/*!-
946 \reimp-
947*/-
948void QMenuBar::resizeEvent(QResizeEvent *)-
949{-
950 Q_D(QMenuBar);-
951 d->itemsDirty = true;-
952 d->updateGeometries();-
953}-
954-
955/*!-
956 \reimp-
957*/-
958void QMenuBar::paintEvent(QPaintEvent *e)-
959{-
960 Q_D(QMenuBar);-
961 QPainter p(this);-
962 QRegion emptyArea(rect());-
963-
964 //draw the items-
965 for (int i = 0; i < d->actions.count(); ++i) {-
966 QAction *action = d->actions.at(i);-
967 QRect adjustedActionRect = d->actionRect(action);-
968 if (adjustedActionRect.isEmpty() || !d->isVisible(action))-
969 continue;-
970 if(!e->rect().intersects(adjustedActionRect))-
971 continue;-
972-
973 emptyArea -= adjustedActionRect;-
974 QStyleOptionMenuItem opt;-
975 initStyleOption(&opt, action);-
976 opt.rect = adjustedActionRect;-
977 p.setClipRect(adjustedActionRect);-
978 style()->drawControl(QStyle::CE_MenuBarItem, &opt, &p, this);-
979 }-
980 //draw border-
981 if(int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this)) {-
982 QRegion borderReg;-
983 borderReg += QRect(0, 0, fw, height()); //left-
984 borderReg += QRect(width()-fw, 0, fw, height()); //right-
985 borderReg += QRect(0, 0, width(), fw); //top-
986 borderReg += QRect(0, height()-fw, width(), fw); //bottom-
987 p.setClipRegion(borderReg);-
988 emptyArea -= borderReg;-
989 QStyleOptionFrame frame;-
990 frame.rect = rect();-
991 frame.palette = palette();-
992 frame.state = QStyle::State_None;-
993 frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth);-
994 frame.midLineWidth = 0;-
995 style()->drawPrimitive(QStyle::PE_PanelMenuBar, &frame, &p, this);-
996 }-
997 p.setClipRegion(emptyArea);-
998 QStyleOptionMenuItem menuOpt;-
999 menuOpt.palette = palette();-
1000 menuOpt.state = QStyle::State_None;-
1001 menuOpt.menuItemType = QStyleOptionMenuItem::EmptyArea;-
1002 menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;-
1003 menuOpt.rect = rect();-
1004 menuOpt.menuRect = rect();-
1005 style()->drawControl(QStyle::CE_MenuBarEmptyArea, &menuOpt, &p, this);-
1006}-
1007-
1008/*!-
1009 \reimp-
1010*/-
1011void QMenuBar::setVisible(bool visible)-
1012{-
1013 if (isNativeMenuBar()) {-
1014 if (!visible)-
1015 QWidget::setVisible(false);-
1016 return;-
1017 }-
1018 QWidget::setVisible(visible);-
1019}-
1020-
1021/*!-
1022 \reimp-
1023*/-
1024void QMenuBar::mousePressEvent(QMouseEvent *e)-
1025{-
1026 Q_D(QMenuBar);-
1027 if(e->button() != Qt::LeftButton)-
1028 return;-
1029-
1030 d->mouseDown = true;-
1031-
1032 QAction *action = d->actionAt(e->pos());-
1033 if (!action || !d->isVisible(action) || !action->isEnabled()) {-
1034 d->setCurrentAction(0);-
1035#ifndef QT_NO_WHATSTHIS-
1036 if (QWhatsThis::inWhatsThisMode())-
1037 QWhatsThis::showText(e->globalPos(), d->whatsThis, this);-
1038#endif-
1039 return;-
1040 }-
1041-
1042 if(d->currentAction == action && d->popupState) {-
1043 if(QMenu *menu = d->activeMenu) {-
1044 d->activeMenu = 0;-
1045 menu->setAttribute(Qt::WA_NoMouseReplay);-
1046 menu->hide();-
1047 }-
1048 } else {-
1049 d->setCurrentAction(action, true);-
1050 }-
1051}-
1052-
1053/*!-
1054 \reimp-
1055*/-
1056void QMenuBar::mouseReleaseEvent(QMouseEvent *e)-
1057{-
1058 Q_D(QMenuBar);-
1059 if(e->button() != Qt::LeftButton || !d->mouseDown)-
1060 return;-
1061-
1062 d->mouseDown = false;-
1063 QAction *action = d->actionAt(e->pos());-
1064 if((d->closePopupMode && action == d->currentAction) || !action || !action->menu()) {-
1065 //we set the current action before activating-
1066 //so that we let the leave event set the current back to 0-
1067 d->setCurrentAction(action, false);-
1068 if(action)-
1069 d->activateAction(action, QAction::Trigger);-
1070 }-
1071 d->closePopupMode = 0;-
1072}-
1073-
1074/*!-
1075 \reimp-
1076*/-
1077void QMenuBar::keyPressEvent(QKeyEvent *e)-
1078{-
1079 Q_D(QMenuBar);-
1080 d->updateGeometries();-
1081 int key = e->key();-
1082 if(isRightToLeft()) { // in reverse mode open/close key for submenues are reversed-
1083 if(key == Qt::Key_Left)-
1084 key = Qt::Key_Right;-
1085 else if(key == Qt::Key_Right)-
1086 key = Qt::Key_Left;-
1087 }-
1088 if(key == Qt::Key_Tab) //means right-
1089 key = Qt::Key_Right;-
1090 else if(key == Qt::Key_Backtab) //means left-
1091 key = Qt::Key_Left;-
1092-
1093 bool key_consumed = false;-
1094 switch(key) {-
1095 case Qt::Key_Up:-
1096 case Qt::Key_Down:-
1097 case Qt::Key_Enter:-
1098 case Qt::Key_Space:-
1099 case Qt::Key_Return: {-
1100 if(!style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this) || !d->currentAction)-
1101 break;-
1102 if(d->currentAction->menu()) {-
1103 d->popupAction(d->currentAction, true);-
1104 } else if(key == Qt::Key_Enter || key == Qt::Key_Return || key == Qt::Key_Space) {-
1105 d->activateAction(d->currentAction, QAction::Trigger);-
1106 d->setCurrentAction(d->currentAction, false);-
1107 d->setKeyboardMode(false);-
1108 }-
1109 key_consumed = true;-
1110 break; }-
1111-
1112 case Qt::Key_Right:-
1113 case Qt::Key_Left: {-
1114 if(d->currentAction) {-
1115 int index = d->actions.indexOf(d->currentAction);-
1116 if (QAction *nextAction = d->getNextAction(index, key == Qt::Key_Left ? -1 : +1)) {-
1117 d->setCurrentAction(nextAction, d->popupState, true);-
1118 key_consumed = true;-
1119 }-
1120 }-
1121 break; }-
1122-
1123 default:-
1124 key_consumed = false;-
1125 }-
1126-
1127 if (!key_consumed && e->matches(QKeySequence::Cancel)) {-
1128 d->setCurrentAction(0);-
1129 d->setKeyboardMode(false);-
1130 key_consumed = true;-
1131 }-
1132-
1133 if(!key_consumed &&-
1134 (!e->modifiers() ||-
1135 (e->modifiers()&(Qt::MetaModifier|Qt::AltModifier))) && e->text().length()==1 && !d->popupState) {-
1136 int clashCount = 0;-
1137 QAction *first = 0, *currentSelected = 0, *firstAfterCurrent = 0;-
1138 {-
1139 QChar c = e->text()[0].toUpper();-
1140 for(int i = 0; i < d->actions.size(); ++i) {-
1141 if (d->actionRects.at(i).isNull())-
1142 continue;-
1143 QAction *act = d->actions.at(i);-
1144 QString s = act->text();-
1145 if(!s.isEmpty()) {-
1146 int ampersand = s.indexOf(QLatin1Char('&'));-
1147 if(ampersand >= 0) {-
1148 if(s[ampersand+1].toUpper() == c) {-
1149 clashCount++;-
1150 if(!first)-
1151 first = act;-
1152 if(act == d->currentAction)-
1153 currentSelected = act;-
1154 else if (!firstAfterCurrent && currentSelected)-
1155 firstAfterCurrent = act;-
1156 }-
1157 }-
1158 }-
1159 }-
1160 }-
1161 QAction *next_action = 0;-
1162 if(clashCount >= 1) {-
1163 if(clashCount == 1 || !d->currentAction || (currentSelected && !firstAfterCurrent))-
1164 next_action = first;-
1165 else-
1166 next_action = firstAfterCurrent;-
1167 }-
1168 if(next_action) {-
1169 key_consumed = true;-
1170 d->setCurrentAction(next_action, true, true);-
1171 }-
1172 }-
1173 if(key_consumed)-
1174 e->accept();-
1175 else-
1176 e->ignore();-
1177}-
1178-
1179/*!-
1180 \reimp-
1181*/-
1182void QMenuBar::mouseMoveEvent(QMouseEvent *e)-
1183{-
1184 Q_D(QMenuBar);-
1185 if (!(e->buttons() & Qt::LeftButton))-
1186 d->mouseDown = false;-
1187 bool popupState = d->popupState || d->mouseDown;-
1188 QAction *action = d->actionAt(e->pos());-
1189 if ((action && d->isVisible(action)) || !popupState)-
1190 d->setCurrentAction(action, popupState);-
1191}-
1192-
1193/*!-
1194 \reimp-
1195*/-
1196void QMenuBar::leaveEvent(QEvent *)-
1197{-
1198 Q_D(QMenuBar);-
1199 if((!hasFocus() && !d->popupState) ||-
1200 (d->currentAction && d->currentAction->menu() == 0))-
1201 d->setCurrentAction(0);-
1202}-
1203-
1204QPlatformMenu *QMenuBarPrivate::getPlatformMenu(QAction *action)-
1205{-
1206 if (!action || !action->menu())
!actionDescription
TRUEnever evaluated
FALSEnever evaluated
!action->menu()Description
TRUEnever evaluated
FALSEnever evaluated
0
1207 return 0;
never executed: return 0;
0
1208-
1209 returnQPlatformMenu *platformMenu = action->menu()->platformMenu();-
1210 if (!platformMenu && platformMenuBar) {
!platformMenuDescription
TRUEnever evaluated
FALSEnever evaluated
platformMenuBarDescription
TRUEnever evaluated
FALSEnever evaluated
0
1211 platformMenu = platformMenuBar->createMenu();-
1212 if (platformMenu)
platformMenuDescription
TRUEnever evaluated
FALSEnever evaluated
0
1213 action->menu()->setPlatformMenu(platformMenu);
never executed: action->menu()->setPlatformMenu(platformMenu);
0
1214 }
never executed: end of block
0
1215-
1216 return platformMenu;
never executed: return platformMenu;
0
1217}-
1218-
1219/*!-
1220 \reimp-
1221*/-
1222void QMenuBar::actionEvent(QActionEvent *e)-
1223{-
1224 Q_D(QMenuBar);-
1225 d->itemsDirty = true;-
1226-
1227 if (d->platformMenuBar) {
d->platformMenuBarDescription
TRUEnever evaluated
FALSEnever evaluated
0
1228#if !defined(Q_OS_WINCE)-
1229 QPlatformMenuBar *nativeMenuBar = d->platformMenuBar;-
1230#else-
1231 QMenuBarPrivate::QWceMenuBarPrivate *nativeMenuBar = d->wce_menubar;-
1232#endif-
1233 if (!nativeMenuBar)
!nativeMenuBarDescription
TRUEnever evaluated
FALSEnever evaluated
0
1234 return;
never executed: return;
0
1235-
1236 if (e->type() == QEvent::ActionAdded) {
e->type() == Q...t::ActionAddedDescription
TRUEnever evaluated
FALSEnever evaluated
0
1237 QPlatformMenu *menu = d->getPlatformMenu(e->action());-
1238 if (menu) {
menuDescription
TRUEnever evaluated
FALSEnever evaluated
0
1239 QPlatformMenu* beforeMenu = NULL;-
1240 for (int beforeIndex = d->indexOf(e->action()) + 1;-
1241 !beforeMenu && (beforeIndex < actions().size());
!beforeMenuDescription
TRUEnever evaluated
FALSEnever evaluated
(beforeIndex <...ions().size())Description
TRUEnever evaluated
FALSEnever evaluated
0
1242 ++beforeIndex)-
1243 {-
1244 beforeMenu = d->getPlatformMenu(actions().at(beforeIndex));-
1245 }
never executed: end of block
0
1246-
1247 menu->setTag(reinterpret_cast<quintptr>(e->action()));-
1248 menu->setText(e->action()->text());-
1249 d->platformMenuBar->insertMenu(menu, beforeMenu);-
1250 }
never executed: end of block
0
1251 } else if (e->type() == QEvent::ActionRemoved) {
never executed: end of block
e->type() == Q...:ActionRemovedDescription
TRUEnever evaluated
FALSEnever evaluated
0
1252 QPlatformMenu *menu = d->getPlatformMenu(e->action());-
1253 if (menu)
menuDescription
TRUEnever evaluated
FALSEnever evaluated
0
1254 d->platformMenuBar->removeMenu(menu);
never executed: d->platformMenuBar->removeMenu(menu);
0
1255 } else if (e->type() == QEvent::ActionChanged) {
never executed: end of block
e->type() == Q...:ActionChangedDescription
TRUEnever evaluated
FALSEnever evaluated
0
1256 QPlatformMenu* cur = d->platformMenuBar->menuForTag(reinterpret_cast<quintptr>(e->action()));-
1257 QPlatformMenu *menu = d->getPlatformMenu(e->action());-
1258-
1259 // the menu associated with the action can change, need to-
1260 // remove and/or insert the new platform menu-
1261 if (menu != cur) {
menu != curDescription
TRUEnever evaluated
FALSEnever evaluated
0
1262 if (cur)
curDescription
TRUEnever evaluated
FALSEnever evaluated
0
1263 d->platformMenuBar->removeMenu(cur);
never executed: d->platformMenuBar->removeMenu(cur);
0
1264 if (menu) {
menuDescription
TRUEnever evaluated
FALSEnever evaluated
0
1265 menu->setTag(reinterpret_cast<quintptr>(e->action()));-
1266-
1267 QPlatformMenu* beforeMenu = NULL;-
1268 for (int beforeIndex = d->indexOf(e->action()) + 1;-
1269 !beforeMenu && (beforeIndex < actions().size());
!beforeMenuDescription
TRUEnever evaluated
FALSEnever evaluated
(beforeIndex <...ions().size())Description
TRUEnever evaluated
FALSEnever evaluated
0
1270 ++beforeIndex)-
1271 {-
1272 beforeMenu = d->getPlatformMenu(actions().at(beforeIndex));-
1273 }
never executed: end of block
0
1274 d->platformMenuBar->insertMenu(menu, beforeMenu);-
1275 }
never executed: end of block
0
1276 } else if (menu) {
never executed: end of block
menuDescription
TRUEnever evaluated
FALSEnever evaluated
0
1277 menu->setText(e->action()->text());-
1278 menu->setVisible(e->action()->isVisible());-
1279 menu->setEnabled(e->action()->isEnabled());-
1280 d->platformMenuBar->syncMenu(menu);-
1281 }
never executed: end of block
0
1282 }
never executed: end of block
0
1283 }
never executed: end of block
0
1284-
1285 if(e->type() == QEvent::ActionAdded) {
e->type() == Q...t::ActionAddedDescription
TRUEnever evaluated
FALSEnever evaluated
0
1286 connect(e->action(), SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));-
1287 connect(e->action(), SIGNAL(hovered()), this, SLOT(_q_actionHovered()));-
1288 } else if(e->type() == QEvent::ActionRemoved) {
never executed: end of block
e->type() == Q...:ActionRemovedDescription
TRUEnever evaluated
FALSEnever evaluated
0
1289 e->action()->disconnect(this);-
1290 }
never executed: end of block
0
1291 if (isVisible()) {
isVisible()Description
TRUEnever evaluated
FALSEnever evaluated
0
1292 d->updateGeometries();-
1293 update();-
1294 }
never executed: end of block
0
1295}
never executed: end of block
0
1296-
1297/*!-
1298 \reimp-
1299*/-
1300void QMenuBar::focusInEvent(QFocusEvent *)-
1301{-
1302 Q_D(QMenuBar);-
1303 if(d->keyboardState)-
1304 d->focusFirstAction();-
1305}-
1306-
1307/*!-
1308 \reimp-
1309*/-
1310void QMenuBar::focusOutEvent(QFocusEvent *)-
1311{-
1312 Q_D(QMenuBar);-
1313 if(!d->popupState) {-
1314 d->setCurrentAction(0);-
1315 d->setKeyboardMode(false);-
1316 }-
1317}-
1318-
1319/*!-
1320 \reimp-
1321 */-
1322void QMenuBar::timerEvent (QTimerEvent *e)-
1323{-
1324 Q_D(QMenuBar);-
1325 if (e->timerId() == d->autoReleaseTimer.timerId()) {-
1326 d->autoReleaseTimer.stop();-
1327 d->setCurrentAction(0);-
1328 }-
1329 QWidget::timerEvent(e);-
1330}-
1331-
1332-
1333void QMenuBarPrivate::handleReparent()-
1334{-
1335 Q_Q(QMenuBar);-
1336 QWidget *newParent = q->parentWidget();-
1337-
1338 //Note: if parent is reparented, then window may change even if parent doesn't.-
1339 // We need to install an avent filter on each parent up to the parent that is-
1340 // also a window (for shortcuts)-
1341 QWidget *newWindow = newParent ? newParent->window() : Q_NULLPTR;-
1342-
1343 QVector<QPointer<QWidget> > newParents;-
1344 // Remove event filters on ex-parents, keep them on still-parents-
1345 // The parents are always ordered in the vector-
1346 foreach (const QPointer<QWidget> &w, oldParents) {-
1347 if (w) {-
1348 if (newParent == w) {-
1349 newParents.append(w);-
1350 if (newParent != newWindow) //stop at the window-
1351 newParent = newParent->parentWidget();-
1352 } else {-
1353 w->removeEventFilter(q);-
1354 }-
1355 }-
1356 }-
1357-
1358 // At this point, newParent is the next one to be added to newParents-
1359 while (newParent && newParent != newWindow) {-
1360 //install event filters all the way up to (excluding) the window-
1361 newParents.append(newParent);-
1362 newParent->installEventFilter(q);-
1363 newParent = newParent->parentWidget();-
1364 }-
1365-
1366 if (newParent && newWindow) {-
1367 // Install the event filter on the window-
1368 newParents.append(newParent);-
1369 newParent->installEventFilter(q);-
1370 }-
1371 oldParents = newParents;-
1372-
1373 if (platformMenuBar) {-
1374 if (newWindow) {-
1375 // force the underlying platform window to be created, since-
1376 // the platform menubar needs it (and we have no other way to-
1377 // discover when the platform window is created)-
1378 newWindow->createWinId();-
1379 platformMenuBar->handleReparent(newWindow->windowHandle());-
1380 } else {-
1381 platformMenuBar->handleReparent(0);-
1382 }-
1383 }-
1384-
1385#ifdef Q_OS_WINCE-
1386 if (qt_wince_is_mobile() && wce_menubar)-
1387 wce_menubar->rebuild();-
1388#endif-
1389}-
1390-
1391/*!-
1392 \reimp-
1393*/-
1394void QMenuBar::changeEvent(QEvent *e)-
1395{-
1396 Q_D(QMenuBar);-
1397 if(e->type() == QEvent::StyleChange) {-
1398 d->itemsDirty = true;-
1399 setMouseTracking(style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, this));-
1400 if(parentWidget())-
1401 resize(parentWidget()->width(), heightForWidth(parentWidget()->width()));-
1402 d->updateGeometries();-
1403 } else if (e->type() == QEvent::ParentChange) {-
1404 d->handleReparent();-
1405 } else if (e->type() == QEvent::FontChange-
1406 || e->type() == QEvent::ApplicationFontChange) {-
1407 d->itemsDirty = true;-
1408 d->updateGeometries();-
1409 }-
1410-
1411 QWidget::changeEvent(e);-
1412}-
1413-
1414/*!-
1415 \reimp-
1416*/-
1417bool QMenuBar::event(QEvent *e)-
1418{-
1419 Q_D(QMenuBar);-
1420 switch (e->type()) {-
1421 case QEvent::KeyPress: {-
1422 QKeyEvent *ke = (QKeyEvent*)e;-
1423#if 0-
1424 if(!d->keyboardState) { //all keypresses..-
1425 d->setCurrentAction(0);-
1426 return ;-
1427 }-
1428#endif-
1429 if(ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {-
1430 keyPressEvent(ke);-
1431 return true;-
1432 }-
1433-
1434 } break;-
1435#ifndef QT_NO_SHORTCUT-
1436 case QEvent::Shortcut: {-
1437 QShortcutEvent *se = static_cast<QShortcutEvent *>(e);-
1438 int shortcutId = se->shortcutId();-
1439 for(int j = 0; j < d->shortcutIndexMap.size(); ++j) {-
1440 if (shortcutId == d->shortcutIndexMap.value(j))-
1441 d->_q_internalShortcutActivated(j);-
1442 }-
1443 } break;-
1444#endif-
1445 case QEvent::Show:-
1446 d->_q_updateLayout();-
1447 break;-
1448 case QEvent::ShortcutOverride: {-
1449 QKeyEvent *kev = static_cast<QKeyEvent*>(e);-
1450 //we only filter out escape if there is a current action-
1451 if (kev->matches(QKeySequence::Cancel) && d->currentAction) {-
1452 e->accept();-
1453 return true;-
1454 }-
1455 }-
1456 break;-
1457-
1458-
1459#ifndef QT_NO_WHATSTHIS-
1460 case QEvent::QueryWhatsThis:-
1461 e->setAccepted(d->whatsThis.size());-
1462 if (QAction *action = d->actionAt(static_cast<QHelpEvent*>(e)->pos())) {-
1463 if (action->whatsThis().size() || action->menu())-
1464 e->accept();-
1465 }-
1466 return true;-
1467#endif-
1468 case QEvent::LayoutDirectionChange:-
1469 d->_q_updateLayout();-
1470 break;-
1471 default:-
1472 break;-
1473 }-
1474 return QWidget::event(e);-
1475}-
1476-
1477/*!-
1478 \reimp-
1479*/-
1480bool QMenuBar::eventFilter(QObject *object, QEvent *event)-
1481{-
1482 Q_D(QMenuBar);-
1483 if (object && (event->type() == QEvent::ParentChange)) //GrandparentChange-
1484 d->handleReparent();-
1485-
1486 if (object == d->leftWidget || object == d->rightWidget) {-
1487 switch (event->type()) {-
1488 case QEvent::ShowToParent:-
1489 case QEvent::HideToParent:-
1490 d->_q_updateLayout();-
1491 break;-
1492 default:-
1493 break;-
1494 }-
1495 }-
1496-
1497 if (style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this)) {-
1498 if (d->altPressed) {-
1499 switch (event->type()) {-
1500 case QEvent::KeyPress:-
1501 case QEvent::KeyRelease:-
1502 {-
1503 QKeyEvent *kev = static_cast<QKeyEvent*>(event);-
1504 if (kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta) {-
1505 if (event->type() == QEvent::KeyPress) // Alt-press does not interest us, we have the shortcut-override event-
1506 break;-
1507 d->setKeyboardMode(!d->keyboardState);-
1508 }-
1509 }-
1510 // fall through-
1511 case QEvent::MouseButtonPress:-
1512 case QEvent::MouseButtonRelease:-
1513 case QEvent::MouseMove:-
1514 case QEvent::FocusIn:-
1515 case QEvent::FocusOut:-
1516 case QEvent::ActivationChange:-
1517 d->altPressed = false;-
1518 qApp->removeEventFilter(this);-
1519 break;-
1520 default:-
1521 break;-
1522 }-
1523 } else if (isVisible()) {-
1524 if (event->type() == QEvent::ShortcutOverride) {-
1525 QKeyEvent *kev = static_cast<QKeyEvent*>(event);-
1526 if ((kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta)-
1527 && kev->modifiers() == Qt::AltModifier) {-
1528 d->altPressed = true;-
1529 qApp->installEventFilter(this);-
1530 }-
1531 }-
1532 }-
1533 }-
1534-
1535 return false;-
1536}-
1537-
1538/*!-
1539 Returns the QAction at \a pt. Returns 0 if there is no action at \a pt or if-
1540the location has a separator.-
1541-
1542 \sa addAction(), addSeparator()-
1543*/-
1544QAction *QMenuBar::actionAt(const QPoint &pt) const-
1545{-
1546 Q_D(const QMenuBar);-
1547 return d->actionAt(pt);-
1548}-
1549-
1550/*!-
1551 Returns the geometry of action \a act as a QRect.-
1552-
1553 \sa actionAt()-
1554*/-
1555QRect QMenuBar::actionGeometry(QAction *act) const-
1556{-
1557 Q_D(const QMenuBar);-
1558 return d->actionRect(act);-
1559}-
1560-
1561/*!-
1562 \reimp-
1563*/-
1564QSize QMenuBar::minimumSizeHint() const-
1565{-
1566 Q_D(const QMenuBar);-
1567 const bool as_gui_menubar = !isNativeMenuBar();-
1568-
1569 ensurePolished();-
1570 QSize ret(0, 0);-
1571 const_cast<QMenuBarPrivate*>(d)->updateGeometries();-
1572 const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this);-
1573 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);-
1574 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);-
1575 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);-
1576 if(as_gui_menubar) {-
1577 int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width();-
1578 d->calcActionRects(w - (2 * fw), 0);-
1579 for (int i = 0; ret.isNull() && i < d->actions.count(); ++i)-
1580 ret = d->actionRects.at(i).size();-
1581 if (!d->extension->isHidden())-
1582 ret += QSize(d->extension->sizeHint().width(), 0);-
1583 ret += QSize(2*fw + hmargin, 2*fw + vmargin);-
1584 }-
1585 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;-
1586 if(d->leftWidget) {-
1587 QSize sz = d->leftWidget->minimumSizeHint();-
1588 ret.setWidth(ret.width() + sz.width());-
1589 if(sz.height() + margin > ret.height())-
1590 ret.setHeight(sz.height() + margin);-
1591 }-
1592 if(d->rightWidget) {-
1593 QSize sz = d->rightWidget->minimumSizeHint();-
1594 ret.setWidth(ret.width() + sz.width());-
1595 if(sz.height() + margin > ret.height())-
1596 ret.setHeight(sz.height() + margin);-
1597 }-
1598 if(as_gui_menubar) {-
1599 QStyleOptionMenuItem opt;-
1600 opt.rect = rect();-
1601 opt.menuRect = rect();-
1602 opt.state = QStyle::State_None;-
1603 opt.menuItemType = QStyleOptionMenuItem::Normal;-
1604 opt.checkType = QStyleOptionMenuItem::NotCheckable;-
1605 opt.palette = palette();-
1606 return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt,-
1607 ret.expandedTo(QApplication::globalStrut()),-
1608 this));-
1609 }-
1610 return ret;-
1611}-
1612-
1613/*!-
1614 \reimp-
1615*/-
1616QSize QMenuBar::sizeHint() const-
1617{-
1618 Q_D(const QMenuBar);-
1619 const bool as_gui_menubar = !isNativeMenuBar();-
1620-
1621 ensurePolished();-
1622 QSize ret(0, 0);-
1623 const_cast<QMenuBarPrivate*>(d)->updateGeometries();-
1624 const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this);-
1625 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);-
1626 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);-
1627 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);-
1628 if(as_gui_menubar) {-
1629 const int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width();-
1630 d->calcActionRects(w - (2 * fw), 0);-
1631 for (int i = 0; i < d->actionRects.count(); ++i) {-
1632 const QRect &actionRect = d->actionRects.at(i);-
1633 ret = ret.expandedTo(QSize(actionRect.x() + actionRect.width(), actionRect.y() + actionRect.height()));-
1634 }-
1635 //the action geometries already contain the top and left-
1636 //margins. So we only need to add those from right and bottom.-
1637 ret += QSize(fw + hmargin, fw + vmargin);-
1638 }-
1639 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;-
1640 if(d->leftWidget) {-
1641 QSize sz = d->leftWidget->sizeHint();-
1642 sz.rheight() += margin;-
1643 ret = ret.expandedTo(sz);-
1644 }-
1645 if(d->rightWidget) {-
1646 QSize sz = d->rightWidget->sizeHint();-
1647 ret.setWidth(ret.width() + sz.width());-
1648 if(sz.height() + margin > ret.height())-
1649 ret.setHeight(sz.height() + margin);-
1650 }-
1651 if(as_gui_menubar) {-
1652 QStyleOptionMenuItem opt;-
1653 opt.rect = rect();-
1654 opt.menuRect = rect();-
1655 opt.state = QStyle::State_None;-
1656 opt.menuItemType = QStyleOptionMenuItem::Normal;-
1657 opt.checkType = QStyleOptionMenuItem::NotCheckable;-
1658 opt.palette = palette();-
1659 return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt,-
1660 ret.expandedTo(QApplication::globalStrut()),-
1661 this));-
1662 }-
1663 return ret;-
1664}-
1665-
1666/*!-
1667 \reimp-
1668*/-
1669int QMenuBar::heightForWidth(int) const-
1670{-
1671 Q_D(const QMenuBar);-
1672 const bool as_gui_menubar = !isNativeMenuBar();-
1673-
1674 const_cast<QMenuBarPrivate*>(d)->updateGeometries();-
1675 int height = 0;-
1676 const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);-
1677 int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);-
1678 int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);-
1679 if(as_gui_menubar) {-
1680 for (int i = 0; i < d->actionRects.count(); ++i)-
1681 height = qMax(height, d->actionRects.at(i).height());-
1682 if (height) //there is at least one non-null item-
1683 height += spaceBelowMenuBar;-
1684 height += 2*fw;-
1685 height += 2*vmargin;-
1686 }-
1687 int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;-
1688 if(d->leftWidget)-
1689 height = qMax(d->leftWidget->sizeHint().height() + margin, height);-
1690 if(d->rightWidget)-
1691 height = qMax(d->rightWidget->sizeHint().height() + margin, height);-
1692 if(as_gui_menubar) {-
1693 QStyleOptionMenuItem opt;-
1694 opt.init(this);-
1695 opt.menuRect = rect();-
1696 opt.state = QStyle::State_None;-
1697 opt.menuItemType = QStyleOptionMenuItem::Normal;-
1698 opt.checkType = QStyleOptionMenuItem::NotCheckable;-
1699 return style()->sizeFromContents(QStyle::CT_MenuBar, &opt, QSize(0, height), this).height(); //not pretty..-
1700 }-
1701 return height;-
1702}-
1703-
1704/*!-
1705 \internal-
1706*/-
1707void QMenuBarPrivate::_q_internalShortcutActivated(int id)-
1708{-
1709 Q_Q(QMenuBar);-
1710 QAction *act = actions.at(id);-
1711 setCurrentAction(act, true, true);-
1712 if (act && !act->menu()) {-
1713 activateAction(act, QAction::Trigger);-
1714 //100 is the same as the default value in QPushButton::animateClick-
1715 autoReleaseTimer.start(100, q);-
1716 } else if (act && q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, q)) {-
1717 // When we open a menu using a shortcut, we should end up in keyboard state-
1718 setKeyboardMode(true);-
1719 }-
1720}-
1721-
1722void QMenuBarPrivate::_q_updateLayout()-
1723{-
1724 Q_Q(QMenuBar);-
1725 itemsDirty = true;-
1726 if (q->isVisible()) {-
1727 updateGeometries();-
1728 q->update();-
1729 }-
1730}-
1731-
1732/*!-
1733 \fn void QMenuBar::setCornerWidget(QWidget *widget, Qt::Corner corner)-
1734-
1735 This sets the given \a widget to be shown directly on the left of the first-
1736 menu item, or on the right of the last menu item, depending on \a corner.-
1737-
1738 The menu bar takes ownership of \a widget, reparenting it into the menu bar.-
1739 However, if the \a corner already contains a widget, this previous widget-
1740 will no longer be managed and will still be a visible child of the menu bar.-
1741-
1742 \note Using a corner other than Qt::TopRightCorner or Qt::TopLeftCorner-
1743 will result in a warning.-
1744*/-
1745void QMenuBar::setCornerWidget(QWidget *w, Qt::Corner corner)-
1746{-
1747 Q_D(QMenuBar);-
1748 switch (corner) {-
1749 case Qt::TopLeftCorner:-
1750 if (d->leftWidget)-
1751 d->leftWidget->removeEventFilter(this);-
1752 d->leftWidget = w;-
1753 break;-
1754 case Qt::TopRightCorner:-
1755 if (d->rightWidget)-
1756 d->rightWidget->removeEventFilter(this);-
1757 d->rightWidget = w;-
1758 break;-
1759 default:-
1760 qWarning("QMenuBar::setCornerWidget: Only TopLeftCorner and TopRightCorner are supported");-
1761 return;-
1762 }-
1763-
1764 if (w) {-
1765 w->setParent(this);-
1766 w->installEventFilter(this);-
1767 }-
1768-
1769 d->_q_updateLayout();-
1770}-
1771-
1772/*!-
1773 Returns the widget on the left of the first or on the right of the last menu-
1774 item, depending on \a corner.-
1775-
1776 \note Using a corner other than Qt::TopRightCorner or Qt::TopLeftCorner-
1777 will result in a warning.-
1778*/-
1779QWidget *QMenuBar::cornerWidget(Qt::Corner corner) const-
1780{-
1781 Q_D(const QMenuBar);-
1782 QWidget *w = 0;-
1783 switch(corner) {-
1784 case Qt::TopLeftCorner:-
1785 w = d->leftWidget;-
1786 break;-
1787 case Qt::TopRightCorner:-
1788 w = d->rightWidget;-
1789 break;-
1790 default:-
1791 qWarning("QMenuBar::cornerWidget: Only TopLeftCorner and TopRightCorner are supported");-
1792 break;-
1793 }-
1794-
1795 return w;-
1796}-
1797-
1798/*!-
1799 \property QMenuBar::nativeMenuBar-
1800 \brief Whether or not a menubar will be used as a native menubar on platforms that support it-
1801 \since 4.6-
1802-
1803 This property specifies whether or not the menubar should be used as a native menubar on-
1804 platforms that support it. The currently supported platforms are \macos, and-
1805 Linux desktops which use the com.canonical.dbusmenu D-Bus interface (such as Ubuntu Unity).-
1806 If this property is \c true, the menubar is used in the native menubar and is not in the window of-
1807 its parent; if \c false the menubar remains in the window. On other platforms,-
1808 setting this attribute has no effect, and reading this attribute will always return \c false.-
1809-
1810 The default is to follow whether the Qt::AA_DontUseNativeMenuBar attribute-
1811 is set for the application. Explicitly setting this property overrides-
1812 the presence (or absence) of the attribute.-
1813*/-
1814-
1815void QMenuBar::setNativeMenuBar(bool nativeMenuBar)-
1816{-
1817 Q_D(QMenuBar);-
1818 if (nativeMenuBar != bool(d->platformMenuBar)) {-
1819 if (!nativeMenuBar) {-
1820 delete d->platformMenuBar;-
1821 d->platformMenuBar = 0;-
1822 } else {-
1823 if (!d->platformMenuBar)-
1824 d->platformMenuBar = QGuiApplicationPrivate::platformTheme()->createPlatformMenuBar();-
1825 }-
1826-
1827 updateGeometry();-
1828 if (!nativeMenuBar && parentWidget())-
1829 setVisible(true);-
1830 }-
1831}-
1832-
1833bool QMenuBar::isNativeMenuBar() const-
1834{-
1835 Q_D(const QMenuBar);-
1836 return bool(d->platformMenuBar);-
1837}-
1838-
1839/*!-
1840 \internal-
1841*/-
1842QPlatformMenuBar *QMenuBar::platformMenuBar()-
1843{-
1844 Q_D(const QMenuBar);-
1845 return d->platformMenuBar;-
1846}-
1847-
1848/*!-
1849 \since 4.4-
1850-
1851 Sets the default action to \a act.-
1852-
1853 The default action is assigned to the left soft key. The menu is assigned-
1854 to the right soft key.-
1855-
1856 Currently there is only support for the default action on Windows-
1857 Mobile. On all other platforms this method is not available.-
1858-
1859 \sa defaultAction()-
1860*/-
1861-
1862#ifdef Q_OS_WINCE-
1863void QMenuBar::setDefaultAction(QAction *act)-
1864{-
1865 Q_D(QMenuBar);-
1866 if (d->defaultAction == act)-
1867 return;-
1868 if (qt_wince_is_mobile())-
1869 if (d->defaultAction) {-
1870 disconnect(d->defaultAction, SIGNAL(changed()), this, SLOT(_q_updateDefaultAction()));-
1871 disconnect(d->defaultAction, SIGNAL(destroyed()), this, SLOT(_q_updateDefaultAction()));-
1872 }-
1873 d->defaultAction = act;-
1874 if (qt_wince_is_mobile())-
1875 if (d->defaultAction) {-
1876 connect(d->defaultAction, SIGNAL(changed()), this, SLOT(_q_updateDefaultAction()));-
1877 connect(d->defaultAction, SIGNAL(destroyed()), this, SLOT(_q_updateDefaultAction()));-
1878 }-
1879 if (d->wce_menubar) {-
1880 d->wce_menubar->rebuild();-
1881 }-
1882}-
1883-
1884/*!-
1885 \since 4.4-
1886-
1887 Returns the current default action.-
1888-
1889 \sa setDefaultAction()-
1890*/-
1891QAction *QMenuBar::defaultAction() const-
1892{-
1893 return d_func()->defaultAction;-
1894}-
1895#endif-
1896-
1897/*!-
1898 \fn void QMenuBar::triggered(QAction *action)-
1899-
1900 This signal is emitted when an action in a menu belonging to this menubar-
1901 is triggered as a result of a mouse click; \a action is the action that-
1902 caused the signal to be emitted.-
1903-
1904 \note QMenuBar has to have ownership of the QMenu in order this signal to work.-
1905-
1906 Normally, you connect each menu action to a single slot using-
1907 QAction::triggered(), but sometimes you will want to connect-
1908 several items to a single slot (most often if the user selects-
1909 from an array). This signal is useful in such cases.-
1910-
1911 \sa hovered(), QAction::triggered()-
1912*/-
1913-
1914/*!-
1915 \fn void QMenuBar::hovered(QAction *action)-
1916-
1917 This signal is emitted when a menu action is highlighted; \a action-
1918 is the action that caused the event to be sent.-
1919-
1920 Often this is used to update status information.-
1921-
1922 \sa triggered(), QAction::hovered()-
1923*/-
1924-
1925// for private slots-
1926-
1927-
1928QT_END_NAMESPACE-
1929-
1930#include <moc_qmenubar.cpp>-
1931-
1932#endif // QT_NO_MENUBAR-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9