QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsmaplayerregistry.h"
20 #include "qgsvectorlayer.h"
21 
23  : QSortFilterProxyModel( parent )
24  , mFilters( All )
25  , mExceptList( QList<QgsMapLayer*>() )
26  , mModel( new QgsMapLayerModel( parent ) )
27 {
28  setSourceModel( mModel );
29  setDynamicSortFilter( true );
30  setSortLocaleAware( true );
31  setFilterCaseSensitivity( Qt::CaseInsensitive );
32  sort( 0 );
33 }
34 
36 {
37  mFilters = filters;
39  return this;
40 }
41 
43 {
44  if ( mExceptList == exceptList )
45  return;
46 
47  mExceptList = exceptList;
49 }
50 
52 {
53  mExceptList.clear();
54 
55  Q_FOREACH ( const QString& id, ids )
56  {
58  if ( l )
59  mExceptList << l;
60  }
62 }
63 
65 {
66  QStringList lst;
67 
68  Q_FOREACH ( QgsMapLayer* l, mExceptList )
69  lst << l->id();
70 
71  return lst;
72 }
73 
74 bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
75 {
76  if ( mFilters.testFlag( All ) && mExceptList.isEmpty() )
77  return true;
78 
79  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
80  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
81  if ( !layer )
82  return false;
83 
84  if ( mExceptList.contains( layer ) )
85  return false;
86 
87  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( layer );
88 
89  if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
90  return false;
91 
92  // layer type
93  if (( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) ||
94  ( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ||
95  ( mFilters.testFlag( PluginLayer ) && layer->type() == QgsMapLayer::PluginLayer ) )
96  return true;
97 
98  // geometry type
99  bool detectGeometry = mFilters.testFlag( NoGeometry ) ||
100  mFilters.testFlag( PointLayer ) ||
101  mFilters.testFlag( LineLayer ) ||
102  mFilters.testFlag( PolygonLayer ) ||
103  mFilters.testFlag( HasGeometry );
104  if ( detectGeometry && layer->type() == QgsMapLayer::VectorLayer )
105  {
106  if ( vl )
107  {
108  if ( mFilters.testFlag( HasGeometry ) && vl->hasGeometryType() )
109  return true;
110  if ( mFilters.testFlag( NoGeometry ) && vl->geometryType() == QGis::NoGeometry )
111  return true;
112  if ( mFilters.testFlag( PointLayer ) && vl->geometryType() == QGis::Point )
113  return true;
114  if ( mFilters.testFlag( LineLayer ) && vl->geometryType() == QGis::Line )
115  return true;
116  if ( mFilters.testFlag( PolygonLayer ) && vl->geometryType() == QGis::Polygon )
117  return true;
118  }
119  }
120 
121  return false;
122 }
123 
124 bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
125 {
126  // default mode is alphabetical order
127  QString leftStr = sourceModel()->data( left ).toString();
128  QString rightStr = sourceModel()->data( right ).toString();
129  return QString::localeAwareCompare( leftStr, rightStr ) < 0;
130 }
QgsMapLayerProxyModel(QObject *parent=nullptr)
QgsMapLayerProxModel creates a proxy model with a QgsMapLayerModel as source model.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
void clear()
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void setFilterCaseSensitivity(Qt::CaseSensitivity cs)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const=0
int localeAwareCompare(const QString &other) const
virtual void sort(int column, Qt::SortOrder order)
virtual void setSourceModel(QAbstractItemModel *sourceModel)
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
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
Get the type of the layer.
Definition: qgsmaplayer.cpp:99
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
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
Get this layer&#39;s unique ID, this ID is used to access this layer from map layer registry.
bool isEmpty() const
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...
void setDynamicSortFilter(bool enable)
void * internalPointer() const
virtual QVariant data(const QModelIndex &index, int role) const=0
QGis::GeometryType geometryType() const
Returns point, line or polygon.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
bool contains(const T &value) const
QAbstractItemModel * sourceModel() const
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
void setSortLocaleAware(bool on)
QgsMapLayerProxyModel * setFilters(const 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:246
Represents a vector layer which manages a vector based data sets.
QString toString() const