~renatofilho/address-book-app/fix-1199122-phase2

« back to all changes in this revision

Viewing changes to src/imports/Ubuntu/Contacts/ContactSimpleListView.qml

  • Committer: Tarmac
  • Author(s): Renato Araujo Oliveira Filho
  • Date: 2013-07-25 17:04:42 UTC
  • mfrom: (20.1.10 favorite-list)
  • Revision ID: tarmac-20130725170442-qq8dd0rcb6kn82t0
Implemented ContactFavoriteListView.

Approved by Bill Filler, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012-2013 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.0
 
18
import QtContacts 5.0
 
19
import Ubuntu.Components 0.1
 
20
import Ubuntu.Components.ListItems 0.1 as ListItem
 
21
 
 
22
/*!
 
23
    \qmltype ContactSimpleListView
 
24
    \inqmlmodule Ubuntu.Contacts 0.1
 
25
    \ingroup ubuntu
 
26
    \brief The ContactSimpleListView provides a simple contact list view
 
27
 
 
28
    The ContactSimpleListView provide a easy way to show the contact list view
 
29
    with all default visuals defined by Ubuntu system.
 
30
 
 
31
    Example:
 
32
    \qml
 
33
        import Ubuntu.Contacts 0.1
 
34
 
 
35
        ContactSimpleListView {
 
36
            anchors.fill: parent
 
37
            onContactClicked: console.debug("Contact ID:" + contactId)
 
38
        }
 
39
    \endqml
 
40
*/
 
