@@ -299,6 +299,7 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
299299 mModel = new QgsBrowserModel ( mBrowserView );
300300
301301 connect ( QgisApp::instance (), SIGNAL ( newProject () ), mModel , SLOT ( updateProjectHome () ) );
302+ connect ( mModel , SIGNAL ( fetchFinished ( const QModelIndex & ) ), SLOT ( fetchFinished ( const QModelIndex & ) ) );
302303
303304 mProxyModel = new QgsBrowserTreeFilterProxyModel ( this );
304305 mProxyModel ->setBrowserModel ( mModel );
@@ -309,26 +310,22 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
309310 mBrowserView ->header ()->setStretchLastSection ( false );
310311
311312 QSettings settings;
312- QString lastPath = settings.value ( " / " + objectName (). toLower () + " /lastExpanded " ).toString ();
313+ mInitPath = settings.value ( lastExpandedKey () ).toString ();
313314
314315 // expand root favourites item
315316 for ( int i = 0 ; i < mModel ->rowCount (); i++ )
316317 {
317318 QModelIndex index = mModel ->index ( i, 0 );
318319 QgsDataItem* item = mModel ->dataItem ( index );
319320 if ( item && item->type () == QgsDataItem::Favourites )
320- mBrowserView ->expand ( index );
321+ {
322+ QModelIndex proxyIndex = mProxyModel ->mapFromSource ( index );
323+ mBrowserView ->expand ( proxyIndex );
324+ }
321325 }
322326
323327 // expand last expanded path from previous session
324- QgsDebugMsg ( " lastPath = " + lastPath );
325- if ( !lastPath.isEmpty () )
326- {
327- expandPath ( lastPath );
328- // save again lastExpanded because QTreeView expands items from deepest and last expanded() signal
329- // is called from highest item and that is stored in settings
330- settings.setValue ( " /" + objectName ().toLower () + " /lastExpanded" , lastPath );
331- }
328+ expandPath ( mInitPath );
332329 }
333330
334331 QDockWidget::showEvent ( e );
@@ -703,23 +700,73 @@ void QgsBrowserDockWidget::itemExpanded( const QModelIndex& index )
703700 if ( !item )
704701 return ;
705702
706- settings.setValue ( " / " + objectName (). toLower () + " /lastExpanded " , item->path () );
703+ settings.setValue ( lastExpandedKey () , item->path () );
707704 QgsDebugMsg ( " last expanded: " + item->path () );
708705}
709706
710707void QgsBrowserDockWidget::expandPath ( QString path )
711708{
712- return ; // debug
713709 QgsDebugMsg ( " path = " + path );
714710
711+ if ( path.isEmpty () )
712+ return ;
713+
715714 if ( !mModel || !mProxyModel )
716715 return ;
716+
717717 QModelIndex srcIndex = mModel ->findPath ( path, Qt::MatchStartsWith );
718718 QModelIndex index = mProxyModel ->mapFromSource ( srcIndex );
719719 QgsDebugMsg ( QString ( " srcIndex.isValid() = %1 index.isValid() = %2" ).arg ( srcIndex.isValid () ).arg ( index.isValid () ) );
720- if ( index.isValid () )
720+
721+ if ( !index.isValid () )
722+ return ;
723+
724+ QgsDataItem *item = mModel ->dataItem ( srcIndex );
725+ if ( !item )
726+ return ;
727+
728+ if ( item->isPopulated () ) // may be already populated if children were added with grandchildren
721729 {
722730 mBrowserView ->expand ( index );
723- mBrowserView ->scrollTo ( index, QAbstractItemView::PositionAtTop );
724731 }
732+ else
733+ {
734+ mModel ->fetchMore ( srcIndex ); // -> fetch in thread -> fetchFinished
735+ }
736+ mBrowserView ->scrollTo ( index, QAbstractItemView::PositionAtTop );
737+ }
738+
739+ void QgsBrowserDockWidget::fetchFinished ( const QModelIndex & index )
740+ {
741+ Q_UNUSED ( index );
742+ QgsDebugMsg ( " Entered" );
743+ QSettings settings;
744+
745+ // Continue expanding mInitPath if user has not expanded another item
746+ if ( mInitPath .isEmpty () )
747+ return ;
748+
749+ QString lastExpanded = settings.value ( lastExpandedKey () ).toString ();
750+
751+ if ( !mInitPath .startsWith ( lastExpanded ) )
752+ {
753+ // User expanded another -> stop mInitPath expansion
754+ QgsDebugMsg ( " Stop init path expansion" );
755+ mInitPath .clear ();
756+ return ;
757+ }
758+
759+ // Expand fetched children in init path
760+ QModelIndex proxyIndex = mProxyModel ->mapFromSource ( index );
761+ if ( index.isValid () )
762+ {
763+ mBrowserView ->expand ( proxyIndex );
764+ }
765+
766+ expandPath ( mInitPath );
767+ }
768+
769+ QString QgsBrowserDockWidget::lastExpandedKey () const
770+ {
771+ return " /" + objectName ().toLower () + " /lastExpanded" ;
725772}
0 commit comments