QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  {
48  {
49  // show joined fields (e.g. auxiliary fields) only if they have a non-hidden editor widget.
50  // This enables them to be bulk field-calculated when a user needs to, but hides them by default
51  // (since there's often MANY of these, e.g. after using the label properties tool on a layer)
52  if ( sourceModel()->data( index, QgsFieldModel::EditorWidgetType ).toString() == QLatin1String( "Hidden" ) )
53  return true;
54 
55  return !sourceModel()->data( index, QgsFieldModel::JoinedFieldIsEditable ).toBool();
56  }
57 
60  //read only
61  return true;
62 
65  //not read only
66  return false;
67  }
68  return false; // avoid warnings
69 }
70 
71 bool QgsFieldProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
72 {
73  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
74 
75  if ( mFilters.testFlag( HideReadOnly ) && isReadOnly( index ) )
76  return false;
77 
78  if ( mFilters.testFlag( AllTypes ) )
79  return true;
80 
81  QVariant typeVar = sourceModel()->data( index, QgsFieldModel::FieldTypeRole );
82 
83  // if expression, consider valid
84  if ( typeVar.isNull() )
85  return true;
86 
87  bool ok;
88  QVariant::Type type = ( QVariant::Type )typeVar.toInt( &ok );
89  if ( !ok )
90  return true;
91 
92  if ( ( mFilters.testFlag( String ) && type == QVariant::String ) ||
93  ( mFilters.testFlag( LongLong ) && type == QVariant::LongLong ) ||
94  ( mFilters.testFlag( Int ) && type == QVariant::Int ) ||
95  ( mFilters.testFlag( Double ) && type == QVariant::Double ) ||
96  ( mFilters.testFlag( Date ) && type == QVariant::Date ) ||
97  ( mFilters.testFlag( Date ) && type == QVariant::DateTime ) ||
98  ( mFilters.testFlag( DateTime ) && type == QVariant::DateTime ) ||
99  ( mFilters.testFlag( Time ) && type == QVariant::Time ) )
100  return true;
101 
102  return false;
103 }
104 
105 bool QgsFieldProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
106 {
107  // empty field is always first
108  if ( sourceModel()->data( left, QgsFieldModel::IsEmptyRole ).toBool() )
109  return true;
110  else if ( sourceModel()->data( right, QgsFieldModel::IsEmptyRole ).toBool() )
111  return false;
112 
113  // order is field order, then expressions
114  bool lok, rok;
115  int leftId = sourceModel()->data( left, QgsFieldModel::FieldIndexRole ).toInt( &lok );
116  int rightId = sourceModel()->data( right, QgsFieldModel::FieldIndexRole ).toInt( &rok );
117 
118  if ( !lok )
119  return false;
120  if ( !rok )
121  return true;
122 
123  return leftId < rightId;
124 }
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
true if a joined field is editable (returns QVariant if not a joined field)
Definition: qgsfieldmodel.h:59
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