QGIS API Documentation 3.43.0-Master (b60ef06885e)
qgsmaplayerproxymodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayerproxymodel.cpp
3 --------------------------------------
4 Date : 01.04.2014
5 Copyright : (C) 2014 Denis Rouzaud
6 Email : denis.rouzaud@gmail.com
7***************************************************************************
8* *
9* This program is free software; you can redistribute it and/or modify *
10* it under the terms of the GNU General Public License as published by *
11* the Free Software Foundation; either version 2 of the License, or *
12* (at your option) any later version. *
13* *
14***************************************************************************/
15
17#include "moc_qgsmaplayerproxymodel.cpp"
18#include "qgsmaplayermodel.h"
19#include "qgsmaplayer.h"
20#include "qgsproject.h"
21#include "qgsvectorlayer.h"
22
24 : QSortFilterProxyModel( parent )
25 , mFilters( Qgis::LayerFilter::All )
26 , mModel( new QgsMapLayerModel( parent ) )
27{
28 setSourceModel( mModel );
29 setDynamicSortFilter( true );
30 setSortLocaleAware( true );
31 setFilterCaseSensitivity( Qt::CaseInsensitive );
32 sort( 0 );
33}
34
36{
37 mFilters = filters;
38 invalidateFilter();
39 return this;
40}
41
43{
44 if ( filters.testFlag( Qgis::LayerFilter::WritableLayer ) && layer->readOnly() )
45 return false;
46
47 if ( filters.testFlag( Qgis::LayerFilter::All ) )
48 return true;
49
50 // layer type
51 if ( ( filters.testFlag( Qgis::LayerFilter::RasterLayer ) && layer->type() == Qgis::LayerType::Raster ) ||
53 ( filters.testFlag( Qgis::LayerFilter::MeshLayer ) && layer->type() == Qgis::LayerType::Mesh ) ||
59 return true;
60
61 // geometry type
62 const bool detectGeometry = filters.testFlag( Qgis::LayerFilter::NoGeometry ) ||
66 if ( detectGeometry && layer->type() == Qgis::LayerType::Vector )
67 {
68 if ( const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
69 {
70 if ( filters.testFlag( Qgis::LayerFilter::HasGeometry ) && vl->isSpatial() )
71 return true;
72 if ( filters.testFlag( Qgis::LayerFilter::NoGeometry ) && vl->geometryType() == Qgis::GeometryType::Null )
73 return true;
74 if ( filters.testFlag( Qgis::LayerFilter::PointLayer ) && vl->geometryType() == Qgis::GeometryType::Point )
75 return true;
76 if ( filters.testFlag( Qgis::LayerFilter::LineLayer ) && vl->geometryType() == Qgis::GeometryType::Line )
77 return true;
78 if ( filters.testFlag( Qgis::LayerFilter::PolygonLayer ) && vl->geometryType() == Qgis::GeometryType::Polygon )
79 return true;
80 }
81 }
82
83 return false;
84}
85
86void QgsMapLayerProxyModel::setLayerWhitelist( const QList<QgsMapLayer *> &layers )
87{
88 setLayerAllowlist( layers );
89}
90
91void QgsMapLayerProxyModel::setLayerAllowlist( const QList<QgsMapLayer *> &layers )
92{
93 if ( mLayerAllowlist == layers )
94 return;
95
96 mLayerAllowlist = layers;
97 invalidateFilter();
98}
99
100void QgsMapLayerProxyModel::setExceptedLayerList( const QList<QgsMapLayer *> &exceptList )
101{
102 if ( mExceptList == exceptList )
103 return;
104
105 mExceptList = exceptList;
106 invalidateFilter();
107}
108
110{
111 mModel->setProject( project );
112}
113
114void QgsMapLayerProxyModel::setExceptedLayerIds( const QStringList &ids )
115{
116 mExceptList.clear();
117
118 const auto constIds = ids;
119 for ( const QString &id : constIds )
120 {
121 QgsMapLayer *l = QgsProject::instance()->mapLayer( id ); // skip-keyword-check
122 if ( l )
123 mExceptList << l;
124 }
125 invalidateFilter();
126}
127
129{
130 QStringList lst;
131
132 const auto constMExceptList = mExceptList;
133 for ( QgsMapLayer *l : constMExceptList )
134 lst << l->id();
135
136 return lst;
137}
138
139void QgsMapLayerProxyModel::setExcludedProviders( const QStringList &providers )
140{
141 mExcludedProviders = providers;
142 invalidateFilter();
143}
144
146{
147 if ( !layer )
148 return false;
149
150 if ( !mLayerAllowlist.isEmpty() && !mLayerAllowlist.contains( layer ) )
151 return false;
152
153 if ( mExceptList.contains( layer ) )
154 return false;
155
156 if ( layer->dataProvider() && mExcludedProviders.contains( layer->providerType() ) )
157 return false;
158
159 if ( !layer->name().contains( mFilterString, Qt::CaseInsensitive ) )
160 return false;
161
162 return layerMatchesFilters( layer, mFilters );
163}
164
165void QgsMapLayerProxyModel::setFilterString( const QString &filter )
166{
167 mFilterString = filter;
168 invalidateFilter();
169}
170
171bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
172{
173 if ( mFilters.testFlag( Qgis::LayerFilter::All ) && mExceptList.isEmpty() && mLayerAllowlist.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
174 return true;
175
176 const QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
177
178 if ( sourceModel()->data( index, static_cast< int >( QgsMapLayerModel::CustomRole::Empty ) ).toBool()
179 || sourceModel()->data( index, static_cast< int >( QgsMapLayerModel::CustomRole::Additional ) ).toBool() )
180 return true;
181
182 return acceptsLayer( static_cast<QgsMapLayer *>( index.internalPointer() ) );
183}
184
185bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
186{
187 // empty row is always first
188 if ( sourceModel()->data( left, static_cast< int >( QgsMapLayerModel::CustomRole::Empty ) ).toBool() )
189 return true;
190 else if ( sourceModel()->data( right, static_cast< int >( QgsMapLayerModel::CustomRole::Empty ) ).toBool() )
191 return false;
192
193 // additional rows are always last
194 const bool leftAdditional = sourceModel()->data( left, static_cast< int >( QgsMapLayerModel::CustomRole::Additional ) ).toBool();
195 const bool rightAdditional = sourceModel()->data( right, static_cast< int >( QgsMapLayerModel::CustomRole::Additional ) ).toBool();
196
197 if ( leftAdditional && !rightAdditional )
198 return false;
199 else if ( rightAdditional && !leftAdditional )
200 return true;
201
202 // default mode is alphabetical order
203 const QString leftStr = sourceModel()->data( left ).toString();
204 const QString rightStr = sourceModel()->data( right ).toString();
205 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
206}
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:54
@ PointCloudLayer
QgsPointCloudLayer.
@ MeshLayer
QgsMeshLayer.
@ VectorTileLayer
QgsVectorTileLayer.
@ AnnotationLayer
QgsAnnotationLayer.
@ All
All layers.
@ TiledSceneLayer
QgsTiledSceneLayer.
@ Polygon
Polygons.
@ Null
No geometry.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ Vector
Vector layer.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ Raster
Raster layer.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
QFlags< LayerFilter > LayerFilters
Definition qgis.h:206
A model for display of map layers in widgets.
void setProject(QgsProject *project)
Sets the QgsProject from which map layers are shown.
@ Additional
True if index corresponds to an additional (non map layer) item.
@ Empty
True if index corresponds to the empty (not set) value.
A proxy model which provides an easy to use model to display the list of layers in widgets.
static bool layerMatchesFilters(const QgsMapLayer *layer, const Qgis::LayerFilters &filters)
Returns if the layer matches the given filters.
void setExceptedLayerIds(const QStringList &ids)
Sets a blocklist of layers (by layer ID) to exclude from the model.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
void setFilterString(const QString &filter)
Sets a filter string, such that only layers with names matching the specified string will be shown.
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
QgsMapLayerProxyModel(QObject *parent=nullptr)
QgsMapLayerProxModel creates a proxy model with a QgsMapLayerModel as source model.
QgsMapLayerProxyModel * setFilters(Qgis::LayerFilters filters)
Sets filter flags which affect how layers are filtered within the model.
bool acceptsLayer(QgsMapLayer *layer) const
Returns true if the proxy model accepts the specified map layer.
void setExcludedProviders(const QStringList &providers)
Sets a blocklist of data providers which should be excluded from the model.
void setLayerAllowlist(const QList< QgsMapLayer * > &layers)
Sets an allowlist of layers to include within the model.
void setProject(QgsProject *project)
Sets the project from which map layers are shown.
Q_DECL_DEPRECATED void setLayerWhitelist(const QList< QgsMapLayer * > &layers)
Sets an allowlist of layers to include within the model.
void setExceptedLayerList(const QList< QgsMapLayer * > &exceptList)
Sets a blocklist of layers to exclude from the model.
Base class for all map layer types.
Definition qgsmaplayer.h:77
QString name
Definition qgsmaplayer.h:81
QString providerType() const
Returns the provider type (provider key) for this layer.
Qgis::LayerType type
Definition qgsmaplayer.h:87
bool readOnly() const
Returns if this layer is read only.
virtual Q_INVOKABLE QgsDataProvider * dataProvider()
Returns the layer's data provider, it may be nullptr.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Represents a vector layer which manages a vector based dataset.