Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make virtual fields updatable
Fix #11547
Fix #10994
  • Loading branch information
m-kuhn committed Mar 27, 2015
1 parent 7905f7f commit c44285b
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 5 deletions.
28 changes: 26 additions & 2 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -292,9 +292,11 @@ class QgsVectorLayer : QgsMapLayer
* @param exp The expression which calculates the field
* @param fld The field to calculate
*
* @note added in 2.6
* @return The index of the new field
*
* @note added in 2.6, return value added in 2.9
*/
void addExpressionField( const QString& exp, const QgsField& fld );
int addExpressionField( const QString& exp, const QgsField& fld );

/**
* Remove an expression field
Expand All @@ -305,6 +307,28 @@ class QgsVectorLayer : QgsMapLayer
*/
void removeExpressionField( int index );

/**
* Returns the expressoin used for a given expression field
*
* @param index An index of an epxression based (virtual) field
*
* @return The expression for the field at index
*
* @note added in 2.9
*/
const QString expressionField( int index );

/**
* Changes the expression used to define an expression based (virtual) field
*
* @param index The index of the expression to change
*
* @param exp The new expression to set
*
* @note added in 2.9
*/
void updateExpressionField( int index, const QString& exp );

/** Get the label object associated with this layer */
QgsLabel *label();

Expand Down
20 changes: 20 additions & 0 deletions src/app/qgsfieldsproperties.cpp
Expand Up @@ -20,6 +20,7 @@
#include "qgsapplication.h"
#include "qgsattributetypedialog.h"
#include "qgsfieldcalculator.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsfieldsproperties.h"
#include "qgslogger.h"
#include "qgsmaplayerregistry.h"
Expand Down Expand Up @@ -249,6 +250,7 @@ void QgsFieldsProperties::setRow( int row, int idx, const QgsField& field )
expressionWidget->setLayout( new QHBoxLayout );
QToolButton* editExpressionButton = new QToolButton;
editExpressionButton->setIcon( QgsApplication::getThemeIcon( "/mIconExpression.svg" ) );
connect( editExpressionButton, SIGNAL(clicked()), this, SLOT(updateExpression()) );
expressionWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
expressionWidget->layout()->addWidget( editExpressionButton );
expressionWidget->layout()->addWidget( new QLabel( mLayer->expressionField( idx ) ) );
Expand Down Expand Up @@ -657,6 +659,24 @@ void QgsFieldsProperties::attributesListCellChanged( int row, int column )
}
}

void QgsFieldsProperties::updateExpression()
{
QToolButton* btn = qobject_cast<QToolButton*>( sender() );
Q_ASSERT( btn );

int index = btn->property( "Index" ).toInt();

const QString exp = mLayer->expressionField( index );

QgsExpressionBuilderDialog dlg( mLayer, exp );

if ( dlg.exec() )
{
mLayer->updateExpressionField( index, dlg.expressionText() );
loadRows();
}
}

void QgsFieldsProperties::on_mCalculateFieldButton_clicked()
{
if ( !mLayer )
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsfieldsproperties.h
Expand Up @@ -183,6 +183,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope

void attributesListCellChanged( int row, int column );

void updateExpression();

/** editing of layer was toggled */
void editingToggled();
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsexpressionfieldbuffer.cpp
Expand Up @@ -33,6 +33,11 @@ void QgsExpressionFieldBuffer::removeExpression( int index )
mExpressions.removeAt( index );
}

void QgsExpressionFieldBuffer::updateExpression( int index, const QString& exp )
{
mExpressions[index].expression = exp;
}

void QgsExpressionFieldBuffer::writeXml( QDomNode& layerNode, QDomDocument& document ) const
{
QDomElement expressionFieldsElem = document.createElement( "expressionfields" );
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsexpressionfieldbuffer.h
Expand Up @@ -58,6 +58,16 @@ class CORE_EXPORT QgsExpressionFieldBuffer
*/
void removeExpression( int index );

/**
* Changes the expression at a given index
*
* @param index The index of the expression to change
* @param exp The new expression to set
*
* @note added in 2.9
*/
void updateExpression( int index, const QString& exp );

/**
* Saves expressions to xml under the layer node
*/
Expand Down
9 changes: 8 additions & 1 deletion src/core/qgsvectorlayer.cpp
Expand Up @@ -2843,12 +2843,13 @@ const QList< QgsVectorJoinInfo >& QgsVectorLayer::vectorJoins() const
return mJoinBuffer->vectorJoins();
}

void QgsVectorLayer::addExpressionField( const QString& exp, const QgsField& fld )
int QgsVectorLayer::addExpressionField( const QString& exp, const QgsField& fld )
{
mExpressionFieldBuffer->addExpression( exp, fld );
updateFields();
int idx = mUpdatedFields.indexFromName( fld.name() );
emit attributeAdded( idx );
return idx;
}

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

void QgsVectorLayer::updateExpressionField( int index, const QString& exp )
{
int oi = mUpdatedFields.fieldOriginIndex( index );
mExpressionFieldBuffer->updateExpression( oi, exp );
}

void QgsVectorLayer::updateFields()
{
if ( !mDataProvider )
Expand Down
26 changes: 24 additions & 2 deletions src/core/qgsvectorlayer.h
Expand Up @@ -655,9 +655,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
* @param exp The expression which calculates the field
* @param fld The field to calculate
*
* @note added in 2.6
* @return The index of the new field
*
* @note added in 2.6, return value added in 2.9
*/
void addExpressionField( const QString& exp, const QgsField& fld );
int addExpressionField( const QString& exp, const QgsField& fld );

/**
* Remove an expression field
Expand All @@ -668,8 +670,28 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*/
void removeExpressionField( int index );

/**
* Returns the expressoin used for a given expression field
*
* @param index An index of an epxression based (virtual) field
*
* @return The expression for the field at index
*
* @note added in 2.9
*/
const QString expressionField( int index );

/**
* Changes the expression used to define an expression based (virtual) field
*
* @param index The index of the expression to change
*
* @param exp The new expression to set
*
* @note added in 2.9
*/
void updateExpressionField( int index, const QString& exp );

/** Get the label object associated with this layer */
QgsLabel *label();

Expand Down

0 comments on commit c44285b

Please sign in to comment.