Debian Codegraph @

src/imports/Lomiri/Contacts/ContactAvatar.qml

raw
1 /*
2 * Copyright (C) 2014 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.12
18 import QtContacts 5.0
19 import Lomiri.Components 1.3
20 import Lomiri.Contacts 0.1
21 import "Contacts.js" as ContactsJS
22
23 LomiriShape {
24 id: avatar
25
26 property var contactElement: null
27 property string fallbackAvatarUrl: "image://theme/stock_contact"
28 property string fallbackDisplayName: ""
29 property bool showAvatarPicture: (avatarUrl != fallbackAvatarUrl) || (initials.length === 0)
30
31 readonly property alias initials: initialsLabel.text
32 readonly property alias displayName: initialsLabel.contactDisplayName
33 readonly property alias avatarUrl: img.avatarUrl
34
35 // this is necessary because the object does not monitor changes on avatarDetail object this will be very expesive and only happens in few cases,
36 // this need to be called manually on these cases
37 function reload()
38 {
39 img.avatarUrl = Qt.binding(function() { return ContactsJS.getAvatar(contactElement, fallbackAvatarUrl) })
40 initialsLabel.contactDisplayName = Qt.binding(function() { return ContactsJS.formatToDisplayWithDetails(contactElement, ContactDetail.Name, [Name.FirstName, Name.MiddleName, Name.LastName], fallbackDisplayName) })
41 }
42
43 aspect: LomiriShape.Flat
44 radius: "medium"
45 backgroundColor: initialsLabel.visible || fallbackIcon.visible ? ContactsJS.contactColor(displayName) : "transparent"
46
47 Label {
48 id: initialsLabel
49 objectName: "avatarInitials"
50
51 property string contactDisplayName: ContactsJS.formatToDisplayWithDetails(contactElement, ContactDetail.Name, [Name.FirstName, Name.MiddleName, Name.LastName], fallbackDisplayName)
52
53 anchors.centerIn: parent
54 text: Contacts.contactInitialsFromString(contactDisplayName)
55 color: "white"
56 visible: (img.status == Image.Error || img.status == Image.Null) && !fallbackIcon.visible
57 fontSize: "large"
58 }
59
60 source: !img.visible ? img : null
61 sourceFillMode: LomiriShape.PreserveAspectCrop
62
63 Image {
64 id: img
65 objectName: "avatarImage"
66
67 property string avatarUrl: ContactsJS.getAvatar(contactElement, fallbackAvatarUrl)
68
69 asynchronous: true
70 source: avatar.showAvatarPicture && (avatar.avatarUrl.indexOf("image://theme/") === -1) ? avatar.avatarUrl : ""
71 height: avatar.height
72 width: height
73 visible: false
74 sourceSize.width: avatar.width
75 sourceSize.height: avatar.height
76 autoTransform: true
77 }
78
79 Icon {
80 id: fallbackIcon
81 objectName: "fallbackIcon"
82
83 source: visible ? img.avatarUrl : ""
84 visible: avatar.showAvatarPicture && (avatar.avatarUrl.indexOf("image://theme/") === 0)
85 color: "white"
86 anchors.centerIn: avatar
87 height: units.gu(3)
88 width: height
89 asynchronous: true
90 }
91 }