Skip to content

Commit

Permalink
re-enabled init expand to last expanded in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Nov 13, 2014
1 parent bafd3e3 commit cbf1d33
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
75 changes: 61 additions & 14 deletions src/app/qgsbrowserdockwidget.cpp
Expand Up @@ -299,6 +299,7 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
mModel = new QgsBrowserModel( mBrowserView );

connect( QgisApp::instance(), SIGNAL( newProject() ), mModel, SLOT( updateProjectHome() ) );
connect( mModel, SIGNAL( fetchFinished( const QModelIndex & ) ), SLOT( fetchFinished( const QModelIndex & ) ) );

mProxyModel = new QgsBrowserTreeFilterProxyModel( this );
mProxyModel->setBrowserModel( mModel );
Expand All @@ -309,26 +310,22 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
mBrowserView->header()->setStretchLastSection( false );

QSettings settings;
QString lastPath = settings.value( "/" + objectName().toLower() + "/lastExpanded" ).toString();
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 )
mBrowserView->expand( index );
{
QModelIndex proxyIndex = mProxyModel->mapFromSource( index );
mBrowserView->expand( proxyIndex );
}
}

// expand last expanded path from previous session
QgsDebugMsg( "lastPath = " + lastPath );
if ( !lastPath.isEmpty() )
{
expandPath( lastPath );
// save again lastExpanded because QTreeView expands items from deepest and last expanded() signal
// is called from highest item and that is stored in settings
settings.setValue( "/" + objectName().toLower() + "/lastExpanded", lastPath );
}
expandPath( mInitPath );
}

QDockWidget::showEvent( e );
Expand Down Expand Up @@ -703,23 +700,73 @@ void QgsBrowserDockWidget::itemExpanded( const QModelIndex& index )
if ( !item )
return;

settings.setValue( "/" + objectName().toLower() + "/lastExpanded", item->path() );
settings.setValue( lastExpandedKey(), item->path() );
QgsDebugMsg( "last expanded: " + item->path() );
}

void QgsBrowserDockWidget::expandPath( QString path )
{
return; // debug
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() )

if ( !index.isValid() )
return;

QgsDataItem *item = mModel->dataItem( srcIndex );
if ( !item )
return;

if ( item->isPopulated() ) // may be already populated if children were added with grandchildren
{
mBrowserView->expand( index );
mBrowserView->scrollTo( index, QAbstractItemView::PositionAtTop );
}
else
{
mModel->fetchMore( srcIndex ); // -> fetch in thread -> fetchFinished
}
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() )
return;

QString lastExpanded = settings.value( lastExpandedKey() ).toString();

if ( !mInitPath.startsWith( lastExpanded ) )
{
// User expanded another -> stop mInitPath expansion
QgsDebugMsg( "Stop init path expansion" );
mInitPath.clear();
return;
}

// Expand fetched children in init path
QModelIndex proxyIndex = mProxyModel->mapFromSource( index );
if ( index.isValid() )
{
mBrowserView->expand( proxyIndex );
}

expandPath( mInitPath );
}

QString QgsBrowserDockWidget::lastExpandedKey() const
{
return "/" + objectName().toLower() + "/lastExpanded";
}
6 changes: 6 additions & 0 deletions src/app/qgsbrowserdockwidget.h
Expand Up @@ -31,6 +31,8 @@ class APP_EXPORT QgsBrowserDockWidget : public QDockWidget, private Ui::QgsBrows
public:
explicit QgsBrowserDockWidget( QString name, QWidget *parent = 0 );
void addFavouriteDirectory( QString favDir );

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

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

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

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

void addLayer( QgsLayerItem *layerItem );

QString lastExpandedKey() const;

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

#endif // QGSBROWSERDOCKWIDGET_H
7 changes: 2 additions & 5 deletions src/core/qgsbrowsermodel.cpp
Expand Up @@ -329,11 +329,6 @@ QModelIndex QgsBrowserModel::findPath( QString path, Qt::MatchFlag matchFlag )
// paths are slash separated identifier
if ( path.startsWith( item->path() + "/" ) )
{
// We have found a preceding item: stop searching on this level and go deeper.
// Currently some providers (e.g. Postgres) are using multithread in
// QgsDataItem::createChildren(), i.e. we cannot get to children here as they
// are not yet created by separate thread
item->populate();
foundChild = true;
theIndex = idx;
break;
Expand Down Expand Up @@ -564,6 +559,7 @@ void QgsBrowserModel::childrenCreated( QgsDataItem* item, QVector <QgsDataItem*>
return;
item->populate( children );
emit dataChanged( index, index );
emit fetchFinished( index );
}

void QgsBrowserModel::refreshChildrenCreated( QgsDataItem* item, QVector <QgsDataItem*> children )
Expand Down Expand Up @@ -594,6 +590,7 @@ void QgsBrowserModel::loadingFrameChanged()
{
if ( watcher->isFinished() )
{
delete watcher;
mWatchers.removeOne( watcher );
continue;
}
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsbrowsermodel.h
Expand Up @@ -108,7 +108,8 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
// Refresh item childs
void refresh( const QModelIndex &index = QModelIndex() );

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

signals:
/** Emited when item children fetch was finished */
void fetchFinished( const QModelIndex & index );

public slots:
// Reload the whole model
void reload();
Expand Down

0 comments on commit cbf1d33

Please sign in to comment.