Skip to content

Commit 1877bcd

Browse files
committed
Only show writable fields in field calculator
Fix #15000
1 parent f43b2ac commit 1877bcd

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

python/gui/qgsfieldmodel.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ class QgsFieldModel : QAbstractItemModel
4646

4747
//! returns the currently used layer
4848
QgsVectorLayer* layer();
49+
/**
50+
* The currently applied filters that define which fields are shown.
51+
*/
52+
Filters filters() const;
53+
54+
/**
55+
* The currently applied filters that define which fields are shown.
56+
*/
57+
void setFilters( const Filters& filter );
4958

5059
public slots:
5160
//! set the layer of whch fields are displayed

src/app/qgsattributetabledialog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
248248
mUpdateExpressionText->registerGetExpressionContextCallback( &_getExpressionContext, mLayer );
249249

250250
mFieldModel = new QgsFieldModel( this );
251+
mFieldModel->setFilters( QgsFieldModel::WritableFields );
251252
mFieldModel->setLayer( mLayer );
252253
mFieldCombo->setModel( mFieldModel );
253254
connect( mRunFieldCalc, SIGNAL( clicked() ), this, SLOT( updateFieldFromExpression() ) );

src/app/qgsfieldcalculator.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,14 @@ void QgsFieldCalculator::populateFields()
435435
const QgsFields& fields = mVectorLayer->fields();
436436
for ( int idx = 0; idx < fields.count(); ++idx )
437437
{
438+
if ( fields.fieldOrigin( idx ) != QgsFields::OriginExpression && fields.fieldOrigin( idx ) != QgsFields::OriginJoin )
439+
{
440+
QString fieldName = fields.at( idx ).name();
438441

439-
QString fieldName = fields[idx].name();
440-
441-
//insert into field list and field combo box
442-
mFieldMap.insert( fieldName, idx );
443-
mExistingFieldComboBox->addItem( fieldName );
442+
//insert into field list and field combo box
443+
mFieldMap.insert( fieldName, idx );
444+
mExistingFieldComboBox->addItem( fieldName );
445+
}
444446
}
445447

446448
if ( mVectorLayer->geometryType() != QGis::NoGeometry )

src/gui/qgsfieldmodel.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,35 @@ void QgsFieldModel::layerDeleted()
9494
updateModel();
9595
}
9696

97+
QgsFieldModel::Filters QgsFieldModel::filters() const
98+
{
99+
return mFilters;
100+
}
101+
102+
void QgsFieldModel::setFilters( const Filters& filters )
103+
{
104+
if ( filters == mFilters )
105+
return;
106+
107+
mFilters = filters;
108+
109+
updateModel();
110+
}
111+
97112
void QgsFieldModel::updateModel()
98113
{
99114
if ( mLayer )
100115
{
101-
QgsFields newFields = mLayer->fields();
116+
QgsFields fields = mLayer->fields();
117+
QgsFields newFields;
118+
for ( int i = 0; i < fields.count(); ++i )
119+
{
120+
if ( fields.fieldOrigin( i ) != QgsFields::OriginExpression && fields.fieldOrigin( i ) != QgsFields::OriginJoin )
121+
{
122+
newFields.append( fields.at( i ), fields.fieldOrigin( i ), fields.fieldOriginIndex( i ) );
123+
}
124+
}
125+
102126
if ( mFields.toList() != newFields.toList() )
103127
{
104128
// Try to handle two special cases: addition of a new field and removal of a field.

src/gui/qgsfieldmodel.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
4242
FieldTypeRole = Qt::UserRole + 6 /*!< return the field type (if a field, return QVariant if expression) */
4343
};
4444

45+
/**
46+
* Filters for which fields should be shown
47+
*/
48+
enum Filter
49+
{
50+
WritableFields = 1 //!< Only show writable fields
51+
};
52+
Q_DECLARE_FLAGS( Filters, Filter )
53+
4554
/**
4655
* @brief QgsFieldModel creates a model to display the fields of a given layer
4756
*/
@@ -74,6 +83,16 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
7483
int columnCount( const QModelIndex &parent ) const override;
7584
QVariant data( const QModelIndex &index, int role ) const override;
7685

86+
/**
87+
* The currently applied filters that define which fields are shown.
88+
*/
89+
Filters filters() const;
90+
91+
/**
92+
* The currently applied filters that define which fields are shown.
93+
*/
94+
void setFilters( const Filters& filter );
95+
7796
public slots:
7897
//! set the layer of whch fields are displayed
7998
void setLayer( QgsVectorLayer *layer );
@@ -84,14 +103,14 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
84103
private slots:
85104
void layerDeleted();
86105

87-
protected:
106+
private:
88107
QgsFields mFields;
89108
QList<QString> mExpression;
90109

91110
QgsVectorLayer* mLayer;
92111
bool mAllowExpression;
112+
Filters mFilters;
93113

94-
private:
95114
void fetchFeature();
96115
};
97116

0 commit comments

Comments
 (0)