QGIS API Documentation  3.0.2-Girona (307d082)
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  if ( mExcludedProviders.contains( layer->dataProvider()->name() ) )
102  return false;
103 
104  if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
105  return false;
106 
107  // layer type
108  if ( ( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) ||
109  ( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ||
110  ( mFilters.testFlag( PluginLayer ) && layer->type() == QgsMapLayer::PluginLayer ) )
111  return true;
112 
113  // geometry type
114  bool detectGeometry = mFilters.testFlag( NoGeometry ) ||
115  mFilters.testFlag( PointLayer ) ||
116  mFilters.testFlag( LineLayer ) ||
117  mFilters.testFlag( PolygonLayer ) ||
118  mFilters.testFlag( HasGeometry );
119  if ( detectGeometry && layer->type() == QgsMapLayer::VectorLayer )
120  {
121  if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( layer ) )
122  {
123  if ( mFilters.testFlag( HasGeometry ) && vl->isSpatial() )
124  return true;
125  if ( mFilters.testFlag( NoGeometry ) && vl->geometryType() == QgsWkbTypes::NullGeometry )
126  return true;
127  if ( mFilters.testFlag( PointLayer ) && vl->geometryType() == QgsWkbTypes::PointGeometry )
128  return true;
129  if ( mFilters.testFlag( LineLayer ) && vl->geometryType() == QgsWkbTypes::LineGeometry )
130  return true;
131  if ( mFilters.testFlag( PolygonLayer ) && vl->geometryType() == QgsWkbTypes::PolygonGeometry )
132  return true;
133  }
134  }
135 
136  return false;
137 }
138 
139 bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
140 {
141  // empty row is always first
142  if ( sourceModel()->data( left, QgsMapLayerModel::EmptyRole ).toBool() )
143  return true;
144  else if ( sourceModel()->data( right, QgsMapLayerModel::EmptyRole ).toBool() )
145  return false;
146 
147  // additional rows are always last
148  bool leftAdditional = sourceModel()->data( left, QgsMapLayerModel::AdditionalRole ).toBool();
149  bool rightAdditional = sourceModel()->data( right, QgsMapLayerModel::AdditionalRole ).toBool();
150 
151  if ( leftAdditional && !rightAdditional )
152  return false;
153  else if ( rightAdditional && !leftAdditional )
154  return true;
155 
156  // default mode is alphabetical order
157  QString leftStr = sourceModel()->data( left ).toString();
158  QString rightStr = sourceModel()->data( right ).toString();
159  return QString::localeAwareCompare( leftStr, rightStr ) < 0;
160 }
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:56
virtual QgsDataProvider * dataProvider()
Returns the layer&#39;s data provider.
QStringList exceptedLayerIds() const
Get the list of maplayer ids which are excluded from the list.
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.
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.
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:383
QgsMapLayerProxyModel * setFilters(QgsMapLayerProxyModel::Filters filters)
setFilters set flags that affect how layers are filtered
const Filters & filters() const
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:381
Represents a vector layer which manages a vector based data sets.
True if index corresponds to the empty (not set) value.