?? qpopupmenu.cpp
字號:
if ( isVisible() ) { updateSize(); update(); }}/*!\reimp*/void QPopupMenu::show(){ if ( !isPopup() && isVisible() ) hide(); if ( isVisible() ) { supressAboutToShow = FALSE; QWidget::show(); return; } if (!supressAboutToShow) emit aboutToShow(); else supressAboutToShow = FALSE; if ( badSize ) updateSize(); QWidget::show(); popupActive = -1;}/*!\reimp*/void QPopupMenu::hide(){ if ( syncMenu == this && qApp ) { qApp->exit_loop(); syncMenu = 0; } if ( !isVisible() ) { QWidget::hide(); return; } emit aboutToHide(); actItem = popupActive = -1; mouseBtDn = FALSE; // mouse button up hidePopups(); QWidget::hide();}/*! Calculates the height in pixels of the item in row \a row. */int QPopupMenu::itemHeight( int row ) const{ return itemHeight( mitems->at( row ) );}/*! Calculates the height in pixels of the item \a mi. */int QPopupMenu::itemHeight( QMenuItem *mi ) const{ if ( mi->widget() ) return QMAX( mi->widget()->height(), QApplication::globalStrut().height() ); if ( mi->custom() && mi->custom()->fullSpan() ) return QMAX( mi->custom()->sizeHint().height(), QApplication::globalStrut().height() ); if ( mi->isSeparator() ) return style().popupMenuItemHeight( checkable, mi, fontMetrics() );// return QMAX( style().popupMenuItemHeight( checkable, mi, fontMetrics() ), QApplication::globalStrut().height() ); return style().popupMenuItemHeight( checkable, mi, fontMetrics());}/*! Draws item \a mi in the area \a x, \a y, \a w, \a h, using painter \a p. The item is drawn active or inactive according to \a act, and using the rightmost \a tab_ pixels for accelerator text. \sa QStyle::drawPopupMenuItem()*/void QPopupMenu::drawItem( QPainter* p, int tab_, QMenuItem* mi, bool act, int x, int y, int w, int h){ bool dis = !mi->isEnabled(); if ( mi->custom() && mi->custom()->fullSpan() ) { QMenuItem dummy; style().drawPopupMenuItem(p, checkable, maxPMWidth, tab_, &dummy, palette(), act, !dis, x, y, w, h); mi->custom()->paint( p, colorGroup(), act, !dis, x, y, w, h ); } else style().drawPopupMenuItem(p, checkable, maxPMWidth, tab_, mi, palette(), act, !dis, x, y, w, h);}/*! Draws all menu items.*/void QPopupMenu::drawContents( QPainter* p ){ QMenuItemListIt it(*mitems); QMenuItem *mi; int row = 0; int x = contentsRect().x(); int y = contentsRect().y(); int itemw = contentsRect().width() / ncols; while ( (mi=it.current()) ) { ++it; int itemh = itemHeight( row ); if ( ncols > 1 && y + itemh > contentsRect().bottom() ) { if ( y < contentsRect().bottom() ) { style().drawPopupMenuItem(p, checkable, maxPMWidth, tab, 0, palette(), FALSE, TRUE, x, y, itemw, contentsRect().bottom()-y); } y = contentsRect().y(); x +=itemw; } drawItem( p, tab, mi, row == actItem, x, y, itemw, itemh ); y += itemh; ++row; } if ( y < contentsRect().bottom() ) style().drawPopupMenuItem(p, checkable, maxPMWidth, tab, 0, palette(), FALSE, TRUE, x, y, itemw, contentsRect().bottom()-y);}/***************************************************************************** Event handlers *****************************************************************************//*!\reimp*/void QPopupMenu::paintEvent( QPaintEvent *e ){ QFrame::paintEvent( e );}/*!\reimp*/void QPopupMenu::closeEvent( QCloseEvent * e) { e->accept(); hide(); byeMenuBar();}/*!\reimp*/void QPopupMenu::mousePressEvent( QMouseEvent *e ){ mouseBtDn = TRUE; // mouse button down int item = itemAtPos( e->pos() ); if ( item == -1 ) { if ( !rect().contains(e->pos()) && !tryMenuBar(e) ) { byeMenuBar(); } return; } register QMenuItem *mi = mitems->at(item); if ( item != actItem ) // new item activated setActiveItem( item ); QPopupMenu *popup = mi->popup(); if ( popup ) { if ( popup->isVisible() ) { // sub menu already open int pactItem = popup->actItem; popup->actItem = -1; popup->hidePopups(); popup->updateRow( pactItem ); } else { // open sub menu hidePopups(); popupSubMenuLater( 20, this ); } } else { hidePopups(); }}/*!\reimp*/void QPopupMenu::mouseReleaseEvent( QMouseEvent *e ){ if ( !mouseBtDn && !parentMenu && actItem < 0 && motion < 6 ) return; mouseBtDn = FALSE; int item = itemAtPos( e->pos() ); if ( item == -1 ) { if ( !rect().contains( e->pos() ) && tryMenuBar(e) ) return; } if ( actItem >= 0 ) { // selected menu item! register QMenuItem *mi = mitems->at(actItem); if ( mi ->widget() ) { QWidget* widgetAt = QApplication::widgetAt( e->globalPos(), TRUE ); if ( widgetAt && widgetAt != this ) { QMouseEvent me( e->type(), widgetAt->mapFromGlobal( e->globalPos() ), e->globalPos(), e->button(), e->state() ); QApplication::sendEvent( widgetAt, &me ); } } QPopupMenu *popup = mi->popup();#ifndef QT_NO_WHATSTHIS bool b = QWhatsThis::inWhatsThisMode();#else const bool b = FALSE;#endif if ( !mi->isEnabled() ) {#ifndef QT_NO_WHATSTHIS if ( b ) { actItem = -1; updateItem( mi->id() ); byeMenuBar(); actSig( mi->id(), b); }#endif } else if ( popup ) { popup->setFirstItemActive(); } else { // normal menu item byeMenuBar(); // deactivate menu bar if ( mi->isEnabled() ) { actItem = -1; updateItem( mi->id() ); active_popup_menu = this; actSig( mi->id(), b ); if ( mi->signal() && !b ) mi->signal()->activate(); active_popup_menu = 0; } } } else { byeMenuBar(); }}/*!\reimp*/void QPopupMenu::mouseMoveEvent( QMouseEvent *e ){ motion++; if ( parentMenu && parentMenu->isPopupMenu ) { QPopupMenu* p = (QPopupMenu*)parentMenu; int myIndex; QPoint pPos; p->findPopup( this, &myIndex ); pPos = p->mapFromParent( mapToGlobal( e->pos() ) ); if ( p->actItem != myIndex && !p->rect().contains( pPos ) ) p->setActiveItem( myIndex ); } if ( (e->state() & Qt::MouseButtonMask) == 0 && !hasMouseTracking() ) return; int item = itemAtPos( e->pos() ); if ( item == -1 ) { // no valid item int lastActItem = actItem; actItem = -1; if ( lastActItem >= 0 ) updateRow( lastActItem ); if ( !rect().contains( e->pos() ) && !tryMenuBar( e ) ) popupSubMenuLater( style() == WindowsStyle ? 256 : 96, this ); } else { // mouse on valid item // but did not register mouse press if ( (e->state() & Qt::MouseButtonMask) && !mouseBtDn ) mouseBtDn = TRUE; // so mouseReleaseEvent will pop down register QMenuItem *mi = mitems->at( item ); if ( mi ->widget() ) { QWidget* widgetAt = QApplication::widgetAt( e->globalPos(), TRUE ); if ( widgetAt && widgetAt != this ) { QMouseEvent me( e->type(), widgetAt->mapFromGlobal( e->globalPos() ), e->globalPos(), e->button(), e->state() ); QApplication::sendEvent( widgetAt, &me ); } } if ( actItem == item ) return; if ( mi->popup() || (popupActive >= 0 && popupActive != item) ) popupSubMenuLater( style() == WindowsStyle ? 256 : 96, this ); else if ( singleSingleShot && *singleSingleShot ) (*singleSingleShot)->stop(); if ( item != actItem ) setActiveItem( item ); }}/*!\reimp*/void QPopupMenu::keyPressEvent( QKeyEvent *e ){#if 0 // disabled until some way to SAVE these settings is possible // also must have global way to disable (eg. for stylus device) if (mouseBtDn && actItem >= 0) { if (e->key() == Key_Shift || e->key() == Key_Control || e->key() == Key_Alt) return; QMenuItem *mi = mitems->at(actItem); int modifier = (((e->state() & ShiftButton) ? SHIFT : 0) | ((e->state() & ControlButton) ? CTRL : 0) | ((e->state() & AltButton) ? ALT : 0)); if (mi) setAccel(modifier + e->key(), mi->id()); ; return; }#endif QMenuItem *mi = 0; QPopupMenu *popup; int dy = 0; bool ok_key = TRUE; switch ( e->key() ) { case Key_Tab: // ignore tab, otherwise it will be passed to the menubar break; case Key_Up: dy = -1; break; case Key_Down: dy = 1; break; case Key_Alt: if ( style() == WindowsStyle ) { byeMenuBar(); } break;#ifdef QT_KEYPAD_MODE case Key_Back: case Key_No:#endif case Key_Escape: if ( tornOff ) { close(); return; } // just hide one hide();#ifndef QT_NO_MENUBAR if ( parentMenu && parentMenu->isMenuBar ) ((QMenuBar*) parentMenu)->goodbye( TRUE );#endif break; case Key_Left: if ( ncols > 1 && actItem >= 0 ) { QRect r( itemGeometry( actItem ) ); int newActItem = itemAtPos( QPoint( r.left() - 1, r.center().y() ) ); if ( newActItem >= 0 ) { setActiveItem( newActItem ); break; } } if ( parentMenu && parentMenu->isPopupMenu ) { ((QPopupMenu *)parentMenu)->hidePopups(); if ( singleSingleShot && *singleSingleShot ) (*singleSingleShot)->stop(); break; } ok_key = FALSE; break; case Key_Right: if ( actItem >= 0 && ( mi=mitems->at(actItem) )->isEnabled() && (popup=mi->popup()) ) { hidePopups(); if ( singleSingleShot && *singleSingleShot ) (*singleSingleShot)->stop(); // ### The next two lines were switched to fix the problem with the first item of the // submenu not being highlighted...any reason why they should have been the other way?? subMenuTimer(); popup->setFirstItemActive(); break; } else if ( actItem == -1 && ( parentMenu && !parentMenu->isMenuBar )) { dy = 1; break; } if ( ncols > 1 && actItem >= 0 ) { QRect r( itemGeometry( actItem ) ); int newActItem = itemAtPos( QPoint( r.right() + 1, r.center().y() ) ); if ( newActItem >= 0 ) { setActiveItem( newActItem ); break; } } ok_key = FALSE; break; case Key_Space: if ( style() != MotifStyle ) break; // for motif, fall through#ifdef QT_KEYPAD_MODE case Key_Select:#endif case Key_Return: case Key_Enter: if ( actItem < 0 ) break; mi = mitems->at( actItem ); popup = mi->popup(); if ( popup ) { hidePopups(); popupSubMenuLater( 20, this ); popup->setFirstItemActive(); } else { actItem = -1; updateItem( mi->id() ); byeMenuBar();#ifndef QT_NO_WHATSTHIS bool b = QWhatsThis::inWhatsThisMode();#else const bool b = FALSE;#endif if ( mi->isEnabled() || b ) { active_popup_menu = this; actSig( mi->id(), b ); if ( mi->signal() && !b ) mi->signal()->activate(); active_popup_menu = 0; } } break;#ifndef QT_NO_WHATSTHIS case Key_F1: if ( actItem < 0 || e->state() != ShiftButton) break; mi = mitems->at( actItem ); if ( !mi->whatsThis().isNull() ){ if ( !QWhatsThis::inWhatsThisMode() ) QWhatsThis::enterWhatsThisMode(); QRect r( itemGeometry( actItem) ); QWhatsThis::leaveWhatsThisMode( mi->whatsThis(), mapToGlobal( r.bottomLeft()) ); } //fall-through!#endif default: ok_key = FALSE; } if ( !ok_key && ( !e->state() || e->state() == AltButton ) && 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; ok_key = TRUE; break; } } } indx++; } if ( mi ) { popup = mi->popup(); if ( popup ) { setActiveItem( indx ); hidePopups(); popupSubMenuLater( 20, this ); popup->setFirstItemActive(); } else { byeMenuBar();#ifndef QT_NO_WHATSTHIS bool b = QWhatsThis::inWhatsThisMode();#else const bool b = FALSE;#endif if ( mi->isEnabled() || b ) { active_popup_menu = this; actSig( mi->id(), b ); if ( mi->signal() && !b ) mi->signal()->activate(); active_popup_menu = 0; } } } }#ifndef QT_NO_MENUBAR if ( !ok_key ) { // send to menu bar register QMenuData *top = this; // find top level while ( top->parentMenu ) top = top->parentMenu; if ( top->isMenuBar ) ((QMenuBar*)top)->tryKeyEvent( this, e ); }#endif if ( dy && actItem < 0 ) { setFirstItemActive(); } else if ( dy ) { // highlight next/prev register int i = actItem; int c = mitems->count(); int n = c;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -