QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsfieldproxymodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfieldproxymodel.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 "qgsfieldproxymodel.h"
17 #include "qgsfieldmodel.h"
18 #include "qgsvectorlayer.h"
19 
21  : QSortFilterProxyModel( parent )
22  , mFilters( All )
23  , mModel( new QgsFieldModel( this ) )
24 {
25  setSourceModel( mModel );
26 }
27 
29 {
30  mFilters = filters;
31  return this;
32 }
33 
34 bool QgsFieldProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
35 {
36  if ( mFilters.testFlag( All ) )
37  return true;
38 
39  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
40  QVariant typeVar = sourceModel()->data( index, QgsFieldModel::FieldTypeRole );
41 
42  // if expression, consider valid
43  if ( typeVar.isNull() )
44  return true;
45 
46  bool ok;
47  QVariant::Type type = ( QVariant::Type )typeVar.toInt( &ok );
48  if ( !ok )
49  return true;
50 
51  if (( mFilters.testFlag( String ) && type == QVariant::String ) ||
52  ( mFilters.testFlag( LongLong ) && type == QVariant::LongLong ) ||
53  ( mFilters.testFlag( Int ) && type == QVariant::Int ) ||
54  ( mFilters.testFlag( Double ) && type == QVariant::Double ) ||
55  ( mFilters.testFlag( Date ) && type == QVariant::Date ) )
56  return true;
57 
58  return false;
59 }
60 
61 bool QgsFieldProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
62 {
63  // order is field order, then expressions
64  bool lok, rok;
65  int leftId = sourceModel()->data( left, QgsFieldModel::FieldIndexRole ).toInt( &lok );
66  int rightId = sourceModel()->data( right, QgsFieldModel::FieldIndexRole ).toInt( &rok );
67 
68  if ( !lok )
69  return false;
70  if ( !rok )
71  return true;
72 
73  return leftId < rightId;
74 }