QGIS API Documentation  2.14.11-Essen
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 : 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 "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;
32  return this;
33 }
34 
35 bool QgsFieldProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
36 {
37  if ( mFilters.testFlag( All ) )
38  return true;
39 
40  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
42 
43  // if expression, consider valid
44  if ( typeVar.isNull() )
45  return true;
46 
47  bool ok;
48  QVariant::Type type = ( QVariant::Type )typeVar.toInt( &ok );
49  if ( !ok )
50  return true;
51 
52  if (( mFilters.testFlag( String ) && type == QVariant::String ) ||
53  ( mFilters.testFlag( LongLong ) && type == QVariant::LongLong ) ||
54  ( mFilters.testFlag( Int ) && type == QVariant::Int ) ||
55  ( mFilters.testFlag( Double ) && type == QVariant::Double ) ||
56  ( mFilters.testFlag( Date ) && type == QVariant::Date ) ||
57  ( mFilters.testFlag( Date ) && type == QVariant::DateTime ) )
58  return true;
59 
60  return false;
61 }
62 
63 bool QgsFieldProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
64 {
65  // order is field order, then expressions
66  bool lok, rok;
67  int leftId = sourceModel()->data( left, QgsFieldModel::FieldIndexRole ).toInt( &lok );
68  int rightId = sourceModel()->data( right, QgsFieldModel::FieldIndexRole ).toInt( &rok );
69 
70  if ( !lok )
71  return false;
72  if ( !rok )
73  return true;
74 
75  return leftId < rightId;
76 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QgsFieldProxyModel * setFilters(const QgsFieldProxyModel::Filters &filters)
setFilters set flags that affect how fields are filtered
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const=0
virtual void setSourceModel(QAbstractItemModel *sourceModel)
The QgsFieldModel class is a model to display the list of fields of a layer in widgets.
Definition: qgsfieldmodel.h:31
The QgsFieldProxyModel class provides an easy to use model to display the list of fields of a layer...
int toInt(bool *ok) const
bool isNull() const
QgsFieldProxyModel(QObject *parent=nullptr)
QgsFieldProxModel creates a proxy model with a QgsFieldModel as source model.
virtual QVariant data(const QModelIndex &index, int role) const=0
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
const Filters & filters() const
QAbstractItemModel * sourceModel() const
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override