Skip to content

Commit

Permalink
[QgsQuick] Value Relation widget
Browse files Browse the repository at this point in the history
Added a widget for value relation field time of a feature form.
  • Loading branch information
vsklencar authored and wonder-sk committed Apr 10, 2019
1 parent 1450985 commit 7631ddd
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/quickgui/plugin/CMakeLists.txt
Expand Up @@ -15,6 +15,7 @@ SET(QGIS_QUICK_PLUGIN_RESOURCES
editor/qgsquickexternalresource.qml
editor/qgsquicktextedit.qml
editor/qgsquickvaluemap.qml
editor/qgsquickvaluerelation.qml
qgsquickfeatureform.qml
qgsquickfeatureformstyling.qml
qgsquickmapcanvas.qml
Expand Down
126 changes: 126 additions & 0 deletions src/quickgui/plugin/editor/qgsquickvaluerelation.qml
@@ -0,0 +1,126 @@
/***************************************************************************
qgsquickvaluerelation.qml
--------------------------------------
Date : 2017
Copyright : (C) 2019 by Viktor Sklencar
Email : viktor.sklencar@lutraconsulting.co.uk
***************************************************************************
* *
* 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

/**
* Value Relation for QGIS Attribute Form
* Requires various global properties set to function, see qgsquickfeatureform Loader section
* Do not use directly from Application QML
*/
Item {
signal valueChanged(var value, bool isNull)

id: fieldItem
enabled: !readOnly
height: customStyle.height
anchors {
left: parent.left
right: parent.right
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
}

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])
}

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

// Workaround to get a signal when the value has changed
onCurrentValueChanged: {
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]
}
}
14 changes: 14 additions & 0 deletions src/quickgui/qgsquickutils.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgsvectorlayer.h"
#include "qgsfeature.h"
#include "qgsapplication.h"
#include "qgsvaluerelationfieldformatter.h"

#include "qgsquickfeaturelayerpair.h"
#include "qgsquickmapsettings.h"
Expand Down Expand Up @@ -135,6 +136,7 @@ const QUrl QgsQuickUtils::getEditorComponentSource( const QString &widgetName )
QString path( "qgsquick%1.qml" );
QStringList supportedWidgets = { QStringLiteral( "textedit" ),
QStringLiteral( "valuemap" ),
QStringLiteral( "valuerelation" ),
QStringLiteral( "checkbox" ),
QStringLiteral( "externalresource" ),
QStringLiteral( "datetime" )
Expand Down Expand Up @@ -329,6 +331,18 @@ QString QgsQuickUtils::dumpScreenInfo() const
return msg;
}

QVariantMap QgsQuickUtils::createValueRelationCache(const QVariantMap &config, const QgsFeature &formFeature)
{
QVariantMap valueMap;
QgsValueRelationFieldFormatter::ValueRelationCache cache = QgsValueRelationFieldFormatter::createCache(config, formFeature);

for ( const QgsValueRelationFieldFormatter::ValueRelationItem &item : qgis::as_const( cache ) )
{
valueMap.insert(item.key.toString(), item.value);
}
return valueMap;
}

qreal QgsQuickUtils::screenDensity() const
{
return mScreenDensity;
Expand Down
2 changes: 2 additions & 0 deletions src/quickgui/qgsquickutils.h
Expand Up @@ -229,6 +229,8 @@ class QUICK_EXPORT QgsQuickUtils: public QObject
//! Returns a string with information about screen size and resolution - useful for debugging
QString dumpScreenInfo() const;

Q_INVOKABLE static QVariantMap createValueRelationCache( const QVariantMap &config, const QgsFeature &formFeature = QgsFeature() );

private:
static void formatToMetricDistance( double srcDistance,
QgsUnitTypes::DistanceUnit srcUnits,
Expand Down

0 comments on commit 7631ddd

Please sign in to comment.