From 275dcc8f8aae242c2260454543cbaa51508dae0c Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Tue, 25 Jan 2022 15:35:14 +0100 Subject: [PATCH] File System view: Select root folder when clicking outside item If the shown items are not filling the whole view, there is some empty space at the bottom. Select the current root directory when the user click there. This avoids funny behavior. Without that change - select a file or subdirectory in a directory - collapse the parent directory - the selected file or subdirectory stays selected, but that is not visible - right-click into the empty space at the bottom - the context menu for the not-visibly selected item is shown - this is especially weird when e.g. choosing "New Folder" That is not an issue if the view fills the whole space, because then a visible item is selected when clicking. Change-Id: I0c65e091e659c20d7b384e96ecf69c36379ed936 Reviewed-by: David Schulz --- src/plugins/coreplugin/foldernavigationwidget.cpp | 15 +++++++++++++++ src/plugins/coreplugin/foldernavigationwidget.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/src/plugins/coreplugin/foldernavigationwidget.cpp b/src/plugins/coreplugin/foldernavigationwidget.cpp index 4726b3264a4..52cae412877 100644 --- a/src/plugins/coreplugin/foldernavigationwidget.cpp +++ b/src/plugins/coreplugin/foldernavigationwidget.cpp @@ -299,6 +299,7 @@ FolderNavigationWidget::FolderNavigationWidget(QWidget *parent) : QWidget(parent m_listView->setEditTriggers(QAbstractItemView::NoEditTriggers); m_listView->setDragEnabled(true); m_listView->setDragDropMode(QAbstractItemView::DragOnly); + m_listView->viewport()->installEventFilter(this); showOnlyFirstColumn(m_listView); setFocusProxy(m_listView); @@ -521,6 +522,20 @@ void FolderNavigationWidget::syncWithFilePath(const Utils::FilePath &filePath) selectFile(filePath); } +bool FolderNavigationWidget::eventFilter(QObject *obj, QEvent *event) +{ + if (obj == m_listView->viewport()) { + if (event->type() == QEvent::MouseButtonPress) { + // select the current root when clicking outside any other item + auto me = static_cast(event); + const QModelIndex index = m_listView->indexAt(me->pos()); + if (!index.isValid()) + m_listView->setCurrentIndex(m_listView->rootIndex()); + } + } + return false; +} + bool FolderNavigationWidget::autoSynchronization() const { return m_autoSync; diff --git a/src/plugins/coreplugin/foldernavigationwidget.h b/src/plugins/coreplugin/foldernavigationwidget.h index dbc3baec370..4cee261369a 100644 --- a/src/plugins/coreplugin/foldernavigationwidget.h +++ b/src/plugins/coreplugin/foldernavigationwidget.h @@ -130,6 +130,8 @@ public: void syncWithFilePath(const Utils::FilePath &filePath); + bool eventFilter(QObject *obj, QEvent *event) override; + protected: void contextMenuEvent(QContextMenuEvent *ev) override;