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 (#35059)

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 af99e24 commit f039403
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
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -72,22 +72,24 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl, int fieldIdx
} }


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


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


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


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

QString expression = mExpressionWidget->expression(); QString expression = mExpressionWidget->expression();
if ( expression.isEmpty() ) if ( expression.isEmpty() )
{ {
Expand Down Expand Up @@ -392,6 +396,11 @@ void QgsAttributeTypeDialog::defaultExpressionChanged()
return; 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() ); QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( editorWidgetType() );


QString previewText = fieldFormatter->representValue( mLayer, mFieldIdx, editorWidgetConfig(), QVariant(), val ); QString previewText = fieldFormatter->representValue( mLayer, mFieldIdx, editorWidgetConfig(), QVariant(), val );
Expand Down
55 changes: 43 additions & 12 deletions src/ui/attributeformconfig/qgsattributetypeedit.ui
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -184,36 +184,65 @@
<property name="title"> <property name="title">
<string>Defaults</string> <string>Defaults</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout_2" columnstretch="0,1"> <layout class="QGridLayout" name="gridLayout_2" columnstretch="0,0,0">
<item row="1" column="0"> <item row="4" column="0">
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="label_3">
<property name="text"> <property name="text">
<string>Default value</string> <string>Default value</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="6" column="0">
<widget class="QLabel" name="mDefaultPreviewLabel"> <widget class="QLabel" name="label_4">
<property name="text"> <property name="text">
<string/> <string>Preview</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="4" column="1">
<widget class="QgsExpressionLineEdit" name="mExpressionWidget" native="true"> <widget class="QgsExpressionLineEdit" name="mExpressionWidget" native="true">
<property name="focusPolicy"> <property name="focusPolicy">
<enum>Qt::StrongFocus</enum> <enum>Qt::StrongFocus</enum>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="0"> <item row="6" column="1">
<widget class="QLabel" name="label_4"> <widget class="QLabel" name="mDefaultPreviewLabel">
<property name="text"> <property name="text">
<string>Preview</string> <string/>
</property> </property>
</widget> </widget>
</item> </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"> <widget class="QCheckBox" name="mApplyDefaultValueOnUpdateCheckBox">
<property name="toolTip"> <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> <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>leConstraintExpressionDescription</tabstop>
<tabstop>mCheckBoxEnforceExpression</tabstop> <tabstop>mCheckBoxEnforceExpression</tabstop>
<tabstop>mExpressionWidget</tabstop> <tabstop>mExpressionWidget</tabstop>
<tabstop>mApplyDefaultValueOnUpdateCheckBox</tabstop>
</tabstops> </tabstops>
<resources/> <resources>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
</resources>
<connections/> <connections/>
</ui> </ui>

0 comments on commit f039403

Please sign in to comment.