Skip to content

Commit c44285b

Browse files
committed
Make virtual fields updatable
Fix #11547 Fix #10994
1 parent 7905f7f commit c44285b

7 files changed

+94
-5
lines changed

python/core/qgsvectorlayer.sip

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,11 @@ class QgsVectorLayer : QgsMapLayer
292292
* @param exp The expression which calculates the field
293293
* @param fld The field to calculate
294294
*
295-
* @note added in 2.6
295+
* @return The index of the new field
296+
*
297+
* @note added in 2.6, return value added in 2.9
296298
*/
297-
void addExpressionField( const QString& exp, const QgsField& fld );
299+
int addExpressionField( const QString& exp, const QgsField& fld );
298300

299301
/**
300302
* Remove an expression field
@@ -305,6 +307,28 @@ class QgsVectorLayer : QgsMapLayer
305307
*/
306308
void removeExpressionField( int index );
307309

310+
/**
311+
* Returns the expressoin used for a given expression field
312+
*
313+
* @param index An index of an epxression based (virtual) field
314+
*
315+
* @return The expression for the field at index
316+
*
317+
* @note added in 2.9
318+
*/
319+
const QString expressionField( int index );
320+
321+
/**
322+
* Changes the expression used to define an expression based (virtual) field
323+
*
324+
* @param index The index of the expression to change
325+
*
326+
* @param exp The new expression to set
327+
*
328+
* @note added in 2.9
329+
*/
330+
void updateExpressionField( int index, const QString& exp );
331+
308332
/** Get the label object associated with this layer */
309333
QgsLabel *label();
310334

src/app/qgsfieldsproperties.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgsapplication.h"
2121
#include "qgsattributetypedialog.h"
2222
#include "qgsfieldcalculator.h"
23+
#include "qgsexpressionbuilderdialog.h"
2324
#include "qgsfieldsproperties.h"
2425
#include "qgslogger.h"
2526
#include "qgsmaplayerregistry.h"
@@ -249,6 +250,7 @@ void QgsFieldsProperties::setRow( int row, int idx, const QgsField& field )
249250
expressionWidget->setLayout( new QHBoxLayout );
250251
QToolButton* editExpressionButton = new QToolButton;
251252
editExpressionButton->setIcon( QgsApplication::getThemeIcon( "/mIconExpression.svg" ) );
253+
connect( editExpressionButton, SIGNAL(clicked()), this, SLOT(updateExpression()) );
252254
expressionWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
253255
expressionWidget->layout()->addWidget( editExpressionButton );
254256
expressionWidget->layout()->addWidget( new QLabel( mLayer->expressionField( idx ) ) );
@@ -657,6 +659,24 @@ void QgsFieldsProperties::attributesListCellChanged( int row, int column )
657659
}
658660
}
659661

662+
void QgsFieldsProperties::updateExpression()
663+
{
664+
QToolButton* btn = qobject_cast<QToolButton*>( sender() );
665+
Q_ASSERT( btn );
666+
667+
int index = btn->property( "Index" ).toInt();
668+
669+
const QString exp = mLayer->expressionField( index );
670+
671+
QgsExpressionBuilderDialog dlg( mLayer, exp );
672+
673+
if ( dlg.exec() )
674+
{
675+
mLayer->updateExpressionField( index, dlg.expressionText() );
676+
loadRows();
677+
}
678+
}
679+
660680
void QgsFieldsProperties::on_mCalculateFieldButton_clicked()
661681
{
662682
if ( !mLayer )

src/app/qgsfieldsproperties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
183183

184184
void attributesListCellChanged( int row, int column );
185185

186+
void updateExpression();
186187

187188
/** editing of layer was toggled */
188189
void editingToggled();

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::updateExpression( int index, const QString& exp )
37+
{
38+
mExpressions[index].expression = exp;
39+
}
40+
3641
void QgsExpressionFieldBuffer::writeXml( QDomNode& layerNode, QDomDocument& document ) const
3742
{
3843
QDomElement expressionFieldsElem = document.createElement( "expressionfields" );

src/core/qgsexpressionfieldbuffer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ class CORE_EXPORT QgsExpressionFieldBuffer
5858
*/
5959
void removeExpression( int index );
6060

61+
/**
62+
* Changes the expression at a given index
63+
*
64+
* @param index The index of the expression to change
65+
* @param exp The new expression to set
66+
*
67+
* @note added in 2.9
68+
*/
69+
void updateExpression( int index, const QString& exp );
70+
6171
/**
6272
* Saves expressions to xml under the layer node
6373
*/

src/core/qgsvectorlayer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2843,12 +2843,13 @@ const QList< QgsVectorJoinInfo >& QgsVectorLayer::vectorJoins() const
28432843
return mJoinBuffer->vectorJoins();
28442844
}
28452845

2846-
void QgsVectorLayer::addExpressionField( const QString& exp, const QgsField& fld )
2846+
int QgsVectorLayer::addExpressionField( const QString& exp, const QgsField& fld )
28472847
{
28482848
mExpressionFieldBuffer->addExpression( exp, fld );
28492849
updateFields();
28502850
int idx = mUpdatedFields.indexFromName( fld.name() );
28512851
emit attributeAdded( idx );
2852+
return idx;
28522853
}
28532854

28542855
void QgsVectorLayer::removeExpressionField( int index )
@@ -2865,6 +2866,12 @@ const QString QgsVectorLayer::expressionField( int index )
28652866
return mExpressionFieldBuffer->expressions().value( oi ).expression;
28662867
}
28672868

2869+
void QgsVectorLayer::updateExpressionField( int index, const QString& exp )
2870+
{
2871+
int oi = mUpdatedFields.fieldOriginIndex( index );
2872+
mExpressionFieldBuffer->updateExpression( oi, exp );
2873+
}
2874+
28682875
void QgsVectorLayer::updateFields()
28692876
{
28702877
if ( !mDataProvider )

src/core/qgsvectorlayer.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
655655
* @param exp The expression which calculates the field
656656
* @param fld The field to calculate
657657
*
658-
* @note added in 2.6
658+
* @return The index of the new field
659+
*
660+
* @note added in 2.6, return value added in 2.9
659661
*/
660-
void addExpressionField( const QString& exp, const QgsField& fld );
662+
int addExpressionField( const QString& exp, const QgsField& fld );
661663

662664
/**
663665
* Remove an expression field
@@ -668,8 +670,28 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
668670
*/
669671
void removeExpressionField( int index );
670672

673+
/**
674+
* Returns the expressoin used for a given expression field
675+
*
676+
* @param index An index of an epxression based (virtual) field
677+
*
678+
* @return The expression for the field at index
679+
*
680+
* @note added in 2.9
681+
*/
671682
const QString expressionField( int index );
672683

684+
/**
685+
* Changes the expression used to define an expression based (virtual) field
686+
*
687+
* @param index The index of the expression to change
688+
*
689+
* @param exp The new expression to set
690+
*
691+
* @note added in 2.9
692+
*/
693+
void updateExpressionField( int index, const QString& exp );
694+
673695
/** Get the label object associated with this layer */
674696
QgsLabel *label();
675697

0 commit comments

Comments
 (0)