QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 : [email protected]
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 
16 #include "qgsmaplayerproxymodel.h"
17 #include "qgsmaplayermodel.h"
18 #include "qgsmaplayer.h"
19 #include "qgsproject.h"
20 #include "qgsvectorlayer.h"
21 #include "qgsrasterlayer.h"
22 #include "qgsmeshlayer.h"
23 #include "qgsvectordataprovider.h"
24 #include "qgsrasterdataprovider.h"
25 #include "qgsmeshdataprovider.h"
26 
28  : QSortFilterProxyModel( parent )
29  , mFilters( All )
30  , mModel( new QgsMapLayerModel( parent ) )
31 {
32  setSourceModel( mModel );
33  setDynamicSortFilter( true );
34  setSortLocaleAware( true );
35  setFilterCaseSensitivity( Qt::CaseInsensitive );
36  sort( 0 );
37 }
38 
40 {
41  mFilters = filters;
42  invalidateFilter();
43  return this;
44 }
45 
46 bool QgsMapLayerProxyModel::layerMatchesFilters( const QgsMapLayer *layer, const Filters &filters )
47 {
48  if ( filters.testFlag( All ) )
49  return true;
50 
51  // layer type
52  if ( ( filters.testFlag( RasterLayer ) && layer->type() == QgsMapLayerType::RasterLayer ) ||
53  ( filters.testFlag( VectorLayer ) && layer->type() == QgsMapLayerType::VectorLayer ) ||
54  ( filters.testFlag( MeshLayer ) && layer->type() == QgsMapLayerType::MeshLayer ) ||
55  ( filters.testFlag( VectorTileLayer ) && layer->type() == QgsMapLayerType::VectorTileLayer ) ||
56  ( filters.testFlag( PointCloudLayer ) && layer->type() == QgsMapLayerType::PointCloudLayer ) ||
57  ( filters.testFlag( AnnotationLayer ) && layer->type() == QgsMapLayerType::AnnotationLayer ) ||
58  ( filters.testFlag( PluginLayer ) && layer->type() == QgsMapLayerType::PluginLayer ) )
59  return true;
60 
61  // geometry type
62  const bool detectGeometry = filters.testFlag( NoGeometry ) ||
63  filters.testFlag( PointLayer ) ||
64  filters.testFlag( LineLayer ) ||
65  filters.testFlag( PolygonLayer ) ||
66  filters.testFlag( HasGeometry );
67  if ( detectGeometry && layer->type() == QgsMapLayerType::VectorLayer )
68  {
69  if ( const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
70  {
71  if ( filters.testFlag( HasGeometry ) && vl->isSpatial() )
72  return true;
73  if ( filters.testFlag( NoGeometry ) && vl->geometryType() == QgsWkbTypes::NullGeometry )
74  return true;
75  if ( filters.testFlag( PointLayer ) && vl->geometryType() == QgsWkbTypes::PointGeometry )
76  return true;
77  if ( filters.testFlag( LineLayer ) && vl->geometryType() == QgsWkbTypes::LineGeometry )
78  return true;
79  if ( filters.testFlag( PolygonLayer ) && vl->geometryType() == QgsWkbTypes::PolygonGeometry )
80  return true;
81  }
82  }
83 
84  return false;
85 }
86 
87 void QgsMapLayerProxyModel::setLayerWhitelist( const QList<QgsMapLayer *> &layers )
88 {
89  setLayerAllowlist( layers );
90 }
91 
92 void QgsMapLayerProxyModel::setLayerAllowlist( const QList<QgsMapLayer *> &layers )
93 {
94  if ( mLayerAllowlist == layers )
95  return;
96 
97  mLayerAllowlist = layers;
98  invalidateFilter();
99 }
100 
101 void QgsMapLayerProxyModel::setExceptedLayerList( const QList<QgsMapLayer *> &exceptList )
102 {
103  if ( mExceptList == exceptList )
104  return;
105 
106  mExceptList = exceptList;
107  invalidateFilter();
108 }
109 
111 {
112  mModel->setProject( project );
113 }
114 
115 void QgsMapLayerProxyModel::setExceptedLayerIds( const QStringList &ids )
116 {
117  mExceptList.clear();
118 
119  const auto constIds = ids;
120  for ( const QString &id : constIds )
121  {
123  if ( l )
124  mExceptList << l;
125  }
126  invalidateFilter();
127 }
128 
130 {
131  QStringList lst;
132 
133  const auto constMExceptList = mExceptList;
134  for ( QgsMapLayer *l : constMExceptList )
135  lst << l->id();
136 
137  return lst;
138 }
139 
140 void QgsMapLayerProxyModel::setExcludedProviders( const QStringList &providers )
141 {
142  mExcludedProviders = providers;
143  invalidateFilter();
144 }
145 
147 {
148  if ( !layer )
149  return false;
150 
151  if ( !mLayerAllowlist.isEmpty() && !mLayerAllowlist.contains( layer ) )
152  return false;
153 
154  if ( mExceptList.contains( layer ) )
155  return false;
156 
157  if ( layer->dataProvider() && mExcludedProviders.contains( layer->providerType() ) )
158  return false;
159 
160  if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
161  return false;
162 
163  if ( !layer->name().contains( mFilterString, Qt::CaseInsensitive ) )
164  return false;
165 
166  return layerMatchesFilters( layer, mFilters );
167 }
168 
169 void QgsMapLayerProxyModel::setFilterString( const QString &filter )
170 {
171  mFilterString = filter;
172  invalidateFilter();
173 }
174 
175 bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
176 {
177  if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mLayerAllowlist.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
178  return true;
179 
180  const QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
181 
182  if ( sourceModel()->data( index, QgsMapLayerModel::EmptyRole ).toBool()
183  || sourceModel()->data( index, QgsMapLayerModel::AdditionalRole ).toBool() )
184  return true;
185 
186  return acceptsLayer( static_cast<QgsMapLayer *>( index.internalPointer() ) );
187 }
188 
189 bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
190 {
191  // empty row is always first
192  if ( sourceModel()->data( left, QgsMapLayerModel::EmptyRole ).toBool() )
193  return true;
194  else if ( sourceModel()->data( right, QgsMapLayerModel::EmptyRole ).toBool() )
195  return false;
196 
197  // additional rows are always last
198  const bool leftAdditional = sourceModel()->data( left, QgsMapLayerModel::AdditionalRole ).toBool();
199  const bool rightAdditional = sourceModel()->data( right, QgsMapLayerModel::AdditionalRole ).toBool();
200 
201  if ( leftAdditional && !rightAdditional )
202  return false;
203  else if ( rightAdditional && !leftAdditional )
204  return true;
205 
206  // default mode is alphabetical order
207  const QString leftStr = sourceModel()->data( left ).toString();
208  const QString rightStr = sourceModel()->data( right ).toString();
209  return QString::localeAwareCompare( leftStr, rightStr ) < 0;
210 }
The QgsMapLayerModel class is a model to display layers in widgets.
void setProject(QgsProject *project)
Sets the QgsProject from which map layers are shown.
@ EmptyRole
True if index corresponds to the empty (not set) value.
@ AdditionalRole
True if index corresponds to an additional (non map layer) item.
The QgsMapLayerProxyModel class provides an easy to use model to display the list of layers in widget...
QgsMapLayerProxyModel::Filters filters
static bool layerMatchesFilters(const QgsMapLayer *layer, const Filters &filters)
Returns if the layer matches the given filters.
QgsMapLayerProxyModel * setFilters(QgsMapLayerProxyModel::Filters filters)
Sets filter flags which affect how layers are filtered within the model.
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.
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.
@ AnnotationLayer
QgsAnnotationLayer.
@ VectorTileLayer
QgsVectorTileLayer.
@ PointCloudLayer
QgsPointCloudLayer.
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:73
QString name
Definition: qgsmaplayer.h:76
QString providerType() const
Returns the provider type (provider key) for this layer.
QgsMapLayerType type
Definition: qgsmaplayer.h:80
bool readOnly() const
Returns if this layer is read only.
Definition: qgsmaplayer.h:532
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:101
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:470
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.
@ PointCloudLayer
Point cloud layer. Added in QGIS 3.18.
@ MeshLayer
Mesh layer. Added in QGIS 3.2.
@ VectorLayer
Vector layer.
@ RasterLayer
Raster layer.
@ VectorTileLayer
Vector tile layer. Added in QGIS 3.14.
@ AnnotationLayer
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ PluginLayer
Plugin based layer.