Skip to content
Permalink
Browse files

browser refresh and restore fixes and improvements

  • Loading branch information
blazek committed Nov 20, 2014
1 parent 44a334b commit d897aa5bde418a624caa566632c4e956d4cd93af
@@ -288,7 +288,6 @@ QgsBrowserDockWidget::QgsBrowserDockWidget( QString name, QWidget * parent ) :

connect( mBrowserView, SIGNAL( customContextMenuRequested( const QPoint & ) ), this, SLOT( showContextMenu( const QPoint & ) ) );
connect( mBrowserView, SIGNAL( doubleClicked( const QModelIndex& ) ), this, SLOT( addLayerAtIndex( const QModelIndex& ) ) );
connect( mBrowserView, SIGNAL( expanded( const QModelIndex& ) ), this, SLOT( itemExpanded( const QModelIndex& ) ) );
}

void QgsBrowserDockWidget::showEvent( QShowEvent * e )
@@ -309,28 +308,20 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
mBrowserView->header()->setResizeMode( 0, QHeaderView::ResizeToContents );
mBrowserView->header()->setStretchLastSection( false );

QSettings settings;
mInitPath = settings.value( lastExpandedKey() ).toString();

// expand root favourites item
for ( int i = 0; i < mModel->rowCount(); i++ )
{
QModelIndex index = mModel->index( i, 0 );
QgsDataItem* item = mModel->dataItem( index );
if ( item && item->type() == QgsDataItem::Favourites )
{
QModelIndex proxyIndex = mProxyModel->mapFromSource( index );
mBrowserView->expand( proxyIndex );
}
}

// expand last expanded path from previous session
expandPath( mInitPath );
restoreState();
}

QDockWidget::showEvent( e );
}

// closeEvent is not called when application is closed
void QgsBrowserDockWidget::hideEvent( QHideEvent * e )
{
QgsDebugMsg( "Entered" );
saveState();
QDockWidget::hideEvent( e );
}

void QgsBrowserDockWidget::showContextMenu( const QPoint & pt )
{
QModelIndex index = mProxyModel->mapToSource( mBrowserView->indexAt( pt ) );
@@ -426,38 +417,61 @@ void QgsBrowserDockWidget::removeFavourite()

void QgsBrowserDockWidget::refresh()
{
//QApplication::setOverrideCursor( Qt::WaitCursor );
refreshModel( QModelIndex() );
//QApplication::restoreOverrideCursor();
}

void QgsBrowserDockWidget::refreshModel( const QModelIndex& index )
{
QgsDebugMsg( "Entered" );
if ( index.isValid() )
QgsDataItem *item = mModel->dataItem( index );
if ( item )
{
QgsDataItem *item = mModel->dataItem( index );
if ( item )
{
QgsDebugMsg( "path = " + item->path() );
}
else
{
QgsDebugMsg( "invalid item" );
}
QgsDebugMsg( "path = " + item->path() );
}
else
{
QgsDebugMsg( "invalid item" );
}

mModel->refresh( index );
if ( item && ( item->capabilities2() & QgsDataItem::Fertile ) )
{
mModel->refresh( index );
}

for ( int i = 0 ; i < mModel->rowCount( index ); i++ )
{
QModelIndex idx = mModel->index( i, 0, index );
QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
if ( mBrowserView->isExpanded( proxyIdx ) || !mProxyModel->hasChildren( proxyIdx ) )
QgsDataItem *child = mModel->dataItem( idx );

// Check also expanded descendants so that the whole expanded path does not get collapsed if one item is collapsed.
// Fast items (usually root items) are refreshed so that when collapsed, it is obvious they are if empty (no expand symbol).
if ( mBrowserView->isExpanded( proxyIdx ) || hasExpandedDescendant( proxyIdx ) || ( child && child->capabilities2() & QgsDataItem::Fast ) )
{
refreshModel( idx );
}
else
{
if ( child && ( child->capabilities2() & QgsDataItem::Fertile ) )
{
child->depopulate();
}
}
}
}

bool QgsBrowserDockWidget::hasExpandedDescendant( const QModelIndex& proxyIndex ) const
{
for ( int i = 0 ; i < mProxyModel->rowCount( proxyIndex ); i++ )
{
QModelIndex proxyIdx = mProxyModel->index( i, 0, proxyIndex );
if ( mBrowserView->isExpanded( proxyIdx ) )
return true;

if ( hasExpandedDescendant( proxyIdx ) )
return true;
}
return false;
}

void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem )
@@ -690,83 +704,134 @@ void QgsBrowserDockWidget::setCaseSensitive( bool caseSensitive )
mProxyModel->setCaseSensitive( caseSensitive );
}

