Kirigami-addons

Avatar.qml
1/*
2 * SPDX-FileCopyrightText: 2020 Carson Black <uhhadd@gmail.com>
3 * SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8import QtQuick 2.15
9import QtQuick.Window 2.15
10import org.kde.kirigami 2.15 as Kirigami
11import org.kde.kirigamiaddons.components 1.0 as Components
12
13/**
14 * @brief An element that represents a user, either with initials, an icon, or a profile image.
15 * @inherit QtQuick.Item
16 */
17Item {
18 id: root
19
20 enum ImageMode {
21 AlwaysShowImage,
22 AdaptiveImageOrInitals,
23 AlwaysShowInitials
24 }
25
26 enum InitialsMode {
27 UseInitials,
28 UseIcon
29 }
30
31//BEGIN properties
32 /**
33 * @brief This property holds the given name of a user.
34 *
35 * The user's name will be used for generating initials and to provide the
36 * accessible name for assistive technology.
37 */
38 property string name
40 /**
41 * @brief This property holds the source of the user's profile picture; an image.
42 * @see QtQuick.Image::source
43 * @property url source
44 */
45 property alias source: avatarImage.source
46
47 /**
48 * @brief This property holds avatar's icon source.
49 *
50 * This icon is displayed when using an icon with ``Avatar.InitialsMode.UseIcon`` and
51 * ``Avatar.ImageNode.AlwaysShowInitials`` enabled.
52 *
53 * @see org::kde::kirigami::Icon::source
54 * @property var iconSource
55 */
56 property alias iconSource: avatarIcon.source
57
58 /**
59 * @brief This property holds how the button should represent the user when no user-set image is available.
60 *
61 * Possible values are:
62 * * ``Avatar.InitialsMode.UseInitials``: Show the user's initials.
63 * * ``Avatar.InitialsMode.UseIcon``: Show a generic icon.
64 *
65 * @see org::kde::kirigamiaddons::components::Avatar::InitialsMode
66 */
67 property int initialsMode: Avatar.InitialsMode.UseInitials
68
69 /**
70 * @brief This property holds how the avatar should be shown.
71 *
72 * This property holds whether the button should always show the image; show the image if one is
73 * available and show initials when it is not; or always show initials.
74 *
75 * Possible values are:
76 * * ``Avatar.ImageMode.AlwaysShowImage``: Always try to show the image; even if it hasn't loaded yet or is undefined.
77 * * ``Avatar.ImageMode.AdaptiveImageOrInitals``: Show the image if it is valid; or show initials if it is not
78 * * ``Avatar.ImageMode.AlwaysShowInitials``: Always show initials
79 *
80 * @see org::kde::kirigamiaddons::components::Avatar::ImageMode
81 */
82 property int imageMode: Avatar.ImageMode.AdaptiveImageOrInitals
83
84 /**
85 * @brief This property sets whether the provided image should be cached.
86 * @see QtQuick.Image::cache
87 * @property bool cache
88 */
89 property alias cache: avatarImage.cache
90
91 /**
92 * @brief Load the image asynchronously.
93 * @see QtQuick.Image::asynchronous
94 * @property bool asynchronous
95 * @since 1.7.0
96 */
97 property alias asynchronous: avatarImage.asynchronous
98
99 /**
100 * @brief This property holds the source size of the user's profile picture.
101 * @see QtQuick.Image::sourceSize
102 * @property int sourceSize
103 */
104 property alias sourceSize: avatarImage.sourceSize
105
106 /**
107 * @brief This property holds the color to use for this avatar.
108 *
109 * If not explicitly set, this defaults to generating a color from the name.
110 *
111 * @property color color
112 */
113 property color color: Components.NameUtils.colorsFromString(name)
114
115 /**
116 * @brief This property holds the color of the avatar's initials.
117 *
118 * If not explicitly set, this defaults to defaultInitialsColor.
119 *
120 * @see defaultInitialsColor
121 * @property color initialsColor
122 */
123 property color initialsColor: defaultInitialsColor
124
125 /**
126 * @brief This property holds the default color of the avatar's initials.
127 *
128 * It depends on the avatar's color.
129 *
130 * @property color defaultInitialsColor
131 */
132 readonly property alias defaultInitialsColor: root.__textColor
133
134 /**
135 * @brief This item holds the parent item on the clipped circle.
136 *
137 * Implementations may add custom graphics which will be clipped along with
138 * the rest of the avatar content.
139 */
140 readonly property alias clippedContent: clippedContent
141
142 implicitWidth: Kirigami.Units.iconSizes.large
143 implicitHeight: Kirigami.Units.iconSizes.large
144
145 Accessible.role: Accessible.Graphic
146 Accessible.name: name
147
148 readonly property real __diameter: Math.min(root.width, root.height)
149
150 readonly property color __textColor: Kirigami.ColorUtils.brightnessForColor(root.color) === Kirigami.ColorUtils.Light
151 ? "black"
152 : "white"
153
154 readonly property bool __showImage: {
155 switch (root.imageMode) {
156 case Avatar.ImageMode.AlwaysShowImage:
157 return true;
158 case Avatar.ImageMode.AdaptiveImageOrInitals:
159 return avatarImage.status === Image.Ready;
160 case Avatar.ImageMode.AlwaysShowInitials:
161 default:
162 return false;
163 }
164 }
165
166 readonly property bool __unsuitableForInitials: Components.NameUtils.isStringUnsuitableForInitials(root.name)
167
168 Item {
169 id: clippedContent
170
171 anchors.centerIn: parent
172
173 width: root.__diameter
174 height: root.__diameter
175
176 Text {
177 anchors.fill: parent
178
179 visible: root.initialsMode === Avatar.InitialsMode.UseInitials &&
180 !root.__showImage &&
181 !root.__unsuitableForInitials &&
182 root.width > Kirigami.Units.gridUnit
183
184 text: Components.NameUtils.initialsFromString(root.name)
185 textFormat: Text.PlainText
186 color: root.color
187
188 font {
189 // this ensures we don't get a both point and pixel size are set warning
190 pointSize: -1
191 pixelSize: Math.round((root.height - Kirigami.Units.largeSpacing) / 2)
192 }
193 fontSizeMode: Text.Fit
194 verticalAlignment: Text.AlignVCenter
195 horizontalAlignment: Text.AlignHCenter
196 }
197
198 Kirigami.Icon {
199 id: avatarIcon
200
201 anchors.fill: parent
202 anchors.margins: Kirigami.Units.largeSpacing
203
204 visible: !root.__showImage
205 && (root.initialsMode === Avatar.InitialsMode.UseIcon
206 || root.__unsuitableForInitials)
207
208 color: root.__textColor
209 source: "user"
210 }
211
212 Image {
213 id: avatarImage
214
215 anchors.fill: parent
216
217 visible: root.__showImage
218
219 fillMode: Image.PreserveAspectCrop
220 mipmap: true
221 sourceSize {
222 width: root.__diameter * root.Screen.devicePixelRatio
223 height: root.__diameter * root.Screen.devicePixelRatio
224 }
225 }
226
227 layer {
228 enabled: true
229 effect: Kirigami.ShadowedTexture {
230 radius: root.__diameter
231
232 border {
233 width: root.__showImage ? 0 : 1.25
234 color: root.color
235 }
236
237 color: Kirigami.ColorUtils.tintWithAlpha(Kirigami.Theme.backgroundColor, border.color, 0.07)
238 }
239 }
240 }
241}
An element that represents a user, either with initials, an icon, or a profile image.
Definition Avatar.qml:15
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.