Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2012-2015 Canonical, Ltd.
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 QtContacts 5.0
19
20 import Lomiri.Components 1.3
21
22 import Lomiri.AddressBook.Base 0.1
23
24 ContactDetailItem {
25 id: root
26
27 property variant emptyFields: []
28 property bool showMiddleName: (detail && (detail.value(Name.MiddleName) !== undefined))
29 readonly property Item middleNameField: showMiddleName ? fieldDelegates[1] : null
30
31 function isEmpty() {
32 return (fields == -1) || (emptyFields.length === fields.length)
33 }
34
35 function save() {
36 var changed = false;
37 var names = []
38 for(var i=0; i < fieldDelegates.length; i++) {
39 var delegate = fieldDelegates[i]
40
41 // Get item from Loader
42 if (delegate.item) {
43 delegate = delegate.item
44 }
45
46 if (delegate.detail && (delegate.field >= 0)) {
47 var value = delegate.detail.value(delegate.field)
48 names.push(value)
49 if (delegate.text != value && !((typeof value === 'undefined') && delegate.text.length ===0)) {
50 delegate.detail.setValue(delegate.field, delegate.text)
51 changed = true;
52 }
53 }
54 }
55
56 return changed
57 }
58
59 spacing: units.gu(1)
60 detail: root.contact ? root.contact.name : null
61 fields: [ Name.FirstName, Name.MiddleName, Name.LastName ]
62 highlightOnFocus: false
63
64 fieldDelegate: TextInputDetail {
65 id: textInputDetail
66 objectName: detailToString(ContactDetail.Name, field)
67
68 function checkIsEmpty() {
69 if (field == -1) {
70 return;
71 }
72
73 var newEmtpyFields = root.emptyFields
74 var indexOf = newEmtpyFields.indexOf(field)
75
76 if ((text.length > 0) && (indexOf !== -1)) {
77 newEmtpyFields.splice(indexOf, 1)
78 } else if ((text.length === 0) && (indexOf === -1)){
79 newEmtpyFields.push(field)
80 }
81 root.emptyFields = newEmtpyFields
82 }
83
84 function placeholderTextFromField(field)
85 {
86 switch (field) {
87 case Name.FirstName:
88 return i18n.dtr("lomiri-addressbook-app", "First name")
89 case Name.MiddleName:
90 return i18n.dtr("lomiri-addressbook-app", "Middle name")
91 case Name.LastName:
92 return i18n.dtr("lomiri-addressbook-app", "Last name")
93 default:
94 return "";
95 }
96 }
97
98 focus: true
99 anchors {
100 left: parent.left
101 right: parent.right
102 margins: units.gu(2)
103 }
104 height: units.gu(4)
105 detail: root.detail
106 visible: field === Name.MiddleName ? root.showMiddleName : true
107
108 placeholderText: placeholderTextFromField(field)
109 inputMethodHints: Qt.ImhNoPredictiveText
110 onTextChanged: checkIsEmpty()
111 onFieldChanged: checkIsEmpty()
112
113 //style
114 font.pixelSize: FontUtils.sizeToPixels("large")
115 }
116 }