Skip to content
Permalink
Browse files

Only show writable fields in field calculator

Fix #15000
  • Loading branch information
m-kuhn committed Jun 11, 2016
1 parent f43b2ac commit 1877bcd0815c32150d651765c93f5eec06e8b242
@@ -46,6 +46,15 @@ class QgsFieldModel : QAbstractItemModel

//! returns the currently used layer
QgsVectorLayer* layer();
/**
* The currently applied filters that define which fields are shown.
*/
Filters filters() const;

/**
* The currently applied filters that define which fields are shown.
*/
void setFilters( const Filters& filter );

public slots:
//! set the layer of whch fields are displayed
@@ -248,6 +248,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
mUpdateExpressionText->registerGetExpressionContextCallback( &_getExpressionContext, mLayer );

mFieldModel = new QgsFieldModel( this );
mFieldModel->setFilters( QgsFieldModel::WritableFields );
mFieldModel->setLayer( mLayer );
mFieldCombo->setModel( mFieldModel );
connect( mRunFieldCalc, SIGNAL( clicked() ), this, SLOT( updateFieldFromExpression() ) );
@@ -435,12 +435,14 @@ void QgsFieldCalculator::populateFields()
const QgsFields& fields = mVectorLayer->fields();
for ( int idx = 0; idx < fields.count(); ++idx )
{
if ( fields.fieldOrigin( idx ) != QgsFields::OriginExpression && fields.fieldOrigin( idx ) != QgsFields::OriginJoin )
{
QString fieldName = fields.at( idx ).name();

QString fieldName = fields[idx].name();

//insert into field list and field combo box
mFieldMap.insert( fieldName, idx );
mExistingFieldComboBox->addItem( fieldName );
//insert into field list and field combo box
mFieldMap.insert( fieldName, idx );
mExistingFieldComboBox->addItem( fieldName );
}
}

if ( mVectorLayer->geometryType() != QGis::NoGeometry )
@@ -94,11 +94,35 @@ void QgsFieldModel::layerDeleted()
updateModel();
}

QgsFieldModel::Filters QgsFieldModel::filters() const
{
return mFilters;
}

void QgsFieldModel::setFilters( const Filters& filters )
{
if ( filters == mFilters )
return;

mFilters = filters;

updateModel();
}

void QgsFieldModel::updateModel()
{
if ( mLayer )
{
QgsFields newFields = mLayer->fields();
QgsFields fields = mLayer->fields();
QgsFields newFields;
for ( int i = 0; i < fields.count(); ++i )
{
if ( fields.fieldOrigin( i ) != QgsFields::OriginExpression && fields.fieldOrigin( i ) != QgsFields::OriginJoin )
{
newFields.append( fields.at( i ), fields.fieldOrigin( i ), fields.fieldOriginIndex( i ) );
}
}

if ( mFields.toList() != newFields.toList() )
{
// Try to handle two special cases: addition of a new field and removal of a field.
@@ -42,6 +42,15 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
FieldTypeRole = Qt::UserRole + 6 /*!< return the field type (if a field, return QVariant if expression) */
};

/**
* Filters for which fields should be shown
*/
enum Filter
{
WritableFields = 1 //!< Only show writable fields
};
Q_DECLARE_FLAGS( Filters, Filter )

/**
* @brief QgsFieldModel creates a model to display the fields of a given layer
*/
@@ -74,6 +83,16 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
int columnCount( const QModelIndex &parent ) const override;
QVariant data( const QModelIndex &index, int role ) const override;

/**
* The currently applied filters that define which fields are shown.
*/
Filters filters() const;

/**
* The currently applied filters that define which fields are shown.
*/
void setFilters( const Filters& filter );

public slots:
//! set the layer of whch fields are displayed
void setLayer( QgsVectorLayer *layer );
@@ -84,14 +103,14 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
private slots:
void layerDeleted();

protected:
private:
QgsFields mFields;
QList<QString> mExpression;

QgsVectorLayer* mLayer;
bool mAllowExpression;
Filters mFilters;

private:
void fetchFeature();
};

1 comment on commit 1877bcd

@nyalldawson

This comment has been minimized.

Copy link
Collaborator

@nyalldawson nyalldawson commented on 1877bcd Jun 15, 2016

@m-kuhn why was this done in QgsFieldModel and not in QgsFieldProxyModel? It would make more sense in the proxy model IMO.

It's also caused a regression - try adding a virtual field to a layer, and it won't appear in any existing field combo boxes.

Please sign in to comment.
You can’t perform that action at this time.