Debian Codegraph @

src/imports/Lomiri/AddressBook/ContactEditor/ContactDetailUrlEditor.qml

raw
1 /*
2 * Copyright (C) 2022 UBports Foundation.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components 1.3
19 import QtContacts 5.0 as QtContacts
20 import Lomiri.Contacts 0.1
21
22 import Lomiri.AddressBook.Base 0.1
23
24 //style needed for the TextArea
25 import Lomiri.Components.Themes.Ambiance 0.1
26
27 ContactDetailBase {
28 id:root
29 property string url
30 property string originalValue
31 property bool newUrlRequested: false
32
33 detailType: QtContacts.ContactDetail.Url
34
35 implicitHeight: contents.height
36
37 visible: !isEmpty() || newUrlRequested
38 activeFocusOnTab: true
39
40 signal newFieldAdded(QtObject field)
41
42 onDetailChanged: {
43 if (detail) {
44 root.url = detail.url
45 root.originalValue = root.url
46 }
47 }
48
49 function isEmpty() {
50 return input.text.length == 0 && (!root.originalValue || root.originalValue.length === 0)
51 }
52
53 function save() {
54 var detailchanged = false
55 if (!root.detail) {
56 root.detail = root.contact.url
57 }
58 if (Qt.inputMethod) {
59 Qt.inputMethod.commit();
60 }
61
62 if (originalValue !== input.text.trim()) {
63 root.detail.setValue(QtContacts.Url, input.text.trim())
64 detailchanged = true
65 }
66 return detailchanged
67 }
68
69
70 Column {
71 id: contents
72 spacing: units.gu(1)
73 width: parent.width
74
75 ContactDetailTitle {
76 title: i18n.dtr("lomiri-addressbook-app", "Web address")
77 }
78
79 TextField {
80 id: input
81 text: root.url
82
83 anchors {
84 margins: units.gu(2)
85 left: parent.left
86 right: parent.right
87 }
88
89 height: units.gu(4)
90
91 placeholderText: i18n.dtr("lomiri-addressbook-app", "Type URL…")
92 inputMethodHints: Qt.ImhUrlCharactersOnly
93 style: TextFieldStyle {
94 overlaySpacing: 0
95 frameSpacing: 0
96 background: Item {}
97 }
98
99 // default style
100 font {
101 family: "Ubuntu"
102 pixelSize: activeFocus ? FontUtils.sizeToPixels("large") : FontUtils.sizeToPixels("medium")
103 }
104
105 onActiveFocusChanged: {
106 if (activeFocus) {
107 idleMakeMeVisible(input)
108 cursorPosition = text.length
109 }
110 }
111 }
112 }
113
114 Connections {
115 target: addNewFieldButton
116 function onFieldSelected(detailType, fieldName, qmlTypeName) {
117 if (detailType === root.detailType) {
118 newUrlRequested = true
119 newFieldAdded(input)
120 }
121 }
122 }
123 }