Skip to content

Commit

Permalink
Only show writable fields in field calculator
Browse files Browse the repository at this point in the history
Fix #15000
  • Loading branch information
m-kuhn committed Jun 11, 2016
1 parent f43b2ac commit 1877bcd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
9 changes: 9 additions & 0 deletions python/gui/qgsfieldmodel.sip
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -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() ) );
Expand Down
12 changes: 7 additions & 5 deletions src/app/qgsfieldcalculator.cpp
Expand Up @@ -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 )
Expand Down
26 changes: 25 additions & 1 deletion src/gui/qgsfieldmodel.cpp
Expand Up @@ -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.
Expand Down
23 changes: 21 additions & 2 deletions src/gui/qgsfieldmodel.h
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 );
Expand All @@ -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();
};

Expand Down

1 comment on commit 1877bcd

@nyalldawson
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.