Skip to content

Commit 33139f3

Browse files
vsklencarwonder-sk
authored andcommittedApr 10, 2019
[QgsQuick] Updated value relation widget
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
1 parent e06652e commit 33139f3

File tree

7 files changed

+38
-24
lines changed

7 files changed

+38
-24
lines changed
 

‎src/quickgui/plugin/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SET(QGIS_QUICK_PLUGIN_SRC
1010
)
1111

1212
SET(QGIS_QUICK_PLUGIN_RESOURCES
13-
editor/qgsquickcombobox.qml
13+
editor/qgsquickeditorwidgetcombobox.qml
1414
editor/qgsquickcheckbox.qml
1515
editor/qgsquickdatetime.qml
1616
editor/qgsquickexternalresource.qml

‎src/quickgui/plugin/editor/qgsquickcombobox.qml renamed to ‎src/quickgui/plugin/editor/qgsquickeditorwidgetcombobox.qml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,13 @@ import QgsQuick 0.1 as QgsQuick
2020

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

29-
property var reverseConfig: ({})
30-
property var currentValue
31-
property var currentMap
32-
property var currentKey
3328
property var comboStyle
3429
anchors { left: parent.left; right: parent.right }
35-
currentIndex: find(reverseConfig[currentValue])
3630

3731
MouseArea {
3832
anchors.fill: parent

‎src/quickgui/plugin/editor/qgsquickvaluemap.qml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ Item {
3535
rightMargin: 10 * QgsQuick.Utils.dp
3636
}
3737

38-
QgsQuick.ComboBox {
39-
id: comboBox
38+
QgsQuick.EditorWidgetComboBox {
39+
// Reversed to model's key-value map. It is used to find index according current value
40+
property var reverseConfig: ({})
41+
property var currentValue: value
42+
4043
comboStyle: customStyle
4144
textRole: 'text'
4245
height: parent.height
@@ -52,29 +55,28 @@ Item {
5255
//it's a list (>=QGIS3.0)
5356
for(var i=0; i<config['map'].length; i++)
5457
{
55-
currentMap = config['map'][i]
56-
currentKey = Object.keys(currentMap)[0]
58+
var currentMap = config['map'][i]
59+
var currentKey = Object.keys(currentMap)[0]
5760
listModel.append( { text: currentKey } )
5861
reverseConfig[currentMap[currentKey]] = currentKey;
5962
}
6063
}
6164
else
6265
{
6366
//it's a map (<=QGIS2.18)
64-
currentMap= config['map'].length ? config['map'][currentIndex] : config['map']
65-
currentKey = Object.keys(currentMap)[0]
67+
var currentMap = config['map'].length ? config['map'][currentIndex] : config['map']
68+
var currentKey = Object.keys(currentMap)[0]
6669
for(var key in config['map']) {
6770
listModel.append( { text: key } )
6871
reverseConfig[config['map'][key]] = key;
6972
}
7073
}
7174
}
72-
7375
currentIndex = find(reverseConfig[value])
7476
}
7577

7678
onCurrentTextChanged: {
77-
currentMap= config['map'].length ? config['map'][currentIndex] : config['map']
79+
var currentMap = config['map'].length ? config['map'][currentIndex] : config['map']
7880
valueChanged(currentMap[currentText], false)
7981
}
8082

‎src/quickgui/plugin/editor/qgsquickvaluerelation.qml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,36 @@ Item {
3535
rightMargin: 10 * QgsQuick.Utils.dp
3636
}
3737

38-
QgsQuick.ComboBox {
38+
QgsQuick.EditorWidgetComboBox {
39+
// Value relation cache map
40+
property var currentMap
41+
// Reversed to currentMap. It is used to find key (currentValue) according value (currentText)
42+
property var reversedMap: ({})
43+
property var currentValue: value
44+
3945
comboStyle: customStyle
46+
textRole: 'text'
47+
height: parent.height
4048
model: ListModel {
4149
id: listModel
4250
}
43-
textRole: 'text'
44-
height: parent.height
4551

4652
Component.onCompleted: {
4753
currentMap = QgsQuick.Utils.createValueRelationCache(config)
4854
var keys = Object.keys(currentMap)
4955
for(var i=0; i< keys.length; i++)
5056
{
51-
currentKey = keys[i]
57+
var currentKey = keys[i]
5258
var valueText = currentMap[currentKey]
5359
listModel.append( { text: valueText } )
54-
reverseConfig[valueText] = currentKey;
60+
reversedMap[valueText] = currentKey;
5561
}
56-
model=listModel
62+
model = listModel
5763
currentIndex = find(currentMap[value])
5864
}
5965

6066
onCurrentTextChanged: {
61-
valueChanged(reverseConfig[currentText], false)
67+
valueChanged(reversedMap[currentText], false)
6268
}
6369

6470
// Workaround to get a signal when the value has changed

‎src/quickgui/plugin/qgsquick.qrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<RCC>
22
<qresource prefix="/QgsQuick">
33
<file>qmldir</file>
4-
<file>editor/qgsquickcombobox.qml</file>
4+
<file>editor/qgsquickeditorwidgetcombobox.qml</file>
55
<file>editor/qgsquickcheckbox.qml</file>
66
<file>editor/qgsquickdatetime.qml</file>
77
<file>editor/qgsquickexternalresource.qml</file>

‎src/quickgui/plugin/qmldir

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
module QgsQuick
1414
plugin qgis_quick_plugin
1515

16-
ComboBox 0.1 qgsquickcombobox.qml
16+
# suppose to be used only internaly in QgsQuick plugin
17+
EditorWidgetComboBox 0.1 qgsquickeditorwidgetcombobox.qml
18+
1719
MapCanvas 0.1 qgsquickmapcanvas.qml
1820
FeatureForm 0.1 qgsquickfeatureform.qml
1921
FeatureFormStyling 0.1 qgsquickfeatureformstyling.qml

‎src/quickgui/qgsquickutils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ class QUICK_EXPORT QgsQuickUtils: public QObject
229229
//! Returns a string with information about screen size and resolution - useful for debugging
230230
QString dumpScreenInfo() const;
231231

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

234244
private:

0 commit comments

Comments
 (0)
Please sign in to comment.