QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "qgsmaplayer.h"
19#include "qgsmaplayermodel.h"
20#include "qgsproject.h"
21#include "qgsvectorlayer.h"
22
23#include "moc_qgsmaplayerproxymodel.cpp"
24
26 : QSortFilterProxyModel( parent )
27 , mFilters( Qgis::LayerFilter::All )
28 , mModel( new QgsMapLayerModel( parent ) )
29{
30 setSourceModel( mModel );
31 setDynamicSortFilter( true );
32 setSortLocaleAware( true );
33 setFilterCaseSensitivity( Qt::CaseInsensitive );
34 sort( 0 );
35}
36
38{
39 mFilters = filters;
40 invalidateFilter();
41 return this;
42}
43
45{
46 if ( filters.testFlag( Qgis::LayerFilter::WritableLayer ) && layer->readOnly() )
47 return false;
48
49 if ( filters.testFlag( Qgis::LayerFilter::All ) )
50 return true;
51
52 // layer type
53 if ( ( filters.testFlag( Qgis::LayerFilter::RasterLayer ) && layer->type() == Qgis::LayerType::Raster ) ||
55 ( filters.testFlag( Qgis::LayerFilter::MeshLayer ) && layer->type() == Qgis::LayerType::Mesh ) ||
61 return true;
62
63 // geometry type
64 const bool detectGeometry = filters.testFlag( Qgis::LayerFilter::NoGeometry ) ||
68 if ( detectGeometry && layer->type() == Qgis::LayerType::Vector )
69 {
70 if ( const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
71 {
72 if ( filters.testFlag( Qgis::LayerFilter::HasGeometry ) && vl->isSpatial() )
73 return true;
74 if ( filters.testFlag( Qgis::LayerFilter::NoGeometry ) && vl->geometryType() == Qgis::GeometryType::Null )
75 return true;
76 if ( filters.testFlag( Qgis::LayerFilter::PointLayer ) && vl->geometryType() == Qgis::GeometryType::Point )
77 return true;
78 if ( filters.testFlag( Qgis::LayerFilter::LineLayer ) && vl->geometryType() == Qgis::GeometryType::Line )
79 return true;
80 if ( filters.testFlag( Qgis::LayerFilter::PolygonLayer ) && vl->geometryType() == Qgis::GeometryType::Polygon )
81 return true;
82 }
83 }
84
85 return false;
86}
87
88void QgsMapLayerProxyModel::setLayerWhitelist( const QList<QgsMapLayer *> &layers )
89{
90 setLayerAllowlist( layers );
91}
92
93void QgsMapLayerProxyModel::setLayerAllowlist( const QList<QgsMapLayer *> &layers )
94{
95 if ( mLayerAllowlist == layers )
96 return;
97
98 mLayerAllowlist = layers;
99 invalidateFilter();
100}
101
102void QgsMapLayerProxyModel::setExceptedLayerList( const QList<QgsMapLayer *> &exceptList )
103{
104 if ( mExceptList == exceptList )
105 return;
106
107 mExceptList = exceptList;
108 invalidateFilter();
109}
110
112{
113 mModel->setProject( project );
114}
115
116void QgsMapLayerProxyModel::setExceptedLayerIds( const QStringList &ids )
117{
118 mExceptList.clear();
119
120 const auto constIds = ids;
121 for ( const QString &id : constIds )
122 {
123 QgsMapLayer *l = QgsProject::instance()->mapLayer( id ); // skip-keyword-check
124 if ( l )
125 mExceptList << l;
126 }
127 invalidateFilter();
128}
129
131{
132 QStringList lst;
133
134 const auto constMExceptList = mExceptList;
135 for ( QgsMapLayer *l : constMExceptList )
136 lst << l->id();
137
138 return lst;
139}
140
141void QgsMapLayerProxyModel::setExcludedProviders( const QStringList &providers )
142{
143 mExcludedProviders = providers;
144 invalidateFilter();
145}
146
148{
149 if ( !layer )
150 return false;
151
152 if ( !mLayerAllowlist.isEmpty() && !mLayerAllowlist.contains( layer ) )
153 return false;
154
155 if ( mExceptList.contains( layer ) )
156 return false;
157
158 if ( layer->dataProvider() && mExcludedProviders.contains( layer->providerType() ) )
159 return false;
160
161 if ( !layer->name().contains( mFilterString, Qt::CaseInsensitive ) )
162 return false;
163
164 return layerMatchesFilters( layer, mFilters );
165}
166
167void QgsMapLayerProxyModel::setFilterString( const QString &filter )
168{
169 mFilterString = filter;
170 invalidateFilter();
171}
172
173bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
174{
175 if ( mFilters.testFlag( Qgis::LayerFilter::All ) && mExceptList.isEmpty() && mLayerAllowlist.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
176 return true;
177
178 const QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
179
180 if ( sourceModel()->data( index, static_cast< int >( QgsMapLayerModel::CustomRole::Empty ) ).toBool()
181 || sourceModel()->data( index, static_cast< int >( QgsMapLayerModel::CustomRole::Additional ) ).toBool() )
182 return true;
183
184 return acceptsLayer( static_cast<QgsMapLayer *>( index.internalPointer() ) );
185}
186
187bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
188{
189 // empty row is always first
190 if ( sourceModel()->data( left, static_cast< int >( QgsMapLayerModel::CustomRole::Empty ) ).toBool() )
191 return true;
192 else if ( sourceModel()->data( right, static_cast< int >( QgsMapLayerModel::CustomRole::Empty ) ).toBool() )
193 return false;
194
195 // additional rows are always last
196 const bool leftAdditional = sourceModel()->data( left, static_cast< int >( QgsMapLayerModel::CustomRole::Additional ) ).toBool();
197 const bool rightAdditional = sourceModel()->data( right, static_cast< int >( QgsMapLayerModel::CustomRole::Additional ) ).toBool();
198
199 if ( leftAdditional && !rightAdditional )
200 return false;
201 else if ( rightAdditional && !leftAdditional )
202 return true;
203
204 // default mode is alphabetical order
205 const QString leftStr = sourceModel()->data( left ).toString();
206 const QString rightStr = sourceModel()->data( right ).toString();
207 return QString::localeAwareCompare( leftStr, rightStr ) < 0;
208}
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
@ PointCloudLayer
QgsPointCloudLayer.
Definition qgis.h:221
@ MeshLayer
QgsMeshLayer.
Definition qgis.h:219
@ VectorTileLayer
QgsVectorTileLayer.
Definition qgis.h:220
@ AnnotationLayer
QgsAnnotationLayer.
Definition qgis.h:222
@ All
All layers.
Definition qgis.h:224
@ TiledSceneLayer
QgsTiledSceneLayer.
Definition qgis.h:223
@ Point
Points.
Definition qgis.h:359
@ Line
Lines.
Definition qgis.h:360
@ Polygon
Polygons.
Definition qgis.h:361
@ Null
No geometry.
Definition qgis.h:363
@ Plugin
Plugin based layer.
Definition qgis.h:193
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
Definition qgis.h:199
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Definition qgis.h:196
@ Vector
Vector layer.
Definition qgis.h:191
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:195
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:194
@ Raster
Raster layer.
Definition qgis.h:192
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Definition qgis.h:197
QFlags< LayerFilter > LayerFilters
Definition qgis.h:227
A model for display of map layers in widgets.
@ Additional
True if index corresponds to an additional (non map layer) item.
@ Empty
True if index corresponds to the empty (not set) value.
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:80
QString name
Definition qgsmaplayer.h:84
QString providerType() const
Returns the provider type (provider key) for this layer.
Qgis::LayerType type
Definition qgsmaplayer.h:90
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:109
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.