Skip to content

Commit

Permalink
FileBrowser: Backup expanded directories and restore that state when …
Browse files Browse the repository at this point in the history
…the tree is reloaded.
  • Loading branch information
CYBERDEViLNL authored and JohannesLorenz committed Apr 20, 2019
1 parent 96cc5e0 commit 407444e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
6 changes: 5 additions & 1 deletion include/FileBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FileBrowser : public SideBarWidget

private slots:
void reloadTree( void );
void expandItems( QTreeWidgetItem * item=NULL );
void expandItems( QTreeWidgetItem * item=NULL, QList<QString> expandedDirs = QList<QString>() );
// call with item=NULL to filter the entire tree
bool filterItems( const QString & filter, QTreeWidgetItem * item=NULL );
void giveFocusToFilter();
Expand Down Expand Up @@ -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<QString> expandedDirs( QTreeWidgetItem * item = nullptr ) const;


protected:
virtual void contextMenuEvent( QContextMenuEvent * e );
Expand Down
40 changes: 33 additions & 7 deletions src/gui/FileBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ bool FileBrowser::filterItems( const QString & filter, QTreeWidgetItem * item )

void FileBrowser::reloadTree( void )
{
QList<QString> expandedDirs = m_fileBrowserTreeWidget->expandedDirs();
const QString text = m_filterEdit->text();
m_filterEdit->clear();
m_fileBrowserTreeWidget->clear();
Expand All @@ -171,32 +172,33 @@ 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<QString> 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 )
{
it->setExpanded( true );
}
Directory *d = dynamic_cast<Directory *> ( 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);
}
}
}
Expand Down Expand Up @@ -326,6 +328,30 @@ FileBrowserTreeWidget::FileBrowserTreeWidget(QWidget * parent ) :

}

QList<QString> FileBrowserTreeWidget::expandedDirs( QTreeWidgetItem * item ) const
{
int numChildren = item ? item->childCount() : topLevelItemCount();
QList<QString> 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<Directory *> ( 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<FileItem *>( itemAt( e->pos() ) );
Expand Down

0 comments on commit 407444e

Please sign in to comment.