summaryrefslogtreecommitdiffstats
path: root/examples/nfc/ndefeditor/MainWindow.qml
blob: d32540fb3250da888bc7c869f2e7e10b67274772 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Controls
import QtQuick.Dialogs
import QtQuick.Layouts

import NdefEditor

ApplicationWindow {
    id: window

    enum TargetDetectedAction {
        NoAction,
        ReadMessage,
        WriteMessage
    }

    StackView {
        id: stack
        initialItem: mainView
        anchors.fill: parent
    }

    property int targetDetectedAction: MainWindow.NoAction

    Component {
        id: mainView

        ColumnLayout {
            ToolBar {
                Layout.fillWidth: true

                RowLayout {
                    anchors.fill: parent
                    Label {
                        text: qsTr("NDEF Editor")
                        elide: Label.ElideRight
                        horizontalAlignment: Qt.AlignHCenter
                        verticalAlignment: Qt.AlignVCenter
                        Layout.fillWidth: true
                    }
                }
            }

            Pane {
                id: writeTab
                Layout.fillWidth: true
                Layout.fillHeight: true

                Flickable {
                    anchors.fill: parent
                    contentHeight: contentItem.childrenRect.height

                    ColumnLayout {
                        width: parent.width

                        ItemDelegate {
                            icon.name: "file_download"
                            text: qsTr("Read Tag")
                            Layout.fillWidth: true

                            onClicked: window.readTag()
                        }

                        ItemDelegate {
                            icon.name: "file_upload"
                            text: qsTr("Write to Tag")
                            Layout.fillWidth: true

                            onClicked: window.writeTag()
                        }

                        ItemDelegate {
                            icon.name: "add"
                            text: qsTr("Add a Record")
                            Layout.fillWidth: true

                            onClicked: addRecordMenu.open()

                            Menu {
                                id: addRecordMenu
                                title: qsTr("Add record")

                                MenuItem {
                                    icon.name: "text_snippet"
                                    text: qsTr("Text record")
                                    onTriggered: stack.push(textRecordDialog)
                                }
                                MenuItem {
                                    icon.name: "link"
                                    text: qsTr("URI record")
                                    onTriggered: stack.push(uriRecordDialog)
                                }
                            }
                        }

                        Item {
                            implicitHeight: 20
                        }

                        Label {
                            text: qsTr(
                                "Use buttons above to read an NFC tag or add records manually.\n\n"
                                + "Once added, swipe to the right to access options for each record.")
                            wrapMode: Text.Wrap
                            visible: messages.count === 0
                            Layout.fillWidth: true
                        }

                        Repeater {
                            id: messages
                            model: messageModel

                            delegate: NdefRecordDelegate {
                                Layout.fillWidth: true

                                onDeleteClicked: messageModel.removeRow(index)

                                onClicked: {
                                    if (recordType === NdefMessageModel.TextRecord) {
                                        stack.push(textRecordDialog, {
                                            text: recordText,
                                            modelIndex: index
                                        })
                                    } else if (recordType === NdefMessageModel.UriRecord) {
                                        stack.push(uriRecordDialog, {
                                            uri: recordText,
                                            modelIndex: index
                                        })
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    NdefMessageModel {
        id: messageModel
    }

    NfcManager {
        id: nfcManager
        onTargetDetected: (target) => { window.handleTargetDetected(target) }
    }

    Connections {
        id: targetConnections
        target: null

        function onNdefMessageRead(message) {
            messageModel.message = message
        }

        function onRequestCompleted() {
            communicationOverlay.close()
            window.targetDetectedAction = MainWindow.NoAction
            releaseTarget()
        }

        function onError(code) {
            communicationOverlay.close()
            errorNotification.errorMessage = window.describeError(code)
            errorNotification.open()
            window.targetDetectedAction = MainWindow.NoAction
            releaseTarget()
        }

        function releaseTarget() {
            target?.destroy()
        }
    }

    function handleTargetDetected(target: NfcTarget) {
        targetConnections.releaseTarget()

        let ok = true

        if (targetDetectedAction === MainWindow.ReadMessage) {
            targetConnections.target = target
            communicationOverlay.message = qsTr("Target detected. Reading messages...")
            errorNotification.errorMessage = qsTr("NDEF read error")
            ok = target.readNdefMessages()
        } else if (targetDetectedAction === MainWindow.WriteMessage) {
            targetConnections.target = target
            communicationOverlay.message = qsTr("Target detected. Writing the message...")
            errorNotification.errorMessage = qsTr("NDEF write error")
            ok = target.writeNdefMessage(messageModel.message)
        } else {
            target.destroy()
            return
        }

        if (!ok) {
            communicationOverlay.close()
            errorNotification.open()
            targetConnections.releaseTarget()
        }
    }

    function readTag() {
        window.targetDetectedAction = MainWindow.ReadMessage
        nfcManager.startTargetDetection()
        communicationOverlay.title = qsTr("Read Tag")
        communicationOverlay.message = qsTr("Approach an NFC tag.")
        communicationOverlay.open()
    }

    function writeTag() {
        window.targetDetectedAction = MainWindow.WriteMessage
        nfcManager.startTargetDetection()
        communicationOverlay.title = qsTr("Write to Tag")
        communicationOverlay.message = qsTr("Approach an NFC tag.")
        communicationOverlay.open()
    }

    function describeError(error) {
        switch (error) {
        case 2: return qsTr("Usupported feature")
        case 3: return qsTr("Target out of range")
        case 4: return qsTr("No response")
        case 5: return qsTr("Checksum mismatch")
        case 6: return qsTr("Invalid parameters")
        case 7: return qsTr("Connection error")
        case 8: return qsTr("NDEF read error")
        case 9: return qsTr("NDEF write error")
        case 10: return qsTr("Command error")
        case 11: return qsTr("Timeout")
        }

        return qsTr("Unknown error")
    }

    Dialog {
        id: communicationOverlay

        property alias message: messageLabel.text

        anchors.centerIn: Overlay.overlay

        modal: true
        standardButtons: Dialog.Cancel

        ColumnLayout {
            Label {
                id: messageLabel
            }

            BusyIndicator {
                Layout.fillWidth: true
            }
        }

        onClosed: nfcManager.stopTargetDetection()
    }

    MessageDialog {
        property alias errorMessage: errorNotification.text

        id: errorNotification

        buttons: MessageDialog.Close
        title: qsTr("Error")

        onButtonClicked: errorNotification.close()
    }

    Component {
        id: uriRecordDialog

        ColumnLayout {
            property alias uri: uriEditor.text
            property int modelIndex: -1

            ToolBar {
                Layout.fillWidth: true

                RowLayout {
                    anchors.fill: parent
                    ToolButton {
                        icon.name: "arrow_back"
                        onClicked: stack.pop()
                    }
                    Label {
                        text: qsTr("URI Record")
                        elide: Label.ElideRight
                        horizontalAlignment: Qt.AlignHCenter
                        verticalAlignment: Qt.AlignVCenter
                        Layout.fillWidth: true
                    }
                    ToolButton {
                        text: qsTr("Save")
                        onClicked: {
                            if (modelIndex < 0) {
                                messageModel.addUriRecord(uri)
                            } else {
                                messageModel.setTextData(modelIndex, uri)
                            }
                            stack.pop()
                        }
                    }
                }
            }

            Pane {
                Layout.fillWidth: true
                Layout.fillHeight: true

                ColumnLayout {
                    anchors.fill: parent

                    TextField {
                        id: uriEditor
                        Layout.fillWidth: true
                        placeholderText: qsTr("https://qt.io")
                        inputMethodHints: Qt.ImhUrlCharactersOnly
                    }
                    Item {
                        Layout.fillHeight: true
                    }
                }
            }
        }
    }

    Component {
        id: textRecordDialog

        ColumnLayout {
            property alias text: textEditor.text
            property int modelIndex: -1

            ToolBar {
                Layout.fillWidth: true

                RowLayout {
                    anchors.fill: parent
                    ToolButton {
                        icon.name: "arrow_back"
                        onClicked: stack.pop()
                    }
                    Label {
                        text: qsTr("Text Record")
                        elide: Label.ElideRight
                        horizontalAlignment: Qt.AlignHCenter
                        verticalAlignment: Qt.AlignVCenter
                        Layout.fillWidth: true
                    }
                    ToolButton {
                        text: qsTr("Save")
                        onClicked: {
                            if (modelIndex < 0) {
                                messageModel.addTextRecord(textEditor.text)
                            } else {
                                messageModel.setTextData(modelIndex, textEditor.text)
                            }
                            stack.pop()
                        }
                    }
                }
            }

            Pane {
                Layout.fillWidth: true
                Layout.fillHeight: true

                ScrollView {
                    anchors.fill: parent

                    TextArea {
                        id: textEditor
                        placeholderText: qsTr("Enter some text...")
                        Layout.fillWidth: true
                    }
                }
            }
        }
    }
}