Skip to content
Permalink
Browse files

Use client side default values when creating new features

Sponsored by DB Fahrwegdienste GmbH
  • Loading branch information
nyalldawson committed Aug 30, 2016
1 parent 546cd67 commit 3a14e77c6530c5d372ab1e80d87ca4ed53bfa275
Showing with 42 additions and 13 deletions.
  1. +17 −2 src/app/qgisapp.cpp
  2. +12 −1 src/app/qgsmergeattributesdialog.cpp
  3. +13 −10 src/core/qgsofflineediting.cpp
@@ -7380,6 +7380,11 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
remap.insert( idx, dst );
}

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( pasteVectorLayer );

int dstAttrCount = pasteVectorLayer->fields().count();

QgsFeatureList::iterator featureIt = features.begin();
@@ -7391,8 +7396,18 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
// pre-initialized with default values
for ( int dst = 0; dst < dstAttr.count(); ++dst )
{
QVariant defVal( pasteVectorLayer->dataProvider()->defaultValue( dst ) );
if ( !defVal.isNull() )
QVariant defVal;
if ( !pasteVectorLayer->defaultValueExpression( dst ).isEmpty() )
{
// client side default expression set - use this in preference to provider default
defVal = pasteVectorLayer->defaultValue( dst, *featureIt, &context );
}
else
{
defVal = pasteVectorLayer->dataProvider()->defaultValue( dst );
}

if ( defVal.isValid() && !defVal.isNull() )
{
dstAttr[ dst ] = defVal;
}
@@ -522,14 +522,21 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
return QgsAttributes();
}

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mVectorLayer );

int widgetIndex = 0;
QgsAttributes results( mFields.count() );
for ( int fieldIdx = 0; fieldIdx < mFields.count(); ++fieldIdx )
{
if ( mHiddenAttributes.contains( fieldIdx ) )
{
//hidden attribute, set to default value
if ( mVectorLayer->dataProvider() )
if ( !mVectorLayer->defaultValueExpression( fieldIdx ).isEmpty() )
results[fieldIdx] = mVectorLayer->defaultValue( fieldIdx, mFeatureList.at( 0 ), &context );
else if ( mVectorLayer->dataProvider() )
results[fieldIdx] = mVectorLayer->dataProvider()->defaultValue( fieldIdx );
else
results[fieldIdx] = QVariant();
@@ -551,6 +558,10 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
{
results[fieldIdx] = currentItem->data( Qt::DisplayRole );
}
else if ( !mVectorLayer->defaultValueExpression( fieldIdx ).isEmpty() )
{
results[fieldIdx] = mVectorLayer->defaultValue( fieldIdx, mFeatureList.at( 0 ), &context );
}
else if ( mVectorLayer->dataProvider() )
{
results[fieldIdx] = mVectorLayer->dataProvider()->defaultValue( fieldIdx );
@@ -746,14 +746,12 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVec
QString sql = QString( "SELECT \"fid\" FROM 'log_added_features' WHERE \"layer_id\" = %1" ).arg( layerId );
QList<int> newFeatureIds = sqlQueryInts( db, sql );

// get default value for each field
const QgsFields& remoteFlds = remoteLayer->fields();
QVector<QVariant> defaultValues( remoteFlds.count() );
for ( int i = 0; i < remoteFlds.count(); ++i )
{
if ( remoteFlds.fieldOrigin( i ) == QgsFields::OriginProvider )
defaultValues[i] = remoteLayer->dataProvider()->defaultValue( remoteFlds.fieldOriginIndex( i ) );
}
QgsFields remoteFlds = remoteLayer->fields();

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( remoteLayer );

// get new features from offline layer
QgsFeatureList features;
@@ -789,8 +787,13 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVec
// (important especially e.g. for postgis primary key generated from a sequence)
for ( int k = 0; k < newAttrs.count(); ++k )
{
if ( newAttrs.at( k ).isNull() && !defaultValues.at( k ).isNull() )
newAttrs[k] = defaultValues.at( k );
if ( !newAttrs.at( k ).isNull() )
continue;

if ( !remoteLayer->defaultValueExpression( k ).isEmpty() )
newAttrs[k] = remoteLayer->defaultValue( k, f, &context );
else if ( remoteFlds.fieldOrigin( k ) == QgsFields::OriginProvider )
newAttrs[k] = remoteLayer->dataProvider()->defaultValue( remoteFlds.fieldOriginIndex( k ) );
}

f.setAttributes( newAttrs );

0 comments on commit 3a14e77

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