QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 : denis.rouzaud@gmail.com
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 "qgsvectorlayer.h"
20 
22  : QSortFilterProxyModel( parent )
23  , mFilters( All )
24  , mExceptList( QList<QgsMapLayer*>() )
25  , mModel( new QgsMapLayerModel( parent ) )
26 {
27  setSourceModel( mModel );
28  setDynamicSortFilter( true );
29  setSortLocaleAware( true );
30  setFilterCaseSensitivity( Qt::CaseInsensitive );
31  sort( 0 );
32 }
33 
35 {
36  mFilters = filters;
37  invalidateFilter();
38  return this;
39 }
40 
41 void QgsMapLayerProxyModel::setExceptedLayerList( QList<QgsMapLayer*> exceptList )
42 {
43  mExceptList = exceptList;
44  invalidateFilter();
45 }
46 
47 bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
48 {
49  if ( mFilters.testFlag( All ) )
50  return true;
51 
52  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
53  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
54  if ( !layer )
55  return false;
56 
57  if ( mExceptList.contains( layer ) )
58  return false;
59 
60  // layer type
61  if (( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) ||
62  ( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ||
63  ( mFilters.testFlag( PluginLayer ) && layer->type() == QgsMapLayer::PluginLayer ) )
64  return true;
65 
66  // geometry type
67  bool detectGeometry = mFilters.testFlag( NoGeometry ) ||
68  mFilters.testFlag( PointLayer ) ||
69  mFilters.testFlag( LineLayer ) ||
70  mFilters.testFlag( PolygonLayer ) ||
71  mFilters.testFlag( HasGeometry );
72  if ( detectGeometry && layer->type() == QgsMapLayer::VectorLayer )
73  {
74  QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
75  if ( vl )
76  {
77  if ( mFilters.testFlag( HasGeometry ) && vl->hasGeometryType() )
78  return true;
79  if ( mFilters.testFlag( NoGeometry ) && vl->geometryType() == QGis::NoGeometry )
80  return true;
81  if ( mFilters.testFlag( PointLayer ) && vl->geometryType() == QGis::Point )
82  return true;
83  if ( mFilters.testFlag( LineLayer ) && vl->geometryType() == QGis::Line )
84  return true;
85  if ( mFilters.testFlag( PolygonLayer ) && vl->geometryType() == QGis::Polygon )
86  return true;
87  }
88  }
89 
90  return false;
91 }
92 
93 bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
94 {
95  // default mode is alphabetical order
96  QString leftStr = sourceModel()->data( left ).toString();
97  QString rightStr = sourceModel()->data( right ).toString();
98  return QString::localeAwareCompare( leftStr, rightStr ) < 0;
99 }
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:92
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
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...
QGis::GeometryType geometryType() const
Returns point, line or polygon.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
void sort(double *heap, int *x, int *y, int N)
Definition: util.cpp:72
bool hasGeometryType() const
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
void setExceptedLayerList(QList< QgsMapLayer * > exceptList)
offer the possibility to except some layers to be listed
QgsMapLayerProxyModel(QObject *parent=0)
QgsMapLayerProxModel creates a proxy model with a QgsMapLayerModel as source model.
Represents a vector layer which manages a vector based data sets.
QgsMapLayerProxyModel * setFilters(Filters filters)
setFilters set flags that affect how layers are filtered
const Filters & filters() const