Skip to content
Permalink
Browse files

Make virtual fields updatable

Fix #11547
Fix #10994
  • Loading branch information
m-kuhn committed Mar 24, 2015
1 parent 7905f7f commit c44285ba04a79be9cee75194e20297a7c53710aa
@@ -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
@@ -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();

@@ -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"
@@ -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 ) ) );
@@ -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 )
@@ -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();
@@ -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" );
@@ -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
*/
@@ -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 )
@@ -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 )
@@ -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
@@ -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();

0 comments on commit c44285b

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