Skip to content

Commit c78a378

Browse files
committed
browser last expanded better
1 parent 47a1618 commit c78a378

File tree

6 files changed

+60
-23
lines changed

6 files changed

+60
-23
lines changed

python/core/qgsbrowsermodel.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class QgsBrowserModel : QAbstractItemModel
6161
void refresh( const QModelIndex &index = QModelIndex() );
6262

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

6666
void connectItem( QgsDataItem *item );
6767

src/app/qgsbrowserdockwidget.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
308308
mBrowserView->header()->setResizeMode( 0, QHeaderView::ResizeToContents );
309309
mBrowserView->header()->setStretchLastSection( false );
310310

311+
QSettings settings;
312+
QString lastPath = settings.value( "/BrowserWidget/lastExpanded" ).toString();
313+
311314
// expand root favourites item
312315
for ( int i = 0; i < mModel->rowCount(); i++ )
313316
{
@@ -318,12 +321,13 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
318321
}
319322

320323
// expand last expanded path from previous session
321-
QSettings settings;
322-
QString lastPath = settings.value( "/BrowserWidget/lastExpanded" ).toString();
323324
QgsDebugMsg( "lastPath = " + lastPath );
324325
if ( !lastPath.isEmpty() )
325326
{
326327
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( "/BrowserWidget/lastExpanded", lastPath );
327331
}
328332
}
329333

@@ -657,8 +661,6 @@ void QgsBrowserDockWidget::toggleFastScan()
657661
}
658662
}
659663

660-
661-
662664
void QgsBrowserDockWidget::showFilterWidget( bool visible )
663665
{
664666
mWidgetFilter->setVisible( visible );
@@ -711,7 +713,7 @@ void QgsBrowserDockWidget::expandPath( QString path )
711713

712714
if ( !mModel || !mProxyModel )
713715
return;
714-
QModelIndex srcIndex = mModel->findPath( path );
716+
QModelIndex srcIndex = mModel->findPath( path, Qt::MatchStartsWith );
715717
QModelIndex index = mProxyModel->mapFromSource( srcIndex );
716718
QgsDebugMsg( QString( "srcIndex.isValid() = %1 index.isValid() = %2" ).arg( srcIndex.isValid() ).arg( index.isValid() ) );
717719
if ( index.isValid() )

src/core/qgsbrowsermodel.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ int QgsBrowserModel::columnCount( const QModelIndex &parent ) const
271271
return 1;
272272
}
273273

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

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

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

312+
if ( matchFlag == Qt::MatchStartsWith )
313+
return theIndex;
314+
307315
QgsDebugMsg( "path not found" );
308316
return QModelIndex(); // not found
309317
}

src/core/qgsbrowsermodel.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
8181
// Refresh item childs
8282
void refresh( const QModelIndex &index = QModelIndex() );
8383

84-
//! return index of a path
85-
QModelIndex findPath( QString path );
84+
/** Return index of item with given path.
85+
* @param path item path
86+
* @param matchFlag supported is Qt::MatchExactly and Qt::MatchStartsWith which has reverse meaning, i.e. find
87+
* item with the longest match from start with path (to get as close/deep as possible to deleted item).
88+
* @return model index, invalid if item not found */
89+
QModelIndex findPath( QString path, Qt::MatchFlag matchFlag = Qt::MatchExactly );
8690

8791
void connectItem( QgsDataItem *item );
8892

