QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
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
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
18#include "qgsfieldmodel.h"
19#include "qgsvariantutils.h"
20
21#include <QString>
22
23#include "moc_qgsfieldproxymodel.cpp"
24
25using namespace Qt::StringLiterals;
26
28 : QSortFilterProxyModel( parent )
29 , mFilters( AllTypes )
30 , mModel( new QgsFieldModel( this ) )
31{
32 setSourceModel( mModel );
33}
34
36{
37 mFilters = filters;
38 invalidateFilter();
39 return this;
40}
41
42bool QgsFieldProxyModel::isReadOnly( const QModelIndex &index ) const
43{
44 const QVariant originVariant = sourceModel()->data( index, static_cast< int >( QgsFieldModel::CustomRole::FieldOrigin ) );
45 if ( QgsVariantUtils::isNull( originVariant ) )
46 {
47 //expression
48 return true;
49 }
50
51 const Qgis::FieldOrigin origin = static_cast< Qgis::FieldOrigin >( originVariant.toInt() );
52 switch ( origin )
53 {
55 {
56 // show joined fields (e.g. auxiliary fields) only if they have a non-hidden editor widget.
57 // This enables them to be bulk field-calculated when a user needs to, but hides them by default
58 // (since there's often MANY of these, e.g. after using the label properties tool on a layer)
59 if ( sourceModel()->data( index, static_cast< int >( QgsFieldModel::CustomRole::EditorWidgetType ) ).toString() == "Hidden"_L1 )
60 return true;
61
62 return !sourceModel()->data( index, static_cast< int >( QgsFieldModel::CustomRole::JoinedFieldIsEditable ) ).toBool();
63 }
64
67 //read only
68 return true;
69
72 {
73 if ( !sourceModel()->data( index, static_cast< int >( QgsFieldModel::CustomRole::FieldIsWidgetEditable ) ).toBool() )
74 {
75 return true;
76 }
77 else
78 {
79 //not read only
80 return false;
81 }
82 }
83
84 }
85 return false; // avoid warnings
86}
87
88bool QgsFieldProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
89{
90 const QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
91
92 if ( mFilters.testFlag( HideReadOnly ) && isReadOnly( index ) )
93 return false;
94
95 if ( mFilters.testFlag( QgsFieldProxyModel::OriginProvider ) )
96 {
97 const Qgis::FieldOrigin origin = static_cast< Qgis::FieldOrigin >( sourceModel()->data( index, static_cast< int >( QgsFieldModel::CustomRole::FieldOrigin ) ).toInt() );
98 switch ( origin )
99 {
104 return false;
105
107 break;
108 }
109 }
110
111 if ( mFilters.testFlag( AllTypes ) )
112 return true;
113
114 const QVariant typeVar = sourceModel()->data( index, static_cast< int >( QgsFieldModel::CustomRole::FieldType ) );
115
116 // if expression, consider valid
117 if ( QgsVariantUtils::isNull( typeVar ) )
118 return true;
119
120 bool ok;
121 const QMetaType::Type type = static_cast<QMetaType::Type>( typeVar.toInt( &ok ) );
122 if ( !ok )
123 return true;
124
125 if ( ( mFilters.testFlag( String ) && type == QMetaType::Type::QString ) ||
126 ( mFilters.testFlag( LongLong ) && type == QMetaType::Type::LongLong ) ||
127 ( mFilters.testFlag( Int ) && type == QMetaType::Type::Int ) ||
128 ( mFilters.testFlag( Double ) && type == QMetaType::Type::Double ) ||
129 ( mFilters.testFlag( Date ) && type == QMetaType::Type::QDate ) ||
130 ( mFilters.testFlag( Date ) && type == QMetaType::Type::QDateTime ) ||
131 ( mFilters.testFlag( DateTime ) && type == QMetaType::Type::QDateTime ) ||
132 ( mFilters.testFlag( Time ) && type == QMetaType::Type::QTime ) ||
133 ( mFilters.testFlag( Binary ) && type == QMetaType::Type::QByteArray ) ||
134 ( mFilters.testFlag( Boolean ) && type == QMetaType::Type::Bool ) )
135 return true;
136
137 return false;
138}
139
140bool QgsFieldProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
141{
142 // empty field is always first
143 if ( sourceModel()->data( left, static_cast< int >( QgsFieldModel::CustomRole::IsEmpty ) ).toBool() )
144 return true;
145 else if ( sourceModel()->data( right, static_cast< int >( QgsFieldModel::CustomRole::IsEmpty ) ).toBool() )
146 return false;
147
148 // order is field order, then expressions
149 bool lok, rok;
150 const int leftId = sourceModel()->data( left, static_cast< int >( QgsFieldModel::CustomRole::FieldIndex ) ).toInt( &lok );
151 const int rightId = sourceModel()->data( right, static_cast< int >( QgsFieldModel::CustomRole::FieldIndex ) ).toInt( &rok );
152
153 if ( !lok )
154 return false;
155 if ( !rok )
156 return true;
157
158 return leftId < rightId;
159}
FieldOrigin
Field origin.
Definition qgis.h:1762
@ Provider
Field originates from the underlying data provider of the vector layer.
Definition qgis.h:1764
@ Edit
Field has been temporarily added in editing mode.
Definition qgis.h:1766
@ Unknown
The field origin has not been specified.
Definition qgis.h:1763
@ Expression
Field is calculated from an expression.
Definition qgis.h:1767
@ Join
Field originates from a joined layer.
Definition qgis.h:1765
A model which displays the list of fields in widgets (optionally associated with a vector layer).
@ FieldIsWidgetEditable
true if a is editable from the widget
@ FieldOrigin
Return the field origin (if a field, returns QVariant if expression).
@ IsEmpty
Return if the index corresponds to the empty value.
@ FieldIndex
Return field index if index corresponds to a field.
@ EditorWidgetType
Editor widget type.
@ FieldType
Return the field type (if a field, return QVariant if expression).
@ JoinedFieldIsEditable
true if a joined field is editable (returns QVariant if not a joined field)
@ DateTime
Datetime fields.
@ HideReadOnly
Hide read-only fields.
@ LongLong
Longlong fields.
@ Double
Double fields.
@ AllTypes
All field types.
@ Date
Date or datetime fields.
@ Binary
Binary fields, since QGIS 3.34.
@ Int
Integer fields.
@ String
String fields.
@ Boolean
Boolean fields, since QGIS 3.34.
@ OriginProvider
Fields with a provider origin, since QGIS 3.38.
const Filters & filters() const
Returns the filters controlling displayed fields.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
QgsFieldProxyModel(QObject *parent=nullptr)
QgsFieldProxModel creates a proxy model with a QgsFieldModel as source model.
QFlags< Filter > Filters
QgsFieldProxyModel * setFilters(QgsFieldProxyModel::Filters filters)
Set flags that affect how fields are filtered in the model.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.