Skip to content

Commit

Permalink
find index of layeritem with the passed uri
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav committed Jan 3, 2019
1 parent 1136aab commit d453006
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/core/qgsbrowsermodel.cpp
Expand Up @@ -366,6 +366,30 @@ QModelIndex QgsBrowserModel::findPath( QAbstractItemModel *model, const QString
return QModelIndex(); // not found
}

QModelIndex QgsBrowserModel::findUri( const QString &uri, QModelIndex index )
{
for ( int i = 0; i < this->rowCount( index ); i++ )
{
QModelIndex idx = this->index( i, 0, index );

if ( qobject_cast<QgsLayerItem *>( dataItem( idx ) ) )
{
QString itemUri = qobject_cast<QgsLayerItem *>( dataItem( idx ) )->uri();

if ( itemUri == uri )
{
QgsDebugMsgLevel( "Arrived " + itemUri, 4 );
return idx; // we have found the item we have been looking for
}
}

QModelIndex childIdx = findUri( uri, idx );
if ( childIdx.isValid() )
return childIdx;
}
return QModelIndex();
}

void QgsBrowserModel::connectItem( QgsDataItem * )
{
// deprecated, no use
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsbrowsermodel.h
Expand Up @@ -136,6 +136,14 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
//! \note not available in Python bindings
static QModelIndex findPath( QAbstractItemModel *model, const QString &path, Qt::MatchFlag matchFlag = Qt::MatchExactly ) SIP_SKIP;

/**
* Returns index of layer item with given uri. It only searches in currently fetched
* items, i.e. it does not fetch children.
* \param uri item uri
* \param index the current index of the parent (to search for children)
* \returns model index, invalid if item not found */
QModelIndex findUri( const QString &uri, QModelIndex index = QModelIndex() );

/**
* \deprecated Deprecated since QGIS 3.4 -- this method has no effect, and is dangerous to call in earlier QGIS versions. Any usage should be removed (and will have no harmful side-effects!).
*/
Expand Down

0 comments on commit d453006

Please sign in to comment.