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 <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2022-01-25 15:35:14 +01:00
parent a4b3085acb
commit 275dcc8f8a
2 changed files with 17 additions and 0 deletions

View File

@@ -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<QMouseEvent *>(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;

View File

@@ -130,6 +130,8 @@ public:
void syncWithFilePath(const Utils::FilePath &filePath);
bool eventFilter(QObject *obj, QEvent *event) override;
protected:
void contextMenuEvent(QContextMenuEvent *ev) override;