41
 
 
42
ListView {
 
43
    id: contactListView
 
44
 
 
45
    /*!
 
46
      \qmlproperty bool showAvatar
 
47
 
 
48
      This property holds if the contact avatar will appear on the list or not.
 
49
      By default this is set to true.
 
50
    */
 
51
    property bool showAvatar: true
 
52
    /*!
 
53
      \qmlproperty int titleDetail
 
54
 
 
55
      This property holds the contact detail which will be used to display the contact title in the delegate
 
56
      By default this is set to ContactDetail.Name.
 
57
    */
 
58
    property int titleDetail: ContactDetail.Name
 
59
    /*!
 
60
      \qmlproperty list<int> titleFields
 
61
 
 
62
      This property holds the list of all fields which will be used to display the contact title in the delegate
 
63
      By default this is set to [ Name.FirstName, Name.LastName ]
 
64
    */
 
65
    property variant titleFields: [ Name.FirstName, Name.LastName ]
 
66
    /*!
 
67
      \qmlproperty int subTitleDetail
 
68
 
 
69
      This property holds the contact detail which will be used to display the contact subtitle in the delegate
 
70
      By default this is set to ContactDetail.Organization
 
71
    */
 
72
    property int subTitleDetail: ContactDetail.Organization
 
73
    /*!
 
74
      \qmlproperty list<int> subTitleFields
 
75
 
 
76
      This property holds the list of all fields which will be used to display the contact subtitle in the delegate
 
77
      By default this is set to [ Organization.Name ]
 
78
    */
 
79
    property variant subTitleFields: [ Organization.Name ]
 
80
    /*!
 
81
      \qmlproperty list<SortOrder> sortOrders
 
82
 
 
83
      This property holds a list of sort orders used by the contacts model.
 
84
      \sa SortOrder
 
85
    */
 
86
    property alias sortOrders: contactsModel.sortOrders
 
87
    /*!
 
88
      \qmlproperty FetchHint fetchHint
 
89
 
 
90
      This property holds the fetch hint instance used by the contact model.
 
91
 
 
92
      \sa FetchHint
 
93
    */
 
94
    property alias fetchHint: contactsModel.fetchHint
 
95
    /*!
 
96
      \qmlproperty Filter filter
 
97
 
 
98
      This property holds the filter instance used by the contact model.
 
99
 
 
100
      \sa Filter
 
101
    */
 
102
    property alias filter: contactsModel.filter
 
103
    /*!
 
104
      \qmlproperty bool loading
 
105
 
 
106
      This property holds when the model still loading new contacts
 
107
    */
 
108
    readonly property bool loading: busyIndicator.busy
 
109
    /*!
 
110
      This handler is called when any error occurs in the contact model
 
111
    */
 
112
    signal error(string message)
 
113
    /*!
 
114
      This handler is called when any contact int the list receives a click.
 
115
    */
 
116
    signal contactClicked(string contactId)
 
117
 
 
118
    function formatToDisplay(contact, contactDetail, detailFields) {
 
119
        if (!contact) {
 
120
            return ""
 
121
        }
 
122
 
 
123
        var detail = contact.detail(contactDetail)
 
124
        var values = ""
 
125
        for (var i=0; i < detailFields.length; i++) {
 
126
            if (i > 0 && detail) {
 
127
                values += " "
 
128
            }
 
129
            if (detail) {
 
130
                values +=  detail.value(detailFields[i])
 
131
            }
 
132
        }
 
133
 
 
134
        return values
 
135
    }
 
136
 
 
137
    clip: true
 
138
    snapMode: ListView.NoSnap
 
139
    section {
 
140
        property: "contact.name.firstName"
 
141
        criteria: ViewSection.FirstCharacter
 
142
        delegate: ListItem.Header {
 
143
            id: listHeader
 
144
            text: section
 
145
        }
 
146
    }
 
147
 
 
148
    anchors.fill: parent
 
149
    model: contactsModel
 
150
    onCountChanged: {
 
151
        busyIndicator.ping()
 
152
    }
 
153
 
 
154
    delegate: ListItem.Subtitled {
 
155
        icon: contactListView.showAvatar && contact && contact.avatar && (contact.avatar.imageUrl != "") ?
 
156
                  Qt.resolvedUrl(contact.avatar.imageUrl) :
 
157
                  "artwork:/avatar-default.png"
 
158
        text: contactListView.formatToDisplay(contact, contactListView.titleDetail, contactListView.titleFields)
 
159
        subText: contactListView.formatToDisplay(contact, contactListView.subTitleDetail, contactListView.subTitleFields)
 
160
 
 
161
        onClicked: {
 
162
            contactListView.currentIndex = index
 
163
            contactListView.contactClicked(contact.contactId)
 
164
        }
 
165
    }
 
166
 
 
167
    ContactModel {
 
168
        id: contactsModel
 
169
 
 
170
        manager: "galera"
 
171
        sortOrders: [
 
172
            SortOrder {
 
173
                id: sortOrder
 
174
 
 
175
                detail: ContactDetail.Name
 
176
                field: Name.FirstName
 
177
                direction: Qt.AscendingOrder
 
178
            }
 
179
        ]
 
180
 
 
181
        fetchHint: FetchHint {
 
182
            detailTypesHint: root.showAvatar ? [contactListView.titleDetail, contactListView.subTitleDetail, ContactDetail.Avatar] :
 
183
                                               [contactListView.titleDetail, contactListView.subTitleDetail]
 
184
        }
 
185
 
 
186
        onErrorChanged: {
 
187
            if (error) {
 
188
                busyIndicator.busy = false
 
189
                contactListView.error(error)
 
190
            }
 
191
        }
 
192
    }
 
193
 
 
194
    // This is a workaround to make sure the spinner will disappear if the model is empty
 
195
    // FIXME: implement a model property to say if the model still busy or not
 
196
    Item {
 
197
        id: busyIndicator
 
198
 
 
199
        property bool busy: false
 
200
 
 
201
        function ping()
 
202
        {
 
203
            timer.restart()
 
204
        }
 
205
 
 
206
        visible: busy
 
207
        anchors.fill: parent
 
208
 
 
209
        Timer {
 
210
            id: timer
 
211
 
 
212
            interval: 6000
 
213
            running: true
 
214
            repeat: false
 
215
            onTriggered: busyIndicator.busy = false
 
216
        }
 
217
    }
 
218
}