QGIS API Documentation  3.2.0-Bonn (bc43194)
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( AllTypes )
23  , mModel( new QgsFieldModel( this ) )
24 {
25  setSourceModel( mModel );
26 }
27 
29 {
30  mFilters = filters;
31  invalidateFilter();
32  return this;
33 }
34 
35 bool QgsFieldProxyModel::isReadOnly( const QModelIndex &index ) const
36 {
37  QVariant originVariant = sourceModel()->data( index, QgsFieldModel::FieldOriginRole );
38  if ( originVariant.isNull() )
39  {
40  //expression
41  return true;
42  }
43 
44  QgsFields::FieldOrigin origin = static_cast< QgsFields::FieldOrigin >( originVariant.toInt() );
45  switch ( origin )
46  {
50  //read only
51  return true;
52 
55  //not read only
56  return false;
57  }
58  return false; // avoid warnings
59 }
60 
61 bool QgsFieldProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
62 {
63  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
64 
65  if ( mFilters.testFlag( HideReadOnly ) && isReadOnly( index ) )
66  return false;
67 
68  if ( mFilters.testFlag( AllTypes ) )
69  return true;
70 
71  QVariant typeVar = sourceModel()->data( index, QgsFieldModel::FieldTypeRole );
72 
73  // if expression, consider valid
74  if ( typeVar.isNull() )
75  return true;
76 
77  bool ok;
78  QVariant::Type type = ( QVariant::Type )typeVar.toInt( &ok );
79  if ( !ok )
80  return true;
81 
82  if ( ( mFilters.testFlag( String ) && type == QVariant::String ) ||
83  ( mFilters.testFlag( LongLong ) && type == QVariant::LongLong ) ||
84  ( mFilters.testFlag( Int ) && type == QVariant::Int ) ||
85  ( mFilters.testFlag( Double ) && type == QVariant::Double ) ||
86  ( mFilters.testFlag( Date ) && type == QVariant::Date ) ||
87  ( mFilters.testFlag( Date ) && type == QVariant::DateTime ) ||
88  ( mFilters.testFlag( Time ) && type == QVariant::Time ) )
89  return true;
90 
91  return false;
92 }
93 
94 bool QgsFieldProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
95 {
96  // empty field is always first
97  if ( sourceModel()->data( left, QgsFieldModel::IsEmptyRole ).toBool() )
98  return true;
99  else if ( sourceModel()->data( right, QgsFieldModel::IsEmptyRole ).toBool() )
100  return false;
101 
102  // order is field order, then expressions
103  bool lok, rok;
104  int leftId = sourceModel()->data( left, QgsFieldModel::FieldIndexRole ).toInt( &lok );
105  int rightId = sourceModel()->data( right, QgsFieldModel::FieldIndexRole ).toInt( &rok );
106 
107  if ( !lok )
108  return false;
109  if ( !rok )
110  return true;
111 
112  return leftId < rightId;
113 }
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfields.h:50
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfields.h:51
Return field index if index corresponds to a field.
Definition: qgsfieldmodel.h:51
QgsFieldProxyModel * setFilters(QgsFieldProxyModel::Filters filters)
Set flags that affect how fields are filtered in the model.
The QgsFieldModel class is a model to display the list of fields of a layer in widgets.
Definition: qgsfieldmodel.h:37
Field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfields.h:49
Return the field origin (if a field, returns QVariant if expression)
Definition: qgsfieldmodel.h:56
It has not been specified where the field comes from.
Definition: qgsfields.h:48
The QgsFieldProxyModel class provides an easy to use model to display the list of fields of a layer...
Date or datetime fields.
QgsFieldProxyModel(QObject *parent=nullptr)
QgsFieldProxModel creates a proxy model with a QgsFieldModel as source model.
Return the field type (if a field, return QVariant if expression)
Definition: qgsfieldmodel.h:55
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
const Filters & filters() const
Returns the filters controlling displayed fields.
Return if the index corresponds to the empty value.
Definition: qgsfieldmodel.h:57
Field is calculated from an expression.
Definition: qgsfields.h:52
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override