Skip to content

Commit

Permalink
[QgsQuick] Updated value relation widget
Browse files Browse the repository at this point in the history
Renamed and simplified shared ComboBox component - now its just styled component without functionality. All has to be defined when creating it.

Added docs to QgsQuickUtils::createValueRelationCache
  • Loading branch information
vsklencar authored and wonder-sk committed Apr 10, 2019
1 parent e06652e commit 33139f3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/quickgui/plugin/CMakeLists.txt
Expand Up @@ -10,7 +10,7 @@ SET(QGIS_QUICK_PLUGIN_SRC
)

SET(QGIS_QUICK_PLUGIN_RESOURCES
editor/qgsquickcombobox.qml
editor/qgsquickeditorwidgetcombobox.qml
editor/qgsquickcheckbox.qml
editor/qgsquickdatetime.qml
editor/qgsquickexternalresource.qml
Expand Down
Expand Up @@ -20,19 +20,13 @@ import QgsQuick 0.1 as QgsQuick

/**
* ComboBox for QGIS Attribute Form - used by QgsQuickValueMap and QgsQuickValueRelation
* Requires various global properties set to function, see qgsquickfeatureform Loader section
* Do not use directly from Application QML
*/
ComboBox {
id: comboBox

property var reverseConfig: ({})
property var currentValue
property var currentMap
property var currentKey
property var comboStyle
anchors { left: parent.left; right: parent.right }
currentIndex: find(reverseConfig[currentValue])

MouseArea {
anchors.fill: parent
Expand Down
18 changes: 10 additions & 8 deletions src/quickgui/plugin/editor/qgsquickvaluemap.qml
Expand Up @@ -35,8 +35,11 @@ Item {
rightMargin: 10 * QgsQuick.Utils.dp
}

QgsQuick.ComboBox {
id: comboBox
QgsQuick.EditorWidgetComboBox {
// Reversed to model's key-value map. It is used to find index according current value
property var reverseConfig: ({})
property var currentValue: value

comboStyle: customStyle
textRole: 'text'
height: parent.height
Expand All @@ -52,29 +55,28 @@ Item {
//it's a list (>=QGIS3.0)
for(var i=0; i<config['map'].length; i++)
{
currentMap = config['map'][i]
currentKey = Object.keys(currentMap)[0]
var currentMap = config['map'][i]
var currentKey = Object.keys(currentMap)[0]
listModel.append( { text: currentKey } )
reverseConfig[currentMap[currentKey]] = currentKey;
}
}
else
{
//it's a map (<=QGIS2.18)
currentMap= config['map'].length ? config['map'][currentIndex] : config['map']
currentKey = Object.keys(currentMap)[0]
var currentMap = config['map'].length ? config['map'][currentIndex] : config['map']
var currentKey = Object.keys(currentMap)[0]
for(var key in config['map']) {
listModel.append( { text: key } )
reverseConfig[config['map'][key]] = key;
}
}
}

currentIndex = find(reverseConfig[value])
}

onCurrentTextChanged: {
currentMap= config['map'].length ? config['map'][currentIndex] : config['map']
var currentMap = config['map'].length ? config['map'][currentIndex] : config['map']
valueChanged(currentMap[currentText], false)
}

Expand Down
20 changes: 13 additions & 7 deletions src/quickgui/plugin/editor/qgsquickvaluerelation.qml
Expand Up @@ -35,30 +35,36 @@ Item {
rightMargin: 10 * QgsQuick.Utils.dp
}

QgsQuick.ComboBox {
QgsQuick.EditorWidgetComboBox {
// Value relation cache map
property var currentMap
// Reversed to currentMap. It is used to find key (currentValue) according value (currentText)
property var reversedMap: ({})
property var currentValue: value

comboStyle: customStyle
textRole: 'text'
height: parent.height
model: ListModel {
id: listModel
}
textRole: 'text'
height: parent.height

Component.onCompleted: {
currentMap = QgsQuick.Utils.createValueRelationCache(config)
var keys = Object.keys(currentMap)
for(var i=0; i< keys.length; i++)
{
currentKey = keys[i]
var currentKey = keys[i]
var valueText = currentMap[currentKey]
listModel.append( { text: valueText } )
reverseConfig[valueText] = currentKey;
reversedMap[valueText] = currentKey;
}
model=listModel
model = listModel
currentIndex = find(currentMap[value])
}

onCurrentTextChanged: {
valueChanged(reverseConfig[currentText], false)
valueChanged(reversedMap[currentText], false)
}

// Workaround to get a signal when the value has changed
Expand Down
2 changes: 1 addition & 1 deletion src/quickgui/plugin/qgsquick.qrc
@@ -1,7 +1,7 @@
<RCC>
<qresource prefix="/QgsQuick">
<file>qmldir</file>
<file>editor/qgsquickcombobox.qml</file>
<file>editor/qgsquickeditorwidgetcombobox.qml</file>
<file>editor/qgsquickcheckbox.qml</file>
<file>editor/qgsquickdatetime.qml</file>
<file>editor/qgsquickexternalresource.qml</file>
Expand Down
4 changes: 3 additions & 1 deletion src/quickgui/plugin/qmldir
Expand Up @@ -13,7 +13,9 @@
module QgsQuick
plugin qgis_quick_plugin

ComboBox 0.1 qgsquickcombobox.qml
# suppose to be used only internaly in QgsQuick plugin
EditorWidgetComboBox 0.1 qgsquickeditorwidgetcombobox.qml

MapCanvas 0.1 qgsquickmapcanvas.qml
FeatureForm 0.1 qgsquickfeatureform.qml
FeatureFormStyling 0.1 qgsquickfeatureformstyling.qml
Expand Down
10 changes: 10 additions & 0 deletions src/quickgui/qgsquickutils.h
Expand Up @@ -229,6 +229,16 @@ class QUICK_EXPORT QgsQuickUtils: public QObject
//! Returns a string with information about screen size and resolution - useful for debugging
QString dumpScreenInfo() const;

/**
* Creates a cache for a value relation field.
* This can be used to keep the value map in the local memory
* if doing multiple lookups in a loop.
* \param config The widget configuration
* \param formFeature The feature currently being edited with current attribute values
* \return A kvp list of values for the widget
*
* \since QGIS 3.6
*/
Q_INVOKABLE static QVariantMap createValueRelationCache( const QVariantMap &config, const QgsFeature &formFeature = QgsFeature() );

private:
Expand Down

0 comments on commit 33139f3

Please sign in to comment.