Skip to content

Commit

Permalink
browser last expanded better
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Nov 7, 2014
1 parent 47a1618 commit c78a378
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion python/core/qgsbrowsermodel.sip
Expand Up @@ -61,7 +61,7 @@ class QgsBrowserModel : QAbstractItemModel
void refresh( const QModelIndex &index = QModelIndex() );

//! return index of a path
QModelIndex findPath( QString path );
QModelIndex findPath( QString path, Qt::MatchFlag matchFlag = Qt::MatchExactly );

void connectItem( QgsDataItem *item );

Expand Down
12 changes: 7 additions & 5 deletions src/app/qgsbrowserdockwidget.cpp
Expand Up @@ -308,6 +308,9 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
mBrowserView->header()->setResizeMode( 0, QHeaderView::ResizeToContents );
mBrowserView->header()->setStretchLastSection( false );

QSettings settings;
QString lastPath = settings.value( "/BrowserWidget/lastExpanded" ).toString();

// expand root favourites item
for ( int i = 0; i < mModel->rowCount(); i++ )
{
Expand All @@ -318,12 +321,13 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
}

// expand last expanded path from previous session
QSettings settings;
QString lastPath = settings.value( "/BrowserWidget/lastExpanded" ).toString();
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( "/BrowserWidget/lastExpanded", lastPath );
}
}

Expand Down Expand Up @@ -657,8 +661,6 @@ void QgsBrowserDockWidget::toggleFastScan()
}
}



void QgsBrowserDockWidget::showFilterWidget( bool visible )
{
mWidgetFilter->setVisible( visible );
Expand Down Expand Up @@ -711,7 +713,7 @@ void QgsBrowserDockWidget::expandPath( QString path )

if ( !mModel || !mProxyModel )
return;
QModelIndex srcIndex = mModel->findPath( path );
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() )
Expand Down
16 changes: 12 additions & 4 deletions src/core/qgsbrowsermodel.cpp
Expand Up @@ -271,7 +271,7 @@ int QgsBrowserModel::columnCount( const QModelIndex &parent ) const
return 1;
}

QModelIndex QgsBrowserModel::findPath( QString path )
QModelIndex QgsBrowserModel::findPath( QString path, Qt::MatchFlag matchFlag )
{
QModelIndex theIndex; // starting from root
bool foundChild = true;
Expand All @@ -280,6 +280,8 @@ QModelIndex QgsBrowserModel::findPath( QString path )
{
foundChild = false; // assume that the next child item will not be found

int bestLength = 0;
QModelIndex bestIndex;
for ( int i = 0; i < rowCount( theIndex ); i++ )
{
QModelIndex idx = index( i, 0, theIndex );
Expand All @@ -293,17 +295,23 @@ QModelIndex QgsBrowserModel::findPath( QString path )
return idx; // we have found the item we have been looking for
}

if ( path.startsWith( item->path() ) )
// not yet perfect, e.g. if a directory was deleted, it can jump to another one which starts with the same name
// but be careful, there are no common path separators, for example WMS contains slashes in its name
if ( path.startsWith( item->path() ) && item->path().length() > bestLength )
{
// we have found a preceding item: stop searching on this level and go deeper
item->populate();
foundChild = true;
theIndex = idx;
break;
bestIndex = idx;
bestLength = item->path().length();
}
}
theIndex = bestIndex;
}

if ( matchFlag == Qt::MatchStartsWith )
return theIndex;

QgsDebugMsg( "path not found" );
return QModelIndex(); // not found
}
Expand Down
8 changes: 6 additions & 2 deletions src/core/qgsbrowsermodel.h
Expand Up @@ -81,8 +81,12 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
// Refresh item childs
void refresh( const QModelIndex &index = QModelIndex() );

//! return index of a path
QModelIndex findPath( QString path );
/** Return index of item with given path.
* @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).
* @return model index, invalid if item not found */
QModelIndex findPath( QString path, Qt::MatchFlag matchFlag = Qt::MatchExactly );

void connectItem( QgsDataItem *item );

Expand Down
40 changes: 29 additions & 11 deletions src/providers/grass/qgsgrassprovidermodule.cpp
Expand Up @@ -25,8 +25,11 @@
QgsGrassLocationItem::QgsGrassLocationItem( QgsDataItem* parent, QString path )
: QgsDataCollectionItem( parent, "", path )
{
QFileInfo fi( path );
mName = fi.baseName();
// modify path to distinguish from directory, so that it can be expanded by path in browser
mPath = markPath( path );
QDir dir( path );
mName = dir.dirName();

mIcon = QIcon( QgsApplication::getThemePixmap( "grass_location.png" ) );
// set Directory type so that when sorted it gets into dirs (after the dir it represents)
mType = QgsDataItem::Directory;
Expand All @@ -43,7 +46,7 @@ QVector<QgsDataItem*>QgsGrassLocationItem::createChildren()
{
QVector<QgsDataItem*> mapsets;

QDir dir( mPath );
QDir dir( clearPath( mPath ) );

QStringList entries = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name );
foreach ( QString name, entries )
Expand All @@ -52,17 +55,30 @@ QVector<QgsDataItem*>QgsGrassLocationItem::createChildren()

if ( QgsGrassMapsetItem::isMapset( path ) )
{
QgsGrassMapsetItem * mapset = new QgsGrassMapsetItem( this, path );
QgsGrassMapsetItem * mapset = new QgsGrassMapsetItem( this, mPath + QDir::separator() + name );
mapsets.append( mapset );
}
}
return mapsets;
}

QString QgsGrassLocationItem::markPath( QString path )
{
QDir dir( path );
QString name = dir.dirName();
dir.cdUp();
return dir.path() + QDir::separator() + "gl:" + name;
}

QString QgsGrassLocationItem::clearPath( QString path )
{
return path.remove( "gl:" );
}

QgsGrassMapsetItem::QgsGrassMapsetItem( QgsDataItem* parent, QString path )
: QgsDataCollectionItem( parent, "", path )
{
QDir dir( path );
QDir dir( QgsGrassLocationItem::clearPath( path ) );
mName = dir.dirName();
dir.cdUp();
mLocation = dir.dirName();
Expand All @@ -83,9 +99,10 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
{
QgsDebugMsg( "Entered" );

QString clearPath = QgsGrassLocationItem::clearPath( mPath );
QVector<QgsDataItem*> items;

QStringList vectorNames = QgsGrass::vectors( mPath );
QStringList vectorNames = QgsGrass::vectors( clearPath );

foreach ( QString name, vectorNames )
{
Expand All @@ -95,10 +112,10 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()

QgsDataCollectionItem *map = 0;
if ( layerNames.size() != 1 )
map = new QgsDataCollectionItem( this, name );
map = new QgsDataCollectionItem( this, name, path );
foreach ( QString layerName, layerNames )
{
QString uri = mPath + QDir::separator() + name + QDir::separator() + layerName;
QString uri = clearPath + QDir::separator() + name + QDir::separator() + layerName;
QgsLayerItem::LayerType layerType = QgsLayerItem::Vector;
QString typeName = layerName.split( "_" )[1];
QString baseLayerName = layerName.split( "_" )[0];
Expand Down Expand Up @@ -127,14 +144,15 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
items.append( map );
}

QStringList rasterNames = QgsGrass::rasters( mPath );
QStringList rasterNames = QgsGrass::rasters( clearPath );

foreach ( QString name, rasterNames )
{
QString uri = mPath + QDir::separator() + "cellhd" + QDir::separator() + name;
QString path = mPath + QDir::separator() + "cellhd" + QDir::separator() + name;
QString uri = clearPath + QDir::separator() + "cellhd" + QDir::separator() + name;
QgsDebugMsg( "uri = " + uri );

QgsLayerItem *layer = new QgsLayerItem( this, name, uri, uri, QgsLayerItem::Raster, "grassraster" );
QgsLayerItem *layer = new QgsLayerItem( this, name, path, uri, QgsLayerItem::Raster, "grassraster" );
layer->populate(); // does nothing, but sets mPopulated to true to show non expandable in browser

items.append( layer );
Expand Down
5 changes: 5 additions & 0 deletions src/providers/grass/qgsgrassprovidermodule.h
Expand Up @@ -26,6 +26,11 @@ class QgsGrassLocationItem : public QgsDataCollectionItem

static bool isLocation( QString path );
QVector<QgsDataItem*> createChildren();

/* Add mark to path to distinguish that from directory path */
static QString markPath( QString path );
/* Remove location mark from path */
static QString clearPath( QString path );
};

class QgsGrassMapsetItem : public QgsDataCollectionItem
Expand Down

0 comments on commit c78a378

Please sign in to comment.