Skip to content
Permalink
Browse files

browser last expanded better

  • Loading branch information
blazek committed Nov 7, 2014
1 parent 47a1618 commit c78a3783a9d92a752caf4c03fc36ae0366bb2e96
@@ -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 );

@@ -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++ )
{
@@ -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 );
}
}

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



void QgsBrowserDockWidget::showFilterWidget( bool visible )
{
mWidgetFilter->setVisible( visible );
@@ -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() )
@@ -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;
@@ -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 );
@@ -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
}
@@ -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 );

@@ -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;
@@ -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 )
@@ -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();
@@ -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 )
{
@@ -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];
@@ -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 );
@@ -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

0 comments on commit c78a378

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