Skip to content

Commit

Permalink
use new features model instead of valuerelation model in qml
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasMizera authored and wonder-sk committed Sep 11, 2020
1 parent fa8f2d0 commit 2bdff7d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/quickgui/plugin/editor/qgsquickeditorwidgetcombobox.qml
Expand Up @@ -26,6 +26,7 @@ ComboBox {
id: comboBox

property var comboStyle
signal itemClicked( var index )
anchors { left: parent.left; right: parent.right }

MouseArea {
Expand All @@ -47,8 +48,9 @@ ComboBox {
text: model.display
font.weight: comboBox.currentIndex === index ? Font.DemiBold : Font.Normal
font.pixelSize: comboStyle.fontPixelSize
highlighted: comboBox.highlightedIndex == index
highlighted: comboBox.highlightedIndex === index
leftPadding: 5 * QgsQuick.Utils.dp
onClicked: comboBox.itemClicked( model.EmitableIndex ? model.EmitableIndex : index )
}

contentItem: Text {
Expand Down
38 changes: 30 additions & 8 deletions src/quickgui/plugin/editor/qgsquickvaluerelation.qml
Expand Up @@ -26,6 +26,14 @@ import QgsQuick 0.1 as QgsQuick
Item {
signal valueChanged(var value, bool isNull)

function itemSelected( index ) {
combobox.itemClicked( index )
}

function openCombobox() {
combobox.popup.open()
}

id: fieldItem
enabled: !readOnly
height: customStyle.fields.height
Expand All @@ -36,31 +44,45 @@ Item {
}

QgsQuick.EditorWidgetComboBox {

id: combobox
property var currentEditorValue: value

comboStyle: customStyle.fields
textRole: 'display'
height: parent.height

model: QgsQuick.ValueRelationListModel {
id: vrModel
model: QgsQuick.FeaturesListModel {
id: vrModel
modelType: QgsQuick.FeaturesListModel.ValueRelation

// recalculate index when model changes
onModelReset: {
combobox.currentIndex = vrModel.rowIndexFromKeyModel( value )
}
}

Component.onCompleted: {
vrModel.populate(config)
currentIndex = vrModel.rowForKey(value);
currentIndex = vrModel.rowIndexFromKeyModel( value )
}

onPressedChanged: {
if( pressed )
{
customWidget.valueRelationOpened( fieldItem, vrModel )
pressed = false // we close combobox and let custom handler react, it can open combobox via openCombobox()
}
}

// Called when user makes selection in the combo box
onCurrentIndexChanged: {
valueChanged(vrModel.keyForRow(currentIndex), false)
onItemClicked: {
currentIndex = vrModel.rowIndexFromKeyModel( index )
valueChanged( index, false )
}

// Called when the same form is used for a different feature
onCurrentEditorValueChanged: {
currentIndex = vrModel.rowForKey(value);
currentIndex = vrModel.rowIndexFromKeyModel( value );
}

}
}
11 changes: 11 additions & 0 deletions src/quickgui/plugin/qgsquickfeatureform.qml
Expand Up @@ -36,6 +36,16 @@ Item {
*/
signal canceled

/**
*/
property var customWidgetCallback: QtObject {

property var valueRelationOpened: function valueRelationOpened( widget, valueRelationModel ) {
// by default just open combobox
widget.openCombobox()
}
}

/**
* A handler for extra events in externalSourceWidget.
*/
Expand Down Expand Up @@ -396,6 +406,7 @@ Item {
property bool readOnly: form.state == "ReadOnly" || !AttributeEditable
property var featurePair: form.model.attributeModel.featureLayerPair
property var activeProject: form.project
property var customWidget: form.customWidgetCallback

active: widget !== 'Hidden'

Expand Down
3 changes: 2 additions & 1 deletion src/quickgui/plugin/qgsquickplugin.cpp
Expand Up @@ -47,6 +47,7 @@
#include "qgsquicksubmodel.h"
#include "qgsquickutils.h"
#include "qgsquickvaluerelationlistmodel.h"
#include "qgsquickfeatureslistmodel.h"

static QObject *_utilsProvider( QQmlEngine *engine, QJSEngine *scriptEngine )
{
Expand All @@ -73,7 +74,6 @@ void QgsQuickPlugin::registerTypes( const char *uri )
qRegisterMetaType< QVariant::Type >( "QVariant::Type" );

qmlRegisterUncreatableType< QgsUnitTypes >( uri, 0, 1, "QgsUnitTypes", "Only enums from QgsUnitTypes can be used" );

qmlRegisterType< QgsProject >( uri, 0, 1, "Project" );
qmlRegisterType< QgsQuickAttributeFormModel >( uri, 0, 1, "AttributeFormModel" );
qmlRegisterType< QgsQuickAttributeModel >( uri, 0, 1, "AttributeModel" );
Expand All @@ -89,6 +89,7 @@ void QgsQuickPlugin::registerTypes( const char *uri )
qmlRegisterType< QgsQuickSubModel >( uri, 0, 1, "SubModel" );
qmlRegisterType< QgsVectorLayer >( uri, 0, 1, "VectorLayer" );
qmlRegisterType< QgsQuickValueRelationListModel > ( uri, 0, 1, "ValueRelationListModel" );
qmlRegisterType< QgsQuickFeaturesListModel >( uri, 0, 1, "FeaturesListModel" );

qmlRegisterSingletonType< QgsQuickUtils >( uri, 0, 1, "Utils", _utilsProvider );
}
Expand Down

0 comments on commit 2bdff7d

Please sign in to comment.