示例#1
0
/*
    \internal
    Checks for possible accelerators, if no widget
    ate the keypres, or we are in the middle of a
    partial key sequence.
*/
bool QAccelManager::dispatchAccelEvent( QWidget* w, QKeyEvent* e )
{
#ifndef QT_NO_STATUSBAR
    // Needs to be declared and used here because of "goto doclash"
    QStatusBar* mainStatusBar = 0;
#endif

    // Modifiers can NOT be accelerators...
    if ( e->key() >= Key_Shift &&
	 e->key() <= Key_Alt )
	 return FALSE;

    SequenceMatch result = Qt::NoMatch;
    QKeySequence tocheck, partial;
    QAccelPrivate* accel = 0;
    QAccelItem* item = 0;
    QAccelPrivate* firstaccel = 0;
    QAccelItem* firstitem = 0;
    QAccelPrivate* lastaccel = 0;
    QAccelItem* lastitem = 0;
    
    QKeyEvent pe = *e;
    int n = -1;
    int hasShift = (e->state()&Qt::ShiftButton)?1:0;
    bool identicalDisabled = FALSE;
    bool matchFound = FALSE;
    do {
	accel = accels.first();
	matchFound = FALSE;
	while ( accel ) {
	    if ( correctSubWindow( w, accel ) ) {
		if ( accel->enabled ) {
		    item = accel->aitems.last();
		    while( item ) {
			if ( Qt::Identical == (result = match( &pe, item, tocheck )) ) {
			    if ( item->enabled ) {
				if ( !firstaccel ) {
				    firstaccel = accel;
				    firstitem = item;
				}
				lastaccel = accel;
				lastitem = item;
				n++;
				matchFound = TRUE;
				if ( n > QMAX(clash,0) )
				    goto doclash;
			    } else {
				identicalDisabled = TRUE;
			    }
			}
			if ( item->enabled && Qt::PartialMatch == result ) {
			    partial = tocheck;
			    matchFound = TRUE;
			}
			item = accel->aitems.prev();
		    }
		} else {
		    item = accel->aitems.last();
		    while( item ) {
			if ( Qt::Identical == match( &pe, item, tocheck ) )
			    identicalDisabled = TRUE;
			item = accel->aitems.prev();
		    }
		}
	    }
	    accel = accels.next();
	}
	pe = QKeyEvent( QEvent::Accel, pe.key(), pe.ascii(), pe.state()&~Qt::ShiftButton, pe.text() );
    } while ( hasShift-- && !matchFound && !identicalDisabled );

#ifndef QT_NO_STATUSBAR
    mainStatusBar = (QStatusBar*) w->topLevelWidget()->child( 0, "QStatusBar" );
#endif
    if ( n < 0 ) { // no match found
	currentState = partial.count() ? PartialMatch : NoMatch;
#ifndef QT_NO_STATUSBAR
	// Only display message if we are, or were, in a partial match
	if ( mainStatusBar && (PartialMatch == currentState || intermediate.count() ) ) {
	    if ( currentState == Qt::PartialMatch ) {
		mainStatusBar->message( (QString)partial + ", ...", 0 );
	    } else if (!identicalDisabled) {
		QString message = QAccel::tr("%1, %2 not defined").
		    arg( (QString)intermediate ).
		    arg( QKeySequence::encodeString( e->key() | translateModifiers(e->state()) ) );
		mainStatusBar->message( message, 2000 );
		// Since we're a NoMatch, reset the clash count
		clash = -1;
	    } else {
	    	mainStatusBar->clear();
	    }
	}
#endif

	bool eatKey = (PartialMatch == currentState || intermediate.count() );
	intermediate = partial;
	if ( eatKey )
	    e->accept();
	return eatKey;
    } else if ( n == 0 ) { // found exactly one match
	clash = -1; // reset
#ifndef QT_NO_STATUSBAR
	if ( currentState == Qt::PartialMatch && mainStatusBar )
		mainStatusBar->clear();
#endif
	currentState = Qt::NoMatch; // Free sequence keylock
	intermediate = QKeySequence();
	lastaccel->activate( lastitem );
	e->accept();
	return TRUE;
    }

 doclash: // found more than one match
#ifndef QT_NO_STATUSBAR
    if ( !mainStatusBar ) // if "goto doclash", we need to get statusbar again.
	mainStatusBar = (QStatusBar*) w->topLevelWidget()->child( 0, "QStatusBar" );
#endif

    QString message = QAccel::tr( "Ambiguous \"%1\" not handled" ).arg( (QString)tocheck );
    if ( clash >= 0 && n > clash ) { // pick next  match
	intermediate = QKeySequence();
	currentState = Qt::NoMatch; // Free sequence keylock
	clash++;
#ifndef QT_NO_STATUSBAR
	if ( mainStatusBar &&
	     !lastitem->signal &&
	     !(lastaccel->parent->receivers( "activatedAmbiguously(int)" )) )
	    mainStatusBar->message( message, 2000 );
#endif
	lastaccel->activateAmbiguously( lastitem );
    } else { // start (or wrap) with the first matching
	intermediate = QKeySequence();
	currentState = Qt::NoMatch; // Free sequence keylock
	clash = 0;
#ifndef QT_NO_STATUSBAR
	if ( mainStatusBar &&
	     !firstitem->signal &&
	     !(firstaccel->parent->receivers( "activatedAmbiguously(int)" )) )
	    mainStatusBar->message( message, 2000 );
#endif
	firstaccel->activateAmbiguously( firstitem );
    }
    e->accept();
    return TRUE;
}