QGIS API Documentation  3.20.0-Odense (decaadbb31)
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( PluginLayer ) && layer->type() == QgsMapLayerType::PluginLayer ) )
58  return true;
59 
60  // geometry type
61  bool detectGeometry = filters.testFlag( NoGeometry ) ||
62  filters.testFlag( PointLayer ) ||
63  filters.testFlag( LineLayer ) ||
64  filters.testFlag( PolygonLayer ) ||
65  filters.testFlag( HasGeometry );
66  if ( detectGeometry && layer->type() == QgsMapLayerType::VectorLayer )
67  {
68  if ( const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
69  {
70  if ( filters.testFlag( HasGeometry ) && vl->isSpatial() )
71  return true;
72  if ( filters.testFlag( NoGeometry ) && vl->geometryType() == QgsWkbTypes::NullGeometry )
73  return true;
74  if ( filters.testFlag( PointLayer ) && vl->geometryType() == QgsWkbTypes::PointGeometry )
75  return true;
76  if ( filters.testFlag( LineLayer ) && vl->geometryType() == QgsWkbTypes::LineGeometry )
77  return true;
78  if ( filters.testFlag( PolygonLayer ) && vl->geometryType() == QgsWkbTypes::PolygonGeometry )
79  return true;
80  }
81  }
82 
83  return false;
84 }
85 
86 void QgsMapLayerProxyModel::setLayerWhitelist( const QList<QgsMapLayer *> &layers )
87 {
88  setLayerAllowlist( layers );
89 }
90 
91 void QgsMapLayerProxyModel::setLayerAllowlist( const QList<QgsMapLayer *> &layers )
92 {
93  if ( mLayerAllowlist == layers )
94  return;
95 
96  mLayerAllowlist = layers;
97  invalidateFilter();
98 }
99 
100 void QgsMapLayerProxyModel::setExceptedLayerList( const QList<QgsMapLayer *> &exceptList )
101 {
102  if ( mExceptList == exceptList )
103  return;
104 
105  mExceptList = exceptList;
106  invalidateFilter();
107 }
108 
109 void QgsMapLayerProxyModel::setExceptedLayerIds( const QStringList &ids )
110 {
111  mExceptList.clear();
112 
113  const auto constIds = ids;
114  for ( const QString &id : constIds )
115  {
117  if ( l )
118  mExceptList << l;
119  }
120  invalidateFilter();
121 }
122 
124 {
125  QStringList lst;
126 
127  const auto constMExceptList = mExceptList;
128  for ( QgsMapLayer *l : constMExceptList )
129  lst << l->id();
130 
131  return lst;
132 }
133 
134 void QgsMapLayerProxyModel::setExcludedProviders( const QStringList &providers )
135 {
136  mExcludedProviders = providers;
137  invalidateFilter();
138 }
139 
141 {
142  if ( !layer )
143  return false;
144 
145  if ( !mLayerAllowlist.isEmpty() && !mLayerAllowlist.contains( layer ) )
146  return false;
147 
148  if ( mExceptList.contains( layer ) )
149  return false;
150 
151  if ( layer->dataProvider() && mExcludedProviders.contains( layer->providerType() ) )
152  return false;
153 
154  if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
155  return false;
156 
157  if ( !layer->name().contains( mFilterString, Qt::CaseInsensitive ) )
158  return false;
159 
160  return layerMatchesFilters( layer, mFilters );
161 }
162 
163 void QgsMapLayerProxyModel::setFilterString( const QString &filter )
164 {
165  mFilterString = filter;
166  invalidateFilter();
167 }
168 
169 bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
170 {
171  if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mLayerAllowlist.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
172  return true;
173 
174  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
175 
176  if ( sourceModel()->data( index, QgsMapLayerModel::EmptyRole ).toBool()
177  || sourceModel()->data( index, QgsMapLayerModel::AdditionalRole ).toBool() )
178  return true;
179 
180  return acceptsLayer( static_cast<QgsMapLayer *>( index.internalPointer() ) );
181 }
182 
183 bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
184 {
185  // empty row is always first
186  if ( sourceModel()->data( left, QgsMapLayerModel::EmptyRole ).toBool() )
187  return true;
188  else if ( sourceModel()->data( right, QgsMapLayerModel::EmptyRole ).toBool() )
189  return false;
190 
191  // additional rows are always last
192  bool leftAdditional = sourceModel()->data( left, QgsMapLayerModel::AdditionalRole ).toBool();
193  bool rightAdditional = sourceModel()->data( right, QgsMapLayerModel::AdditionalRole ).toBool();
194 
195  if ( leftAdditional && !rightAdditional )
196  return false;
197  else if ( rightAdditional && !leftAdditional )
198  return true;
199 
200  // default mode is alphabetical order
201  QString leftStr = sourceModel()->data( left ).toString();
202  QString rightStr = sourceModel()->data( right ).toString();
203  return QString::localeAwareCompare( leftStr, rightStr ) < 0;
204 }
The QgsMapLayerModel class is a model to display layers in widgets.
@ 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.
@ VectorTileLayer
QgsVectorTileLayer.
@ PointCloudLayer
QgsPointCloudLayer.
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:70
QString name
Definition: qgsmaplayer.h:73
QString providerType() const
Returns the provider type (provider key) for this layer.
QgsMapLayerType type
Definition: qgsmaplayer.h:77
bool readOnly() const
Returns if this layer is read only.
Definition: qgsmaplayer.h:487
virtual Q_INVOKABLE QgsDataProvider * dataProvider()
Returns the layer's data provider, it may be nullptr.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:467
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
Added in 3.18.
@ MeshLayer
Added in 3.2.
@ VectorTileLayer
Added in 3.14.