QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
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 "qgsmaplayermodel.h"
18#include "qgsmaplayer.h"
19#include "qgsproject.h"
20#include "qgsvectorlayer.h"
21
23 : QSortFilterProxyModel( parent )
24 , mFilters( Qgis::LayerFilter::All )
25 , mModel( new QgsMapLayerModel( parent ) )
26{
27 setSourceModel( mModel );
28 setDynamicSortFilter( true );
29 setSortLocaleAware( true );
30 setFilterCaseSensitivity( Qt::CaseInsensitive );
31 sort( 0 );
32}
33
35{
36 mFilters = filters;
37 invalidateFilter();
38 return this;
39}
40
42{
43 if ( filters.testFlag( Qgis::LayerFilter::All ) )
44 return true;
45
46 // layer type
47 if ( ( filters.testFlag( Qgis::LayerFilter::RasterLayer ) && layer->type() == Qgis::LayerType::Raster ) ||
49 ( filters.testFlag( Qgis::LayerFilter::MeshLayer ) && layer->type() == Qgis::LayerType::Mesh ) ||
55 return true;
56
57 // geometry type
58 const bool detectGeometry = filters.testFlag( Qgis::LayerFilter::NoGeometry ) ||
63 if ( detectGeometry && layer->type() == Qgis::LayerType::Vector )
64 {
65 if ( const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
66 {
67 if ( filters.testFlag( Qgis::LayerFilter::HasGeometry ) && vl->isSpatial() )
68 return true;
69 if ( filters.testFlag( Qgis::LayerFilter::NoGeometry ) && vl->geometryType() == Qgis::GeometryType::Null )
70 return true;
71 if ( filters.testFlag( Qgis::LayerFilter::PointLayer ) && vl->geometryType() == Qgis::GeometryType::Point )
72 return true;
73 if ( filters.testFlag( Qgis::LayerFilter::LineLayer ) && vl->geometryType() == Qgis::GeometryType::Line )
74 return true;
75 if ( filters.testFlag( Qgis::LayerFilter::PolygonLayer ) && vl->geometryType() == Qgis::GeometryType::Polygon )
76 return true;
77 }
78 }
79
80 return false;
81}
82
83void QgsMapLayerProxyModel::setLayerWhitelist( const QList<QgsMapLayer *> &layers )
84{
85 setLayerAllowlist( layers );
86}
87
88void QgsMapLayerProxyModel::setLayerAllowlist( const QList<QgsMapLayer *> &layers )
89{
90 if ( mLayerAllowlist == layers )
91 return;
92
93 mLayerAllowlist = layers;
94 invalidateFilter();
95}
96
97void QgsMapLayerProxyModel::setExceptedLayerList( const QList<QgsMapLayer *> &exceptList )
98{
99 if ( mExceptList == exceptList )
100 return;
101
102 mExceptList = exceptList;
103 invalidateFilter();
104}
105
107{
108 mModel->setProject( project );
109}
110
111void QgsMapLayerProxyModel::setExceptedLayerIds( const QStringList &ids )
112{
113 mExceptList.clear();
114
115 const auto constIds = ids;
116 for ( const QString &id : constIds )
117 {
119 if ( l )
120 mExceptList << l;
121 }
122 invalidateFilter();
123}
124
126{
127 QStringList lst;
128
129 const auto constMExceptList = mExceptList;
130 for ( QgsMapLayer *l : constMExceptList )
131 lst << l->id();
132
133 return lst;
134}
135
136void QgsMapLayerProxyModel::setExcludedProviders( const QStringList &providers )
137{
138 mExcludedProviders = providers;
139 invalidateFilter();
140}
141
143{
144 if ( !layer )
145 return false;
146
147 if ( !mLayerAllowlist.isEmpty() && !mLayerAllowlist.contains( layer ) )
148 return false;
149
150 if ( mExceptList.contains( layer ) )
151 return false;
152
153 if ( layer->dataProvider() && mExcludedProviders.contains( layer->providerType() ) )
154 return false;
155
156 if ( mFilters.testFlag( Qgis::LayerFilter::WritableLayer ) && layer->readOnly() )
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}
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:54
@ PointCloudLayer
QgsPointCloudLayer.
@ MeshLayer
QgsMeshLayer.
@ VectorTileLayer
QgsVectorTileLayer.
@ AnnotationLayer
QgsAnnotationLayer.
@ 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:151
The QgsMapLayerModel class is a model to display 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.
The QgsMapLayerProxyModel class provides an easy to use model to display the list of layers in widget...
static bool layerMatchesFilters(const QgsMapLayer *layer, const Qgis::LayerFilters &filters)
Returns if the layer matches the given filters.
Qgis::LayerFilters 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:75
QString name
Definition: qgsmaplayer.h:78
QString providerType() const
Returns the provider type (provider key) for this layer.
Qgis::LayerType type
Definition: qgsmaplayer.h:82
bool readOnly() const
Returns if this layer is read only.
Definition: qgsmaplayer.h:527
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.
Definition: qgsproject.cpp:481
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 data sets.