📄 qmenubar.cpp
字号:
mi, g, e, (i == actItem && actItemDown) ); } } if ( mseparator == InWindowsStyle && gs == WindowsStyle ) { p->setPen( g.light() ); p->drawLine( 0, height()-1, width()-1, height()-1 ); p->setPen( g.dark() ); p->drawLine( 0, height()-2, width()-1, height()-2 ); }}/*!\reimp*/void QMenuBar::mousePressEvent( QMouseEvent *e ){ if ( e->button() != LeftButton ) return; mouseBtDn = TRUE; // mouse button down int item = itemAtPos( e->pos() ); if ( item == actItem && popupvisible ) { if ( moveActiveTime.elapsed() > 50 ) toggleclose = 1; } setActiveItem( item, TRUE, FALSE );}/*!\reimp*/void QMenuBar::mouseReleaseEvent( QMouseEvent *e ){ if ( e->button() != LeftButton ) return; if ( !mouseBtDn ) return; mouseBtDn = FALSE; // mouse button up int item = itemAtPos( e->pos() ); if ( item >= 0 && !mitems->at(item)->isEnabled() || actItem >= 0 && !mitems->at(actItem)->isEnabled() ) { hidePopups(); return; } bool showMenu = TRUE; if ( toggleclose && style() == WindowsStyle && actItem == item ) showMenu = FALSE; setActiveItem( item, showMenu, !hasMouseTracking() ); toggleclose = 0;}/*!\reimp*/void QMenuBar::mouseMoveEvent( QMouseEvent *e ){ int item = itemAtPos( e->pos() ); if ( !mouseBtDn && !popupvisible) { if ( item >= 0 ) { if ( !hasmouse ) { hasmouse = 1; if ( actItem == item ) actItem = -1; // trigger update } } setActiveItem( item, FALSE, FALSE ); return; } if ( item != actItem && item >= 0 && ( popupvisible || mouseBtDn ) ) { setActiveItem( item, TRUE, FALSE ); if ( popupvisible ) moveActiveTime.start(); }}/*!\reimp*/void QMenuBar::leaveEvent( QEvent * e ){ hasmouse = 0; int actId = idAt( actItem ); if ( !hasFocus() && !popupvisible ) actItem = -1; updateItem( actId ); QFrame::leaveEvent( e );}/*!\reimp*/void QMenuBar::keyPressEvent( QKeyEvent *e ){ if ( actItem < 0 ) return; QMenuItem *mi = 0; int dx = 0; switch ( e->key() ) { case Key_Left: dx = -1; break; case Key_Right: case Key_Tab: dx = 1; break; case Key_Up: case Key_Down: case Key_Enter: case Key_Return: if ( style() == WindowsStyle ) setActiveItem( actItem ); break; case Key_Escape: setAltMode( FALSE ); break; } if ( dx ) { // highlight next/prev register int i = actItem; int c = mitems->count(); int n = c; while ( n-- ) { i = i + dx; if ( i == c ) i = 0; else if ( i < 0 ) i = c - 1; mi = mitems->at( i ); // ### fix windows-style traversal - currently broken due to // QMenuBar's reliance on QPopupMenu if ( /* (style() == WindowsStyle || */ mi->isEnabled() /* ) */ && !mi->isSeparator() ) break; } setActiveItem( i, popupvisible ); } else if ( !e->state() && e->text().length()==1 ) { QChar c = e->text()[0].upper(); QMenuItemListIt it(*mitems); register QMenuItem *m; int indx = 0; while ( (m=it.current()) ) { ++it; QString s = m->text(); if ( !s.isEmpty() ) { int i = s.find( '&' ); if ( i >= 0 ) { if ( s[i+1].upper() == c ) { mi = m; break; } } } indx++; } if ( mi ) { setActiveItem( indx ); } }}/*!\reimp*/void QMenuBar::resizeEvent( QResizeEvent * ){ QRect fr = frameRect(); if ( !fr.isNull() ) { QRect r( fr.x(), fr.y(), width(), height() ); setFrameRect( r ); } if ( badSize ) return; badSize = TRUE; if ( isVisible() ) calculateRects();}/*! Wins the price for the most stupid virtual function in Qt. Second price went to setWindowsAltMode() \sa setWindowsAltMode */void QMenuBar::setActItem( int , bool ){}/*! Sets actItem to \a i and calls repaint for the changed things. Takes care to optimize the repainting. Assumes that calculateRects() has been called as appropriate.*/void QMenuBar::setActiveItem( int i, bool show, bool activate_first_item ){ if ( i == actItem && (uint)show == popupvisible ) return; QMenuItem* mi = 0; if ( i >= 0 ) mi = mitems->at( i ); if ( mi && !mi->isEnabled() ) return; popupvisible = i >= 0 ? (show) : 0; actItemDown = popupvisible; if ( i < 0 || actItem < 0 ) { // just one item needs repainting int n = QMAX( actItem, i ); actItem = i; if ( irects && n >= 0 ) repaint( irects[n], FALSE ); } else if ( QABS(i-actItem) == 1 ) { // two neighbouring items need repainting int o = actItem; actItem = i; if ( irects ) repaint( irects[i].unite( irects[o] ), FALSE ); } else { // two non-neighbouring items need repainting int o = actItem; actItem = i; if ( irects ) { repaint( irects[o], FALSE ); repaint( irects[i], FALSE ); } } hidePopups(); if ( actItem < 0 || !popupvisible || !mi ) return;#ifndef QT_NO_POPUPMENU QPopupMenu *popup = mi->popup(); if ( popup ) { emit highlighted( mi->id() ); openActPopup(); if ( activate_first_item ) popup->setFirstItemActive(); } else#endif { // not a popup goodbye( FALSE ); if ( mi->signal() ) // activate signal mi->signal()->activate(); emit activated( mi->id() ); }}void QMenuBar::setAltMode( bool enable ){ waitforalt = 0; actItemDown = FALSE; if ( enable ) { if ( !QMenuData::d->aWidget ) QMenuData::d->aWidget = qApp->focusWidget(); setFocus(); updateItem( idAt( actItem ) ); } else { if ( QMenuData::d->aWidget ) QMenuData::d->aWidget->setFocus(); int actId = idAt( actItem ); actItem = -1; updateItem( actId ); QMenuData::d->aWidget = 0; }}/*! Wins the second price for the most stupid virtual function in Qt. First price went to setActItem() \sa setActItem*/void QMenuBar::setWindowsAltMode( bool, int ){}/*! Sets up keyboard accelerators for the menu bar. */#ifndef QT_NO_ACCELvoid QMenuBar::setupAccelerators(){ delete autoaccel; autoaccel = 0; QMenuItemListIt it(*mitems); register QMenuItem *mi; while ( (mi=it.current()) ) { ++it; if ( !mi->isEnabled() ) // ### when we have a good solution for the accel vs. focus widget problem, remove that. That is only a workaround continue; QString s = mi->text(); if ( !s.isEmpty() ) { int i = QAccel::shortcutKey( s ); if ( i ) { if ( !autoaccel ) { autoaccel = new QAccel( this ); CHECK_PTR( autoaccel ); autoaccel->setIgnoreWhatsThis( TRUE ); connect( autoaccel, SIGNAL(activated(int)), SLOT(accelActivated(int)) ); connect( autoaccel, SIGNAL(destroyed()), SLOT(accelDestroyed()) ); } autoaccel->insertItem( i, mi->id() ); } }#ifndef QT_NO_POPUPMENU if ( mi->popup() ) { // reuse QPopupMenu* popup = mi->popup(); if (popup->parentMenu) popup->parentMenu->menuDelPopup(popup); menuInsPopup(popup); popup->updateAccel( this ); if ( !popup->isEnabled() ) popup->enableAccel( FALSE ); }#endif }}#endif/*!\reimp */bool QMenuBar::customWhatsThis() const{ return TRUE;}/*!\reimp */void QMenuBar::focusInEvent( QFocusEvent * ){ if ( actItem < 0 ) { int i = -1; while ( actItem < 0 && ++i < (int) mitems->count() ) { QMenuItem* mi = mitems->at( i ); if ( mi && mi->isEnabled() && !mi->isSeparator() ) setActiveItem( i, FALSE ); } } else if ( !popupvisible ) { updateItem( idAt( actItem ) ); }}/*!\reimp */void QMenuBar::focusOutEvent( QFocusEvent * ){ updateItem( idAt( actItem ) ); if ( !popupvisible ) setAltMode( FALSE );}/*! \reimp*/QSize QMenuBar::sizeHint() const{ if ( badSize ) ( (QMenuBar*)this )->calculateRects(); QSize s( style().defaultFrameWidth(), 0 ); if ( irects ) { for ( int i = 0; i < (int)mitems->count(); ++i ) s.setWidth( s.width() + irects[ i ].width() + 2 ); } s.setHeight( height() ); return s.expandedTo( QApplication::globalStrut() );}/*! \reimp*/QSize QMenuBar::minimumSize() const{ if ( parent() && parent()->inherits( "QToolBar" ) ) return sizeHint(); return QFrame::minimumSize();}/*! \reimp*/QSize QMenuBar::minimumSizeHint() const{ return minimumSize();}/*! Sets the default popup orientation. By default, menus pop "down" the screen. By calling setDefaultUp(TRUE) the menu will pop "up". You might call this for menus that are \e below the document to which they refer. If the menu would not fit on the screen, the other direction is used rather than the default. \sa defaultUp();*/void QMenuBar::setDefaultUp( bool on ){ defaultup = on;}/*! Returns whether the menus default to popping "down" the screen (the default), or "up". \sa setDefaultUp();*/bool QMenuBar::isDefaultUp() const{ return defaultup;}/*!\reimp */void QMenuBar::activateItemAt( int index ){ if ( index >= 0 && index < (int) mitems->count() ) setActiveItem( index ); else goodbye( FALSE );}#endif // QT_NO_MENUBAR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -