|
| 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 | +} |
0 commit comments