void QgsBrowserDockWidget::itemExpanded( const QModelIndex& index )
void QgsBrowserDockWidget::saveState()
{
if ( !mModel || !mProxyModel )
return;
QgsDebugMsg( "Entered" );
QSettings settings;
QModelIndex srcIndex = mProxyModel->mapToSource( index );
QgsDataItem *item = mModel->dataItem( srcIndex );
if ( !item )
return;

settings.setValue( lastExpandedKey(), item->path() );
QgsDebugMsg( "last expanded: " + item->path() );
QStringList expandedPaths = expandedPathsList( QModelIndex() );
settings.setValue( expandedPathsKey(), expandedPaths );
QgsDebugMsg( "expandedPaths = " + expandedPaths.join( " " ) );
}

void QgsBrowserDockWidget::expandPath( QString path )
void QgsBrowserDockWidget::restoreState()
{
QgsDebugMsg( "path = " + path );

if ( path.isEmpty() )
return;

if ( !mModel || !mProxyModel )
return;

QModelIndex srcIndex = mModel->findPath( path, Qt::MatchStartsWith );
QModelIndex index = mProxyModel->mapFromSource( srcIndex );
QgsDebugMsg( QString( "srcIndex.isValid() = %1 index.isValid() = %2" ).arg( srcIndex.isValid() ).arg( index.isValid() ) );

if ( !index.isValid() )
return;

QgsDataItem *item = mModel->dataItem( srcIndex );
if ( !item )
return;
QgsDebugMsg( "Entered" );
QSettings settings;
QStringList expandedPaths = settings.value( expandedPathsKey(), QVariant() ).toStringList();

if ( item->isPopulated() ) // may be already populated if children were added with grandchildren
if ( !expandedPaths.isEmpty() )
{
mBrowserView->expand( index );
QSet<QModelIndex> expandIndexSet;
foreach ( QString path, expandedPaths )
{
QModelIndex expandIndex = mModel->findPath( path, Qt::MatchStartsWith );
if ( expandIndex.isValid() )
expandIndexSet.insert( expandIndex );
}
foreach ( QModelIndex expandIndex, expandIndexSet )
{
QModelIndex proxyExpandIndex = mProxyModel->mapFromSource( expandIndex );
expand( proxyExpandIndex );
}
}
else
{
mModel->fetchMore( srcIndex ); // -> fetch in thread -> fetchFinished
// expand root favourites item
QModelIndex index = mModel->findPath( "favourites:" );
QModelIndex proxyIndex = mProxyModel->mapFromSource( index );
mBrowserView->expand( proxyIndex );
}
mBrowserView->scrollTo( index, QAbstractItemView::PositionAtTop );
}

void QgsBrowserDockWidget::fetchFinished( const QModelIndex & index )
{
Q_UNUSED( index );
QgsDebugMsg( "Entered" );
QSettings settings;

// Continue expanding mInitPath if user has not expanded another item
if ( mInitPath.isEmpty() )
QgsDataItem *item = mModel->dataItem( index );
if ( !item )
return;

QString lastExpanded = settings.value( lastExpandedKey() ).toString();
QgsDebugMsg( "path = " + item->path() );

QSettings settings;
QStringList expandedPaths = settings.value( expandedPathsKey(), QVariant() ).toStringList();

if ( !mInitPath.startsWith( lastExpanded ) )
// Check if user did not collapse it in the meantime
QModelIndex proxyIndex = mProxyModel->mapFromSource( index );
if ( !treeExpanded( proxyIndex ) )
{
// User expanded another -> stop mInitPath expansion
QgsDebugMsg( "Stop init path expansion" );
mInitPath.clear();
foreach ( QString path, expandedPaths )
{
if ( path.startsWith( item->path() + "/" ) )
expandedPaths.removeOne( path );
}
settings.setValue( expandedPathsKey(), expandedPaths );
return;
}

// Expand fetched children in init path
QModelIndex proxyIndex = mProxyModel->mapFromSource( index );
if ( index.isValid() )
QSet<QModelIndex> expandIndexSet;
foreach ( QString path, expandedPaths )
{
mBrowserView->expand( proxyIndex );
if ( path.startsWith( item->path() + "/" ) )
{
QModelIndex expandIndex = mModel->findPath( path, Qt::MatchStartsWith );
if ( expandIndex.isValid() )
expandIndexSet.insert( expandIndex );
}
}
foreach ( QModelIndex expandIndex, expandIndexSet )
{
QModelIndex proxyExpandIndex = mProxyModel->mapFromSource( expandIndex );
expand( proxyExpandIndex );
}
}

