Fix activating the Selection Mode with a keyboard shortcut

If a spacebar is used as a keyboard shortcut to activate the Selection Mode, then allow this shortcut to be triggered only if the view has a keyboard focus.

BUG: 465489
This commit is contained in:
Eugene Popov 2023-04-25 13:05:39 +00:00 committed by Felix Ernst
parent f186f694d4
commit 7b6a67e520
8 changed files with 60 additions and 18 deletions

View file

@ -547,6 +547,19 @@ void DolphinMainWindow::showTarget()
}); });
} }
bool DolphinMainWindow::event(QEvent *event)
{
if (event->type() == QEvent::ShortcutOverride) {
const QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Space && m_activeViewContainer->view()->handleSpaceAsNormalKey()) {
event->accept();
return true;
}
}
return KXmlGuiWindow::event(event);
}
void DolphinMainWindow::showEvent(QShowEvent *event) void DolphinMainWindow::showEvent(QShowEvent *event)
{ {
KXmlGuiWindow::showEvent(event); KXmlGuiWindow::showEvent(event);
@ -1722,6 +1735,7 @@ void DolphinMainWindow::setupActions()
"</para>")); "</para>"));
toggleSelectionModeAction->setIcon(QIcon::fromTheme(QStringLiteral("quickwizard"))); toggleSelectionModeAction->setIcon(QIcon::fromTheme(QStringLiteral("quickwizard")));
toggleSelectionModeAction->setCheckable(true); toggleSelectionModeAction->setCheckable(true);
actionCollection()->setDefaultShortcut(toggleSelectionModeAction, Qt::Key_Space );
connect(toggleSelectionModeAction, &QAction::triggered, this, &DolphinMainWindow::toggleSelectionMode); connect(toggleSelectionModeAction, &QAction::triggered, this, &DolphinMainWindow::toggleSelectionMode);
// A special version of the toggleSelectionModeAction for the toolbar that also contains a menu // A special version of the toggleSelectionModeAction for the toolbar that also contains a menu

View file

@ -238,6 +238,9 @@ Q_SIGNALS:
void settingsChanged(); void settingsChanged();
protected: protected:
/** @see QObject::event() */
bool event(QEvent *event) override;
/** @see QWidget::showEvent() */ /** @see QWidget::showEvent() */
void showEvent(QShowEvent *event) override; void showEvent(QShowEvent *event) override;

View file

@ -228,6 +228,11 @@ bool KItemListController::selectionMode() const
return m_selectionMode; return m_selectionMode;
} }
bool KItemListController::isSearchAsYouTypeActive() const
{
return m_keyboardManager->isSearchAsYouTypeActive();
}
bool KItemListController::keyPressEvent(QKeyEvent *event) bool KItemListController::keyPressEvent(QKeyEvent *event)
{ {
int index = m_selectionManager->currentItem(); int index = m_selectionManager->currentItem();
@ -439,10 +444,13 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle); m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
m_selectionManager->beginAnchoredSelection(index); m_selectionManager->beginAnchoredSelection(index);
break; break;
} else if (m_keyboardManager->addKeyBeginsNewSearch()) { // File names shouldn't start with a space, } else {
// so we can use this press as a keyboard shortcut instead. // Select the current item if it is not selected yet.
Q_EMIT selectionModeChangeRequested(!m_selectionMode); const int current = m_selectionManager->currentItem();
break; if (!m_selectionManager->isSelected(current)) {
m_selectionManager->setSelected(current);
break;
}
} }
} }
Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string. Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.

View file

@ -123,6 +123,11 @@ public:
void setSelectionModeEnabled(bool enabled); void setSelectionModeEnabled(bool enabled);
bool selectionMode() const; bool selectionMode() const;
/**
* @return \c true if search as you type is active, or \c false otherwise.
*/
bool isSearchAsYouTypeActive() const;
bool processEvent(QEvent *event, const QTransform &transform); bool processEvent(QEvent *event, const QTransform &transform);
Q_SIGNALS: Q_SIGNALS:

View file

