Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2020 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 Lomiri.Components.ListItems 1.3
20 import Lomiri.Components.Pickers 1.3
21 import QtContacts 5.0 as QtContact
22 import Lomiri.Contacts 0.1
23
24 import Lomiri.AddressBook.Base 0.1
25
26 //style
27 import Lomiri.Components.Themes.Ambiance 0.1
28
29 ContactDetailBase {
30 id:root
31 property date date
32 property date originalValue
33 property bool newBirthdayRequested: false
34
35 detailType: QtContact.ContactDetail.Birthday
36
37 implicitHeight: contents.height
38
39
40 visible: !isEmpty() || newBirthdayRequested
41 activeFocusOnTab: true
42
43 onDetailChanged: {
44 if (detail && isDateValid(detail.birthday)) {
45 root.date = new Date(detail.birthday.getTime())
46 root.originalValue = root.date
47 }
48 }
49
50 function isDateValid(dt) {
51 return dt instanceof Date && !isNaN(dt.valueOf())
52 }
53
54 function isEmpty() {
55 return input.text.length == 0
56 }
57
58 function save() {
59 var detailchanged = false
60 if (isDateValid(root.date)) {
61 // create the birthday detail if needed
62 if (!root.detail) {
63 root.detail = root.contact.birthday
64 }
65 if (input.text.length == 0) {
66 root.detail.setValue(QtContact.Birthday, "")
67 detailchanged = true
68 }else {
69 var dt = new Date(root.date.getTime())
70 if (!isDateValid(originalValue) || originalValue.getTime() !== dt.getTime()) {
71 root.detail.setValue(QtContact.Birthday,dt)
72 detailchanged = true
73 }
74 }
75
76 }
77 return detailchanged
78 }
79
80 function selectDate(){
81
82 if (!isDateValid(root.date)) {
83 var dt = new Date()
84 dt.setFullYear(dt.getFullYear()-30)
85 dt.setHours(0,0,0,0)
86 root.date = dt
87 }
88
89 var pickerPanel = PickerPanel.openDatePicker(root, "date", "Years|Months|Days")
90 if ( pickerPanel !== null) {
91 // activeFocus style on birthday field
92 root.editing = true
93 pickerPanel.closed.connect(function() { root.editing = false} )
94
95 var picker = pickerPanel.picker
96 var dmax = new Date()
97 picker.maximum = dmax
98 var dmin = new Date()
99 dmin.setFullYear(dmax.getFullYear()-110)
100 picker.minimum = dmin
101 }
102 }
103
104 Column {
105 id: contents
106 spacing: units.gu(1)
107 width: parent.width
108
109 ContactDetailTitle {
110 title: i18n.dtr("lomiri-addressbook-app", "Birthday")
111 }
112
113 TextField {
114 id: input
115 text: Qt.formatDate(root.date)
116 readOnly: true
117
118 anchors {
119 margins: units.gu(2)
120 left: parent.left
121 right: parent.right
122 }
123 height: units.gu(4)
124
125 placeholderText: i18n.dtr("lomiri-addressbook-app", "Enter a birthday")
126 style: TextFieldStyle {
127 overlaySpacing: 0
128 frameSpacing: 0
129 background: Item {}
130 }
131
132 MouseArea {
133 anchors.fill: parent
134 onClicked: {
135 // prevent focus stealing from the previous TextField
136 root.forceActiveFocus();
137 selectDate() }
138 }
139
140 AbstractButton {
141 id: clearButton
142 objectName: "clear_button"
143 activeFocusOnPress: false
144 activeFocusOnTab: false
145
146 anchors {
147 top: parent.top
148 right: parent.right
149 margins: units.gu(0.5)
150 verticalCenter: parent.verticalCenter
151
152 }
153 /* draggedItemMouseArea and dragger in TextCursor are reparented to the
154 TextField and end up being on top of the clear button.
155 Ensure that the clear button receives touch/mouse events first.
156 */
157 z: 100
158 width: visible ? icon.width : 0
159
160 Icon {
161 id: icon
162 anchors.verticalCenter: parent.verticalCenter
163 width: units.gu(2.5)
164 height: width
165 // use icon from icon-theme
166 name: "edit-clear"
167 }
168
169 onClicked: {
170 input.text = ""
171 }
172 }
173 }
174 }
175
176 Keys.onReturnPressed:selectDate()
177
178 Connections {
179 target: addNewFieldButton
180 function onFieldSelected(detailType, fieldName, qmlTypeName) {
181 if (detailType === root.detailType) {
182 newBirthdayRequested = true
183 input.text = Qt.binding(function() { return Qt.formatDate(root.date)})
184 selectDate()
185 }
186 }
187 }
188 }