QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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  , mModel( new QgsMapLayerModel( this ) )
25 {
26  setSourceModel( mModel );
27 }
28 
30 {
31  mFilters = filters;
32  invalidateFilter();
33  return this;
34 }
35 
36 bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
37 {
38  if ( mFilters.testFlag( All ) )
39  return true;
40 
41  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
42  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
43  if ( !layer )
44  return false;
45 
46  // layer type
47  if (( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) ||
48  ( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ||
49  ( mFilters.testFlag( PluginLayer ) && layer->type() == QgsMapLayer::PluginLayer ) )
50  return true;
51 
52  // geometry type
53  bool detectGeometry = mFilters.testFlag( NoGeometry ) ||
54  mFilters.testFlag( PointLayer ) ||
55  mFilters.testFlag( LineLayer ) ||
56  mFilters.testFlag( PolygonLayer ) ||
57  mFilters.testFlag( HasGeometry );
58  if ( detectGeometry && layer->type() == QgsMapLayer::VectorLayer )
59  {
60  QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
61  if ( vl )
62  {
63  if ( mFilters.testFlag( HasGeometry ) && vl->hasGeometryType() )
64  return true;
65  if ( mFilters.testFlag( NoGeometry ) && vl->geometryType() == QGis::NoGeometry )
66  return true;
67  if ( mFilters.testFlag( PointLayer ) && vl->geometryType() == QGis::Point )
68  return true;
69  if ( mFilters.testFlag( LineLayer ) && vl->geometryType() == QGis::Line )
70  return true;
71  if ( mFilters.testFlag( PolygonLayer ) && vl->geometryType() == QGis::Polygon )
72  return true;
73  }
74  }
75 
76  return false;
77 }
78 
79 bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
80 {
81  // default mode is alphabetical order
82  QString leftId = sourceModel()->data( left ).toString();
83  QString rightId = sourceModel()->data( right ).toString();
84  return QString::localeAwareCompare( leftId, rightId ) < 0;
85 }
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:48
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:89
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
bool lessThan(const QModelIndex &left, const QModelIndex &right) const
The QgsMapLayerModel class is a model to display layers in widgets.
The QgsMapLayerProxModel class provides an easy to use model to display the list of layers in widgets...
QGis::GeometryType geometryType() const
Returns point, line or polygon.
QgsMapLayerModel * mModel
bool hasGeometryType() const
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
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