QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 "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::All ) )
45 return true;
46
47 // layer type
48 if ( ( filters.testFlag( Qgis::LayerFilter::RasterLayer ) && layer->type() == Qgis::LayerType::Raster ) ||
50 ( filters.testFlag( Qgis::LayerFilter::MeshLayer ) && layer->type() == Qgis::LayerType::Mesh ) ||
56 return true;
57
58 // geometry type
59 const bool detectGeometry = filters.testFlag( Qgis::LayerFilter::NoGeometry ) ||
64 if ( detectGeometry && layer->type() == Qgis::LayerType::Vector )
65 {
66 if ( const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
67 {
68 if ( filters.testFlag( Qgis::LayerFilter::HasGeometry ) && vl->isSpatial() )
69 return true;
70 if ( filters.testFlag( Qgis::LayerFilter::NoGeometry ) && vl->geometryType() == Qgis::GeometryType::Null )
71 return true;
72 if ( filters.testFlag( Qgis::LayerFilter::PointLayer ) && vl->geometryType() == Qgis::GeometryType::Point )
73 return true;
74 if ( filters.testFlag( Qgis::LayerFilter::LineLayer ) && vl->geometryType() == Qgis::GeometryType::Line )
75 return true;
76 if ( filters.testFlag( Qgis::LayerFilter::PolygonLayer ) && vl->geometryType() == Qgis::GeometryType::Polygon )
77 return true;
78 }
79 }
80
81 return false;
82}
83
84void QgsMapLayerProxyModel::setLayerWhitelist( const QList<QgsMapLayer *> &layers )
85{
86 setLayerAllowlist( layers );
87}
88
89void QgsMapLayerProxyModel::setLayerAllowlist( const QList<QgsMapLayer *> &layers )
90{
91 if ( mLayerAllowlist == layers )
92 return;
93
94 mLayerAllowlist = layers;
95 invalidateFilter();
96}
97
98void QgsMapLayerProxyModel::setExceptedLayerList( const QList<QgsMapLayer *> &exceptList )
99{
100 if ( mExceptList == exceptList )
101 return;
102
103 mExceptList = exceptList;
104 invalidateFilter();
105}
106
108{
109 mModel->setProject( project );
110}
111
112void QgsMapLayerProxyModel::setExceptedLayerIds( const QStringList &ids )
113{
114 mExceptList.clear();
115
116 const auto constIds = ids;
117 for ( const QString &id : constIds )
118 {
119 QgsMapLayer *l = QgsProject::instance()->mapLayer( id ); // skip-keyword-check
120 if ( l )
121 mExceptList << l;
122 }
123 invalidateFilter();
124}
125
127{
128 QStringList lst;
129
130 const auto constMExceptList = mExceptList;
131 for ( QgsMapLayer *l : constMExceptList )
132 lst << l->id();
133
134 return lst;
135}
136
137void QgsMapLayerProxyModel::setExcludedProviders( const QStringList &providers )
138{
139 mExcludedProviders = providers;
140 invalidateFilter();
141}
142
144{
145 if ( !layer )
146 return false;
147
148 if ( !mLayerAllowlist.isEmpty() && !mLayerAllowlist.contains( layer ) )
149 return false;
150
151 if ( mExceptList.contains( layer ) )
152 return false;
153
154 if ( layer->dataProvider() && mExcludedProviders.contains( layer->providerType() ) )
155 return false;
156
157 if ( mFilters.testFlag( Qgis::LayerFilter::WritableLayer ) && layer->readOnly() )
158 return false;
159
160 if ( !layer->name().contains( mFilterString, Qt::CaseInsensitive ) )
161 return false;
162
163 return layerMatchesFilters( layer, mFilters );
164}
165
166void QgsMapLayerProxyModel::setFilterString( const QString &filter )
167{
168 mFilterString = filter;
169 invalidateFilter();
170}
171
172bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
173{
174 if ( mFilters.testFlag( Qgis::LayerFilter::All ) && mExceptList.isEmpty() && mLayerAllowlist.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
175 return true;
176
177 const QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
178
179 if ( sourceModel()->data( index, static_cast< int >( QgsMapLayerModel::CustomRole::Empty ) ).toBool()
180 || sourceModel()->data( index, static_cast< int >( QgsMapLayerModel::CustomRole::Additional ) ).toBool() )
181 return true;
182
183 return acceptsLayer( static_cast<QgsMapLayer *>( index.internalPointer() ) );
184}
185
186bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
187{
188 // empty row is always first
189 if ( sourceModel()->data( left, static_cast< int >( QgsMapLayerModel::CustomRole::Empty ) ).toBool() )
190 return true;
191 else if ( sourceModel()->data( right, static_cast< int >( QgsMapLayerModel::CustomRole::Empty ) ).toBool() )
192 return false;
193
194 // additional rows are always last
195 const bool leftAdditional = sourceModel()->data( left, static_cast< int >( QgsMapLayerModel::CustomRole::Additional ) ).toBool();
196 const bool rightAdditional = sourceModel()->data( right, static_cast< int >( QgsMapLayerModel::CustomRole::Additional ) ).toBool();
197
198 if ( leftAdditional && !rightAdditional )
199 return false;
200 else if ( rightAdditional && !leftAdditional )
201 return true;
202
203 // default mode is alphabetical order
204 const QString leftStr = sourceModel()->data( left ).toString();
205 const QString rightStr = sourceModel()->data( right ).toString();
206 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
207}
The Qgis class provides global constants 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
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.
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:76
QString name
Definition qgsmaplayer.h:80
QString providerType() const
Returns the provider type (provider key) for this layer.
Qgis::LayerType type
Definition qgsmaplayer.h:86
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 data sets.