Skip to content

Commit cbf1d33

Browse files
committedNov 13, 2014
re-enabled init expand to last expanded in browser
1 parent bafd3e3 commit cbf1d33

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed
 

‎src/app/qgsbrowserdockwidget.cpp

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

710707
void 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
}

‎src/app/qgsbrowserdockwidget.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class APP_EXPORT QgsBrowserDockWidget : public QDockWidget, private Ui::QgsBrows
3131
public:
3232
explicit QgsBrowserDockWidget( QString name, QWidget *parent = 0 );
3333
void addFavouriteDirectory( QString favDir );
34+
35+
/* Expand next not expanded item in path */
3436
void expandPath( QString path );
3537

3638
public slots:
@@ -55,6 +57,7 @@ class APP_EXPORT QgsBrowserDockWidget : public QDockWidget, private Ui::QgsBrows
5557
void toggleFastScan();
5658

5759
void itemExpanded( const QModelIndex& index );
60+
void fetchFinished( const QModelIndex & index );
5861

5962
protected:
6063
void refreshModel( const QModelIndex& index );
@@ -63,9 +66,12 @@ class APP_EXPORT QgsBrowserDockWidget : public QDockWidget, private Ui::QgsBrows
6366

6467
void addLayer( QgsLayerItem *layerItem );
6568

69+
QString lastExpandedKey() const;
70+
6671
QgsBrowserTreeView* mBrowserView;
6772
QgsBrowserModel* mModel;
6873
QgsBrowserTreeFilterProxyModel* mProxyModel;
74+
QString mInitPath;
6975
};
7076

7177
#endif // QGSBROWSERDOCKWIDGET_H

‎src/core/qgsbrowsermodel.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,6 @@ QModelIndex QgsBrowserModel::findPath( QString path, Qt::MatchFlag matchFlag )
329329
// paths are slash separated identifier
330330
if ( path.startsWith( item->path() + "/" ) )
331331
{
332-
// We have found a preceding item: stop searching on this level and go deeper.
333-
// Currently some providers (e.g. Postgres) are using multithread in
334-
// QgsDataItem::createChildren(), i.e. we cannot get to children here as they
335-
// are not yet created by separate thread
336-
item->populate();
337332
foundChild = true;
338333
theIndex = idx;
339334
break;
@@ -564,6 +559,7 @@ void QgsBrowserModel::childrenCreated( QgsDataItem* item, QVector <QgsDataItem*>
564559
return;
565560
item->populate( children );
566561
emit dataChanged( index, index );
562+
emit fetchFinished( index );
567563
}
568564

569565
void QgsBrowserModel::refreshChildrenCreated( QgsDataItem* item, QVector <QgsDataItem*> children )
@@ -594,6 +590,7 @@ void QgsBrowserModel::loadingFrameChanged()
594590
{
595591
if ( watcher->isFinished() )
596592
{
593+
delete watcher;
597594
mWatchers.removeOne( watcher );
598595
continue;
599596
}

‎src/core/qgsbrowsermodel.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
108108
// Refresh item childs
109109
void refresh( const QModelIndex &index = QModelIndex() );
110110

111-
/** Return index of item with given path.
111+
/** Return index of item with given path. It only searches in currently fetched
112+
* items, i.e. it does not fetch children.
112113
* @param path item path
113114
* @param matchFlag supported is Qt::MatchExactly and Qt::MatchStartsWith which has reverse meaning, i.e. find
114115
* item with the longest match from start with path (to get as close/deep as possible to deleted item).
@@ -122,6 +123,10 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
122123
static QVector<QgsDataItem*> createChildren( QgsDataItem *item );
123124
bool fetching( QgsDataItem *item ) const;
124125

126+
signals:
127+
/** Emited when item children fetch was finished */
128+
void fetchFinished( const QModelIndex & index );
129+
125130
public slots:
126131
// Reload the whole model
127132
void reload();

0 commit comments

Comments
 (0)
Please sign in to comment.