/
qgsfieldproxymodel.cpp
75 lines (63 loc) · 2.55 KB
/
qgsfieldproxymodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/***************************************************************************
qgsfieldproxymodel.cpp
--------------------------------------
Date : 01.04.2014
Copyright : (C) 2014 Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsfieldproxymodel.h"
#include "qgsfieldmodel.h"
#include "qgsvectorlayer.h"
QgsFieldProxyModel::QgsFieldProxyModel( QObject *parent )
: QSortFilterProxyModel( parent )
, mFilters( All )
, mModel( new QgsFieldModel( this ) )
{
setSourceModel( mModel );
}
QgsFieldProxyModel *QgsFieldProxyModel::setFilters( const Filters& filters )
{
mFilters = filters;
invalidateFilter();
return this;
}
bool QgsFieldProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
{
if ( mFilters.testFlag( All ) )
return true;
QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
QVariant typeVar = sourceModel()->data( index, QgsFieldModel::FieldTypeRole );
// if expression, consider valid
if ( typeVar.isNull() )
return true;
bool ok;
QVariant::Type type = ( QVariant::Type )typeVar.toInt( &ok );
if ( !ok )
return true;
if (( mFilters.testFlag( String ) && type == QVariant::String ) ||
( mFilters.testFlag( LongLong ) && type == QVariant::LongLong ) ||
( mFilters.testFlag( Int ) && type == QVariant::Int ) ||
( mFilters.testFlag( Double ) && type == QVariant::Double ) ||
( mFilters.testFlag( Date ) && type == QVariant::Date ) )
return true;
return false;
}
bool QgsFieldProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
{
// order is field order, then expressions
bool lok, rok;
int leftId = sourceModel()->data( left, QgsFieldModel::FieldIndexRole ).toInt( &lok );
int rightId = sourceModel()->data( right, QgsFieldModel::FieldIndexRole ).toInt( &rok );
if ( !lok )
return false;
if ( !rok )
return true;
return leftId < rightId;
}