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