Skip to content

Commit d453006

Browse files
committedJan 3, 2019
find index of layeritem with the passed uri
1 parent 1136aab commit d453006

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
 

‎src/core/qgsbrowsermodel.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,30 @@ QModelIndex QgsBrowserModel::findPath( QAbstractItemModel *model, const QString
366366
return QModelIndex(); // not found
367367
}
368368

369+
QModelIndex QgsBrowserModel::findUri( const QString &uri, QModelIndex index )
370+
{
371+
for ( int i = 0; i < this->rowCount( index ); i++ )
372+
{
373+
QModelIndex idx = this->index( i, 0, index );
374+
375+
if ( qobject_cast<QgsLayerItem *>( dataItem( idx ) ) )
376+
{
377+
QString itemUri = qobject_cast<QgsLayerItem *>( dataItem( idx ) )->uri();
378+
379+
if ( itemUri == uri )
380+
{
381+
QgsDebugMsgLevel( "Arrived " + itemUri, 4 );
382+
return idx; // we have found the item we have been looking for
383+
}
384+
}
385+
386+
QModelIndex childIdx = findUri( uri, idx );
387+
if ( childIdx.isValid() )
388+
return childIdx;
389+
}
390+
return QModelIndex();
391+
}
392+
369393
void QgsBrowserModel::connectItem( QgsDataItem * )
370394
{
371395
// deprecated, no use

‎src/core/qgsbrowsermodel.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
136136
//! \note not available in Python bindings
137137
static QModelIndex findPath( QAbstractItemModel *model, const QString &path, Qt::MatchFlag matchFlag = Qt::MatchExactly ) SIP_SKIP;
138138

139+
/**
140+
* Returns index of layer item with given uri. It only searches in currently fetched
141+
* items, i.e. it does not fetch children.
142+
* \param uri item uri
143+
* \param index the current index of the parent (to search for children)
144+
* \returns model index, invalid if item not found */
145+
QModelIndex findUri( const QString &uri, QModelIndex index = QModelIndex() );
146+
139147
/**
140148
* \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!).
141149
*/

0 commit comments

Comments
 (0)
Please sign in to comment.