Core: Go to pattern start when pressing home in locator

If the cursor is already at that position move the cursor to the start
of the line edit. This mimics the editor behavior that home moves the
cursor to the relevant position for that line, and if the cursor is at
that position move to the actual start of the line.

Change-Id: Ieedbc0de0f93b0ffa3f3b64ccbcb04471b004e8e
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
David Schulz
2022-09-26 14:46:48 +02:00
parent 76238bed6c
commit 4780996c46

View File

@@ -718,6 +718,39 @@ bool LocatorWidget::eventFilter(QObject *obj, QEvent *event)
QToolTip::hideText();
auto keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->matches(QKeySequence::MoveToStartOfBlock)
|| keyEvent->matches(QKeySequence::SelectStartOfBlock)
|| keyEvent->matches(QKeySequence::MoveToStartOfLine)
|| keyEvent->matches(QKeySequence::SelectStartOfLine)) {
const int filterEndIndex = currentText().indexOf(' ');
if (filterEndIndex > 0 && filterEndIndex < currentText().length() - 1) {
const bool startsWithShortcutString
= Utils::anyOf(Locator::filters(),
[shortcutString = currentText().left(filterEndIndex)](
const ILocatorFilter *filter) {
return filter->isEnabled() && !filter->isHidden()
&& filter->shortcutString() == shortcutString;
});
if (startsWithShortcutString) {
const int cursorPosition = m_fileLineEdit->cursorPosition();
const int patternStart = filterEndIndex + 1;
const bool mark = keyEvent->matches(QKeySequence::SelectStartOfBlock)
|| keyEvent->matches(QKeySequence::SelectStartOfLine);
if (cursorPosition == patternStart) {
m_fileLineEdit->home(mark);
} else {
const int diff = m_fileLineEdit->cursorPosition() - patternStart;
if (diff < 0)
m_fileLineEdit->cursorForward(mark, qAbs(diff));
else
m_fileLineEdit->cursorBackward(mark, diff);
}
return true;
}
}
}
switch (keyEvent->key()) {
case Qt::Key_PageUp:
case Qt::Key_PageDown: