Skip to content

Commit

Permalink
Allow adding features from the RelationReference widget
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Feb 28, 2017
1 parent 81bfa52 commit 51aa2f5
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 67 deletions.
5 changes: 5 additions & 0 deletions images/images.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,10 @@
<file>themes/qfield/xxhdpi/ic_clear_black_18dp.png</file>
<file>themes/qfield/xxxhdpi/ic_clear_black_18dp.png</file>
<file>qtquickcontrols2.conf</file>
<file>themes/qfield/hdpi/ic_add_black_48dp.png</file>
<file>themes/qfield/mdpi/ic_add_black_48dp.png</file>
<file>themes/qfield/xhdpi/ic_add_black_48dp.png</file>
<file>themes/qfield/xxhdpi/ic_add_black_48dp.png</file>
<file>themes/qfield/xxxhdpi/ic_add_black_48dp.png</file>
</qresource>
</RCC>
Binary file added images/themes/qfield/hdpi/ic_add_black_48dp.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/themes/qfield/mdpi/ic_add_black_48dp.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/themes/qfield/xhdpi/ic_add_black_48dp.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/themes/qfield/xxhdpi/ic_add_black_48dp.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/themes/qfield/xxxhdpi/ic_add_black_48dp.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/attributeformmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ void AttributeFormModel::create()
mSourceModel->create();
}

QVariant AttributeFormModel::attribute( const QString& name )
{
return mSourceModel->attribute( name );
}

bool AttributeFormModel::filterAcceptsRow( int source_row, const QModelIndex& source_parent ) const
{
return mSourceModel->data( mSourceModel->index( source_row, 0, source_parent ), CurrentlyVisible ).toBool();
Expand Down
1 change: 1 addition & 0 deletions src/attributeformmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class AttributeFormModel : public QSortFilterProxyModel

Q_INVOKABLE void save();
Q_INVOKABLE void create();
Q_INVOKABLE QVariant attribute( const QString& name );

signals:
void featureModelChanged();
Expand Down
9 changes: 9 additions & 0 deletions src/attributeformmodelbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,15 @@ bool AttributeFormModelBase::constraintsValid() const
return mConstraintsValid;
}

QVariant AttributeFormModelBase::attribute( const QString& name )
{
if ( !mLayer )
return QVariant();

int idx = mLayer->fields().indexOf( name );
return mFeatureModel->feature().attribute( idx );
}

void AttributeFormModelBase::setConstraintsValid( bool constraintsValid )
{
if ( constraintsValid == mConstraintsValid )
Expand Down
2 changes: 2 additions & 0 deletions src/attributeformmodelbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class AttributeFormModelBase : public QStandardItemModel

bool constraintsValid() const;

QVariant attribute( const QString& name );

signals:
void featureModelChanged();
void hasTabsChanged();
Expand Down
2 changes: 1 addition & 1 deletion src/qml/FeatureForm.qml
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ Page {
}

RowLayout {
spacing: 20
anchors.fill: parent
Layout.margins: 0

ToolButton {
id: saveButton
Expand Down
198 changes: 132 additions & 66 deletions src/qml/editorwidgets/RelationReference.qml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import QtQuick 2.0
import QtQuick.Controls 2.0
import QtGraphicalEffects 1.0
import QtQuick.Layouts 1.0
import ".."
import "../js/style.js" as Style

import org.qfield 1.0
import org.qgis 1.0
Expand All @@ -16,96 +19,159 @@ Item {

height: childrenRect.height + 10 * dp

RowLayout {
anchors { left: parent.left; right: parent.right }

ComboBox {
id: comboBox
ComboBox {
id: comboBox

property var currentValue: value
property var _relation
property var _cachedCurrentValue
property var currentValue: value
property var _relation
property var _cachedCurrentValue

anchors { left: parent.left; right: parent.right }
textRole: 'display'

textRole: 'display'
Layout.fillWidth: true

model: FeatureListModel {
id: featureListModel
}
model: FeatureListModel {
id: featureListModel
}

Component.onCompleted: {
_relation = qgisProject.relationManager.relation(config['Relation'])
featureListModel.currentLayer = _relation.referencedLayer
featureListModel.keyField = _relation.resolveReferencedField(field.name)
currentIndex = featureListModel.findKey(value)
}
Component.onCompleted: {
_relation = qgisProject.relationManager.relation(config['Relation'])
featureListModel.currentLayer = _relation.referencedLayer
featureListModel.keyField = _relation.resolveReferencedField(field.name)
currentIndex = featureListModel.findKey(value)
}

onCurrentIndexChanged: {
var idx = featureListModel.index(currentIndex, 0, undefined)
var newValue = featureListModel.data(idx, FeatureListModel.KeyFieldRole)
valueChanged(newValue, false)
}
onCurrentIndexChanged: {
var idx = featureListModel.index(currentIndex, 0, undefined)
var newValue = featureListModel.data(idx, FeatureListModel.KeyFieldRole)
valueChanged(newValue, false)
}

// Workaround to get a signal when the value has changed
onCurrentValueChanged: {
currentIndex = featureListModel.findKey(value)
// currentIndex = find(reverseConfig[value])
}
// Workaround to get a signal when the value has changed
onCurrentValueChanged: {
currentIndex = featureListModel.findKey(currentValue)
}

Connections {
target: featureListModel.currentLayer
Connections {
target: featureListModel

onModelAboutToBeReset: {
comboBox._cachedCurrentValue = value
onModelAboutToBeReset: {
comboBox._cachedCurrentValue = value
}

onModelReset: {
comboBox.currentIndex = featureListModel.findKey(value)
}
}

onModelReset: {
currentIndex = featureListModel.findKey(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: 36 * dp
text: comboBox.textRole ? (Array.isArray(comboBox.model) ? modelData[comboBox.textRole] : model[comboBox.textRole]) : modelData
font.weight: comboBox.currentIndex === index ? Font.DemiBold : Font.Normal
font.pointSize: 12
highlighted: comboBox.highlightedIndex == index
}
}

MouseArea {
anchors.fill: parent
propagateComposedEvents: true
contentItem: Text {
height: 36 * dp
text: comboBox.displayText
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
}

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;
background: Item {
implicitWidth: 120 * dp
implicitHeight: 36 * dp

Rectangle {
anchors.fill: parent
id: backgroundRect
border.color: comboBox.pressed ? "#17a81a" : "#21be2b"
border.width: comboBox.visualFocus ? 2 : 1
color: "#dddddd"
radius: 2
}
}
// [/hidpi fixes]
}

// [hidpi fixes]
delegate: ItemDelegate {
width: comboBox.width
height: 36 * dp
text: comboBox.textRole ? (Array.isArray(comboBox.model) ? modelData[comboBox.textRole] : model[comboBox.textRole]) : modelData
font.weight: comboBox.currentIndex === index ? Font.DemiBold : Font.Normal
font.pointSize: 12
highlighted: comboBox.highlightedIndex == index
Button {
iconSource: Style.getThemeIcon( "ic_add_black_48dp" )
bgcolor: "white"
onClicked: {
addFeatureForm.active = true
}
}
}

contentItem: Text {
height: 36 * dp
text: comboBox.displayText
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
Loader {
id: addFeatureForm
sourceComponent: addFeatureFormComponent
active: false
onLoaded: {
item.open()
}
}

background: Item {
implicitWidth: 120 * dp
implicitHeight: 36 * dp
Component {
id: addFeatureFormComponent
Popup {
id: popup
parent: ApplicationWindow.overlay

x: 24 * dp
y: 24 * dp
width: parent.width - 48 * dp
height: parent.width - 48 * dp
modal: true
focus: true
closePolicy: Popup.CloseOnEscape

FeatureForm {
model: AttributeFormModel {
id: attributeFormModel

featureModel: FeatureModel {
currentLayer: comboBox._relation.referencedLayer
}
}

Rectangle {
anchors.fill: parent
id: backgroundRect
border.color: comboBox.pressed ? "#17a81a" : "#21be2b"
border.width: comboBox.visualFocus ? 2 : 1
color: "#dddddd"
radius: 2

state: "Add"

onSaved: {
var referencedValue = attributeFormModel.attribute(comboBox._relation.resolveReferencedField(field.name))
comboBox.currentValue = referencedValue
popup.close()
}

onCancelled: {
popup.close()
}
}

onClosed: {
addFeatureForm.active = false
}
}
// [/hidpi fixes]
}
}

0 comments on commit 51aa2f5

Please sign in to comment.