generated from opengisch/qfield-template-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.qml
179 lines (140 loc) · 4.63 KB
/
main.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import org.qfield
import org.qgis
import Theme
import "qrc:/qml" as QFieldItems
Item {
id: plugin
property var mainWindow: iface.mainWindow()
property var positionSource: iface.findItemByObjectName('positionSource')
property var dashBoard: iface.findItemByObjectName('dashBoard')
property var overlayFeatureFormDrawer: iface.findItemByObjectName('overlayFeatureFormDrawer')
property var fieldNames: []
Component.onCompleted: {
iface.addItemToPluginsToolbar(selectLayerButton)
}
QfToolButton {
id: selectLayerButton
bgcolor: Theme.darkGray
iconSource: "icon.svg"
iconColor: Theme.mainColor
round: true
onClicked: layerSelectionDialog.open()
}
Dialog {
id: layerSelectionDialog
parent: mainWindow.contentItem
title: qsTr("Select Layer by Name")
standardButtons: Dialog.Ok | Dialog.Cancel
anchors.centerIn: parent
width: Math.min(300, parent.width - 50)
ColumnLayout {
width: parent.width
TextField {
id: layerNameInput
placeholderText: qsTr("Enter layer name")
Layout.fillWidth: true
}
}
onOpened: {
layerNameInput.forceActiveFocus()
}
onAccepted: {
var layerName = layerNameInput.text.trim()
if (layerName) {
let layer = qgisProject.mapLayersByName(layerName)[0]
if (layer) {
dashBoard.activeLayer = layer
dashBoard.ensureEditableLayerSelected()
mainWindow.displayToast(qsTr("Layer '%1' set as active").arg(layerName))
if (!positionSource.active || !positionSource.positionInformation.latitudeValid || !positionSource.positionInformation.longitudeValid) {
mainWindow.displayToast(qsTr('It requires positioning to be active and returning a valid position'))
return
}
if (dashBoard.activeLayer.geometryType() != Qgis.GeometryType.Point) {
mainWindow.displayToast(qsTr('It requires the active vector layer to be a point geometry'))
return
}
fieldNames = dashBoard.activeLayer.fields
fieldSelectionDialog.open()
} else {
mainWindow.displayToast(qsTr("Layer '%1' not found").arg(layerName), "warning")
}
}
layerNameInput.text = ""
}
onRejected: {
layerNameInput.text = ""
}
}
Dialog {
id: fieldSelectionDialog
parent: mainWindow.contentItem
title: qsTr("Enter Field Values")
standardButtons: Dialog.Ok | Dialog.Cancel
anchors.centerIn: parent
width: Math.min(700, parent.width)
// height: Math.min(300, parent.height - 50)
ColumnLayout {
width: parent.width
TextArea {
id: inputTextArea
placeholderText: qsTr("Enter field values")
topPadding: 10
bottomPadding: 10
rightPadding: 0
Layout.fillWidth: true
Layout.fillHeight: true
wrapMode: TextInput.Wrap
}
}
onOpened: {
inputTextArea.forceActiveFocus()
inputTextArea.text = ""
}
onAccepted: {
parseInputText(inputTextArea.text)
}
onRejected: {
inputTextArea.text = ""
}
}
function parseInputText(inputText) {
let lines = inputText.split(";");
let pos = positionSource.projectedPosition;
let wkt = 'POINT(' + pos.x + ' ' + pos.y + ')';
let geometry = GeometryUtils.createGeometryFromWkt(wkt);
let feature = FeatureUtils.createBlankFeature(dashBoard.activeLayer.fields, geometry);
for (let i = 0; i < lines.length; i++) {
let line = lines[i].trim();
if (line === "") {
continue;
}
let words = line.split(/\s+/);
let attributeName = words[0].toLowerCase();
let firstWord = convertToTitleCase(words[1]);
let attributeValue = [firstWord].concat(words.slice(2)).join(" ");
let attributeIndex = fieldNames.indexOf(attributeName);
if (attributeIndex === -1) {
mainWindow.displayToast(qsTr("Attribute '%1' not found").arg(attributeName), "warning");
continue;
}
feature.setAttribute(attributeIndex, attributeValue);
}
overlayFeatureFormDrawer.featureModel.feature = feature;
overlayFeatureFormDrawer.featureModel.resetAttributes(true);
overlayFeatureFormDrawer.state = 'Add';
overlayFeatureFormDrawer.open();
}
function convertToTitleCase(word) {
if (!word) {
return word;
}
if (!isNaN(word)) {
return word;
}
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
}