Skip to content

Commit d558077

Browse files
committedOct 12, 2016
Fix renaming a virtual field leads to crash (fix #15669)
(cherry-picked from 3a76708)
1 parent 6ee6398 commit d558077

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed
 

‎python/core/qgsexpressionfieldbuffer.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ class QgsExpressionFieldBuffer
2727
*/
2828
void removeExpression( int index );
2929

30+
/**
31+
* Renames an expression field at a given index
32+
*
33+
* @param index The index of the expression to change
34+
* @param name New name for field
35+
*
Code has comments. Press enter to view.
36+
* @note added in 3.0
37+
*/
38+
void renameExpression( int index, const QString& name );
39+
3040
/**
3141
* Changes the expression at a given index
3242
*

‎src/core/qgsexpressionfieldbuffer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ void QgsExpressionFieldBuffer::removeExpression( int index )
3333
mExpressions.removeAt( index );
3434
}
3535

36+
void QgsExpressionFieldBuffer::renameExpression( int index, const QString& name )
37+
{
38+
mExpressions[index].field.setName( name );
39+
}
40+
3641
void QgsExpressionFieldBuffer::updateExpression( int index, const QString& exp )
3742
{
3843
mExpressions[index].expression = exp;

‎src/core/qgsexpressionfieldbuffer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ class CORE_EXPORT QgsExpressionFieldBuffer
6767
*/
6868
void removeExpression( int index );
6969

70+
/**
71+
* Renames an expression field at a given index
72+
*
73+
* @param index The index of the expression to change
74+
* @param name New name for field
75+
*
76+
* @note added in 3.0
77+
*/
78+
void renameExpression( int index, const QString& name );
79+
7080
/**
7181
* Changes the expression at a given index
7282
*

‎src/core/qgsvectorlayer.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,12 +2405,41 @@ void QgsVectorLayer::remAttributeAlias( int attIndex )
24052405
}
24062406
}
24072407

2408-
bool QgsVectorLayer::renameAttribute( int attIndex, const QString& newName )
2408+
bool QgsVectorLayer::renameAttribute( int index, const QString& newName )
24092409
{
2410-
if ( !mEditBuffer || !mDataProvider )
2410+
if ( index < 0 || index >= fields().count() )
24112411
return false;
24122412

2413-
return mEditBuffer->renameAttribute( attIndex, newName );
2413+
switch ( mUpdatedFields.fieldOrigin( index ) )
2414+
{
2415+
case QgsFields::OriginExpression:
2416+
{
2417+
if ( mExpressionFieldBuffer )
2418+
{
2419+
int oi = mUpdatedFields.fieldOriginIndex( index );
2420+
mExpressionFieldBuffer->renameExpression( oi, newName );
2421+
updateFields();
2422+
return true;
2423+
}
2424+
else
2425+
{
2426+
return false;
2427+
}
2428+
}
2429+
2430+
case QgsFields::OriginProvider:
2431+
case QgsFields::OriginEdit:
2432+
2433+
if ( !mEditBuffer || !mDataProvider )
2434+
return false;
2435+
2436+
return mEditBuffer->renameAttribute( index, newName );
2437+
2438+
case QgsFields::OriginJoin:
2439+
case QgsFields::OriginUnknown:
2440+
return false;
2441+
2442+
}
Code has comments. Press enter to view.
24142443
}
24152444

24162445
void QgsVectorLayer::addAttributeAlias( int attIndex, const QString& aliasString )

‎tests/src/python/test_qgsvectorlayer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,16 @@ def checkFieldNames(names):
10331033
#layer.undoStack().redo()
10341034
#checkFieldNames(['fldint'])
10351035

1036+
def test_RenameExpressionField(self):
1037+
layer = createLayerWithOnePoint()
1038+
exp_field_idx = layer.addExpressionField('1+1', QgsField('math_is_hard', QVariant.Int))
1039+
1040+
#rename and check
1041+
self.assertTrue(layer.renameAttribute(exp_field_idx, 'renamed'))
1042+
self.assertEqual(layer.fields()[exp_field_idx].name(), 'renamed')
1043+
f = next(layer.getFeatures())
1044+
self.assertEqual(f.fields()[exp_field_idx].name(), 'renamed')
1045+
10361046
def test_fields(self):
10371047
layer = createLayerWithOnePoint()
10381048

0 commit comments

Comments
 (0)
Please sign in to comment.