void QgsBrowserDockWidget::expand( const QModelIndex & proxyIndex )
{
mBrowserView->expand( proxyIndex );
QModelIndex parentIndex = mProxyModel->parent( proxyIndex );
if ( parentIndex.isValid() )
expand( parentIndex );
}

bool QgsBrowserDockWidget::treeExpanded( const QModelIndex & proxyIndex )
{
if ( !mBrowserView->isExpanded( proxyIndex ) )
return false;
QModelIndex parentIndex = mProxyModel->parent( proxyIndex );
if ( parentIndex.isValid() )
return treeExpanded( parentIndex );

expandPath( mInitPath );
return true; // root
}

QString QgsBrowserDockWidget::expandedPathsKey() const
{
return "/" + objectName().toLower() + "/expandedPaths";
}

QString QgsBrowserDockWidget::lastExpandedKey() const
QStringList QgsBrowserDockWidget::expandedPathsList( const QModelIndex & proxyIndex )
{
return "/" + objectName().toLower() + "/lastExpanded";
QStringList paths;

for ( int i = 0; i < mProxyModel->rowCount( proxyIndex ); i++ )
{
QModelIndex childProxyIndex = mProxyModel->index( i, 0, proxyIndex );
if ( mBrowserView->isExpanded( childProxyIndex ) )
{
QStringList childrenPaths = expandedPathsList( childProxyIndex );
if ( !childrenPaths.isEmpty() )
{
paths.append( childrenPaths );
}
else
{
QModelIndex childIndex = mProxyModel->mapToSource( childProxyIndex );
QgsDataItem* item = mModel->dataItem( childIndex );
if ( item )
{
paths.append( item->path() );
}
}
}
}
return paths;
}
@@ -30,11 +30,9 @@ class APP_EXPORT QgsBrowserDockWidget : public QDockWidget, private Ui::QgsBrows
Q_OBJECT
public:
explicit QgsBrowserDockWidget( QString name, QWidget *parent = 0 );
//~QgsBrowserDockWidget();
void addFavouriteDirectory( QString favDir );

/* Expand next not expanded item in path */
void expandPath( QString path );

public slots:
void addLayerAtIndex( const QModelIndex& index );
void showContextMenu( const QPoint & );
@@ -56,22 +54,36 @@ class APP_EXPORT QgsBrowserDockWidget : public QDockWidget, private Ui::QgsBrows
void showProperties();
void toggleFastScan();

void itemExpanded( const QModelIndex& index );
void fetchFinished( const QModelIndex & index );

protected:
void refreshModel( const QModelIndex& index );

void showEvent( QShowEvent * event );

void addLayer( QgsLayerItem *layerItem );
void hideEvent( QHideEvent * event );

QString lastExpandedKey() const;
void addLayer( QgsLayerItem *layerItem );

QgsBrowserTreeView* mBrowserView;
QgsBrowserModel* mModel;
QgsBrowserTreeFilterProxyModel* mProxyModel;
QString mInitPath;

private:
QString expandedPathsKey() const;
// Get list of expanded items paths recursively
QStringList expandedPathsList( const QModelIndex & proxyIndex );

// Expand path recursively to root
void expand( const QModelIndex & proxyIndex );
// returns true if expanded from root to item
bool treeExpanded( const QModelIndex & proxyIndex );

void saveState();
void restoreState();
// returns true if at least one descendat is expanded, used in refresh
bool hasExpandedDescendant( const QModelIndex& proxyIndex ) const;
};

#endif // QGSBROWSERDOCKWIDGET_H

0 comments on commit d897aa5

Please sign in to comment.
You can’t perform that action at this time.