Skip to content

Commit 3a14e77

Browse files
committedAug 30, 2016
Use client side default values when creating new features
Sponsored by DB Fahrwegdienste GmbH
1 parent 546cd67 commit 3a14e77

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7380,6 +7380,11 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
73807380
remap.insert( idx, dst );
73817381
}
73827382

7383+
QgsExpressionContext context;
7384+
context << QgsExpressionContextUtils::globalScope()
7385+
<< QgsExpressionContextUtils::projectScope()
7386+
<< QgsExpressionContextUtils::layerScope( pasteVectorLayer );
7387+
73837388
int dstAttrCount = pasteVectorLayer->fields().count();
73847389

73857390
QgsFeatureList::iterator featureIt = features.begin();
@@ -7391,8 +7396,18 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
73917396
// pre-initialized with default values
73927397
for ( int dst = 0; dst < dstAttr.count(); ++dst )
73937398
{
7394-
QVariant defVal( pasteVectorLayer->dataProvider()->defaultValue( dst ) );
7395-
if ( !defVal.isNull() )
7399+
QVariant defVal;
7400+
if ( !pasteVectorLayer->defaultValueExpression( dst ).isEmpty() )
7401+
{
7402+
// client side default expression set - use this in preference to provider default
7403+
defVal = pasteVectorLayer->defaultValue( dst, *featureIt, &context );
7404+
}
7405+
else
7406+
{
7407+
defVal = pasteVectorLayer->dataProvider()->defaultValue( dst );
7408+
}
7409+
7410+
if ( defVal.isValid() && !defVal.isNull() )
73967411
{
73977412
dstAttr[ dst ] = defVal;
73987413
}

‎src/app/qgsmergeattributesdialog.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,21 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
522522
return QgsAttributes();
523523
}
524524

525+
QgsExpressionContext context;
526+
context << QgsExpressionContextUtils::globalScope()
527+
<< QgsExpressionContextUtils::projectScope()
528+
<< QgsExpressionContextUtils::layerScope( mVectorLayer );
529+
525530
int widgetIndex = 0;
526531
QgsAttributes results( mFields.count() );
527532
for ( int fieldIdx = 0; fieldIdx < mFields.count(); ++fieldIdx )
528533
{
529534
if ( mHiddenAttributes.contains( fieldIdx ) )
530535
{
531536
//hidden attribute, set to default value
532-
if ( mVectorLayer->dataProvider() )
537+
if ( !mVectorLayer->defaultValueExpression( fieldIdx ).isEmpty() )
538+
results[fieldIdx] = mVectorLayer->defaultValue( fieldIdx, mFeatureList.at( 0 ), &context );
539+
else if ( mVectorLayer->dataProvider() )
533540
results[fieldIdx] = mVectorLayer->dataProvider()->defaultValue( fieldIdx );
534541
else
535542
results[fieldIdx] = QVariant();
@@ -551,6 +558,10 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
551558
{
552559
results[fieldIdx] = currentItem->data( Qt::DisplayRole );
553560
}
561+
else if ( !mVectorLayer->defaultValueExpression( fieldIdx ).isEmpty() )
562+
{
563+
results[fieldIdx] = mVectorLayer->defaultValue( fieldIdx, mFeatureList.at( 0 ), &context );
564+
}
554565
else if ( mVectorLayer->dataProvider() )
555566
{
556567
results[fieldIdx] = mVectorLayer->dataProvider()->defaultValue( fieldIdx );

‎src/core/qgsofflineediting.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -746,14 +746,12 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVec
746746
QString sql = QString( "SELECT \"fid\" FROM 'log_added_features' WHERE \"layer_id\" = %1" ).arg( layerId );
747747
QList<int> newFeatureIds = sqlQueryInts( db, sql );
748748

749-
// get default value for each field
750-
const QgsFields& remoteFlds = remoteLayer->fields();
751-
QVector<QVariant> defaultValues( remoteFlds.count() );
752-
for ( int i = 0; i < remoteFlds.count(); ++i )
753-
{
754-
if ( remoteFlds.fieldOrigin( i ) == QgsFields::OriginProvider )
755-
defaultValues[i] = remoteLayer->dataProvider()->defaultValue( remoteFlds.fieldOriginIndex( i ) );
756-
}
749+
QgsFields remoteFlds = remoteLayer->fields();
750+
751+
QgsExpressionContext context;
752+
context << QgsExpressionContextUtils::globalScope()
753+
<< QgsExpressionContextUtils::projectScope()
754+
<< QgsExpressionContextUtils::layerScope( remoteLayer );
757755

758756
// get new features from offline layer
759757
QgsFeatureList features;
@@ -789,8 +787,13 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVec
789787
// (important especially e.g. for postgis primary key generated from a sequence)
790788
for ( int k = 0; k < newAttrs.count(); ++k )
791789
{
792-
if ( newAttrs.at( k ).isNull() && !defaultValues.at( k ).isNull() )
793-
newAttrs[k] = defaultValues.at( k );
790+
if ( !newAttrs.at( k ).isNull() )
791+
continue;
792+
793+
if ( !remoteLayer->defaultValueExpression( k ).isEmpty() )
794+
newAttrs[k] = remoteLayer->defaultValue( k, f, &context );
795+
else if ( remoteFlds.fieldOrigin( k ) == QgsFields::OriginProvider )
796+
newAttrs[k] = remoteLayer->dataProvider()->defaultValue( remoteFlds.fieldOriginIndex( k ) );
794797
}
795798

796799
f.setAttributes( newAttrs );

0 commit comments

Comments
 (0)
Please sign in to comment.