📄 qmenubar.cpp
字号:
register QAction *act = d->actionList.at(i); QString s = act->text(); if(!s.isEmpty()) { int ampersand = s.indexOf('&'); if(ampersand >= 0) { if(s[ampersand+1].toUpper() == c) { clashCount++; if(!first) first = act; if(act == d->currentAction) currentSelected = act; else if (!firstAfterCurrent && currentSelected) firstAfterCurrent = act; } } } } } QAction *next_action = 0; if(clashCount >= 1) { if(clashCount == 1 || !d->currentAction || (currentSelected && !firstAfterCurrent)) next_action = first; else next_action = firstAfterCurrent; } if(next_action) { key_consumed = true; d->setCurrentAction(next_action, true, true); } } if(key_consumed) e->accept(); else e->ignore();}/*! \reimp*/void QMenuBar::mouseMoveEvent(QMouseEvent *e){ Q_D(QMenuBar); d->mouseDown = e->buttons() & Qt::LeftButton; QAction *action = d->actionAt(e->pos()); bool popupState = d->popupState || d->mouseDown; if(action && d->isVisible(action) || !popupState) d->setCurrentAction(action, popupState);}/*! \reimp*/void QMenuBar::leaveEvent(QEvent *){ Q_D(QMenuBar); if(!hasFocus() && !d->popupState) d->setCurrentAction(0);}/*! \reimp*/void QMenuBar::actionEvent(QActionEvent *e){ Q_D(QMenuBar); d->itemsDirty = true;#ifdef Q_WS_MAC if(d->mac_menubar) { if(e->type() == QEvent::ActionAdded) d->mac_menubar->addAction(e->action(), d->mac_menubar->findAction(e->before())); else if(e->type() == QEvent::ActionRemoved) d->mac_menubar->removeAction(e->action()); else if(e->type() == QEvent::ActionChanged) d->mac_menubar->syncAction(e->action()); }#endif if(e->type() == QEvent::ActionAdded) { connect(e->action(), SIGNAL(triggered()), this, SLOT(_q_actionTriggered())); connect(e->action(), SIGNAL(hovered()), this, SLOT(_q_actionHovered())); } else if(e->type() == QEvent::ActionRemoved) { e->action()->disconnect(this); } if (isVisible()) { d->updateGeometries(); update(); }}/*! \reimp*/void QMenuBar::focusInEvent(QFocusEvent *){ Q_D(QMenuBar); if(d->keyboardState && !d->currentAction && !d->actionList.isEmpty()) d->setCurrentAction(d->actionList.first());}/*! \reimp*/void QMenuBar::focusOutEvent(QFocusEvent *){ Q_D(QMenuBar); if(!d->popupState) { d->setCurrentAction(0); d->setKeyboardMode(false); }}void QMenuBarPrivate::handleReparent(){ Q_Q(QMenuBar); QWidget *newParent = q->parentWidget(); //Note: if parent is reparented, then window may change even if parent doesn't // we need to install an event filter on parent, and remove the old one if (oldParent != newParent) { if (oldParent) oldParent->removeEventFilter(q); if (newParent) newParent->installEventFilter(q); } //we also need event filter on top-level (for shortcuts) QWidget *newWindow = newParent ? newParent->window() : 0; if (oldWindow != newWindow) { if (oldParent && oldParent != oldWindow) oldWindow->removeEventFilter(q); if (newParent && newParent != newWindow) newWindow->installEventFilter(q); } oldParent = newParent; oldWindow = newWindow;}#ifdef QT3_SUPPORT/*! Sets whether the menu bar should automatically resize itself when its parent widget is resized. This feature is provided to help porting to Qt 4. We recommend against using it in new code. \sa autoGeometry()*/void QMenuBar::setAutoGeometry(bool b){ Q_D(QMenuBar); d->doAutoResize = b;}/*! Returns true if the menu bar automatically resizes itself when its parent widget is resized; otherwise returns false. This feature is provided to help porting to Qt 4. We recommend against using it in new code. \sa setAutoGeometry()*/bool QMenuBar::autoGeometry() const{ Q_D(const QMenuBar); return d->doAutoResize;}#endif/*! \reimp*/void QMenuBar::changeEvent(QEvent *e){ Q_D(QMenuBar); if(e->type() == QEvent::StyleChange) { d->itemsDirty = true; setMouseTracking(style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, this)); if(parentWidget()) resize(parentWidget()->width(), heightForWidth(parentWidget()->width())); } else if (e->type() == QEvent::ParentChange) d->handleReparent(); QWidget::changeEvent(e);}/*! \reimp*/bool QMenuBar::event(QEvent *e){ Q_D(QMenuBar); switch (e->type()) { case QEvent::KeyPress: { QKeyEvent *ke = (QKeyEvent*)e;#if 0 if(!d->keyboardState) { //all keypresses.. d->setCurrentAction(0); return ; }#endif if(ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) { keyPressEvent(ke); return true; } } break;#ifndef QT_NO_SHORTCUT case QEvent::Shortcut: { QShortcutEvent *se = static_cast<QShortcutEvent *>(e); int shortcutId = se->shortcutId(); for(int j = 0; j < d->shortcutIndexMap.size(); ++j) { if (shortcutId == d->shortcutIndexMap.value(j)) d->_q_internalShortcutActivated(j); } } break;#endif case QEvent::Show:#ifdef QT3_SUPPORT // If itemsDirty == true, updateGeometries sends the MenubarUpdated event. if (!d->itemsDirty) { QMenubarUpdatedEvent menubarUpdated(this); QApplication::sendEvent(parentWidget(), &menubarUpdated); }#endif d->updateGeometries(); break;#ifdef QT3_SUPPORT case QEvent::Hide: { QMenubarUpdatedEvent menubarUpdated(this); QApplication::sendEvent(parentWidget(), &menubarUpdated); } break;#endif#ifndef QT_NO_WHATSTHIS case QEvent::QueryWhatsThis: e->setAccepted(d->whatsThis.size()); if (QAction *action = d->actionAt(static_cast<QHelpEvent*>(e)->pos())) { if (action->whatsThis().size() || action->menu()) e->accept(); } return true;#endif default: break; } return QWidget::event(e);}/*! \reimp*/bool QMenuBar::eventFilter(QObject *object, QEvent *event){ Q_D(QMenuBar); if (object == parent() && object) {#ifdef QT3_SUPPORT if (d->doAutoResize && event->type() == QEvent::Resize) { QResizeEvent *e = (QResizeEvent *)event; int w = e->size().width(); setGeometry(0, y(), w, heightForWidth(w)); return false; }#endif if (event->type() == QEvent::ParentChange) //GrandparentChange d->handleReparent(); } if (object == d->leftWidget || object == d->rightWidget) { switch (event->type()) { case QEvent::ShowToParent: case QEvent::HideToParent: d->_q_updateLayout(); break; default: break; } } if (style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this)) { if (d->altPressed) { switch (event->type()) { case QEvent::KeyPress: case QEvent::KeyRelease: { QKeyEvent *kev = static_cast<QKeyEvent*>(event); if (kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta) { if (event->type() == QEvent::KeyPress) // Alt-press does not interest us, we have the shortcut-override event break; d->setKeyboardMode(!d->keyboardState); } } // fall through case QEvent::MouseButtonPress: case QEvent::FocusIn: case QEvent::FocusOut: case QEvent::ActivationChange: d->altPressed = false; qApp->removeEventFilter(this); break; default: break; } } else if (isVisible()) { if (event->type() == QEvent::ShortcutOverride) { QKeyEvent *kev = static_cast<QKeyEvent*>(event); if ((kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta) && kev->modifiers() == Qt::AltModifier) { d->altPressed = true; qApp->installEventFilter(this); } } } } return false;}/*! \internal Return the item at \a pt, or 0 if there is no item there or if it is a separator item.*/QAction *QMenuBar::actionAt(const QPoint &pt) const{ Q_D(const QMenuBar); return d->actionAt(pt);}/*! \internal Returns the geometry of action \a act.*/QRect QMenuBar::actionGeometry(QAction *act) const{ Q_D(const QMenuBar); return d->actionRect(act);}/*! \reimp*/QSize QMenuBar::minimumSizeHint() const{ Q_D(const QMenuBar);#ifdef Q_WS_MAC const bool as_gui_menubar = !d->mac_menubar;#else const bool as_gui_menubar = true;#endif ensurePolished(); QSize ret(0, 0); const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this); const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this); int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this); int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this); if(as_gui_menubar) { QMap<QAction*, QRect> actionRects; QList<QAction*> actionList; int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width(); d->calcActionRects(w - (2 * fw), 0, actionRects, actionList); if (d->actionList.count() > 0) { ret = d->actionRect(d->actionList.at(0)).size(); if (!d->extension->isHidden()) ret += QSize(d->extension->sizeHint().width(), 0); } ret += QSize(2*fw + hmargin, 2*fw + vmargin); } int margin = 2*vmargin + 2*fw + spaceBelowMenuBar; if(d->leftWidget) { QSize sz = d->leftWidget->minimumSizeHint(); ret.setWidth(ret.width() + sz.width()); if(sz.height() + margin > ret.height()) ret.setHeight(sz.height() + margin); } if(d->rightWidget) { QSize sz = d->rightWidget->minimumSizeHint(); ret.setWidth(ret.width() + sz.width()); if(sz.height() + margin > ret.height()) ret.setHeight(sz.height() + margin); } if(as_gui_menubar) { QStyleOptionMenuItem opt; opt.rect = rect(); opt.menuRect = rect(); opt.state = QStyle::State_None; opt.menuItemType = QStyleOptionMenuItem::Normal; opt.checkType = QStyleOptionMenuItem::NotCheckable; opt.palette = palette(); return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt, ret.expandedTo(QApplication::globalStrut()), this)); } return ret;}/*! \reimp*/QSize QMenuBar::sizeHint() const{ Q_D(const QMenuBar);#ifdef Q_WS_MAC const bool as_gui_menubar = !d->mac_menubar;#else const bool as_gui_menubar = true;#endif ensurePolished(); QSize ret(0, 0); const int hmargin = style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, this); const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this); int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this); int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this); if(as_gui_menubar) { QMap<QAction*, QRect> actionRects; QList<QAction*> actionList; const int w = parentWidget() ? parentWidget()->width() : QApplication::desktop()->width(); d->calcActionRects(w - (2 * fw), 0, actionRects, actionList); for (QMap<QAction*, QRect>::const_iterator i = actionRects.begin(); i != actionRects.constEnd(); ++i) { QRect actionRect(i.value()); if(actionRect.x() + actionRect.width() > ret.width()) ret.setWidth(actionRect.x() + actionRect.width()); if(actionRect.y() + actionRect.height() > ret.height()) ret.setHeight(actionRect.y() + actionRect.height()); } ret += QSize(2*fw + 2*hmargin, 2*fw + 2*vmargin); } int margin = 2*vmargin + 2*fw + spaceBelowMenuBar; if(d->leftWidget) { QSize sz = d->leftWidget->sizeHint(); ret.setWidth(ret.width() + sz.width()); if(sz.height() + margin > ret.height()) ret.setHeight(sz.height() + margin); } if(d->rightWidget) { QSize sz = d->rightWidget->sizeHint(); ret.setWidth(ret.width() + sz.width()); if(sz.height() + margin > ret.height()) ret.setHeight(sz.height() + margin); } if(as_gui_menubar) { QStyleOptionMenuItem opt; opt.rect = rect(); opt.menuRect = rect(); opt.state = QStyle::State_None; opt.menuItemType = QStyleOptionMenuItem::Normal; opt.checkType = QStyleOptionMenuItem::NotCheckable; opt.palette = palette(); return (style()->sizeFromContents(QStyle::CT_MenuBar, &opt, ret.expandedTo(QApplication::globalStrut()), this)); } return ret;}/*! \reimp*/int QMenuBar::heightForWidth(int) const{ Q_D(const QMenuBar);#ifdef Q_WS_MAC const bool as_gui_menubar = !d->mac_menubar;#else const bool as_gui_menubar = true;#endif int height = 0; const int vmargin = style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, this); int fw = style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, this); int spaceBelowMenuBar = style()->styleHint(QStyle::SH_MainWindow_SpaceBelowMenuBar, 0, this); if(as_gui_menubar) { if (d->actionList.count()) { // assume all actionrects have the same height
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -