Skip to content

Commit 8ae7e3f

Browse files
committed
Use client side default values when creating new features
Sponsored by DB Fahrwegdienste GmbH
1 parent 66cb422 commit 8ae7e3f

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

src/app/qgisapp.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -7345,6 +7345,11 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
73457345
remap.insert( idx, dst );
73467346
}
73477347

7348+
QgsExpressionContext context;
7349+
context << QgsExpressionContextUtils::globalScope()
7350+
<< QgsExpressionContextUtils::projectScope()
7351+
<< QgsExpressionContextUtils::layerScope( pasteVectorLayer );
7352+
73487353
int dstAttrCount = pasteVectorLayer->fields().count();
73497354

73507355
QgsFeatureList::iterator featureIt = features.begin();
@@ -7356,8 +7361,18 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
73567361
// pre-initialized with default values
73577362
for ( int dst = 0; dst < dstAttr.count(); ++dst )
73587363
{
7359-
QVariant defVal( pasteVectorLayer->dataProvider()->defaultValue( dst ) );
7360-
if ( !defVal.isNull() )
7364+
QVariant defVal;
7365+
if ( !pasteVectorLayer->defaultValueExpression( dst ).isEmpty() )
7366+
{
7367+
// client side default expression set - use this in preference to provider default
7368+
defVal = pasteVectorLayer->defaultValue( dst, *featureIt, &context );
7369+
}
7370+
else
7371+
{
7372+
defVal = pasteVectorLayer->dataProvider()->defaultValue( dst );
7373+
}
7374+
7375+
if ( defVal.isValid() && !defVal.isNull() )
73617376
{
73627377
dstAttr[ dst ] = defVal;
73637378
}

src/app/qgsmergeattributesdialog.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -523,14 +523,21 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
523523
return QgsAttributes();
524524
}
525525

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

src/core/qgsofflineediting.cpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -735,14 +735,12 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVec
735735
QString sql = QString( "SELECT \"fid\" FROM 'log_added_features' WHERE \"layer_id\" = %1" ).arg( layerId );
736736
QList<int> newFeatureIds = sqlQueryInts( db, sql );
737737

738-
// get default value for each field
739-
const QgsFields& remoteFlds = remoteLayer->fields();
740-
QVector<QVariant> defaultValues( remoteFlds.count() );
741-
for ( int i = 0; i < remoteFlds.count(); ++i )
742-
{
743-
if ( remoteFlds.fieldOrigin( i ) == QgsFields::OriginProvider )
744-
defaultValues[i] = remoteLayer->dataProvider()->defaultValue( remoteFlds.fieldOriginIndex( i ) );
745-
}
738+
QgsFields remoteFlds = remoteLayer->fields();
739+
740+
QgsExpressionContext context;
741+
context << QgsExpressionContextUtils::globalScope()
742+
<< QgsExpressionContextUtils::projectScope()
743+
<< QgsExpressionContextUtils::layerScope( remoteLayer );
746744

747745
// get new features from offline layer
748746
QgsFeatureList features;
@@ -778,8 +776,13 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVec
778776
// (important especially e.g. for postgis primary key generated from a sequence)
779777
for ( int k = 0; k < newAttrs.count(); ++k )
780778
{
781-
if ( newAttrs.at( k ).isNull() && !defaultValues.at( k ).isNull() )
782-
newAttrs[k] = defaultValues.at( k );
779+
if ( !newAttrs.at( k ).isNull() )
780+
continue;
781+
782+
if ( !remoteLayer->defaultValueExpression( k ).isEmpty() )
783+
newAttrs[k] = remoteLayer->defaultValue( k, f, &context );
784+
else if ( remoteFlds.fieldOrigin( k ) == QgsFields::OriginProvider )
785+
newAttrs[k] = remoteLayer->dataProvider()->defaultValue( remoteFlds.fieldOriginIndex( k ) );
783786
}
784787

785788
f.setAttributes( newAttrs );

0 commit comments

Comments
 (0)