Debian Codegraph @

src/imports/Lomiri/AddressBook/ContactEditor/ContactDetailNoteEditor.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 note
30 property string originalValue
31 property bool newNoteRequested: false
32
33 detailType: QtContacts.ContactDetail.Note
34
35 implicitHeight: contents.implicitHeight
36
37 visible: !isEmpty() || newNoteRequested
38 activeFocusOnTab: true
39
40 signal newFieldAdded(QtObject field)
41
42 onDetailChanged: {
43 if (detail) {
44 root.note = detail.note
45 root.originalValue = root.note
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.note
57 }
58
59 if (Qt.inputMethod) {
60 Qt.inputMethod.commit();
61 }
62
63 if (originalValue.replace(/(\r\n|\n|\r)/gm, "") !== input.text.replace(/(\r\n|\n|\r)/gm, "")) {
64 root.detail.note = input.text
65 detailchanged = true
66 }
67 return detailchanged
68 }
69
70 Column {
71 id: contents
72 spacing: units.gu(1)
73 width: parent.width
74
75 ContactDetailTitle {
76 id: detailTitle
77 title: i18n.dtr("lomiri-addressbook-app", "Note")
78 }
79
80 TextArea {
81 id: input
82 property real textAreaHeight: lineCount * font.pixelSize
83 anchors {
84 margins: units.gu(2)
85 left: parent.left
86 right: parent.right
87 }
88 height: textAreaHeight < units.gu(8) ? units.gu(8) : Math.min(units.gu(26), textAreaHeight)
89
90 text: root.note
91 persistentSelection: false
92 wrapMode: TextEdit.WordWrap
93 inputMethodHints: Qt.ImhNoPredictiveText
94 placeholderText:i18n.dtr("lomiri-addressbook-app", "Write note")
95 style: TextFieldStyle {
96 overlaySpacing: 0
97 frameSpacing: 0
98 background: Item {}
99 }
100
101 // default style
102 font {
103 family: "Ubuntu"
104 pixelSize: activeFocus ? FontUtils.sizeToPixels("large") : FontUtils.sizeToPixels("medium")
105 }
106
107 onActiveFocusChanged: {
108 if (activeFocus) {
109 idleMakeMeVisible(input)
110 deselect()
111 cursorPosition = text.length
112 }
113 }
114 }
115 }
116
117 // when user is just swiping to the page, avoid the TextArea to become active
118 MouseArea {
119 enabled: !input.activeFocus
120 anchors.fill: contents
121 onClicked: input.forceActiveFocus()
122 }
123
124 Connections {
125 target: addNewFieldButton
126 function onFieldSelected(detailType, fieldName, qmlTypeName) {
127 if (detailType === root.detailType) {
128 newNoteRequested = true
129 newFieldAdded(input)
130 }
131 }
132 }
133 }