FileBrowser: Backup expanded directories and restore that state when the tree is reloaded.

This commit is contained in:
CYBERDEViLNL
2019-04-03 19:54:01 +02:00
committed by Johannes Lorenz
parent 96cc5e0e5e
commit 407444ea1d
2 changed files with 38 additions and 8 deletions

View File

@@ -55,7 +55,7 @@ public:
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();
@@ -87,6 +87,10 @@ public:
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 );

View File

@@ -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();
@@ -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<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 )
@@ -189,14 +190,15 @@ void FileBrowser::expandItems( QTreeWidgetItem * item )
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);
}
}
}
@@ -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() ) );