Debian Codegraph @

src/imports/Lomiri/Contacts/ContactSimpleListView.qml

raw ◀ ▶
3 Debian patches touch this file
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 import Lomiri.Components 1.3
20 import Lomiri.Components.ListItems 1.3 as ListItem
21 import Lomiri.Contacts 0.1
22
23 import "ContactList.js" as Sections
24
25 /*!
26 \qmltype ContactSimpleListView
27 \inqmlmodule Lomiri.Contacts 0.1
28 \ingroup ubuntu
29 \brief The ContactSimpleListView provides a simple contact list view
30
31 The ContactSimpleListView provide a easy way to show the contact list view
32 with all default visuals defined by Ubuntu system.
33
34 Example:
35 \qml
36 import Lomiri.Contacts 0.1
37
38 ContactSimpleListView {
39 anchors.fill: parent
40 onContactClicked: console.debug("Contact ID:" + contactId)
41 }
42 \endqml
43 */
44
45 MultipleSelectionListView {
46 id: contactListView
47
48 /*!
49 \qmlproperty bool showAvatar
50
51 This property holds if the contact avatar will appear on the list or not.
52 By default this is set to true.
53 */
54 property bool showAvatar: true
55
56 /*!
57 \qmlproperty list<SortOrder> sortOrders
58
59 This property holds a list of sort orders used by the contacts model.
60 \sa SortOrder
61 */
62 property list<SortOrder> sortOrders : [
63 SortOrder {
64 detail: ContactDetail.Tag
65 field: Tag.Tag
66 direction: Qt.AscendingOrder
67 blankPolicy: SortOrder.BlanksLast
68 caseSensitivity: Qt.CaseInsensitive
69 },
70 // empty tags will be sorted by display Label
71 SortOrder {
72 detail: ContactDetail.DisplayLabel
73 field: DisplayLabel.Label
74 direction: Qt.AscendingOrder
75 blankPolicy: SortOrder.BlanksLast
76 caseSensitivity: Qt.CaseInsensitive
77 }
78 ]
79 /*!
80 \qmlproperty FetchHint fetchHint
81
82 This property holds the fetch hint instance used by the contact model.
83
84 \sa FetchHint
85 */
86 property var fetchHint : FetchHint {
87 detailTypesHint: {
88 var hints = [ ContactDetail.Tag, // sections
89 ContactDetail.DisplayLabel // label
90 ]
91
92 if (contactListView.showAvatar) {
93 hints.push(ContactDetail.Avatar)
94 }
95 return hints
96 }
97 }
98 /*!
99 \qmlproperty bool multiSelectionEnabled
100
101 This property holds if the multi selection mode is enabled or not
102 By default this is set to false
103 */
104 property bool multiSelectionEnabled: false
105 /*!
106 \qmlproperty string defaultAvatarImage
107
108 This property holds the default image url to be used when the current contact does
109 not contains a photo
110 */
111 property string defaultAvatarImageUrl: "image://theme/contact"
112 /*!
113 \qmlproperty bool loading
114
115 This property holds when the model still loading new contacts
116 */
117 readonly property bool loading: busyIndicator.busy
118 /*!
119 \qmlproperty bool showSections
120
121 This property holds if the listview will show or not the section headers
122 By default this is set to true
123 */
124 property bool showSections: true
125
126 /*!
127 \qmlproperty string manager
128
129 This property holds the manager uri of the contact backend engine.
•130 By default this is set to "org.nemomobile.contacts.sqlite"
131 */
132 property string manager: ContactManager.manager()
133
134 /*!
135 \qmlproperty Action leftSideAction
136
137 This property holds the available actions when swipe the contact item from left to right
138 */
139 property Action leftSideAction
140
141 /*!
142 \qmlproperty list<Action> rightSideActions
143
144 This property holds the available actions when swipe the contact item from right to left
145 */
146 property list<Action> rightSideActions
147
148 /*!
149 \qmlproperty highlightSelected
150
151 This property holds if the current contact should be highlighted or not
152 */
153 property bool highlightSelected: false
154
155 /* internal */
156 property var _currentSwipedItem: null
157
158 /*!
159 This handler is called when any error occurs in the contact model
160 */
161 signal error(string message)
162 /*!
163 This handler is called when any contact in the list receives a click
164 */
165 signal contactClicked(QtObject contact)
166 /*!
167 This handler is called when the contact delegate disapear (height === 0) caused by the function call makeDisappear
168 */
169 signal contactDisappeared(QtObject contact)
170 /*!
171 Retrieve the contact index inside of the list based on contact id or contact name if the id is empty
172 */
173 function getIndex(contact)
174 {
175 var contacts = listModel.contacts
176 var contactId = null
177 var firstName
178 var middleName
179 var lastName
180
181 if (contact.contactId !== "qtcontacts:::") {
182 contactId = contact.contactId
183 } else {
184 firstName = contact.name.firstName
185 middleName = contact.name.middleName
186 lastName = contact.name.lastName
187 }
188
189 for (var i = 0, count = contacts.length; i < count; i++) {
190 var c = contacts[i]
191 if (contactId && (c.contactId === contactId)) {
192 return i
193 } else if ((c.name.firstName === firstName) &&
194 (c.name.middleName === middleName) &&
195 (c.name.lastName === lastName)) {
196 return i
197 }
198 }
199
200 return -1
201 }
202
203 /*!
204 Scroll the list to requested contact if the contact exists in the list
205 */
206 function positionViewAtContact(contact)
207 {
208 currentIndex = getIndex(contact)
209 positionViewAtIndex(currentIndex, ListView.Center)
210 }
211
212
213 /*!
214 Scroll the list to requested contact if the contact exists in the list
215 */
216 function positionViewAtContactId(contactId)
217 {
218 var contacts = listModel.contacts
219
220 for (var i = 0, count = contacts.length; i < count; i++) {
221 var c = contacts[i]
222 if (c.contactId === contactId) {
223 currentIndex = i
224 positionViewAtIndex(i, ListView.Center)
225 return
226 }
227 }
228 }
229
•230 function getSectionText(index) {
231 return Sections.sectionValueForContact(listModel.contacts[index])
232 }
233
234 /*!
235 private
236 Fetch contact and emit contact clicked signal
237 */
238 function _fetchContact(index, contact)
239 {
240 if (contact) {
241 contactFetch.fetchContact(contact.contactId)
242 }
243 }
244
245 function _updateSwipeState(item)
246 {
247 if (item.swipping) {
248 return
249 }
250
251 if (item.swipeState !== "Normal") {
252 if (contactListView._currentSwipedItem !== item) {
253 if (contactListView._currentSwipedItem) {
254 contactListView._currentSwipedItem.resetSwipe()
255 }
256 contactListView._currentSwipedItem = item
257 }
258 } else if (item.swipeState !== "Normal" && contactListView._currentSwipedItem === item) {
259 contactListView._currentSwipedItem = null
260 }
261 }
262
263 highlightFollowsCurrentItem: true
264 section {
•265 property: showSections ? "contact.displayLabel.label" : ""
266 criteria: ViewSection.FirstCharacter
267 labelPositioning: ViewSection.InlineLabels
268 delegate: SectionDelegate {
269 anchors {
270 left: parent.left
271 right: parent.right
272 margins: units.gu(2)
273 }
•274 text: section != "" ? section.toUpperCase() : "#"
275 }
276 }
277
278 onCountChanged: {
279 busyIndicator.ping()
280 dirtyModel.restart()
281 }
282
283 listDelegate: ContactDelegate {
284 id: contactDelegate
285
286 property var removalAnimation
287
288 function remove()
289 {
290 removalAnimation.start()
291 }
292
•293 color: selected ? Theme.palette.selected.background : Theme.palette.normal.background
294
295 flicking: contactListView.flicking
296 width: parent.width
297 selected: (contactListView.multiSelectionEnabled && contactListView.isSelected(contactDelegate))
298 || (!contactListView.isInSelectionMode && contactListView.highlightSelected && (contactListView.currentIndex == index))
299 selectionMode: contactListView.isInSelectionMode
300 defaultAvatarUrl: contactListView.defaultAvatarImageUrl
301 isCurrentItem: ListView.isCurrentItem
302
303 // actions
304 leftSideAction: contactListView.leftSideAction
305 rightSideActions: contactListView.rightSideActions
306
•307 ListView.onRemove: SequentialAnimation {
308 PropertyAction { target: contactDelegate; property: "ListView.delayRemove"; value: true }
309 NumberAnimation { target: contactDelegate; property: "height"; to: 0; duration: 250; easing.type: Easing.InOutQuad }
310 PropertyAction { target: contactDelegate; property: "ListView.delayRemove"; value: false }
311 }
312
313 // used by swipe to delete
314 removalAnimation: SequentialAnimation {
315 alwaysRunToEnd: true
316
317 PropertyAction {
318 target: contactDelegate
319 property: "ListView.delayRemove"
320 value: true
321 }
322 LomiriNumberAnimation {
323 target: contactDelegate
324 property: "height"
325 to: 1
326 }
327 PropertyAction {
328 target: contactDelegate
329 property: "ListView.delayRemove"
330 value: false
331 }
332 ScriptAction {
333 script: contactListView.listModel.removeContact(contact.contactId)
334 }
335 }
336
337 onClicked: {
338 if (contactListView.isInSelectionMode) {
339 if (!contactListView.selectItem(contactDelegate)) {
340 contactListView.deselectItem(contactDelegate)
341 }
342 } else {
343 contactListView.currentIndex = index
344 contactListView._fetchContact(index, contact)
345 }
346 }
347
348 onPressAndHold: {
349 if (contactListView.multiSelectionEnabled) {
350 contactListView.currentIndex = -1
351 contactListView.startSelection()
352 contactListView.selectItem(contactDelegate)
353 }
354 }
355
356 onSwippingChanged: contactListView._updateSwipeState(contactDelegate)
357 onSwipeStateChanged: contactListView._updateSwipeState(contactDelegate)
358 }
359
360 ContactFetch {
361 id: contactFetch
362
363 model: root.listModel
364 onContactFetched: contactListView.contactClicked(contact)
365 }
366 // This is a workaround to make sure the spinner will disappear if the model is empty
367 // FIXME: implement a model property to say if the model still busy or not
368 Item {
369 id: busyIndicator
370
371 property bool busy: timer.running || priv.currentOperation !== -1
372
373 function ping()
374 {
375 timer.restart()
376 }
377
378 visible: busy
379 anchors.fill: parent
380
381 Timer {
382 id: timer
383
384 interval: 3000
385 running: true
386 repeat: false
387 }
388 }
389
390 Timer {
391 id: dirtyModel
392
393 interval: 1000
394 running: false
395 repeat: false
396 onTriggered: Sections.initSectionData(contactListView)
397 }
398
399 QtObject {
400 id: priv
401
402 property int currentOperation: -1
403 property int pendingTargetIndex: 0
404 property variant pendingTargetMode: null
405 }
406 }