mirror of
https://github.com/LMMS/lmms.git
synced 2026-05-18 19:55:00 -04:00
FileBrowser: Backup expanded directories and restore that state when the tree is reloaded.
This commit is contained in:
committed by
Johannes Lorenz
parent
96cc5e0e5e
commit
407444ea1d
@@ -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 );
|
||||
|
||||
@@ -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() ) );
|
||||
|
||||
Reference in New Issue
Block a user