QShortcutMap::find: Use plain for-loop instead of do-while

The logic was a bit convoluted, using a do-while loop when the
base premise still was that we needed a valid iterator, and
hiding the check of a QKeySequence::NoMatch to break out of
the loop far from the match() that produced it.

The tempRes (now match) variable no longer has to live outside
the loop, and the oneKSResult variable has been renamed to
clarify its use.

Task-number: QTBUG-116873
Change-Id: I730e768eae2e9a653bf4e28ceece2fe7277ef45d
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Andrey Butirsky <butirsky@gmail.com>
bb10
Tor Arne Vestbø 2023-09-15 20:45:33 +02:00
parent e2738ca949
commit 3aff1e1678
1 changed files with 21 additions and 19 deletions

View File

@ -410,26 +410,30 @@ QKeySequence::SequenceMatch QShortcutMap::find(QKeyEvent *e, int ignoredModifier
QKeySequence::SequenceMatch result = QKeySequence::NoMatch;
for (int i = d->newEntries.size()-1; i >= 0 ; --i) {
QShortcutEntry entry(d->newEntries.at(i)); // needed for searching
qCDebug(lcShortcutMap) << "- checking entry" << entry.id << entry.keyseq;
qCDebug(lcShortcutMap) << "Looking for shortcuts matching" << entry.keyseq;
QKeySequence::SequenceMatch bestMatchForEntry = QKeySequence::NoMatch;
const auto itEnd = d->sequences.constEnd();
auto it = std::lower_bound(d->sequences.constBegin(), itEnd, entry);
for (; it != itEnd; ++it) {
QKeySequence::SequenceMatch match = matches(entry.keyseq, (*it).keyseq);
qCDebug(lcShortcutMap) << " -" << match << "for shortcut" << it->keyseq;
QKeySequence::SequenceMatch oneKSResult = QKeySequence::NoMatch;
QKeySequence::SequenceMatch tempRes = QKeySequence::NoMatch;
do {
if (it == itEnd)
// If we got a valid match, there might still be more keys to check against,
// but if we get no match, we know that there are no more possible matches.
if (match == QKeySequence::NoMatch)
break;
tempRes = matches(entry.keyseq, (*it).keyseq);
oneKSResult = qMax(oneKSResult, tempRes);
qCDebug(lcShortcutMap) << " - matches returned" << tempRes << "for" << entry.keyseq << it->keyseq
<< "- correctContext()?" << it->correctContext();
if (tempRes != QKeySequence::NoMatch && (*it).correctContext()) {
if (tempRes == QKeySequence::ExactMatch) {
bestMatchForEntry = qMax(bestMatchForEntry, match);
if ((*it).correctContext()) {
if (match == QKeySequence::ExactMatch) {
if ((*it).enabled)
d->identicals.append(&*it);
else
identicalDisabledFound = true;
} else if (tempRes == QKeySequence::PartialMatch) {
} else if (match == QKeySequence::PartialMatch) {
// We don't need partials, if we have identicals
if (d->identicals.size())
break;
@ -437,20 +441,18 @@ QKeySequence::SequenceMatch QShortcutMap::find(QKeyEvent *e, int ignoredModifier
// key events when all partials are disabled!
partialFound |= (*it).enabled;
}
} else {
qCDebug(lcShortcutMap) << " - But context was not correct";
}
++it;
// If we got a valid match on this run, there might still be more keys to check against,
// so we'll loop once more. If we get NoMatch, there's guaranteed no more possible
// matches in the shortcutmap.
} while (tempRes != QKeySequence::NoMatch);
}
// If the type of match improves (ergo, NoMatch->Partial, or Partial->Exact), clear the
// previous list. If this match is equal or better than the last match, append to the list
if (oneKSResult > result) {
if (bestMatchForEntry > result) {
okEntries.clear();
qCDebug(lcShortcutMap) << "Found better match (" << d->newEntries << "), clearing key sequence list";
}
if (oneKSResult && oneKSResult >= result) {
if (bestMatchForEntry && bestMatchForEntry >= result) {
okEntries << d->newEntries.at(i);
qCDebug(lcShortcutMap) << "Added ok key sequence" << d->newEntries;
}