Qt 4.8
qmenubar.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 #ifdef Q_WS_X11
59 #include <qpluginloader.h>
60 #endif
61 
62 #ifndef QT_NO_MENUBAR
63 
64 #ifdef QT3_SUPPORT
65 #include <private/qaction_p.h>
66 #include <qmenudata.h>
67 #endif
68 
69 #include "qmenu_p.h"
70 #include "qmenubar_p.h"
71 #include "qdebug.h"
72 #ifdef Q_WS_X11
73 #include "qmenubar_x11_p.h"
74 #endif
75 
76 #ifdef Q_WS_WINCE
77 extern bool qt_wince_is_mobile(); //defined in qguifunctions_wce.cpp
78 #endif
79 
80 #ifdef QT_SOFTKEYS_ENABLED
81 #include <private/qsoftkeymanager_p.h>
82 #endif
83 
85 
87 {
88 public:
89  explicit QMenuBarExtension(QWidget *parent);
90 
91  QSize sizeHint() const;
92  void paintEvent(QPaintEvent *);
93 };
94 
96  : QToolButton(parent)
97 {
98  setObjectName(QLatin1String("qt_menubar_ext_button"));
99  setAutoRaise(true);
100 #ifndef QT_NO_MENU
102 #endif
104 }
105 
107 {
108  QStylePainter p(this);
110  initStyleOption(&opt);
111  // We do not need to draw both extension arrows
112  opt.features &= ~QStyleOptionToolButton::HasMenu;
114 }
115 
116 
118 {
120  return QSize(ext, ext);
121 }
122 
123 
128 {
129  for(int i = 0; i < actions.size(); ++i) {
130  if(actionRect(actions.at(i)).contains(p))
131  return actions.at(i);
132  }
133  return 0;
134 }
135 
136 QRect QMenuBarPrivate::menuRect(bool extVisible) const
137 {
138  Q_Q(const QMenuBar);
139 
140  int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
141  QRect result = q->rect();
142  result.adjust(hmargin, 0, -hmargin, 0);
143 
144  if (extVisible) {
145  if (q->isRightToLeft())
146  result.setLeft(result.left() + extension->sizeHint().width());
147  else
148  result.setWidth(result.width() - extension->sizeHint().width());
149  }
150 
151  if (leftWidget && leftWidget->isVisible()) {
152  QSize sz = leftWidget->sizeHint();
153  if (q->isRightToLeft())
154  result.setRight(result.right() - sz.width());
155  else
156  result.setLeft(result.left() + sz.width());
157  }
158 
159  if (rightWidget && rightWidget->isVisible()) {
160  QSize sz = rightWidget->sizeHint();
161  if (q->isRightToLeft())
162  result.setLeft(result.left() + sz.width());
163  else
164  result.setRight(result.right() - sz.width());
165  }
166 
167  return result;
168 }
169 
171 {
172  return !hiddenActions.contains(action);
173 }
174 
176 {
177  Q_Q(QMenuBar);
178  if(!itemsDirty)
179  return;
180  int q_width = q->width()-(q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q)*2);
181  int q_start = -1;
182  if(
183 #ifdef Q_WS_X11
184  platformMenuBar->allowCornerWidgets() &&
185 #endif
186  (leftWidget || rightWidget)) {
187  int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q)
188  + q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
189  int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, q)
190  + q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
191  if (leftWidget && leftWidget->isVisible()) {
192  QSize sz = leftWidget->sizeHint();
193  q_width -= sz.width();
194  q_start = sz.width();
195  QPoint pos(hmargin, (q->height() - leftWidget->height()) / 2);
196  QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));
197  leftWidget->setGeometry(vRect);
198  }
199  if (rightWidget && rightWidget->isVisible()) {
200  QSize sz = rightWidget->sizeHint();
201  q_width -= sz.width();
202  QPoint pos(q->width() - sz.width() - hmargin, vmargin);
203  QRect vRect = QStyle::visualRect(q->layoutDirection(), q->rect(), QRect(pos, sz));
204  rightWidget->setGeometry(vRect);
205  }
206  }
207 
208 #ifdef Q_WS_MAC
209  if(q->isNativeMenuBar()) {//nothing to see here folks, move along..
210  itemsDirty = false;
211  return;
212  }
213 #endif
214  calcActionRects(q_width, q_start);
215  currentAction = 0;
216 #ifndef QT_NO_SHORTCUT
217  if(
218 #ifdef Q_WS_X11
219  !platformMenuBar->shortcutsHandledByNativeMenuBar() &&
220 #endif
221  itemsDirty) {
222  for(int j = 0; j < shortcutIndexMap.size(); ++j)
223  q->releaseShortcut(shortcutIndexMap.value(j));
224  shortcutIndexMap.resize(0); // faster than clear
225  for(int i = 0; i < actions.count(); i++)
226  shortcutIndexMap.append(q->grabShortcut(QKeySequence::mnemonic(actions.at(i)->text())));
227  }
228 #endif
229 #ifdef Q_WS_X11
230  if(q->isNativeMenuBar()) {//nothing to see here folks, move along..
231  itemsDirty = false;
232  return;
233  }
234 #endif
235  itemsDirty = false;
236 
237  hiddenActions.clear();
238  //this is the menu rectangle without any extension
239  QRect menuRect = this->menuRect(false);
240 
241  //we try to see if the actions will fit there
242  bool hasHiddenActions = false;
243  for (int i = 0; i < actions.count(); ++i) {
244  const QRect &rect = actionRects.at(i);
245  if (rect.isValid() && !menuRect.contains(rect)) {
246  hasHiddenActions = true;
247  break;
248  }
249  }
250 
251  //...and if not, determine the ones that fit on the menu with the extension visible
252  if (hasHiddenActions) {
253  menuRect = this->menuRect(true);
254  for (int i = 0; i < actions.count(); ++i) {
255  const QRect &rect = actionRects.at(i);
256  if (rect.isValid() && !menuRect.contains(rect)) {
257  hiddenActions.append(actions.at(i));
258  }
259  }
260  }
261 
262  if (hiddenActions.count() > 0) {
263  QMenu *pop = extension->menu();
264  if (!pop) {
265  pop = new QMenu(q);
266  extension->setMenu(pop);
267  }
268  pop->clear();
269  pop->addActions(hiddenActions);
270 
271  int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q);
272  int x = q->isRightToLeft()
273  ? menuRect.left() - extension->sizeHint().width() + 1
274  : menuRect.right();
275  extension->setGeometry(x, vmargin, extension->sizeHint().width(), menuRect.height() - vmargin*2);
276  extension->show();
277  } else {
278  extension->hide();
279  }
280  q->updateGeometry();
281 #ifdef QT3_SUPPORT
282  if (parent) {
283  QMenubarUpdatedEvent menubarUpdated(q);
284  QApplication::sendEvent(parent, &menubarUpdated);
285  }
286 #endif
287 }
288 
290 {
291  const int index = actions.indexOf(act);
292 
293  //makes sure the geometries are up-to-date
294  const_cast<QMenuBarPrivate*>(this)->updateGeometries();
295 
296  if (index < 0 || index >= actionRects.count())
297  return QRect(); // that can happen in case of native menubar
298 
299  return actionRects.at(index);
300 }
301 
303 {
304  if(!currentAction) {
305  updateGeometries();
306  int index = 0;
307  while (index < actions.count() && actionRects.at(index).isNull()) ++index;
308  if (index < actions.count())
309  setCurrentAction(actions.at(index));
310  }
311 }
312 
314 {
315  Q_Q(QMenuBar);
316  if (b && !q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, q)) {
317  setCurrentAction(0);
318  return;
319  }
320  keyboardState = b;
321  if(b) {
323  if (fw != q)
324  keyboardFocusWidget = fw;
325  focusFirstAction();
326  q->setFocus(Qt::MenuBarFocusReason);
327  } else {
328  if(!popupState)
329  setCurrentAction(0);
330  if(keyboardFocusWidget) {
331  if (QApplication::focusWidget() == q)
332  keyboardFocusWidget->setFocus(Qt::MenuBarFocusReason);
333  keyboardFocusWidget = 0;
334  }
335  }
336  q->update();
337 }
338 
339 void QMenuBarPrivate::popupAction(QAction *action, bool activateFirst)
340 {
341  Q_Q(QMenuBar);
342  if(!action || !action->menu() || closePopupMode)
343  return;
344  popupState = true;
345  if (action->isEnabled() && action->menu()->isEnabled()) {
346  closePopupMode = 0;
347  activeMenu = action->menu();
348  activeMenu->d_func()->causedPopup.widget = q;
349  activeMenu->d_func()->causedPopup.action = action;
350 
351  QRect adjustedActionRect = actionRect(action);
352  QPoint pos(q->mapToGlobal(QPoint(adjustedActionRect.left(), adjustedActionRect.bottom() + 1)));
353  QSize popup_size = activeMenu->sizeHint();
354 
355  //we put the popup menu on the screen containing the bottom-center of the action rect
356  QRect screenRect = QApplication::desktop()->screenGeometry(pos + QPoint(adjustedActionRect.width() / 2, 0));
357  pos = QPoint(qMax(pos.x(), screenRect.x()), qMax(pos.y(), screenRect.y()));
358 
359  const bool fitUp = (q->mapToGlobal(adjustedActionRect.topLeft()).y() >= popup_size.height());
360  const bool fitDown = (pos.y() + popup_size.height() <= screenRect.bottom());
361  const bool rtl = q->isRightToLeft();
362  const int actionWidth = adjustedActionRect.width();
363 
364  if (!fitUp && !fitDown) { //we should shift the menu
365  bool shouldShiftToRight = !rtl;
366  if (rtl && popup_size.width() > pos.x())
367  shouldShiftToRight = true;
368  else if (actionWidth + popup_size.width() + pos.x() > screenRect.right())
369  shouldShiftToRight = false;
370 
371  if (shouldShiftToRight) {
372  pos.rx() += actionWidth + (rtl ? popup_size.width() : 0);
373  } else {
374  //shift to left
375  if (!rtl)
376  pos.rx() -= popup_size.width();
377  }
378  } else if (rtl) {
379  pos.rx() += actionWidth;
380  }
381 
382  if(!defaultPopDown || (fitUp && !fitDown))
383  pos.setY(qMax(screenRect.y(), q->mapToGlobal(QPoint(0, adjustedActionRect.top()-popup_size.height())).y()));
384  activeMenu->popup(pos);
385  if(activateFirst)
386  activeMenu->d_func()->setFirstActionActive();
387  }
388  q->update(actionRect(action));
389 }
390 
391 void QMenuBarPrivate::setCurrentAction(QAction *action, bool popup, bool activateFirst)
392 {
393  if(currentAction == action && popup == popupState)
394  return;
395 
396  autoReleaseTimer.stop();
397 
398  doChildEffects = (popup && !activeMenu);
399  Q_Q(QMenuBar);
400  QWidget *fw = 0;
401  if(QMenu *menu = activeMenu) {
402  activeMenu = 0;
403  if (popup) {
404  fw = q->window()->focusWidget();
406  }
407  menu->hide();
408  }
409 
410  if(currentAction)
411  q->update(actionRect(currentAction));
412 
413  popupState = popup;
414 #ifndef QT_NO_STATUSTIP
415  QAction *previousAction = currentAction;
416 #endif
417  currentAction = action;
418  if (action) {
419  activateAction(action, QAction::Hover);
420  if(popup)
421  popupAction(action, activateFirst);
422  q->update(actionRect(action));
423 #ifndef QT_NO_STATUSTIP
424  } else if (previousAction) {
425  QString empty;
426  QStatusTipEvent tip(empty);
427  QApplication::sendEvent(q, &tip);
428 #endif
429  }
430  if (fw)
432 }
433 
434 void QMenuBarPrivate::calcActionRects(int max_width, int start) const
435 {
436  Q_Q(const QMenuBar);
437 
438  if(!itemsDirty)
439  return;
440 
441  //let's reinitialize the buffer
442  actionRects.resize(actions.count());
443  actionRects.fill(QRect());
444 
445  const QStyle *style = q->style();
446 
447  const int itemSpacing = style->pixelMetric(QStyle::PM_MenuBarItemSpacing, 0, q);
448  int max_item_height = 0, separator = -1, separator_start = 0, separator_len = 0;
449 
450  //calculate size
451  const QFontMetrics fm = q->fontMetrics();
452  const int hmargin = style->pixelMetric(QStyle::PM_MenuBarHMargin, 0, q),
453  vmargin = style->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q),
454  icone = style->pixelMetric(QStyle::PM_SmallIconSize, 0, q);
455  for(int i = 0; i < actions.count(); i++) {
456  QAction *action = actions.at(i);
457  if(!action->isVisible())
458  continue;
459 
460  QSize sz;
461 
462  //calc what I think the size is..
463  if(action->isSeparator()) {
464  if (style->styleHint(QStyle::SH_DrawMenuBarSeparator, 0, q))
465  separator = i;
466  continue; //we don't really position these!
467  } else {
468  const QString s = action->text();
469  QIcon is = action->icon();
470  // If an icon is set, only the icon is visible
471  if (!is.isNull())
472  sz = sz.expandedTo(QSize(icone, icone));
473  else if (!s.isEmpty())
474  sz = fm.size(Qt::TextShowMnemonic, s);
475  }
476 
477  //let the style modify the above size..
479  q->initStyleOption(&opt, action);
480  sz = q->style()->sizeFromContents(QStyle::CT_MenuBarItem, &opt, sz, q);
481 
482  if(!sz.isEmpty()) {
483  { //update the separator state
484  int iWidth = sz.width() + itemSpacing;
485  if(separator == -1)
486  separator_start += iWidth;
487  else
488  separator_len += iWidth;
489  }
490  //maximum height
491  max_item_height = qMax(max_item_height, sz.height());
492  //append
493  actionRects[i] = QRect(0, 0, sz.width(), sz.height());
494  }
495  }
496 
497  //calculate position
498  const int fw = q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
499  int x = fw + ((start == -1) ? hmargin : start) + itemSpacing;
500  int y = fw + vmargin;
501  for(int i = 0; i < actions.count(); i++) {
502  QRect &rect = actionRects[i];
503  if (rect.isNull())
504  continue;
505 
506  //resize
507  rect.setHeight(max_item_height);
508 
509  //move
510  if(separator != -1 && i >= separator) { //after the separator
511  int left = (max_width - separator_len - hmargin - itemSpacing) + (x - separator_start - hmargin);
512  if(left < separator_start) { //wrap
513  separator_start = x = hmargin;
514  y += max_item_height;
515  }
516  rect.moveLeft(left);
517  } else {
518  rect.moveLeft(x);
519  }
520  rect.moveTop(y);
521 
522  //keep moving along..
523  x += rect.width() + itemSpacing;
524 
525  //make sure we follow the layout direction
526  rect = QStyle::visualRect(q->layoutDirection(), q->rect(), rect);
527  }
528 }
529 
531 {
532  Q_Q(QMenuBar);
533  if (!action || !action->isEnabled())
534  return;
535  action->activate(action_e);
536  if (action_e == QAction::Hover)
537  action->showStatusText(q);
538 
539 // if(action_e == QAction::Trigger)
540 // emit q->activated(action);
541 // else if(action_e == QAction::Hover)
542 // emit q->highlighted(action);
543 }
544 
545 
547 {
548  Q_Q(QMenuBar);
549  if (QAction *action = qobject_cast<QAction *>(q->sender())) {
550  emit q->triggered(action);
551 #ifdef QT3_SUPPORT
552  emit q->activated(q->findIdForAction(action));
553 #endif
554  }
555 }
556 
558 {
559  Q_Q(QMenuBar);
560  if (QAction *action = qobject_cast<QAction *>(q->sender())) {
561  emit q->hovered(action);
562 #ifndef QT_NO_ACCESSIBILITY
563  if (QAccessible::isActive()) {
564  int actionIndex = actions.indexOf(action);
565  ++actionIndex;
568  }
569 #endif //QT_NO_ACCESSIBILITY
570 #ifdef QT3_SUPPORT
571  emit q->highlighted(q->findIdForAction(action));
572 #endif
573  }
574 }
575 
583 void QMenuBar::initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const
584 {
585  if (!option || !action)
586  return;
587  Q_D(const QMenuBar);
588  option->palette = palette();
589  option->state = QStyle::State_None;
590  if (isEnabled() && action->isEnabled())
591  option->state |= QStyle::State_Enabled;
592  else
594  option->fontMetrics = fontMetrics();
595  if (d->currentAction && d->currentAction == action) {
596  option->state |= QStyle::State_Selected;
597  if (d->popupState && !d->closePopupMode)
598  option->state |= QStyle::State_Sunken;
599  }
600  if (hasFocus() || d->currentAction)
601  option->state |= QStyle::State_HasFocus;
602  option->menuRect = rect();
605  option->text = action->text();
606  option->icon = action->icon();
607 }
608 
750 {
751  Q_Q(QMenuBar);
753  q->setAttribute(Qt::WA_CustomWhatsThis);
754 #ifdef Q_WS_MAC
755  macCreateMenuBar(q->parentWidget());
756  if(mac_menubar)
757  q->hide();
758 #endif
759 #ifdef Q_WS_WINCE
760  if (qt_wince_is_mobile()) {
761  wceCreateMenuBar(q->parentWidget());
762  if(wce_menubar)
763  q->hide();
764  }
765  else {
767  }
768 #endif
769 #ifdef Q_WS_X11
770  platformMenuBar = qt_guiPlatformMenuBarFactory()->create();
771  platformMenuBar->init(q);
772 #endif
773 
774  q->setBackgroundRole(QPalette::Button);
775  oldWindow = oldParent = 0;
776 #ifdef QT3_SUPPORT
777  doAutoResize = false;
778 #endif
779 #ifdef QT_SOFTKEYS_ENABLED
780  menuBarAction = 0;
781 #endif
782 #ifdef Q_WS_X11
783  cornerWidgetToolBar = 0;
784  cornerWidgetContainer = 0;
785 #endif
786  handleReparent();
787  q->setMouseTracking(q->style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, q));
788 
789  extension = new QMenuBarExtension(q);
790  extension->setFocusPolicy(Qt::NoFocus);
791  extension->hide();
792 }
793 
794 //Gets the next action for keyboard navigation
795 QAction *QMenuBarPrivate::getNextAction(const int _start, const int increment) const
796 {
797  Q_Q(const QMenuBar);
798  const_cast<QMenuBarPrivate*>(this)->updateGeometries();
799  bool allowActiveAndDisabled = q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, q);
800  const int start = (_start == -1 && increment == -1) ? actions.count() : _start;
801  const int end = increment == -1 ? 0 : actions.count() - 1;
802 
803  for (int i = start; i != end;) {
804  i += increment;
805  QAction *current = actions.at(i);
806  if (!actionRects.at(i).isNull() && (allowActiveAndDisabled || current->isEnabled()))
807  return current;
808  }
809 
810  if (_start != -1) //let's try from the beginning or the end
811  return getNextAction(-1, increment);
812 
813  return 0;
814 }
815 
820 {
821  Q_D(QMenuBar);
822  d->init();
823 }
824 
825 #ifdef QT3_SUPPORT
826 
830 QMenuBar::QMenuBar(QWidget *parent, const char *name) : QWidget(*new QMenuBarPrivate, parent, 0)
831 {
832  Q_D(QMenuBar);
833  d->init();
835 }
836 #endif
837 
842 {
843 #ifdef Q_WS_MAC
844  Q_D(QMenuBar);
845  d->macDestroyMenuBar();
846 #endif
847 #ifdef Q_WS_WINCE
848  Q_D(QMenuBar);
849  if (qt_wince_is_mobile())
850  d->wceDestroyMenuBar();
851 #endif
852 #ifdef Q_WS_S60
853  Q_D(QMenuBar);
854  d->symbianDestroyMenuBar();
855 #endif
856 #ifdef Q_WS_X11
857  Q_D(QMenuBar);
858  delete d->cornerWidgetToolBar;
859 #endif
860 }
861 
875 {
876  QAction *ret = new QAction(text, this);
877  addAction(ret);
878  return ret;
879 }
880 
894 QAction *QMenuBar::addAction(const QString &text, const QObject *receiver, const char* member)
895 {
896  QAction *ret = new QAction(text, this);
897  QObject::connect(ret, SIGNAL(triggered(bool)), receiver, member);
898  addAction(ret);
899  return ret;
900 }
901 
909 {
910  QMenu *menu = new QMenu(title, this);
911  addAction(menu->menuAction());
912  return menu;
913 }
914 
921 QMenu *QMenuBar::addMenu(const QIcon &icon, const QString &title)
922 {
923  QMenu *menu = new QMenu(title, this);
924  menu->setIcon(icon);
925  addAction(menu->menuAction());
926  return menu;
927 }
928 
938 {
939  QAction *action = menu->menuAction();
940  addAction(action);
941  return action;
942 }
943 
948 {
949  QAction *ret = new QAction(this);
950  ret->setSeparator(true);
951  addAction(ret);
952  return ret;
953 }
954 
964 {
965  QAction *action = new QAction(this);
966  action->setSeparator(true);
967  insertAction(before, action);
968  return action;
969 }
970 
978 {
979  QAction *action = menu->menuAction();
980  insertAction(before, action);
981  return action;
982 }
983 
989 {
990  Q_D(const QMenuBar);
991  return d->currentAction;
992 }
993 
1003 {
1004  Q_D(QMenuBar);
1005  d->setCurrentAction(act, true, false);
1006 }
1007 
1008 
1022 {
1023  QList<QAction*> acts = actions();
1024  for(int i = 0; i < acts.size(); i++)
1025  removeAction(acts[i]);
1026 }
1027 
1044 {
1045  Q_D(QMenuBar);
1046  d->defaultPopDown = !b;
1047 }
1048 
1050 {
1051  Q_D(const QMenuBar);
1052  return !d->defaultPopDown;
1053 }
1054 
1059 {
1060  Q_D(QMenuBar);
1061  d->itemsDirty = true;
1062  d->updateGeometries();
1063 }
1064 
1069 {
1070  Q_D(QMenuBar);
1071  QPainter p(this);
1072  QRegion emptyArea(rect());
1073 
1074  //draw the items
1075  for (int i = 0; i < d->actions.count(); ++i) {
1076  QAction *action = d->actions.at(i);
1077  QRect adjustedActionRect = d->actionRect(action);
1078  if (adjustedActionRect.isEmpty() || !d->isVisible(action))
1079  continue;
1080  if(!e->rect().intersects(adjustedActionRect))
1081  continue;
1082 
1083  emptyArea -= adjustedActionRect;
1085  initStyleOption(&opt, action);
1086  opt.rect = adjustedActionRect;
1087  p.setClipRect(adjustedActionRect);
1088  style()->drawControl(QStyle::CE_MenuBarItem, &opt, &p, this);
1089  }
1090  //draw border
1091  if(int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this)) {
1092  QRegion borderReg;
1093  borderReg += QRect(0, 0, fw, height()); //left
1094  borderReg += QRect(width()-fw, 0, fw, height()); //right
1095  borderReg += QRect(0, 0, width(), fw); //top
1096  borderReg += QRect(0, height()-fw, width(), fw); //bottom
1097  p.setClipRegion(borderReg);
1098  emptyArea -= borderReg;
1099  QStyleOptionFrame frame;
1100  frame.rect = rect();
1101  frame.palette = palette();
1102  frame.state = QStyle::State_None;
1104  frame.midLineWidth = 0;
1105  style()->drawPrimitive(QStyle::PE_PanelMenuBar, &frame, &p, this);
1106  }
1107  p.setClipRegion(emptyArea);
1108  QStyleOptionMenuItem menuOpt;
1109  menuOpt.palette = palette();
1110  menuOpt.state = QStyle::State_None;
1113  menuOpt.rect = rect();
1114  menuOpt.menuRect = rect();
1115  style()->drawControl(QStyle::CE_MenuBarEmptyArea, &menuOpt, &p, this);
1116 }
1117 
1122 {
1123 #ifdef Q_WS_X11
1124  Q_D(QMenuBar);
1125  d->platformMenuBar->setVisible(visible);
1126 #else
1127 #if defined(Q_WS_MAC) || defined(Q_OS_WINCE) || defined(Q_WS_S60)
1128  if (isNativeMenuBar()) {
1129 #ifndef Q_WS_S60
1130  if (!visible)
1131  QWidget::setVisible(false);
1132 #endif
1133  return;
1134  }
1135 #endif
1136  QWidget::setVisible(visible);
1137 #endif // Q_WS_X11
1138 }
1139 
1144 {
1145  Q_D(QMenuBar);
1146  if(e->button() != Qt::LeftButton)
1147  return;
1148 
1149  d->mouseDown = true;
1150 
1151  QAction *action = d->actionAt(e->pos());
1152  if (!action || !d->isVisible(action)) {
1153  d->setCurrentAction(0);
1154 #ifndef QT_NO_WHATSTHIS
1156  QWhatsThis::showText(e->globalPos(), d->whatsThis, this);
1157 #endif
1158  return;
1159  }
1160 
1161  if(d->currentAction == action && d->popupState) {
1162  if(QMenu *menu = d->activeMenu) {
1163  d->activeMenu = 0;
1164  menu->hide();
1165  }
1166 #ifdef Q_WS_WIN
1167  if((d->closePopupMode = style()->styleHint(QStyle::SH_MenuBar_DismissOnSecondClick)))
1168  update(d->actionRect(action));
1169 #endif
1170  } else {
1171  d->setCurrentAction(action, true);
1172  }
1173 }
1174 
1179 {
1180  Q_D(QMenuBar);
1181  if(e->button() != Qt::LeftButton || !d->mouseDown)
1182  return;
1183 
1184  d->mouseDown = false;
1185  QAction *action = d->actionAt(e->pos());
1186  if((d->closePopupMode && action == d->currentAction) || !action || !action->menu()) {
1187  //we set the current action before activating
1188  //so that we let the leave event set the current back to 0
1189  d->setCurrentAction(action, false);
1190  if(action)
1191  d->activateAction(action, QAction::Trigger);
1192  }
1193  d->closePopupMode = 0;
1194 }
1195 
1200 {
1201  Q_D(QMenuBar);
1202  d->updateGeometries();
1203  int key = e->key();
1204  if(isRightToLeft()) { // in reverse mode open/close key for submenues are reversed
1205  if(key == Qt::Key_Left)
1206  key = Qt::Key_Right;
1207  else if(key == Qt::Key_Right)
1208  key = Qt::Key_Left;
1209  }
1210  if(key == Qt::Key_Tab) //means right
1211  key = Qt::Key_Right;
1212  else if(key == Qt::Key_Backtab) //means left
1213  key = Qt::Key_Left;
1214 
1215  bool key_consumed = false;
1216  switch(key) {
1217  case Qt::Key_Up:
1218  case Qt::Key_Down:
1219  case Qt::Key_Enter:
1220  case Qt::Key_Space:
1221  case Qt::Key_Return: {
1222  if(!style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this) || !d->currentAction)
1223  break;
1224  if(d->currentAction->menu()) {
1225  d->popupAction(d->currentAction, true);
1226  } else if(key == Qt::Key_Enter || key == Qt::Key_Return || key == Qt::Key_Space) {
1227  d->activateAction(d->currentAction, QAction::Trigger);
1228  d->setCurrentAction(d->currentAction, false);
1229  d->setKeyboardMode(false);
1230  }
1231  key_consumed = true;
1232  break; }
1233 
1234  case Qt::Key_Right:
1235  case Qt::Key_Left: {
1236  if(d->currentAction) {
1237  int index = d->actions.indexOf(d->currentAction);
1238  if (QAction *nextAction = d->getNextAction(index, key == Qt::Key_Left ? -1 : +1)) {
1239  d->setCurrentAction(nextAction, d->popupState, true);
1240  key_consumed = true;
1241  }
1242  }
1243  break; }
1244 
1245  case Qt::Key_Escape:
1246  d->setCurrentAction(0);
1247  d->setKeyboardMode(false);
1248  key_consumed = true;
1249  break;
1250 
1251  default:
1252  key_consumed = false;
1253  }
1254 
1255  if(!key_consumed &&
1256  (!e->modifiers() ||
1257  (e->modifiers()&(Qt::MetaModifier|Qt::AltModifier))) && e->text().length()==1 && !d->popupState) {
1258  int clashCount = 0;
1259  QAction *first = 0, *currentSelected = 0, *firstAfterCurrent = 0;
1260  {
1261  QChar c = e->text()[0].toUpper();
1262  for(int i = 0; i < d->actions.size(); ++i) {
1263  if (d->actionRects.at(i).isNull())
1264  continue;
1265  QAction *act = d->actions.at(i);
1266  QString s = act->text();
1267  if(!s.isEmpty()) {
1268  int ampersand = s.indexOf(QLatin1Char('&'));
1269  if(ampersand >= 0) {
1270  if(s[ampersand+1].toUpper() == c) {
1271  clashCount++;
1272  if(!first)
1273  first = act;
1274  if(act == d->currentAction)
1275  currentSelected = act;
1276  else if (!firstAfterCurrent && currentSelected)
1277  firstAfterCurrent = act;
1278  }
1279  }
1280  }
1281  }
1282  }
1283  QAction *next_action = 0;
1284  if(clashCount >= 1) {
1285  if(clashCount == 1 || !d->currentAction || (currentSelected && !firstAfterCurrent))
1286  next_action = first;
1287  else
1288  next_action = firstAfterCurrent;
1289  }
1290  if(next_action) {
1291  key_consumed = true;
1292  d->setCurrentAction(next_action, true, true);
1293  }
1294  }
1295  if(key_consumed)
1296  e->accept();
1297  else
1298  e->ignore();
1299 }
1300 
1305 {
1306  Q_D(QMenuBar);
1307  if (!(e->buttons() & Qt::LeftButton))
1308  d->mouseDown = false;
1309  bool popupState = d->popupState || d->mouseDown;
1310  QAction *action = d->actionAt(e->pos());
1311  if ((action && d->isVisible(action)) || !popupState)
1312  d->setCurrentAction(action, popupState);
1313 }
1314 
1319 {
1320  Q_D(QMenuBar);
1321  if((!hasFocus() && !d->popupState) ||
1322  (d->currentAction && d->currentAction->menu() == 0))
1323  d->setCurrentAction(0);
1324 }
1325 
1330 {
1331  Q_D(QMenuBar);
1332  d->itemsDirty = true;
1333 #ifdef Q_WS_X11
1334  d->platformMenuBar->actionEvent(e);
1335 #endif
1336 #if defined (Q_WS_MAC) || defined(Q_OS_WINCE) || defined(Q_WS_S60)
1337  if (isNativeMenuBar()) {
1338 #ifdef Q_WS_MAC
1340 #elif defined(Q_WS_S60)
1341  QMenuBarPrivate::QSymbianMenuBarPrivate *nativeMenuBar = d->symbian_menubar;
1342 #else
1343  QMenuBarPrivate::QWceMenuBarPrivate *nativeMenuBar = d->wce_menubar;
1344 #endif
1345  if (!nativeMenuBar)
1346  return;
1347  if(e->type() == QEvent::ActionAdded)
1348  nativeMenuBar->addAction(e->action(), e->before());
1349  else if(e->type() == QEvent::ActionRemoved)
1350  nativeMenuBar->removeAction(e->action());
1351  else if(e->type() == QEvent::ActionChanged)
1352  nativeMenuBar->syncAction(e->action());
1353  }
1354 #endif
1355 
1356  if(e->type() == QEvent::ActionAdded) {
1357  connect(e->action(), SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
1358  connect(e->action(), SIGNAL(hovered()), this, SLOT(_q_actionHovered()));
1359  } else if(e->type() == QEvent::ActionRemoved) {
1360  e->action()->disconnect(this);
1361  }
1362  if (isVisible()) {
1363  d->updateGeometries();
1364  update();
1365  }
1366 }
1367 
1372 {
1373  Q_D(QMenuBar);
1374  if(d->keyboardState)
1375  d->focusFirstAction();
1376 }
1377 
1382 {
1383  Q_D(QMenuBar);
1384  if(!d->popupState) {
1385  d->setCurrentAction(0);
1386  d->setKeyboardMode(false);
1387  }
1388 }
1389 
1394 {
1395  Q_D(QMenuBar);
1396  if (e->timerId() == d->autoReleaseTimer.timerId()) {
1397  d->autoReleaseTimer.stop();
1398  d->setCurrentAction(0);
1399  }
1401 }
1402 
1403 
1405 {
1406  Q_Q(QMenuBar);
1407  QWidget *newParent = q->parentWidget();
1408  //Note: if parent is reparented, then window may change even if parent doesn't
1409 
1410  // we need to install an event filter on parent, and remove the old one
1411 
1412  if (oldParent != newParent) {
1413  if (oldParent)
1414  oldParent->removeEventFilter(q);
1415  if (newParent)
1416  newParent->installEventFilter(q);
1417  }
1418 
1419  //we also need event filter on top-level (for shortcuts)
1420  QWidget *newWindow = newParent ? newParent->window() : 0;
1421 
1422  if (oldWindow != newWindow) {
1423  if (oldParent && oldParent != oldWindow)
1424  oldWindow->removeEventFilter(q);
1425 
1426  if (newParent && newParent != newWindow)
1427  newWindow->installEventFilter(q);
1428  }
1429 
1430 #ifdef Q_WS_X11
1431  platformMenuBar->handleReparent(oldParent, newParent, oldWindow, newWindow);
1432 #endif
1433 
1434  oldParent = newParent;
1435  oldWindow = newWindow;
1436 
1437 #ifdef Q_WS_MAC
1438  if (q->isNativeMenuBar() && !macWidgetHasNativeMenubar(newParent)) {
1439  // If the new parent got a native menubar from before, keep that
1440  // menubar rather than replace it with this one (because a parents
1441  // menubar has precedence over children menubars).
1442  macDestroyMenuBar();
1443  macCreateMenuBar(newParent);
1444  }
1445 #endif
1446 
1447 #ifdef Q_WS_WINCE
1448  if (qt_wince_is_mobile() && wce_menubar)
1449  wce_menubar->rebuild();
1450 #endif
1451 #ifdef Q_WS_S60
1452 
1453  // Construct symbian_menubar when this code path is entered first time
1454  // and when newParent != NULL
1455  if (!symbian_menubar)
1456  symbianCreateMenuBar(newParent);
1457 
1458  // Reparent and rebuild menubar when parent is changed
1459  if (symbian_menubar) {
1460  if (oldParent != newParent)
1461  reparentMenuBar(oldParent, newParent);
1462  q->hide();
1463  symbian_menubar->rebuild();
1464  }
1465 
1466 #ifdef QT_SOFTKEYS_ENABLED
1467  // Constuct menuBarAction when this code path is entered first time
1468  if (!menuBarAction) {
1469  if (newParent) {
1470  menuBarAction = QSoftKeyManager::createAction(QSoftKeyManager::MenuSoftKey, newParent);
1471  newParent->addAction(menuBarAction);
1472  }
1473  } else {
1474  // If reparenting i.e. we already have menuBarAction, remove it from old parent
1475  // and add for a new parent
1476  if (oldParent)
1477  oldParent->removeAction(menuBarAction);
1478  if (newParent)
1479  newParent->addAction(menuBarAction);
1480  }
1481 #endif // QT_SOFTKEYS_ENABLED
1482 #endif // Q_WS_S60
1483 }
1484 
1485 #ifdef QT3_SUPPORT
1486 
1495 void QMenuBar::setAutoGeometry(bool b)
1496 {
1497  Q_D(QMenuBar);
1498  d->doAutoResize = b;
1499 }
1500 
1510 bool QMenuBar::autoGeometry() const
1511 {
1512  Q_D(const QMenuBar);
1513  return d->doAutoResize;
1514 }
1515 #endif
1516 
1521 {
1522  Q_D(QMenuBar);
1523  if(e->type() == QEvent::StyleChange) {
1524  d->itemsDirty = true;
1526  if(parentWidget())
1528  d->updateGeometries();
1529  } else if (e->type() == QEvent::ParentChange) {
1530  d->handleReparent();
1531  } else if (e->type() == QEvent::FontChange
1532  || e->type() == QEvent::ApplicationFontChange) {
1533  d->itemsDirty = true;
1534  d->updateGeometries();
1535 #ifdef QT_SOFTKEYS_ENABLED
1536  } else if (e->type() == QEvent::LanguageChange) {
1537  if (d->menuBarAction)
1539 #endif
1540  }
1541 
1543 }
1544 
1549 {
1550  Q_D(QMenuBar);
1551  switch (e->type()) {
1552  case QEvent::KeyPress: {
1553  QKeyEvent *ke = (QKeyEvent*)e;
1554 #if 0
1555  if(!d->keyboardState) { //all keypresses..
1556  d->setCurrentAction(0);
1557  return ;
1558  }
1559 #endif
1560  if(ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
1561  keyPressEvent(ke);
1562  return true;
1563  }
1564 
1565  } break;
1566 #ifndef QT_NO_SHORTCUT
1567  case QEvent::Shortcut: {
1568  QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
1569  int shortcutId = se->shortcutId();
1570  for(int j = 0; j < d->shortcutIndexMap.size(); ++j) {
1571  if (shortcutId == d->shortcutIndexMap.value(j))
1572  d->_q_internalShortcutActivated(j);
1573  }
1574  } break;
1575 #endif
1576  case QEvent::Show:
1577 #ifdef QT3_SUPPORT
1578  if(QWidget *p = parentWidget()) {
1579  // If itemsDirty == true, updateGeometries sends the MenubarUpdated event.
1580  if (!d->itemsDirty) {
1581  QMenubarUpdatedEvent menubarUpdated(this);
1582  QApplication::sendEvent(p, &menubarUpdated);
1583  }
1584  }
1585 #endif
1586  d->_q_updateLayout();
1587  break;
1588  case QEvent::ShortcutOverride: {
1589  QKeyEvent *kev = static_cast<QKeyEvent*>(e);
1590  //we only filter out escape if there is a current action
1591  if (kev->key() == Qt::Key_Escape && d->currentAction) {
1592  e->accept();
1593  return true;
1594  }
1595  }
1596  break;
1597 
1598 #ifdef QT3_SUPPORT
1599  case QEvent::Hide: {
1600  if(QWidget *p = parentWidget()) {
1601  QMenubarUpdatedEvent menubarUpdated(this);
1602  QApplication::sendEvent(p, &menubarUpdated);
1603  }
1604  } break;
1605 #endif
1606 
1607 #ifndef QT_NO_WHATSTHIS
1609  e->setAccepted(d->whatsThis.size());
1610  if (QAction *action = d->actionAt(static_cast<QHelpEvent*>(e)->pos())) {
1611  if (action->whatsThis().size() || action->menu())
1612  e->accept();
1613  }
1614  return true;
1615 #endif
1617  d->_q_updateLayout();
1618  break;
1619  default:
1620  break;
1621  }
1622  return QWidget::event(e);
1623 }
1624 
1629 {
1630  Q_D(QMenuBar);
1631 #ifdef Q_WS_X11
1632  if (d->platformMenuBar->menuBarEventFilter(object, event)) {
1633  return true;
1634  }
1635 #endif
1636  if (object == parent() && object) {
1637 #ifdef QT3_SUPPORT
1638  if (d->doAutoResize && event->type() == QEvent::Resize) {
1639  QResizeEvent *e = (QResizeEvent *)event;
1640  int w = e->size().width();
1641  setGeometry(0, y(), w, heightForWidth(w));
1642  return false;
1643  }
1644 #endif
1645  if (event->type() == QEvent::ParentChange) //GrandparentChange
1646  d->handleReparent();
1647  }
1648  if (object == d->leftWidget || object == d->rightWidget) {
1649  switch (event->type()) {
1650  case QEvent::ShowToParent:
1651  case QEvent::HideToParent:
1652  d->_q_updateLayout();
1653  break;
1654  default:
1655  break;
1656  }
1657  }
1658 
1660  if (d->altPressed) {
1661  switch (event->type()) {
1662  case QEvent::KeyPress:
1663  case QEvent::KeyRelease:
1664  {
1665  QKeyEvent *kev = static_cast<QKeyEvent*>(event);
1666  if (kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta) {
1667  if (event->type() == QEvent::KeyPress) // Alt-press does not interest us, we have the shortcut-override event
1668  break;
1669  d->setKeyboardMode(!d->keyboardState);
1670  }
1671  }
1672  // fall through
1675  case QEvent::MouseMove:
1676  case QEvent::FocusIn:
1677  case QEvent::FocusOut:
1679  d->altPressed = false;
1680  qApp->removeEventFilter(this);
1681  break;
1682  default:
1683  break;
1684  }
1685  } else if (isVisible()) {
1686  if (event->type() == QEvent::ShortcutOverride) {
1687  QKeyEvent *kev = static_cast<QKeyEvent*>(event);
1688  if ((kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta)
1689  && kev->modifiers() == Qt::AltModifier) {
1690  d->altPressed = true;
1691  qApp->installEventFilter(this);
1692  }
1693  }
1694  }
1695  }
1696 
1697  return false;
1698 }
1699 
1707 {
1708  Q_D(const QMenuBar);
1709  return d->actionAt(pt);
1710 }
1711 
1718 {
1719  Q_D(const QMenuBar);
1720  return d->actionRect(act);
1721 }
1722 
1727 {
1728  Q_D(const QMenuBar);
1729 #if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60) || defined(Q_WS_X11)
1730  const bool as_gui_menubar = !isNativeMenuBar();
1731 #else
1732  const bool as_gui_menubar = true;
1733 #endif
1734 
1735  ensurePolished();
1736  QSize ret(0, 0);
1737  const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1738  const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this);
1739  const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
1740  int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
1741  int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
1742  if(as_gui_menubar) {
1743  int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width();
1744  d->calcActionRects(w - (2 * fw), 0);
1745  for (int i = 0; ret.isNull() && i < d->actions.count(); ++i)
1746  ret = d->actionRects.at(i).size();
1747  if (!d->extension->isHidden())
1748  ret += QSize(d->extension->sizeHint().width(), 0);
1749  ret += QSize(2*fw + hmargin, 2*fw + vmargin);
1750  }
1751  int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1752 #ifdef Q_WS_X11
1753  if (d->platformMenuBar->allowCornerWidgets()) {
1754 #endif
1755  if(d->leftWidget) {
1756  QSize sz = d->leftWidget->minimumSizeHint();
1757  ret.setWidth(ret.width() + sz.width());
1758  if(sz.height() + margin > ret.height())
1759  ret.setHeight(sz.height() + margin);
1760  }
1761  if(d->rightWidget) {
1762  QSize sz = d->rightWidget->minimumSizeHint();
1763  ret.setWidth(ret.width() + sz.width());
1764  if(sz.height() + margin > ret.height())
1765  ret.setHeight(sz.height() + margin);
1766  }
1767 #ifdef Q_WS_X11
1768  }
1769 #endif
1770  if(as_gui_menubar) {
1772  opt.rect = rect();
1773  opt.menuRect = rect();
1774  opt.state = QStyle::State_None;
1777  opt.palette = palette();
1778  return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt,
1780  this));
1781  }
1782  return ret;
1783 }
1784 
1789 {
1790  Q_D(const QMenuBar);
1791 #if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60) || defined(Q_WS_X11)
1792  const bool as_gui_menubar = !isNativeMenuBar();
1793 #else
1794  const bool as_gui_menubar = true;
1795 #endif
1796 
1797 
1798  ensurePolished();
1799  QSize ret(0, 0);
1800  const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1801  const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this);
1802  const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
1803  int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
1804  int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
1805  if(as_gui_menubar) {
1806  const int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width();
1807  d->calcActionRects(w - (2 * fw), 0);
1808  for (int i = 0; i < d->actionRects.count(); ++i) {
1809  const QRect &actionRect = d->actionRects.at(i);
1810  ret = ret.expandedTo(QSize(actionRect.x() + actionRect.width(), actionRect.y() + actionRect.height()));
1811  }
1812  //the action geometries already contain the top and left
1813  //margins. So we only need to add those from right and bottom.
1814  ret += QSize(fw + hmargin, fw + vmargin);
1815  }
1816  int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1817 #ifdef Q_WS_X11
1818  if(d->platformMenuBar->allowCornerWidgets()) {
1819 #endif
1820  if(d->leftWidget) {
1821  QSize sz = d->leftWidget->sizeHint();
1822  ret.setWidth(ret.width() + sz.width());
1823  if(sz.height() + margin > ret.height())
1824  ret.setHeight(sz.height() + margin);
1825  }
1826  if(d->rightWidget) {
1827  QSize sz = d->rightWidget->sizeHint();
1828  ret.setWidth(ret.width() + sz.width());
1829  if(sz.height() + margin > ret.height())
1830  ret.setHeight(sz.height() + margin);
1831  }
1832 #ifdef Q_WS_X11
1833  }
1834 #endif
1835  if(as_gui_menubar) {
1837  opt.rect = rect();
1838  opt.menuRect = rect();
1839  opt.state = QStyle::State_None;
1842  opt.palette = palette();
1843  return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt,
1845  this));
1846  }
1847  return ret;
1848 }
1849 
1854 {
1855  Q_D(const QMenuBar);
1856 #if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60) || defined(Q_WS_X11)
1857  const bool as_gui_menubar = !isNativeMenuBar();
1858 #else
1859  const bool as_gui_menubar = true;
1860 #endif
1861 
1862  const_cast<QMenuBarPrivate*>(d)->updateGeometries();
1863  int height = 0;
1864  const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this);
1865  int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
1866  int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this);
1867  if(as_gui_menubar) {
1868  for (int i = 0; i < d->actionRects.count(); ++i)
1869  height = qMax(height, d->actionRects.at(i).height());
1870  if (height) //there is at least one non-null item
1871  height += spaceBelowMenuBar;
1872  height += 2*fw;
1873  height += 2*vmargin;
1874  }
1875  int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
1876 #ifdef Q_WS_X11
1877  if(d->platformMenuBar->allowCornerWidgets()) {
1878 #endif
1879  if(d->leftWidget)
1880  height = qMax(d->leftWidget->sizeHint().height() + margin, height);
1881  if(d->rightWidget)
1882  height = qMax(d->rightWidget->sizeHint().height() + margin, height);
1883 #ifdef Q_WS_X11
1884  }
1885 #endif
1886  if(as_gui_menubar) {
1888  opt.init(this);
1889  opt.menuRect = rect();
1890  opt.state = QStyle::State_None;
1893  return style()->sizeFromContents(QStyle::CT_MenuBar, &opt, QSize(0, height), this).height(); //not pretty..
1894  }
1895  return height;
1896 }
1897 
1902 {
1903  Q_Q(QMenuBar);
1904  QAction *act = actions.at(id);
1905 #ifdef Q_WS_X11
1906  if (q->isNativeMenuBar()) {
1907  platformMenuBar->popupAction(act);
1908  } else {
1909 #endif
1910  setCurrentAction(act, true, true);
1911 #ifdef Q_WS_X11
1912  }
1913 #endif
1914  if (act && !act->menu()) {
1915  activateAction(act, QAction::Trigger);
1916  //100 is the same as the default value in QPushButton::animateClick
1917  autoReleaseTimer.start(100, q);
1918  } else if (act && q->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, q)) {
1919  // When we open a menu using a shortcut, we should end up in keyboard state
1920  setKeyboardMode(true);
1921  }
1922 }
1923 
1925 {
1926  Q_Q(QMenuBar);
1927  itemsDirty = true;
1928  if (q->isVisible()) {
1929  updateGeometries();
1930  q->update();
1931  }
1932 }
1933 
1934 #ifdef Q_WS_X11
1936 {
1937  Q_Q(QMenuBar);
1938  if (!cornerWidgetToolBar) {
1939  QMainWindow *window = qobject_cast<QMainWindow *>(q->window());
1940  if (!window) {
1941  qWarning() << "Menubar parent is not a QMainWindow, not showing corner widgets";
1942  return;
1943  }
1944  cornerWidgetToolBar = window->addToolBar(QApplication::translate("QMenuBar", "Corner Toolbar"));
1945  cornerWidgetToolBar->setObjectName(QLatin1String("CornerToolBar"));
1946  cornerWidgetContainer = new QWidget;
1947  cornerWidgetToolBar->addWidget(cornerWidgetContainer);
1948  new QHBoxLayout(cornerWidgetContainer);
1949  } else {
1950  QLayout *layout = cornerWidgetContainer->layout();
1951  while (layout->count() > 0) {
1952  layout->takeAt(0);
1953  }
1954  }
1955  if (leftWidget) {
1956  leftWidget->setParent(cornerWidgetContainer);
1957  cornerWidgetContainer->layout()->addWidget(leftWidget);
1958  }
1959  if (rightWidget) {
1960  rightWidget->setParent(cornerWidgetContainer);
1961  cornerWidgetContainer->layout()->addWidget(rightWidget);
1962  }
1963 }
1964 #endif
1965 
1966 
1981 {
1982  Q_D(QMenuBar);
1983  switch (corner) {
1984  case Qt::TopLeftCorner:
1985  if (d->leftWidget)
1986  d->leftWidget->removeEventFilter(this);
1987  d->leftWidget = w;
1988  break;
1989  case Qt::TopRightCorner:
1990  if (d->rightWidget)
1991  d->rightWidget->removeEventFilter(this);
1992  d->rightWidget = w;
1993  break;
1994  default:
1995  qWarning("QMenuBar::setCornerWidget: Only TopLeftCorner and TopRightCorner are supported");
1996  return;
1997  }
1998 
1999 #ifdef Q_WS_X11
2000  if(!d->platformMenuBar->allowCornerWidgets()) {
2001  d->updateCornerWidgetToolBar();
2002  } else {
2003 #endif
2004  if (w) {
2005  w->setParent(this);
2006  w->installEventFilter(this);
2007  }
2008 #ifdef Q_WS_X11
2009  }
2010 #endif
2011 
2012  d->_q_updateLayout();
2013 }
2014 
2023 {
2024  Q_D(const QMenuBar);
2025  QWidget *w = 0;
2026  switch(corner) {
2027  case Qt::TopLeftCorner:
2028  w = d->leftWidget;
2029  break;
2030  case Qt::TopRightCorner:
2031  w = d->rightWidget;
2032  break;
2033  default:
2034  qWarning("QMenuBar::cornerWidget: Only TopLeftCorner and TopRightCorner are supported");
2035  break;
2036  }
2037 
2038  return w;
2039 }
2040 
2063 {
2064  Q_D(QMenuBar);
2065 #ifdef Q_WS_X11
2066  d->platformMenuBar->setNativeMenuBar(nativeMenuBar);
2067 #else
2068  if (d->nativeMenuBar == -1 || (nativeMenuBar != bool(d->nativeMenuBar))) {
2069  d->nativeMenuBar = nativeMenuBar;
2070 #ifdef Q_WS_MAC
2071  if (!d->nativeMenuBar) {
2072  extern void qt_mac_clear_menubar();
2074  d->macDestroyMenuBar();
2075  const QList<QAction *> &menubarActions = actions();
2076  for (int i = 0; i < menubarActions.size(); ++i) {
2077  const QAction *action = menubarActions.at(i);
2078  if (QMenu *menu = action->menu()) {
2079  delete menu->d_func()->mac_menu;
2080  menu->d_func()->mac_menu = 0;
2081  }
2082  }
2083  } else {
2084  d->macCreateMenuBar(parentWidget());
2085  }
2086  macUpdateMenuBar();
2087  updateGeometry();
2088  if (!d->nativeMenuBar && parentWidget())
2089  setVisible(true);
2090 #endif
2091  }
2092 #endif // Q_WS_X11
2093 }
2094 
2096 {
2097  Q_D(const QMenuBar);
2098 #ifdef Q_WS_X11
2099  return d->platformMenuBar->isNativeMenuBar();
2100 #else
2101  if (d->nativeMenuBar == -1) {
2103  }
2104  return d->nativeMenuBar;
2105 #endif
2106 }
2107 
2125 #ifdef Q_WS_WINCE
2127 {
2128  Q_D(QMenuBar);
2129  if (d->defaultAction == act)
2130  return;
2131 #ifdef Q_WS_WINCE
2132  if (qt_wince_is_mobile())
2133  if (d->defaultAction) {
2134  disconnect(d->defaultAction, SIGNAL(changed()), this, SLOT(_q_updateDefaultAction()));
2135  disconnect(d->defaultAction, SIGNAL(destroyed()), this, SLOT(_q_updateDefaultAction()));
2136  }
2137 #endif
2138  d->defaultAction = act;
2139 #ifdef Q_WS_WINCE
2140  if (qt_wince_is_mobile())
2141  if (d->defaultAction) {
2142  connect(d->defaultAction, SIGNAL(changed()), this, SLOT(_q_updateDefaultAction()));
2143  connect(d->defaultAction, SIGNAL(destroyed()), this, SLOT(_q_updateDefaultAction()));
2144  }
2145  if (d->wce_menubar) {
2146  d->wce_menubar->rebuild();
2147  }
2148 #endif
2149 }
2150 
2162 {
2163  return d_func()->defaultAction;
2164 }
2165 #endif
2166 
2196 #ifdef QT3_SUPPORT
2197 
2201 int QMenuBar::frameWidth() const
2202 {
2203  return style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this);
2204 }
2205 
2206 int QMenuBar::insertAny(const QIcon *icon, const QString *text, const QObject *receiver, const char *member,
2207  const QKeySequence *shortcut, const QMenu *popup, int id, int index)
2208 {
2209  QAction *act = popup ? popup->menuAction() : new QAction(this);
2210  if(id != -1)
2211  static_cast<QMenuItem*>(act)->setId(id);
2212  if(icon)
2213  act->setIcon(*icon);
2214  if(text)
2215  act->setText(*text);
2216  if(shortcut)
2217  act->setShortcut(*shortcut);
2218  if(receiver && member)
2219  QObject::connect(act, SIGNAL(triggered(bool)), receiver, member);
2220  if(index == -1 || index >= actions().count())
2221  addAction(act);
2222  else
2223  insertAction(actions().value(index), act);
2224  return findIdForAction(act);
2225 }
2226 
2242 {
2243  QAction *act = new QAction(this);
2244  act->setSeparator(true);
2245  if(index == -1 || index >= actions().count())
2246  addAction(act);
2247  else
2248  insertAction(actions().value(index), act);
2249  return findIdForAction(act);
2250 }
2251 
2255 bool QMenuBar::setItemParameter(int id, int param)
2256 {
2257  if(QAction *act = findActionForId(id)) {
2258  act->d_func()->param = param;
2259  return true;
2260  }
2261  return false;
2262 }
2263 
2267 int QMenuBar::itemParameter(int id) const
2268 {
2269  if(QAction *act = findActionForId(id))
2270  return act->d_func()->param;
2271  return id;
2272 }
2273 
2274 QAction *QMenuBar::findActionForId(int id) const
2275 {
2276  QList<QAction *> list = actions();
2277  for (int i = 0; i < list.size(); ++i) {
2278  QAction *act = list.at(i);
2279  if (findIdForAction(act) == id)
2280  return act;
2281  }
2282  return 0;
2283 }
2284 
2285 int QMenuBar::findIdForAction(QAction *act) const
2286 {
2287  Q_ASSERT(act);
2288  return act->d_func()->id;
2289 }
2290 #endif
2291 
2761 // for private slots
2762 
2763 
2765 
2766 #include <moc_qmenubar.cpp>
2767 
2768 #endif // QT_NO_MENUBAR
bool isSeparator() const
Returns true if this action is a separator action; otherwise it returns false.
Definition: qaction.cpp:839
void setKeyboardMode(bool)
Definition: qmenubar.cpp:313
QPoint pos() const
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
QRect menuRect
the rectangle for the entire menu
Definition: qstyleoption.h:449
QAction * before() const
If type() is ActionAdded , returns the action that should appear before action(). ...
Definition: qevent.h:640
double d
Definition: qnumeric_p.h:62
bool eventFilter(QObject *, QEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1628
bool qt_wince_is_mobile()
static void updateAccessibility(QObject *, int who, Event reason)
Notifies accessibility clients about a change in object&#39;s accessibility information.
QWidget * parentWidget() const
Returns the parent of this widget, or 0 if it does not have any parent widget.
Definition: qwidget.h:1035
bool isEnabled() const
Definition: qaction.cpp:1208
void setDefaultUp(bool)
Definition: qmenubar.cpp:1043
~QMenuBar()
Destroys the menu bar.
Definition: qmenubar.cpp:841
The QKeyEvent class describes a key event.
Definition: qevent.h:224
QWidget * focusWidget() const
Returns the last child of this widget that setFocus had been called on.
Definition: qwidget.cpp:6863
void resizeEvent(QResizeEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1058
bool isNull() const
Returns true if the rectangle is a null rectangle, otherwise returns false.
Definition: qrect.h:231
void setShortcut(const QKeySequence &shortcut)
Definition: qaction.cpp:450
void setHeight(int h)
Sets the height of the rectangle to the given height.
Definition: qrect.h:445
static QString standardSoftKeyText(StandardSoftKey standardKey)
QRect actionRect(QAction *) const
Definition: qmenubar.cpp:289
QMenuBarExtension(QWidget *parent)
Definition: qmenubar.cpp:95
int y() const
unsigned char c[8]
Definition: qnumeric_p.h:62
The QFontMetrics class provides font metrics information.
Definition: qfontmetrics.h:65
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
void syncAction(QWceMenuAction *)
void setNativeMenuBar(bool nativeMenuBar)
Definition: qmenubar.cpp:2062
QMenuBar(QWidget *parent=0)
Constructs a menu bar with parent parent.
Definition: qmenubar.cpp:819
void moveLeft(int pos)
Moves the rectangle horizontally, leaving the rectangle&#39;s left edge at the given x coordinate...
Definition: qrect.h:350
bool isVisible(QAction *action)
Definition: qmenubar.cpp:170
int width
the width of the widget excluding any window frame
Definition: qwidget.h:166
bool showStatusText(QWidget *widget=0)
Updates the relevant status bar for the widget specified by sending a QStatusTipEvent to its parent w...
Definition: qaction.cpp:1315
void focusFirstAction()
Definition: qmenubar.cpp:302
void activate(ActionEvent event)
Sends the relevant signals for ActionEvent event.
Definition: qaction.cpp:1326
void setSeparator(bool b)
If b is true then this action will be considered a separator.
Definition: qaction.cpp:823
void ensurePolished() const
Ensures that the widget has been polished by QStyle (i.e., has a proper font and palette).
Definition: qwidget.cpp:10024
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
The QStyleOptionMenuItem class is used to describe the parameter necessary for drawing a menu item...
Definition: qstyleoption.h:435
QIcon icon
the icon for the menu item
Definition: qstyleoption.h:451
virtual QAbstractPlatformMenuBar * create()=0
bool isVisible() const
Definition: qwidget.h:1005
ActionEvent
This enum type is used when calling QAction::activate()
Definition: qaction.h:176
static QSize globalStrut()
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
static QKeySequence mnemonic(const QString &text)
Returns the shortcut key sequence for the mnemonic in text, or an empty key sequence if no mnemonics ...
void setClipRect(const QRectF &, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip region to the given rectangle using the given clip operation...
Definition: qpainter.cpp:2801
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
void setAccepted(bool accepted)
Definition: qcoreevent.h:306
static void setAttribute(Qt::ApplicationAttribute attribute, bool on=true)
Sets the attribute attribute if on is true; otherwise clears the attribute.
QString toUpper() const Q_REQUIRED_RESULT
Returns an uppercase copy of the string.
Definition: qstring.cpp:5483
QString text
the action&#39;s descriptive text
Definition: qaction.h:76
#define SLOT(a)
Definition: qobjectdefs.h:226
void removeEventFilter(QObject *)
Removes an event filter object obj from this object.
Definition: qobject.cpp:2099
QAction * insertMenu(QAction *before, QMenu *menu)
This convenience function inserts menu before action before and returns the menus menuAction()...
Definition: qmenubar.cpp:977
virtual void timerEvent(QTimerEvent *)
This event handler can be reimplemented in a subclass to receive timer events for the object...
Definition: qobject.cpp:1294
void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const
Initialize option with the values from the menu bar and information from action.
Definition: qmenubar.cpp:583
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void actionEvent(QActionEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1329
virtual int styleHint(StyleHint stylehint, const QStyleOption *opt=0, const QWidget *widget=0, QStyleHintReturn *returnData=0) const =0
Returns an integer representing the specified style hint for the given widget described by the provid...
The QShortcutEvent class provides an event which is generated when the user presses a key combination...
Definition: qevent.h:675
QSize expandedTo(const QSize &) const
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition: qsize.h:187
int left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:240
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
void init(const QWidget *w)
Use initFrom(widget) instead.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QAction * defaultAction() const
Returns the current default action.
Definition: qmenubar.cpp:2161
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QActionEvent class provides an event that is generated when a QAction is added, removed, or changed.
Definition: qevent.h:632
QAction * addSeparator()
Appends a separator to the menu.
Definition: qmenubar.cpp:947
void timerEvent(QTimerEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1393
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qwidget.h:1017
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
int bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:249
The QString class provides a Unicode character string.
Definition: qstring.h:83
QFontMetrics fontMetrics
the font metrics that should be used when drawing text in the control
Definition: qstyleoption.h:91
bool hasFocus() const
Definition: qwidget.cpp:6583
void setHeight(int h)
Sets the height to the given height.
Definition: qsize.h:135
T * qobject_cast(QObject *object)
Definition: qobject.h:375
void paintEvent(QPaintEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1068
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
void paintEvent(QPaintEvent *)
Reimplemented Function
Definition: qmenubar.cpp:106
int height() const
void addAction(QAction *action)
Appends the action action to this widget&#39;s list of actions.
Definition: qwidget.cpp:3317
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QString text
the text for the menu item
Definition: qstyleoption.h:450
void mouseMoveEvent(QMouseEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1304
The QStyleOptionToolButton class is used to describe the parameters for drawing a tool button...
Definition: qstyleoption.h:768
QAction * getNextAction(const int start, const int increment) const
Definition: qmenubar.cpp:795
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
static QString translate(const char *context, const char *key, const char *disambiguation=0, Encoding encoding=CodecForTr)
QStyle * style() const
Definition: qwidget.cpp:2742
void focusInEvent(QFocusEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1371
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
#define Q_Q(Class)
Definition: qglobal.h:2483
bool isVisible() const
Definition: qaction.cpp:1246
void setWidth(int w)
Sets the width to the given width.
Definition: qsize.h:132
QAction * insertSeparator(QAction *before)
This convenience function creates a new separator action, i.e.
Definition: qmenubar.cpp:963
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
Qt::KeyboardModifiers modifiers() const
Returns the keyboard modifier flags that existed immediately after the event occurred.
Definition: qevent.cpp:999
void setCurrentColorGroup(ColorGroup cg)
Set the palette&#39;s current color group to cg.
Definition: qpalette.h:105
int key() const
Returns the code of the key that was pressed or released.
Definition: qevent.h:231
#define SIGNAL(a)
Definition: qobjectdefs.h:227
int lineWidth
the line width for drawing the frame
Definition: qstyleoption.h:124
int width() const
Returns the width.
Definition: qsize.h:126
bool isNativeMenuBar() const
Definition: qmenubar.cpp:2095
QWidget * cornerWidget(Qt::Corner corner=Qt::TopRightCorner) const
Returns the widget on the left of the first or on the right of the last menu item, depending on corner.
Definition: qmenubar.cpp:2022
virtual QLayoutItem * takeAt(int index)=0
Must be implemented in subclasses to remove the layout item at index from the layout, and return the item.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
void _q_internalShortcutActivated(int)
Definition: qmenubar.cpp:1901
bool visible
whether the widget is visible
Definition: qwidget.h:191
QSize size(int flags, const QString &str, int tabstops=0, int *tabarray=0) const
Returns the size in pixels of text.
void calcActionRects(int max_width, int start) const
Definition: qmenubar.cpp:434
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
#define qApp
QWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a widget which is a child of parent, with widget flags set to f.
Definition: qwidget.cpp:1189
void popupAction(QAction *, bool)
Definition: qmenubar.cpp:339
int width() const
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenubar.cpp:874
The QLayout class is the base class of geometry managers.
Definition: qlayout.h:90
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
virtual void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition: qwidget.cpp:9170
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition: qicon.cpp:769
void adjust(int x1, int y1, int x2, int y2)
Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle.
Definition: qrect.h:434
void setRight(int pos)
Sets the right edge of the rectangle to the given x coordinate.
Definition: qrect.h:264
const QPalette & palette() const
QFontMetrics fontMetrics() const
Returns the font metrics for the widget&#39;s current font.
Definition: qwidget.h:984
virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w=0) const =0
Draws the given primitive element with the provided painter using the style options specified by opti...
The QResizeEvent class contains event parameters for resize events.
Definition: qevent.h:349
const char * styleHint(const QFontDef &request)
QPlatformMenuBarFactoryInterface * qt_guiPlatformMenuBarFactory()
Q_CORE_EXPORT void qWarning(const char *,...)
QIcon icon
the action&#39;s icon
Definition: qaction.h:75
int & rx()
Returns a reference to the x coordinate of this point.
Definition: qpoint.h:140
void setText(const QString &text)
Definition: qaction.cpp:860
int timerId() const
Returns the unique timer identifier, which is the same identifier as returned from QObject::startTime...
Definition: qcoreevent.h:346
The QStyleOptionFrame class is used to describe the parameters for drawing a frame.
Definition: qstyleoption.h:118
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
void handleReparent()
Definition: qmenubar.cpp:1404
QAction * action() const
Returns the action that is changed, added, or removed.
Definition: qevent.h:639
static bool inWhatsThisMode()
Returns true if the user interface is in "What&#39;s This?" mode; otherwise returns false.
Definition: qwhatsthis.cpp:648
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
void mouseReleaseEvent(QMouseEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1178
MenuItemType menuItemType
the type of menu item
Definition: qstyleoption.h:445
QAction * actionAt(const QPoint &) const
Returns the QAction at pt.
Definition: qmenubar.cpp:1706
void addActions(QList< QAction *> actions)
Appends the actions actions to this widget&#39;s list of actions.
Definition: qwidget.cpp:3327
Qt::MouseButton button() const
Returns the button that caused the event.
Definition: qevent.h:101
QRect rect() const
void clear()
Removes all the menu&#39;s actions.
Definition: qmenu.cpp:1755
bool contains(const QPoint &p, bool proper=false) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false...
Definition: qrect.cpp:1101
bool isEnabled() const
Definition: qwidget.h:948
virtual void setVisible(bool visible)
Definition: qwidget.cpp:7991
void hide()
Hides the widget.
Definition: qwidget.h:501
Corner
Definition: qnamespace.h:1456
static bool isActive()
Returns true if an accessibility implementation has been requested during the runtime of the applicat...
void setDefaultAction(QAction *)
Sets the default action to act.
Definition: qmenubar.cpp:2126
void setMouseTracking(bool enable)
Definition: qwidget.h:990
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
static QDesktopWidget * desktop()
Returns the desktop widget (also called the root window).
void clear()
Removes all the actions from the menu bar.
Definition: qmenubar.cpp:1021
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w=0) const =0
Returns the size of the element described by the specified option and type, based on the provided con...
void mousePressEvent(QMouseEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1143
static QAction * createAction(StandardSoftKey standardKey, QWidget *actionWidget)
bool isEmpty() const
Returns true if the rectangle is empty, otherwise returns false.
Definition: qrect.h:234
QPalette palette
the palette that should be used when painting the control
Definition: qstyleoption.h:92
void setCornerWidget(QWidget *w, Qt::Corner corner=Qt::TopRightCorner)
This sets the given widget to be shown directly on the left of the first menu item, or on the right of the last menu item, depending on corner.
Definition: qmenubar.cpp:1980
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
void setFocus()
Gives the keyboard input focus to this widget (or its focus proxy) if this widget or one of its paren...
Definition: qwidget.h:432
int shortcutId()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qevent.h:683
void setY(int y)
Sets the y coordinate of this point to the given y coordinate.
Definition: qpoint.h:137
void setCurrentAction(QAction *, bool=false, bool=false)
Definition: qmenubar.cpp:391
QMenu * menu() const
Returns the menu contained by this action.
Definition: qaction.cpp:793
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
QAction * addMenu(QMenu *menu)
Appends menu to the menu bar.
Definition: qmenubar.cpp:937
void addAction(QAction *, QAction *=0)
static QRect visualRect(Qt::LayoutDirection direction, const QRect &boundingRect, const QRect &logicalRect)
Returns the given logicalRectangle converted to screen coordinates based on the specified direction...
Definition: qstyle.cpp:2087
const QRect screenGeometry(int screen=-1) const
The QMenuBar class provides a horizontal menu bar.
Definition: qmenubar.h:62
The QKeySequence class encapsulates a key sequence as used by shortcuts.
Definition: qkeysequence.h:72
Qt::MouseButtons buttons() const
Returns the button state when the event was generated.
Definition: qevent.h:102
void setClipRegion(const QRegion &, Qt::ClipOperation op=Qt::ReplaceClip)
Sets the clip region to the given region using the specified clip operation.
Definition: qpainter.cpp:2917
int heightForWidth(int) const
Reimplemented Function
Definition: qmenubar.cpp:1853
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the list, searching forward from index...
Definition: qlist.h:847
void addToolBar(Qt::ToolBarArea area, QToolBar *toolbar)
Adds the toolbar into the specified area in this main window.
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
ToolButtonFeatures features
an OR combination of the tool button&#39;s features
Definition: qstyleoption.h:778
int x() const
void resize(int w, int h)
This corresponds to resize(QSize(w, h)).
Definition: qwidget.h:1014
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
void setLeft(int pos)
Sets the left edge of the rectangle to the given x coordinate.
Definition: qrect.h:258
virtual int count() const =0
Must be implemented in subclasses to return the number of items in the layout.
static QCoreApplication * instance()
Returns a pointer to the application&#39;s QCoreApplication (or QApplication) instance.
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
void addWidget(QWidget *w)
Adds widget w to this layout in a manner specific to the layout.
Definition: qlayout.cpp:319
void changeEvent(QEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1520
QString text() const
Returns the Unicode text that this key generated.
Definition: qevent.h:236
The QStatusTipEvent class provides an event that is used to show messages in a status bar...
Definition: qevent.h:606
const QSize & size() const
Returns the new size of the widget.
Definition: qevent.h:355
virtual QLayout * layout()
If this item is a QLayout, it is returned as a QLayout; otherwise 0 is returned.
int midLineWidth
the mid-line width for drawing the frame
Definition: qstyleoption.h:125
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QMenu * menu() const
Returns the associated menu, or 0 if no menu has been defined.
QRect menuRect(bool) const
Definition: qmenubar.cpp:136
virtual void init(QMenuBar *)=0
void setIcon(const QIcon &icon)
Definition: qmenu.cpp:754
void removeAction(QAction *action)
Removes the action action from this widget&#39;s list of actions.
Definition: qwidget.cpp:3386
int key
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void installEventFilter(QObject *)
Installs an event filter filterObj on this object.
Definition: qobject.cpp:2070
void removeAction(QWceMenuAction *)
virtual void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w=0) const =0
Draws the given element with the provided painter with the style options specified by option...
The QMainWindow class provides a main application window.
Definition: qmainwindow.h:63
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
virtual void setVisible(bool visible)
Reimplemented Function
Definition: qmenubar.cpp:1121
bool event(QEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1548
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
QList< QAction * > actions() const
Returns the (possibly empty) list of this widget&#39;s actions.
Definition: qwidget.cpp:3407
void insertAction(QAction *before, QAction *action)
Inserts the action action to this widget&#39;s list of actions, before the action before.
Definition: qwidget.cpp:3342
void setAutoRaise(bool enable)
void setActiveAction(QAction *action)
Sets the currently highlighted action to act.
Definition: qmenubar.cpp:1002
void setWidth(int w)
Sets the width of the rectangle to the given width.
Definition: qrect.h:442
friend class QMenu
Definition: qmenubar.h:350
void _q_actionTriggered()
Definition: qmenubar.cpp:546
QSize minimumSizeHint() const
Reimplemented Function
Definition: qmenubar.cpp:1726
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void setIcon(const QIcon &icon)
Definition: qaction.cpp:772
void leaveEvent(QEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1318
static bool macUpdateMenuBar()
Definition: qmenu_mac.mm:2055
void initStyleOption(QStyleOptionToolButton *option) const
Initialize option with the values from this QToolButton.
CheckType checkType
the type of checkmark of the menu item
Definition: qstyleoption.h:446
The QHBoxLayout class lines up widgets horizontally.
Definition: qboxlayout.h:129
void setPopupMode(ToolButtonPopupMode mode)
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
void updateGeometries()
Definition: qmenubar.cpp:175
void _q_updateLayout()
Definition: qmenubar.cpp:1924
quint16 index
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
return(isPopup||isToolTip)
bool nativeMenuBar
Whether or not a menubar will be used as a native menubar on platforms that support it...
Definition: qmenubar.h:67
bool isNull() const
Returns true if both the width and height is 0; otherwise returns false.
Definition: qsize.h:117
void triggered(QAction *action)
This signal is emitted when an action in a menu belonging to this menubar is triggered as a result of...
void qt_mac_clear_menubar()
Definition: qmenu_mac.mm:2015
The QStylePainter class is a convenience class for drawing QStyle elements inside a widget...
Definition: qstylepainter.h:55
QLayout * layout() const
Returns the layout manager that is installed on this widget, or 0 if no layout manager is installed...
Definition: qwidget.cpp:10073
void ignore()
Clears the accept flag parameter of the event object, the equivalent of calling setAccepted(false).
Definition: qcoreevent.h:310
void accept()
Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
Definition: qcoreevent.h:309
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
bool isRightToLeft() const
Definition: qwidget.h:428
bool intersects(const QRect &r) const
Returns true if this rectangle intersects with the given rectangle (i.
Definition: qrect.cpp:1429
QAction * menuAction() const
Returns the action associated with this menu.
Definition: qmenu.cpp:711
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
void updateGeometry()
Notifies the layout system that this widget has changed and may need to change geometry.
Definition: qwidget.cpp:10372
bool event(QEvent *)
This is the main event handler; it handles event event.
Definition: qwidget.cpp:8636
QAction * activeAction() const
Returns the QAction that is currently highlighted.
Definition: qmenubar.cpp:988
void updateCornerWidgetToolBar()
Definition: qmenubar.cpp:1935
static bool testAttribute(Qt::ApplicationAttribute attribute)
Returns true if attribute attribute is set; otherwise returns false.
void moveTop(int pos)
Moves the rectangle vertically, leaving the rectangle&#39;s top edge at the given y coordinate.
Definition: qrect.h:353
bool isEmpty() const
Returns true if either of the width and height is less than or equal to 0; otherwise returns false...
Definition: qsize.h:120
The QToolButton class provides a quick-access button to commands or options, usually used inside a QT...
Definition: qtoolbutton.h:59
void drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex &opt)
Use the widget&#39;s style to draw a complex control cc specified by the QStyleOptionComplex option...
Definition: qstylepainter.h:92
QLayout * layout()
Reimplemented Function
const QRect & rect() const
Returns the rectangle that needs to be updated.
Definition: qevent.h:305
bool isValid() const
Returns true if the rectangle is valid, otherwise returns false.
Definition: qrect.h:237
static void showText(const QPoint &pos, const QString &text, QWidget *w=0)
Shows text as a "What&#39;s This?" window, at global position pos.
Definition: qwhatsthis.cpp:754
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
static const KeyPair *const end
void setIcon(const QIcon &icon)
void activateAction(QAction *, QAction::ActionEvent)
Definition: qmenubar.cpp:530
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
const QPoint & globalPos() const
Returns the global position of the mouse cursor at the time of the event.
Definition: qevent.h:96
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
static QWidget * focusWidget()
Returns the application widget that has the keyboard input focus, or 0 if no widget in this applicati...
bool isDefaultUp() const
Definition: qmenubar.cpp:1049
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90
void _q_actionHovered()
Definition: qmenubar.cpp:557
QSize sizeHint() const
Reimplemented Function
Definition: qmenubar.cpp:1788
The QFocusEvent class contains event parameters for widget focus events.
Definition: qevent.h:275
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
QAction * actionAt(QPoint p) const
Definition: qmenubar.cpp:127
QSize sizeHint() const
Definition: qmenubar.cpp:117
QRect actionGeometry(QAction *) const
Returns the geometry of action act as a QRect.
Definition: qmenubar.cpp:1717
#define text
Definition: qobjectdefs.h:80
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
QPoint topLeft() const
Returns the position of the rectangle&#39;s top-left corner.
Definition: qrect.h:288
void hovered(QAction *action)
This signal is emitted when a menu action is highlighted; action is the action that caused the event ...
void focusOutEvent(QFocusEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1381
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60
void keyPressEvent(QKeyEvent *)
Reimplemented Function
Definition: qmenubar.cpp:1199