QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 : [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 "qgsvectorlayer.h"
20 
22  : QSortFilterProxyModel( parent )
23  , mFilters( All )
24  , mModel( new QgsMapLayerModel( parent ) )
25 {
26  setSourceModel( mModel );
27  setDynamicSortFilter( true );
28  setSortLocaleAware( true );
29  setFilterCaseSensitivity( Qt::CaseInsensitive );
30  sort( 0 );
31 }
32 
34 {
35  mFilters = filters;
36  invalidateFilter();
37  return this;
38 }
39 
40 bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
41 {
42  if ( mFilters.testFlag( All ) )
43  return true;
44 
45  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
46  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
47  if ( !layer )
48  return false;
49 
50  // layer type
51  if (( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) ||
52  ( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ||
53  ( mFilters.testFlag( PluginLayer ) && layer->type() == QgsMapLayer::PluginLayer ) )
54  return true;
55 
56  // geometry type
57  bool detectGeometry = mFilters.testFlag( NoGeometry ) ||
58  mFilters.testFlag( PointLayer ) ||
59  mFilters.testFlag( LineLayer ) ||
60  mFilters.testFlag( PolygonLayer ) ||
61  mFilters.testFlag( HasGeometry );
62  if ( detectGeometry && layer->type() == QgsMapLayer::VectorLayer )
63  {
64  QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
65  if ( vl )
66  {
67  if ( mFilters.testFlag( HasGeometry ) && vl->hasGeometryType() )
68  return true;
69  if ( mFilters.testFlag( NoGeometry ) && vl->geometryType() == QGis::NoGeometry )
70  return true;
71  if ( mFilters.testFlag( PointLayer ) && vl->geometryType() == QGis::Point )
72  return true;
73  if ( mFilters.testFlag( LineLayer ) && vl->geometryType() == QGis::Line )
74  return true;
75  if ( mFilters.testFlag( PolygonLayer ) && vl->geometryType() == QGis::Polygon )
76  return true;
77  }
78  }
79 
80  return false;
81 }
82 
83 bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
84 {
85  // default mode is alphabetical order
86  QString leftStr = sourceModel()->data( left ).toString();
87  QString rightStr = sourceModel()->data( right ).toString();
88  return QString::localeAwareCompare( leftStr, rightStr ) < 0;
89 }