Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix showing correct values
  • Loading branch information
tomasMizera authored and PeterPetrik committed Nov 17, 2020
1 parent 00c6ce2 commit 8f617e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
38 changes: 19 additions & 19 deletions src/quickgui/plugin/editor/qgsquickvaluerelation.qml
Expand Up @@ -31,15 +31,10 @@ Item {
property var widgetValue: value
property var currentFeatureLayerPair: featurePair
property string widgetType: ""
property bool isReadOnly: readOnly

property var model: QgsQuick.FeaturesListModel {
id: vrModel

// recalculate index when model changes
onModelReset: {
combobox.currentIndex = vrModel.rowFromAttribute( QgsQuick.FeaturesListModel.KeyColumn, value )
updateField()
}
}

id: fieldItem
Expand All @@ -52,9 +47,11 @@ Item {
rightMargin: 10 * QgsQuick.Utils.dp
}

Component.onCompleted: {
onCurrentFeatureLayerPairChanged: {
vrModel.setupValueRelation( config )
widgetType = customWidget.getTypeOfWidget( fieldItem, vrModel )
vrModel.currentFeature = currentFeatureLayerPair.feature
if ( widgetType === "" )
widgetType = customWidget.getTypeOfWidget( fieldItem, vrModel )
updateField()
}

Expand Down Expand Up @@ -89,8 +86,11 @@ Item {

// Called when data in different fields are changed.
function dataUpdated( feature ) {
vrModel.currentFeature = feature
combobox.popup.close()
if ( !isReadOnly ) {
vrModel.currentFeature = feature
updateField()
combobox.popup.close()
}
}

/**
Expand All @@ -111,37 +111,37 @@ Item {
* if value to be set is undefined, -1, empty string or similiar, it also resets current value
*/
function updateField() {

if ( vrModel.currentFeature !== currentFeatureLayerPair.feature )
vrModel.currentFeature = currentFeatureLayerPair.feature

if ( widgetValue == null || widgetValue === "" ) {
if ( widgetValue == null || widgetValue === "" || widgetValue === -1 ) {
textField.clear()
combobox.currentIndex = -1
return
}
let reset = false

if ( widgetType === "textfield" ) {
if ( allowMultipleValues && widgetValue.startsWith('{') )
{
let strings = vrModel.convertMultivalueFormat( widgetValue )
textField.text = strings.join(", ")
if ( !strings || strings.length === 0 )
setValue( "", true )
reset = true
}
else {
let text = vrModel.attributeFromValue( QgsQuick.FeaturesListModel.KeyColumn, widgetValue, QgsQuick.FeaturesListModel.FeatureTitle )
textField.text = text || "" // if text is undefined, clear text
textField.text = text || ""
if ( !text )
setValue( -1, true)
reset = true
}
}
else if ( widgetType === "combobox" ) {
let index = vrModel.rowFromAttribute( QgsQuick.FeaturesListModel.KeyColumn, widgetValue )
combobox.currentIndex = index
if ( index < 0 )
setValue( -1, true )
reset = true
}

if ( reset && !isReadOnly )
setValue( -1, true )
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/quickgui/plugin/qgsquickfeatureform.qml
Expand Up @@ -94,11 +94,18 @@ Item {
*/
property var valueRelationOpened: function valueRelationOpened( widget, valueRelationModel ) {}

/**
* Called when field for value relation is created, by default it returns value "combobox".
* Return value of this function sets corresponding widget type. Currently accepted values are:
* - combobox -> QML combobox component.
* - textfield -> custom text widget that shows only title of selected feature in value relation
* and calls function "valueRelationOpened" when it is clicked.
* \param widget valuerelation widget for specific field to send valueChanged signal.
* \param valueRelationModel model of type FeaturesListModel bears features of related layer.
*/
property var getTypeOfWidget: function getTypeOfWidget( widget, valueRelationModel ) {
return "combobox"
}

property int valueRelationLimit: 4
}

/**
Expand Down

0 comments on commit 8f617e4

Please sign in to comment.