From e5d638ecc02e50209f4cabdf92239656e2c29e6a Mon Sep 17 00:00:00 2001 From: CYBERDEViLNL Date: Wed, 3 Apr 2019 19:54:01 +0200 Subject: [PATCH] FileBrowser: Backup expanded directories and restore that state when the tree is reloaded. --- include/FileBrowser.h | 6 +++++- src/gui/FileBrowser.cpp | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/include/FileBrowser.h b/include/FileBrowser.h index 7e0482ce36f..4b92dd5496d 100644 --- a/include/FileBrowser.h +++ b/include/FileBrowser.h @@ -55,7 +55,7 @@ class FileBrowser : public SideBarWidget private slots: void reloadTree( void ); - void expandItems( QTreeWidgetItem * item=NULL ); + void expandItems( QTreeWidgetItem * item=NULL, QList expandedDirs = QList() ); // call with item=NULL to filter the entire tree bool filterItems( const QString & filter, QTreeWidgetItem * item=NULL ); void giveFocusToFilter(); @@ -87,6 +87,10 @@ class FileBrowserTreeWidget : public QTreeWidget FileBrowserTreeWidget( QWidget * parent ); virtual ~FileBrowserTreeWidget() = default; + //! This method returns a QList with paths (QString's) of all directories + //! that are expanded in the tree. + QList expandedDirs( QTreeWidgetItem * item = nullptr ) const; + protected: virtual void contextMenuEvent( QContextMenuEvent * e ); diff --git a/src/gui/FileBrowser.cpp b/src/gui/FileBrowser.cpp index 4311e4e053b..7eeb87ed29c 100644 --- a/src/gui/FileBrowser.cpp +++ b/src/gui/FileBrowser.cpp @@ -163,6 +163,7 @@ bool FileBrowser::filterItems( const QString & filter, QTreeWidgetItem * item ) void FileBrowser::reloadTree( void ) { + QList expandedDirs = m_fileBrowserTreeWidget->expandedDirs(); const QString text = m_filterEdit->text(); m_filterEdit->clear(); m_fileBrowserTreeWidget->clear(); @@ -171,17 +172,17 @@ void FileBrowser::reloadTree( void ) { addItems( *it ); } - expandItems(); + expandItems(NULL, expandedDirs); m_filterEdit->setText( text ); filterItems( text ); } -void FileBrowser::expandItems( QTreeWidgetItem * item ) +void FileBrowser::expandItems( QTreeWidgetItem * item, QList expandedDirs ) { int numChildren = item ? item->childCount() : m_fileBrowserTreeWidget->topLevelItemCount(); - for( int i = 0; i < numChildren; ++i ) + for (int i = 0; i < numChildren; ++i) { QTreeWidgetItem * it = item ? item->child( i ) : m_fileBrowserTreeWidget->topLevelItem(i); if ( m_recurse ) @@ -189,14 +190,15 @@ void FileBrowser::expandItems( QTreeWidgetItem * item ) it->setExpanded( true ); } Directory *d = dynamic_cast ( it ); - if( d ) + if (d) { d->update(); - d->setExpanded( false ); + bool expand = expandedDirs.contains( d->fullName() ); + d->setExpanded( expand ); } - if( m_recurse && it->childCount() ) + if (m_recurse && it->childCount()) { - expandItems(it); + expandItems(it, expandedDirs); } } } @@ -326,6 +328,30 @@ FileBrowserTreeWidget::FileBrowserTreeWidget(QWidget * parent ) : } +QList FileBrowserTreeWidget::expandedDirs( QTreeWidgetItem * item ) const +{ + int numChildren = item ? item->childCount() : topLevelItemCount(); + QList dirs; + for (int i = 0; i < numChildren; ++i) + { + QTreeWidgetItem * it = item ? item->child(i) : topLevelItem(i); + + // Add expanded top level directories. + if (it->isExpanded() && (it->type() == TypeDirectoryItem)) + { + Directory *d = static_cast ( it ); + dirs.append( d->fullName() ); + } + + // Add expanded child directories (recurse). + if (it->childCount()) + { + dirs.append( expandedDirs( it ) ); + } + } + return dirs; +} + void FileBrowserTreeWidget::contextMenuEvent(QContextMenuEvent * e ) { FileItem * f = dynamic_cast( itemAt( e->pos() ) );