QGIS API Documentation  3.6.0-Noosa (5873452)
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 void QgsMapLayerProxyModel::setLayerWhitelist( const QList<QgsMapLayer *> &layers )
47 {
48  if ( mLayerWhitelist == layers )
49  return;
50 
51  mLayerWhitelist = layers;
52  invalidateFilter();
53 }
54 
55 void QgsMapLayerProxyModel::setExceptedLayerList( const QList<QgsMapLayer *> &exceptList )
56 {
57  if ( mExceptList == exceptList )
58  return;
59 
60  mExceptList = exceptList;
61  invalidateFilter();
62 }
63 
64 void QgsMapLayerProxyModel::setExceptedLayerIds( const QStringList &ids )
65 {
66  mExceptList.clear();
67 
68  Q_FOREACH ( const QString &id, ids )
69  {
71  if ( l )
72  mExceptList << l;
73  }
74  invalidateFilter();
75 }
76 
78 {
79  QStringList lst;
80 
81  Q_FOREACH ( QgsMapLayer *l, mExceptList )
82  lst << l->id();
83 
84  return lst;
85 }
86 
87 void QgsMapLayerProxyModel::setExcludedProviders( const QStringList &providers )
88 {
89  mExcludedProviders = providers;
90  invalidateFilter();
91 }
92 
93 void QgsMapLayerProxyModel::setFilterString( const QString &filter )
94 {
95  mFilterString = filter;
96  invalidateFilter();
97 }
98 
99 bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
100 {
101  if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mLayerWhitelist.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
102  return true;
103 
104  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
105 
106  if ( sourceModel()->data( index, QgsMapLayerModel::EmptyRole ).toBool()
107  || sourceModel()->data( index, QgsMapLayerModel::AdditionalRole ).toBool() )
108  return true;
109 
110  QgsMapLayer *layer = static_cast<QgsMapLayer *>( index.internalPointer() );
111  if ( !layer )
112  return false;
113 
114  if ( !mLayerWhitelist.isEmpty() && !mLayerWhitelist.contains( layer ) )
115  return false;
116 
117  if ( mExceptList.contains( layer ) )
118  return false;
119 
120  if ( mExcludedProviders.contains( layer->dataProvider()->name() ) )
121  return false;
122 
123  if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
124  return false;
125 
126  if ( !layer->name().contains( mFilterString, Qt::CaseInsensitive ) )
127  return false;
128 
129  // layer type
130  if ( ( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) ||
131  ( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ||
132  ( mFilters.testFlag( MeshLayer ) && layer->type() == QgsMapLayer::MeshLayer ) ||
133  ( mFilters.testFlag( PluginLayer ) && layer->type() == QgsMapLayer::PluginLayer ) )
134  return true;
135 
136  // geometry type
137  bool detectGeometry = mFilters.testFlag( NoGeometry ) ||
138  mFilters.testFlag( PointLayer ) ||
139  mFilters.testFlag( LineLayer ) ||
140  mFilters.testFlag( PolygonLayer ) ||
141  mFilters.testFlag( HasGeometry );
142  if ( detectGeometry && layer->type() == QgsMapLayer::VectorLayer )
143  {
144  if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( layer ) )
145  {
146  if ( mFilters.testFlag( HasGeometry ) && vl->isSpatial() )
147  return true;
148  if ( mFilters.testFlag( NoGeometry ) && vl->geometryType() == QgsWkbTypes::NullGeometry )
149  return true;
150  if ( mFilters.testFlag( PointLayer ) && vl->geometryType() == QgsWkbTypes::PointGeometry )
151  return true;
152  if ( mFilters.testFlag( LineLayer ) && vl->geometryType() == QgsWkbTypes::LineGeometry )
153  return true;
154  if ( mFilters.testFlag( PolygonLayer ) && vl->geometryType() == QgsWkbTypes::PolygonGeometry )
155  return true;
156  }
157  }
158 
159  return false;
160 }
161 
162 bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
163 {
164  // empty row is always first
165  if ( sourceModel()->data( left, QgsMapLayerModel::EmptyRole ).toBool() )
166  return true;
167  else if ( sourceModel()->data( right, QgsMapLayerModel::EmptyRole ).toBool() )
168  return false;
169 
170  // additional rows are always last
171  bool leftAdditional = sourceModel()->data( left, QgsMapLayerModel::AdditionalRole ).toBool();
172  bool rightAdditional = sourceModel()->data( right, QgsMapLayerModel::AdditionalRole ).toBool();
173 
174  if ( leftAdditional && !rightAdditional )
175  return false;
176  else if ( rightAdditional && !leftAdditional )
177  return true;
178 
179  // default mode is alphabetical order
180  QString leftStr = sourceModel()->data( left ).toString();
181  QString rightStr = sourceModel()->data( right ).toString();
182  return QString::localeAwareCompare( leftStr, rightStr ) < 0;
183 }
QgsMapLayerProxyModel(QObject *parent=nullptr)
QgsMapLayerProxModel creates a proxy model with a QgsMapLayerModel as source model.
Base class for all map layer types.
Definition: qgsmaplayer.h:64
virtual QgsDataProvider * dataProvider()
Returns the layer&#39;s data provider, it may be null.
QStringList exceptedLayerIds() const
Returns the blacklist of layer IDs which are excluded from the model.
void setExceptedLayerIds(const QStringList &ids)
Sets a blacklist of layers (by layer ID) to exclude from the model.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
virtual QString name() const =0
Returns a provider name.
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
True if index corresponds to an additional (non map layer) item.
Added in 3.2.
Definition: qgsmaplayer.h:111
void setExceptedLayerList(const QList< QgsMapLayer *> &exceptList)
Sets a blacklist of layers to exclude from the model.
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
The QgsMapLayerModel class is a model to display layers in widgets.
The QgsMapLayerProxyModel class provides an easy to use model to display the list of layers in widget...
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
void setLayerWhitelist(const QList< QgsMapLayer *> &layers)
Sets a whitelist of layers to include within the model.
void setExcludedProviders(const QStringList &providers)
Sets a blacklist of data providers which should be excluded from the model.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:430
QgsMapLayerProxyModel * setFilters(QgsMapLayerProxyModel::Filters filters)
Sets filter flags which affect how layers are filtered within the model.
const Filters & filters() const
Returns the filter flags which affect how layers are filtered within the model.
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
bool readOnly() const
Returns if this layer is read only.
Definition: qgsmaplayer.h:453
QString name
Definition: qgsmaplayer.h:68
Represents a vector layer which manages a vector based data sets.
void setFilterString(const QString &filter)
Sets a filter string, such that only layers with names matching the specified string will be shown...
True if index corresponds to the empty (not set) value.