src/providers/grass/qgsgrassprovidermodule.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
QgsGrassLocationItem::QgsGrassLocationItem( QgsDataItem* parent, QString path )
2626
: QgsDataCollectionItem( parent, "", path )
2727
{
28-
QFileInfo fi( path );
29-
mName = fi.baseName();
28+
// modify path to distinguish from directory, so that it can be expanded by path in browser
29+
mPath = markPath( path );
30+
QDir dir( path );
31+
mName = dir.dirName();
32+
3033
mIcon = QIcon( QgsApplication::getThemePixmap( "grass_location.png" ) );
3134
// set Directory type so that when sorted it gets into dirs (after the dir it represents)
3235
mType = QgsDataItem::Directory;
@@ -43,7 +46,7 @@ QVector<QgsDataItem*>QgsGrassLocationItem::createChildren()
4346
{
4447
QVector<QgsDataItem*> mapsets;
4548

46-
QDir dir( mPath );
49+
QDir dir( clearPath( mPath ) );
4750

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

5356
if ( QgsGrassMapsetItem::isMapset( path ) )
5457
{
55-
QgsGrassMapsetItem * mapset = new QgsGrassMapsetItem( this, path );
58+
QgsGrassMapsetItem * mapset = new QgsGrassMapsetItem( this, mPath + QDir::separator() + name );
5659
mapsets.append( mapset );
5760
}
5861
}
5962
return mapsets;
6063
}
6164

65+
QString QgsGrassLocationItem::markPath( QString path )
66+
{
67+
QDir dir( path );
68+
QString name = dir.dirName();
69+
dir.cdUp();
70+
return dir.path() + QDir::separator() + "gl:" + name;
71+
}
72+
73+
QString QgsGrassLocationItem::clearPath( QString path )
74+
{
75+
return path.remove( "gl:" );
76+
}
77+
6278
QgsGrassMapsetItem::QgsGrassMapsetItem( QgsDataItem* parent, QString path )
6379
: QgsDataCollectionItem( parent, "", path )
6480
{
65-
QDir dir( path );
81+
QDir dir( QgsGrassLocationItem::clearPath( path ) );
6682
mName = dir.dirName();
6783
dir.cdUp();
6884
mLocation = dir.dirName();
@@ -83,9 +99,10 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
8399
{
84100
QgsDebugMsg( "Entered" );
85101

102+
QString clearPath = QgsGrassLocationItem::clearPath( mPath );
86103
QVector<QgsDataItem*> items;
87104

88-
QStringList vectorNames = QgsGrass::vectors( mPath );
105+
QStringList vectorNames = QgsGrass::vectors( clearPath );
89106

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

96113
QgsDataCollectionItem *map = 0;
97114
if ( layerNames.size() != 1 )
98-
map = new QgsDataCollectionItem( this, name );
115+
map = new QgsDataCollectionItem( this, name, path );
99116
foreach ( QString layerName, layerNames )
100117
{
101-
QString uri = mPath + QDir::separator() + name + QDir::separator() + layerName;
118+
QString uri = clearPath + QDir::separator() + name + QDir::separator() + layerName;
102119
QgsLayerItem::LayerType layerType = QgsLayerItem::Vector;
103120
QString typeName = layerName.split( "_" )[1];
104121
QString baseLayerName = layerName.split( "_" )[0];
@@ -127,14 +144,15 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
127144
items.append( map );
128145
}
129146

130-
QStringList rasterNames = QgsGrass::rasters( mPath );
147+
QStringList rasterNames = QgsGrass::rasters( clearPath );
131148

132149
foreach ( QString name, rasterNames )
133150
{
134-
QString uri = mPath + QDir::separator() + "cellhd" + QDir::separator() + name;
151+
QString path = mPath + QDir::separator() + "cellhd" + QDir::separator() + name;
152+
QString uri = clearPath + QDir::separator() + "cellhd" + QDir::separator() + name;
135153
QgsDebugMsg( "uri = " + uri );
136154

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

140158
items.append( layer );

src/providers/grass/qgsgrassprovidermodule.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class QgsGrassLocationItem : public QgsDataCollectionItem
2626

2727
static bool isLocation( QString path );
2828
QVector<QgsDataItem*> createChildren();
29+
30+
/* Add mark to path to distinguish that from directory path */
31+
static QString markPath( QString path );
32+
/* Remove location mark from path */
33+
static QString clearPath( QString path );
2934
};
3035

3136
class QgsGrassMapsetItem : public QgsDataCollectionItem

0 commit comments

Comments
 (0)