Skip to content
Permalink
Browse files

re-enabled init expand to last expanded in browser

  • Loading branch information
blazek committed Nov 13, 2014
1 parent bafd3e3 commit cbf1d33b9c4936fcc2e1d9de397825e302e7e246
Showing with 75 additions and 20 deletions.
  1. +61 −14 src/app/qgsbrowserdockwidget.cpp
  2. +6 −0 src/app/qgsbrowserdockwidget.h
  3. +2 −5 src/core/qgsbrowsermodel.cpp
  4. +6 −1 src/core/qgsbrowsermodel.h
@@ -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 );
@@ -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 );
@@ -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";
}
@@ -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:
@@ -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 );
@@ -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
@@ -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;
@@ -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 )
@@ -594,6 +590,7 @@ void QgsBrowserModel::loadingFrameChanged()
{
if ( watcher->isFinished() )
{
delete watcher;
mWatchers.removeOne( watcher );
continue;
}
@@ -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).
@@ -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();

0 comments on commit cbf1d33

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