Skip to content

Commit

Permalink
fix 'fields and values' not accessible in expression editor in defaul…
Browse files Browse the repository at this point in the history
…t values (#35058)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
qgis-bot and github-actions[bot] committed Mar 13, 2020
1 parent 4f87d7a commit 14032d3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
17 changes: 13 additions & 4 deletions src/app/attributeformconfig/qgsattributetypedialog.cpp
Expand Up @@ -72,22 +72,24 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl, int fieldIdx
}

mExpressionWidget->registerExpressionContextGenerator( this );
mExpressionWidget->setLayer( mLayer );

connect( mExpressionWidget, &QgsExpressionLineEdit::expressionChanged, this, &QgsAttributeTypeDialog::defaultExpressionChanged );
connect( mUniqueCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
{
mCheckBoxEnforceUnique->setEnabled( checked );
if ( !checked )
mCheckBoxEnforceUnique->setChecked( false );
}
);
} );
connect( notNullCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
{
mCheckBoxEnforceNotNull->setEnabled( checked );
if ( !checked )
mCheckBoxEnforceNotNull->setChecked( false );
}
);
} );

mWarnDefaultValueHasFieldsWidget->setVisible( false );
connect( mApplyDefaultValueOnUpdateCheckBox, &QCheckBox::stateChanged, this, &QgsAttributeTypeDialog::defaultExpressionChanged );

constraintExpressionWidget->setAllowEmptyFieldName( true );
constraintExpressionWidget->setLayer( vl );
Expand Down Expand Up @@ -358,6 +360,8 @@ void QgsAttributeTypeDialog::setLabelOnTop( bool onTop )

void QgsAttributeTypeDialog::defaultExpressionChanged()
{
mWarnDefaultValueHasFieldsWidget->hide();

QString expression = mExpressionWidget->expression();
if ( expression.isEmpty() )
{
Expand Down Expand Up @@ -392,6 +396,11 @@ void QgsAttributeTypeDialog::defaultExpressionChanged()
return;
}

// if the expression uses fields and it's not on update,
// there is no warranty that the field will be available
bool expressionHasFields = exp.referencedAttributeIndexes( mLayer->fields() ).count() > 0;
mWarnDefaultValueHasFieldsWidget->setVisible( expressionHasFields && !mApplyDefaultValueOnUpdateCheckBox->isChecked() );

QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( editorWidgetType() );

QString previewText = fieldFormatter->representValue( mLayer, mFieldIdx, editorWidgetConfig(), QVariant(), val );
Expand Down
55 changes: 43 additions & 12 deletions src/ui/attributeformconfig/qgsattributetypeedit.ui
Expand Up @@ -184,36 +184,65 @@
<property name="title">
<string>Defaults</string>
</property>
<layout class="QGridLayout" name="gridLayout_2" columnstretch="0,1">
<item row="1" column="0">
<layout class="QGridLayout" name="gridLayout_2" columnstretch="0,0,0">
<item row="4" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Default value</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="mDefaultPreviewLabel">
<item row="6" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string/>
<string>Preview</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="4" column="1">
<widget class="QgsExpressionLineEdit" name="mExpressionWidget" native="true">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<item row="6" column="1">
<widget class="QLabel" name="mDefaultPreviewLabel">
<property name="text">
<string>Preview</string>
<string/>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<item row="7" column="0" colspan="2">
<widget class="QWidget" name="mWarnDefaultValueHasFieldsWidget" native="true">
<layout class="QHBoxLayout" name="mWarnDefaultValueHasFields">
<item>
<widget class="QLabel" name="label_8">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../../images/images.qrc">:/images/themes/default/mIconWarning.svg</pixmap>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>Using fields in a default value expression only works if "Apply default value on update" is checked.</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="8" column="0" colspan="2">
<widget class="QCheckBox" name="mApplyDefaultValueOnUpdateCheckBox">
<property name="toolTip">
<string>&lt;p&gt;With this option checked, the default value will not only be used when the feature is first created, but also whenever a feature's attribute or geometry is changed.&lt;/p&gt;&lt;p&gt;This is often useful for a last_modified timestamp or to record the username that last modified the feature.&lt;/p&gt;</string>
Expand Down Expand Up @@ -274,8 +303,10 @@
<tabstop>leConstraintExpressionDescription</tabstop>
<tabstop>mCheckBoxEnforceExpression</tabstop>
<tabstop>mExpressionWidget</tabstop>
<tabstop>mApplyDefaultValueOnUpdateCheckBox</tabstop>
</tabstops>
<resources/>
<resources>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit 14032d3

Please sign in to comment.