Skip to content
Permalink
Browse files

[QgsQuick] Refactored QgsQuickValueMap and QgsQuickValueRelation widg…

…ets. Shared code extracted to a separate file.
  • Loading branch information
vsklencar authored and wonder-sk committed Apr 9, 2019
1 parent 3c7a160 commit e06652e23624561ff169583e079484b21a3b34f9
@@ -10,6 +10,7 @@ SET(QGIS_QUICK_PLUGIN_SRC
)

SET(QGIS_QUICK_PLUGIN_RESOURCES
editor/qgsquickcombobox.qml
editor/qgsquickcheckbox.qml
editor/qgsquickdatetime.qml
editor/qgsquickexternalresource.qml
@@ -0,0 +1,85 @@
/***************************************************************************
qgsquickcombobox.qml
--------------------------------------
Date : 2017
Copyright : (C) 2017 by Matthias Kuhn
Email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

import QtQuick 2.7
import QtQuick.Controls 2.2
import QtGraphicalEffects 1.0
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
propagateComposedEvents: true

onClicked: mouse.accepted = false
onPressed: { forceActiveFocus(); mouse.accepted = false; }
onReleased: mouse.accepted = false;
onDoubleClicked: mouse.accepted = false;
onPositionChanged: mouse.accepted = false;
onPressAndHold: mouse.accepted = false;
}

// [hidpi fixes]
delegate: ItemDelegate {
width: comboBox.width
height: comboBox.height * 0.8
text: modelData
font.weight: comboBox.currentIndex === index ? Font.DemiBold : Font.Normal
font.pixelSize: comboStyle.fontPixelSize
highlighted: comboBox.highlightedIndex == index
leftPadding: 5 * QgsQuick.Utils.dp
}

contentItem: Text {
height: comboBox.height * 0.8
text: comboBox.displayText
font.pixelSize: comboStyle.fontPixelSize
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
leftPadding: 5 * QgsQuick.Utils.dp
color: comboStyle.fontColor
}

background: Item {
implicitWidth: 120 * QgsQuick.Utils.dp
implicitHeight: comboBox.height * 0.8

Rectangle {
anchors.fill: parent
id: backgroundRect
border.color: comboBox.pressed ? comboStyle.activeColor : comboStyle.normalColor
border.width: comboBox.visualFocus ? 2 : 1
color: comboStyle.backgroundColor
radius: comboStyle.cornerRadius
}
}
// [/hidpi fixes]
}
@@ -35,19 +35,13 @@ Item {
rightMargin: 10 * QgsQuick.Utils.dp
}

ComboBox {
QgsQuick.ComboBox {
id: comboBox

property var reverseConfig: ({})
property var currentValue: value
property var currentMap
property var currentKey
comboStyle: customStyle
textRole: 'text'
height: parent.height
anchors { left: parent.left; right: parent.right }
currentIndex: find(reverseConfig[value])

ListModel {
id: listModel
model: ListModel {
id: listModel
}

Component.onCompleted: {
@@ -63,14 +57,14 @@ Item {
listModel.append( { text: currentKey } )
reverseConfig[currentMap[currentKey]] = currentKey;
}
model=listModel
textRole = 'text'
}
else
{
//it's a map (<=QGIS2.18)
model = Object.keys(config['map']);
currentMap= config['map'].length ? config['map'][currentIndex] : config['map']
currentKey = Object.keys(currentMap)[0]
for(var key in config['map']) {
listModel.append( { text: key } )
reverseConfig[config['map'][key]] = key;
}
}
@@ -89,53 +83,5 @@ Item {
currentIndex = find(reverseConfig[value])
}

MouseArea {
anchors.fill: parent
propagateComposedEvents: true

onClicked: mouse.accepted = false
onPressed: { forceActiveFocus(); mouse.accepted = false; }
onReleased: mouse.accepted = false;
onDoubleClicked: mouse.accepted = false;
onPositionChanged: mouse.accepted = false;
onPressAndHold: mouse.accepted = false;
}

// [hidpi fixes]
delegate: ItemDelegate {
width: comboBox.width
height: comboBox.height * 0.8
text: config['map'].length ? model.text : modelData
font.weight: comboBox.currentIndex === index ? Font.DemiBold : Font.Normal
font.pixelSize: customStyle.fontPixelSize
highlighted: comboBox.highlightedIndex == index
leftPadding: 5 * QgsQuick.Utils.dp
}

contentItem: Text {
height: comboBox.height * 0.8
text: comboBox.displayText
font.pixelSize: customStyle.fontPixelSize
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
leftPadding: 5 * QgsQuick.Utils.dp
color: customStyle.fontColor
}

background: Item {
implicitWidth: 120 * QgsQuick.Utils.dp
implicitHeight: comboBox.height * 0.8

Rectangle {
anchors.fill: parent
id: backgroundRect
border.color: comboBox.pressed ? customStyle.activeColor : customStyle.normalColor
border.width: comboBox.visualFocus ? 2 : 1
color: customStyle.backgroundColor
radius: customStyle.cornerRadius
}
}
// [/hidpi fixes]
}
}
@@ -1,7 +1,7 @@
/***************************************************************************
qgsquickvaluerelation.qml
--------------------------------------
Date : 2017
Date : 2019
Copyright : (C) 2019 by Viktor Sklencar
Email : viktor.sklencar@lutraconsulting.co.uk
***************************************************************************
@@ -35,34 +35,26 @@ Item {
rightMargin: 10 * QgsQuick.Utils.dp
}

ComboBox {
id: comboBox

property var reverseConfig: ({})
property var currentValue: value
property var currentMap: ({})
property var currentKey
height: parent.height
anchors { left: parent.left; right: parent.right }
currentIndex: find(value)

ListModel {
id: listModel
QgsQuick.ComboBox {
comboStyle: customStyle
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 valueText = currentMap[currentKey]
listModel.append( { text: valueText } )
reverseConfig[valueText] = currentKey;
}
model=listModel
textRole = 'text'
currentIndex = find(currentMap[value])
currentMap = QgsQuick.Utils.createValueRelationCache(config)
var keys = Object.keys(currentMap)
for(var i=0; i< keys.length; i++)
{
currentKey = keys[i]
var valueText = currentMap[currentKey]
listModel.append( { text: valueText } )
reverseConfig[valueText] = currentKey;
}
model=listModel
currentIndex = find(currentMap[value])
}

onCurrentTextChanged: {
@@ -74,53 +66,5 @@ Item {
currentIndex = find(currentMap[value])
}

MouseArea {
anchors.fill: parent
propagateComposedEvents: true

onClicked: mouse.accepted = false
onPressed: { forceActiveFocus(); mouse.accepted = false; }
onReleased: mouse.accepted = false;
onDoubleClicked: mouse.accepted = false;
onPositionChanged: mouse.accepted = false;
onPressAndHold: mouse.accepted = false;
}

// [hidpi fixes]
delegate: ItemDelegate {
width: comboBox.width
height: comboBox.height * 0.8
text: modelData
font.weight: comboBox.currentIndex === index ? Font.DemiBold : Font.Normal
font.pixelSize: customStyle.fontPixelSize
highlighted: comboBox.highlightedIndex == index
leftPadding: 5 * QgsQuick.Utils.dp
}

contentItem: Text {
height: comboBox.height * 0.8
text: comboBox.displayText
font.pixelSize: customStyle.fontPixelSize
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
leftPadding: 5 * QgsQuick.Utils.dp
color: customStyle.fontColor
}

background: Item {
implicitWidth: 120 * QgsQuick.Utils.dp
implicitHeight: comboBox.height * 0.8

Rectangle {
anchors.fill: parent
id: backgroundRect
border.color: comboBox.pressed ? customStyle.activeColor : customStyle.normalColor
border.width: comboBox.visualFocus ? 2 : 1
color: customStyle.backgroundColor
radius: customStyle.cornerRadius
}
}
// [/hidpi fixes]
}
}
@@ -1,6 +1,7 @@
<RCC>
<qresource prefix="/QgsQuick">
<file>qmldir</file>
<file>editor/qgsquickcombobox.qml</file>
<file>editor/qgsquickcheckbox.qml</file>
<file>editor/qgsquickdatetime.qml</file>
<file>editor/qgsquickexternalresource.qml</file>
@@ -13,6 +13,7 @@
module QgsQuick
plugin qgis_quick_plugin

ComboBox 0.1 qgsquickcombobox.qml
MapCanvas 0.1 qgsquickmapcanvas.qml
FeatureForm 0.1 qgsquickfeatureform.qml
FeatureFormStyling 0.1 qgsquickfeatureformstyling.qml

0 comments on commit e06652e

Please sign in to comment.
You can’t perform that action at this time.