QGIS API Documentation  2.99.0-Master (0a63d1f)
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 "qgsvectordataprovider.h"
23 #include "qgsrasterdataprovider.h"
24 
26  : QSortFilterProxyModel( parent )
27  , mFilters( All )
28  , mExceptList( QList<QgsMapLayer*>() )
29  , mModel( new QgsMapLayerModel( parent ) )
30 {
31  setSourceModel( mModel );
32  setDynamicSortFilter( true );
33  setSortLocaleAware( true );
34  setFilterCaseSensitivity( Qt::CaseInsensitive );
35  sort( 0 );
36 }
37 
39 {
40  mFilters = filters;
41  invalidateFilter();
42  return this;
43 }
44 
45 void QgsMapLayerProxyModel::setExceptedLayerList( const QList<QgsMapLayer*>& exceptList )
46 {
47  if ( mExceptList == exceptList )
48  return;
49 
50  mExceptList = exceptList;
51  invalidateFilter();
52 }
53 
54 void QgsMapLayerProxyModel::setExceptedLayerIds( const QStringList& ids )
55 {
56  mExceptList.clear();
57 
58  Q_FOREACH ( const QString& id, ids )
59  {
61  if ( l )
62  mExceptList << l;
63  }
64  invalidateFilter();
65 }
66 
68 {
69  QStringList lst;
70 
71  Q_FOREACH ( QgsMapLayer* l, mExceptList )
72  lst << l->id();
73 
74  return lst;
75 }
76 
77 void QgsMapLayerProxyModel::setExcludedProviders( const QStringList& providers )
78 {
79  mExcludedProviders = providers;
80  invalidateFilter();
81 }
82 
83 bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
84 {
85  if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mExcludedProviders.isEmpty() )
86  return true;
87 
88  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
89 
90  if ( sourceModel()->data( index, QgsMapLayerModel::EmptyRole ).toBool()
91  || sourceModel()->data( index, QgsMapLayerModel::AdditionalRole ).toBool() )
92  return true;
93 
94  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
95  if ( !layer )
96  return false;
97 
98  if ( mExceptList.contains( layer ) )
99  return false;
100 
101  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( layer );
102  if ( vl && mExcludedProviders.contains( vl->dataProvider()->name() ) )
103  return false;
104  QgsRasterLayer* rl = qobject_cast<QgsRasterLayer*>( layer );
105  if ( rl && mExcludedProviders.contains( rl->dataProvider()->name() ) )
106  return false;
107 
108  if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
109  return false;
110 
111  // layer type
112  if (( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) ||
113  ( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ||
114  ( mFilters.testFlag( PluginLayer ) && layer->type() == QgsMapLayer::PluginLayer ) )
115  return true;
116 
117  // geometry type
118  bool detectGeometry = mFilters.testFlag( NoGeometry ) ||
119  mFilters.testFlag( PointLayer ) ||
120  mFilters.testFlag( LineLayer ) ||
121  mFilters.testFlag( PolygonLayer ) ||
122  mFilters.testFlag( HasGeometry );
123  if ( detectGeometry && layer->type() == QgsMapLayer::VectorLayer )
124  {
125  if ( vl )
126  {
127  if ( mFilters.testFlag( HasGeometry ) && vl->hasGeometryType() )
128  return true;
129  if ( mFilters.testFlag( NoGeometry ) && vl->geometryType() == QgsWkbTypes::NullGeometry )
130  return true;
131  if ( mFilters.testFlag( PointLayer ) && vl->geometryType() == QgsWkbTypes::PointGeometry )
132  return true;
133  if ( mFilters.testFlag( LineLayer ) && vl->geometryType() == QgsWkbTypes::LineGeometry )
134  return true;
135  if ( mFilters.testFlag( PolygonLayer ) && vl->geometryType() == QgsWkbTypes::PolygonGeometry )
136  return true;
137  }
138  }
139 
140  return false;
141 }
142 
143 bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
144 {
145  // empty row is always first
146  if ( sourceModel()->data( left, QgsMapLayerModel::EmptyRole ).toBool() )
147  return true;
148  else if ( sourceModel()->data( right, QgsMapLayerModel::EmptyRole ).toBool() )
149  return false;
150 
151  // additional rows are always last
152  bool leftAdditional = sourceModel()->data( left, QgsMapLayerModel::AdditionalRole ).toBool();
153  bool rightAdditional = sourceModel()->data( right, QgsMapLayerModel::AdditionalRole ).toBool();
154 
155  if ( leftAdditional && !rightAdditional )
156  return false;
157  else if ( rightAdditional && !leftAdditional )
158  return true;
159 
160  // default mode is alphabetical order
161  QString leftStr = sourceModel()->data( left ).toString();
162  QString rightStr = sourceModel()->data( right ).toString();
163  return QString::localeAwareCompare( leftStr, rightStr ) < 0;
164 }
QgsMapLayerProxyModel(QObject *parent=nullptr)
QgsMapLayerProxModel creates a proxy model with a QgsMapLayerModel as source model.
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:50
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QStringList exceptedLayerIds() const
Get the list of maplayer ids which are excluded from the list.
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
void setExceptedLayerIds(const QStringList &ids)
Set the list of maplayer ids which are excluded from the list.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
Definition: qgsmaplayer.cpp:96
virtual QString name() const =0
Return 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.
bool hasGeometryType() const
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
void setExceptedLayerList(const QList< QgsMapLayer *> &exceptList)
offer the possibility to except some layers to be listed
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 setExcludedProviders(const QStringList &providers)
Sets a list of data providers which should be excluded from the model.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:350
QgsMapLayerProxyModel * setFilters(QgsMapLayerProxyModel::Filters filters)
setFilters set flags that affect how layers are filtered
const Filters & filters() const
bool readOnly() const
Returns if this layer is read only.
Definition: qgsmaplayer.h:292
QgsRasterDataProvider * dataProvider()
Returns the data provider.
QgsVectorDataProvider * dataProvider()
Returns the data provider.
Represents a vector layer which manages a vector based data sets.
True if index corresponds to the empty (not set) value.