Skip to content

Commit 7631ddd

Browse files
vsklencarwonder-sk
authored andcommitted
[QgsQuick] Value Relation widget
Added a widget for value relation field time of a feature form.
1 parent 1450985 commit 7631ddd

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

src/quickgui/plugin/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ SET(QGIS_QUICK_PLUGIN_RESOURCES
1515
editor/qgsquickexternalresource.qml
1616
editor/qgsquicktextedit.qml
1717
editor/qgsquickvaluemap.qml
18+
editor/qgsquickvaluerelation.qml
1819
qgsquickfeatureform.qml
1920
qgsquickfeatureformstyling.qml
2021
qgsquickmapcanvas.qml
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/***************************************************************************
2+
qgsquickvaluerelation.qml
3+
--------------------------------------
4+
Date : 2017
5+
Copyright : (C) 2019 by Viktor Sklencar
6+
Email : viktor.sklencar@lutraconsulting.co.uk
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
import QtQuick 2.7
17+
import QtQuick.Controls 2.2
18+
import QtGraphicalEffects 1.0
19+
import QgsQuick 0.1 as QgsQuick
20+
21+
/**
22+
* Value Relation for QGIS Attribute Form
23+
* Requires various global properties set to function, see qgsquickfeatureform Loader section
24+
* Do not use directly from Application QML
25+
*/
26+
Item {
27+
signal valueChanged(var value, bool isNull)
28+
29+
id: fieldItem
30+
enabled: !readOnly
31+
height: customStyle.height
32+
anchors {
33+
left: parent.left
34+
right: parent.right
35+
rightMargin: 10 * QgsQuick.Utils.dp
36+
}
37+
38+
ComboBox {
39+
id: comboBox
40+
41+
property var reverseConfig: ({})
42+
property var currentValue: value
43+
property var currentMap
44+
property var currentKey
45+
height: parent.height
46+
anchors { left: parent.left; right: parent.right }
47+
currentIndex: find(value)
48+
49+
ListModel {
50+
id: listModel
51+
}
52+
53+
Component.onCompleted: {
54+
currentMap = QgsQuick.Utils.createValueRelationCache(config)
55+
var keys = Object.keys(currentMap)
56+
for(var i=0; i< keys.length; i++)
57+
{
58+
currentKey = keys[i]
59+
var valueText = currentMap[currentKey]
60+
listModel.append( { text: valueText } )
61+
reverseConfig[valueText] = currentKey;
62+
}
63+
model=listModel
64+
textRole = 'text'
65+
currentIndex = find(currentMap[value])
66+
}
67+
68+
onCurrentTextChanged: {
69+
valueChanged(reverseConfig[currentText], false)
70+
}
71+
72+
// Workaround to get a signal when the value has changed
73+
onCurrentValueChanged: {
74+
currentIndex = find(currentMap[value])
75+
}
76+
77+
MouseArea {
78+
anchors.fill: parent
79+
propagateComposedEvents: true
80+
81+
onClicked: mouse.accepted = false
82+
onPressed: { forceActiveFocus(); mouse.accepted = false; }
83+
onReleased: mouse.accepted = false;
84+
onDoubleClicked: mouse.accepted = false;
85+
onPositionChanged: mouse.accepted = false;
86+
onPressAndHold: mouse.accepted = false;
87+
}
88+
89+
// [hidpi fixes]
90+
delegate: ItemDelegate {
91+
width: comboBox.width
92+
height: comboBox.height * 0.8
93+
text: modelData
94+
font.weight: comboBox.currentIndex === index ? Font.DemiBold : Font.Normal
95+
font.pixelSize: customStyle.fontPixelSize
96+
highlighted: comboBox.highlightedIndex == index
97+
leftPadding: 5 * QgsQuick.Utils.dp
98+
}
99+
100+
contentItem: Text {
101+
height: comboBox.height * 0.8
102+
text: comboBox.displayText
103+
font.pixelSize: customStyle.fontPixelSize
104+
horizontalAlignment: Text.AlignLeft
105+
verticalAlignment: Text.AlignVCenter
106+
elide: Text.ElideRight
107+
leftPadding: 5 * QgsQuick.Utils.dp
108+
color: customStyle.fontColor
109+
}
110+
111+
background: Item {
112+
implicitWidth: 120 * QgsQuick.Utils.dp
113+
implicitHeight: comboBox.height * 0.8
114+
115+
Rectangle {
116+
anchors.fill: parent
117+
id: backgroundRect
118+
border.color: comboBox.pressed ? customStyle.activeColor : customStyle.normalColor
119+
border.width: comboBox.visualFocus ? 2 : 1
120+
color: customStyle.backgroundColor
121+
radius: customStyle.cornerRadius
122+
}
123+
}
124+
// [/hidpi fixes]
125+
}
126+
}

src/quickgui/qgsquickutils.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgsvectorlayer.h"
2626
#include "qgsfeature.h"
2727
#include "qgsapplication.h"
28+
#include "qgsvaluerelationfieldformatter.h"
2829

2930
#include "qgsquickfeaturelayerpair.h"
3031
#include "qgsquickmapsettings.h"
@@ -135,6 +136,7 @@ const QUrl QgsQuickUtils::getEditorComponentSource( const QString &widgetName )
135136
QString path( "qgsquick%1.qml" );
136137
QStringList supportedWidgets = { QStringLiteral( "textedit" ),
137138
QStringLiteral( "valuemap" ),
139+
QStringLiteral( "valuerelation" ),
138140
QStringLiteral( "checkbox" ),
139141
QStringLiteral( "externalresource" ),
140142
QStringLiteral( "datetime" )
@@ -329,6 +331,18 @@ QString QgsQuickUtils::dumpScreenInfo() const
329331
return msg;
330332
}
331333

334+
QVariantMap QgsQuickUtils::createValueRelationCache(const QVariantMap &config, const QgsFeature &formFeature)
335+
{
336+
QVariantMap valueMap;
337+
QgsValueRelationFieldFormatter::ValueRelationCache cache = QgsValueRelationFieldFormatter::createCache(config, formFeature);
338+
339+
for ( const QgsValueRelationFieldFormatter::ValueRelationItem &item : qgis::as_const( cache ) )
340+
{
341+
valueMap.insert(item.key.toString(), item.value);
342+
}
343+
return valueMap;
344+
}
345+
332346
qreal QgsQuickUtils::screenDensity() const
333347
{
334348
return mScreenDensity;

src/quickgui/qgsquickutils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ 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+
Q_INVOKABLE static QVariantMap createValueRelationCache( const QVariantMap &config, const QgsFeature &formFeature = QgsFeature() );
233+
232234
private:
233235
static void formatToMetricDistance( double srcDistance,
234236
QgsUnitTypes::DistanceUnit srcUnits,

0 commit comments

Comments
 (0)