@ -27,6 +27,11 @@ bool KItemListKeyboardSearchManager::shouldClearSearchIfInputTimeReached()
return (keyboardInputTimeElapsed > m_timeout) || !keyboardTimeWasValid; return (keyboardInputTimeElapsed > m_timeout) || !keyboardTimeWasValid;
} }
bool KItemListKeyboardSearchManager::isSearchAsYouTypeActive() const
{
return !m_searchedString.isEmpty() && !m_keyboardInputTime.hasExpired(m_timeout);
}
void KItemListKeyboardSearchManager::addKeys(const QString &keys) void KItemListKeyboardSearchManager::addKeys(const QString &keys)
{ {
if (shouldClearSearchIfInputTimeReached()) { if (shouldClearSearchIfInputTimeReached()) {
@ -63,11 +68,6 @@ void KItemListKeyboardSearchManager::addKeys(const QString &keys)
m_keyboardInputTime.start(); m_keyboardInputTime.start();
} }
bool KItemListKeyboardSearchManager::addKeyBeginsNewSearch() const
{
return m_keyboardInputTime.hasExpired(m_timeout) || m_searchedString.isEmpty();
}
void KItemListKeyboardSearchManager::setTimeout(qint64 milliseconds) void KItemListKeyboardSearchManager::setTimeout(qint64 milliseconds)
{ {
m_timeout = milliseconds; m_timeout = milliseconds;

View file

@ -34,11 +34,6 @@ public:
* Add \a keys to the text buffer used for searching. * Add \a keys to the text buffer used for searching.
*/ */
void addKeys(const QString &keys); void addKeys(const QString &keys);
/**
* @returns true if the next call to addKeys() will trigger a new search.
* Returns false if the next added key char will be added to the search string that was used previously.
*/
bool addKeyBeginsNewSearch() const;
/** /**
* Sets the delay after which the search is cancelled to \a milliseconds. * Sets the delay after which the search is cancelled to \a milliseconds.
@ -51,6 +46,11 @@ public:
void cancelSearch(); void cancelSearch();
/**
* @return \c true if search as you type is active, or \c false otherwise.
*/
bool isSearchAsYouTypeActive() const;
public Q_SLOTS: public Q_SLOTS:
void slotCurrentChanged(int current, int previous); void slotCurrentChanged(int current, int previous);
@ -71,7 +71,6 @@ Q_SIGNALS:
private: private:
bool shouldClearSearchIfInputTimeReached(); bool shouldClearSearchIfInputTimeReached();
private:
QString m_searchedString; QString m_searchedString;
bool m_isSearchRestarted; bool m_isSearchRestarted;
/** Measures the time since the last key press. */ /** Measures the time since the last key press. */

View file

@ -1828,6 +1828,11 @@ void DolphinView::hideToolTip(const ToolTipManager::HideBehavior behavior)
} }
} }
bool DolphinView::handleSpaceAsNormalKey() const
{
return !m_container->hasFocus() || m_container->controller()->isSearchAsYouTypeActive();
}
void DolphinView::slotTwoClicksRenamingTimerTimeout() void DolphinView::slotTwoClicksRenamingTimerTimeout()
{ {
const KItemListSelectionManager *selectionManager = m_container->controller()->selectionManager(); const KItemListSelectionManager *selectionManager = m_container->controller()->selectionManager();

View file

@ -335,6 +335,17 @@ public:
*/ */
void hideToolTip(const ToolTipManager::HideBehavior behavior = ToolTipManager::HideBehavior::Later); void hideToolTip(const ToolTipManager::HideBehavior behavior = ToolTipManager::HideBehavior::Later);
/**
* Check if the space key should be handled as a normal key, even if it's
* used as a keyboard shortcut.
*
* See BUG 465489
*/
bool handleSpaceAsNormalKey() const;
/** Activates the view if the item list container gets focus. */
bool eventFilter(QObject *watched, QEvent *event) override;
public Q_SLOTS: public Q_SLOTS:
/** /**
* Changes the directory to \a url. If the current directory is equal to * Changes the directory to \a url. If the current directory is equal to
@ -426,9 +437,6 @@ public Q_SLOTS:
*/ */
void updateViewState(); void updateViewState();
/** Activates the view if the item list container gets focus. */
bool eventFilter(QObject *watched, QEvent *event) override;
Q_SIGNALS: Q_SIGNALS:
/** /**
* Is emitted if the view has been activated by e. g. a mouse click. * Is emitted if the view has been activated by e. g. a mouse click.