Warning, /libraries/kirigami-addons/src/components/Avatar.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Carson Black <uhhadd@gmail.com>
0003  *  SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 import QtQuick 2.15
0009 import QtQuick.Window 2.15
0010 import org.kde.kirigami 2.15 as Kirigami
0011 import org.kde.kirigamiaddons.components 1.0 as Components
0012 
0013 /**
0014  * @brief An element that represents a user, either with initials, an icon, or a profile image.
0015  * @inherit QtQuick.Item
0016  */
0017 Item {
0018     id: root
0019 
0020     enum ImageMode {
0021         AlwaysShowImage,
0022         AdaptiveImageOrInitals,
0023         AlwaysShowInitials
0024     }
0025 
0026     enum InitialsMode {
0027         UseInitials,
0028         UseIcon
0029     }
0030 
0031 //BEGIN properties
0032     /**
0033      * @brief This property holds the given name of a user.
0034      *
0035      * The user's name will be used for generating initials and to provide the
0036      * accessible name for assistive technology.
0037      */
0038     property string name
0039 
0040     /**
0041      * @brief This property holds the source of the user's profile picture; an image.
0042      * @see QtQuick.Image::source
0043      * @property url source
0044      */
0045     property alias source: avatarImage.source
0046 
0047     /**
0048      * @brief This property holds avatar's icon source.
0049      *
0050      * This icon  is displayed when using an icon with ``Avatar.InitialsMode.UseIcon`` and
0051      * ``Avatar.ImageNode.AlwaysShowInitials`` enabled.
0052      *
0053      * @see org::kde::kirigami::Icon::source
0054      * @property var iconSource
0055      */
0056     property alias iconSource: avatarIcon.source
0057 
0058     /**
0059      * @brief This property holds how the button should represent the user when no user-set image is available.
0060      *
0061      * Possible values are:
0062      * * ``Avatar.InitialsMode.UseInitials``: Show the user's initials.
0063      * * ``Avatar.InitialsMode.UseIcon``: Show a generic icon.
0064      *
0065      * @see org::kde::kirigamiaddons::components::Avatar::InitialsMode
0066      */
0067     property int initialsMode: Avatar.InitialsMode.UseInitials
0068 
0069     /**
0070      * @brief This property holds how the avatar should be shown.
0071      *
0072      * This property holds whether the button should always show the image; show the image if one is
0073      * available and show initials when it is not; or always show initials.
0074      *
0075      * Possible values are:
0076      * * ``Avatar.ImageMode.AlwaysShowImage``: Always try to show the image; even if it hasn't loaded yet or is undefined.
0077      * * ``Avatar.ImageMode.AdaptiveImageOrInitals``: Show the image if it is valid; or show initials if it is not
0078      * * ``Avatar.ImageMode.AlwaysShowInitials``: Always show initials
0079      *
0080      * @see org::kde::kirigamiaddons::components::Avatar::ImageMode
0081      */
0082     property int imageMode: Avatar.ImageMode.AdaptiveImageOrInitals
0083 
0084     /**
0085      * @brief This property sets whether the provided image should be cached.
0086      * @see QtQuick.Image::cache
0087      * @property bool cache
0088      */
0089     property alias cache: avatarImage.cache
0090 
0091     /**
0092      * @brief This property holds the source size of the user's profile picture.
0093      * @see QtQuick.Image::sourceSize
0094      * @property int sourceSize
0095      */
0096     property alias sourceSize: avatarImage.sourceSize
0097 
0098     /**
0099      * @brief This property holds the color to use for this avatar.
0100      *
0101      * If not explicitly set, this defaults to generating a color from the name.
0102      *
0103      * @property color color
0104      */
0105     property color color: Components.NameUtils.colorsFromString(name)
0106 
0107     /**
0108      * @brief This property holds the color of the avatar's initials.
0109      *
0110      * If not explicitly set, this defaults to defaultInitialsColor.
0111      *
0112      * @see defaultInitialsColor
0113      * @property color initialsColor
0114      */
0115     property color initialsColor: defaultInitialsColor
0116 
0117     /**
0118      * @brief This property holds the default color of the avatar's initials.
0119      *
0120      * It depends on the avatar's color.
0121      *
0122      * @property color defaultInitialsColor
0123      */
0124     readonly property alias defaultInitialsColor: root.__textColor
0125 
0126     /**
0127      * @brief This item holds the parent item on the clipped circle.
0128      *
0129      * Implementations may add custom graphics which will be clipped along with
0130      * the rest of the avatar content.
0131      */
0132     readonly property alias clippedContent: clippedContent
0133 
0134     implicitWidth: Kirigami.Units.iconSizes.large
0135     implicitHeight: Kirigami.Units.iconSizes.large
0136 
0137     Accessible.role: Accessible.Graphic
0138     Accessible.name: name
0139 
0140     readonly property real __diameter: Math.min(root.width, root.height)
0141 
0142     readonly property color __textColor: Kirigami.ColorUtils.brightnessForColor(root.color) === Kirigami.ColorUtils.Light
0143                             ? "black"
0144                             : "white"
0145 
0146     readonly property bool __showImage: {
0147         switch (root.imageMode) {
0148         case Avatar.ImageMode.AlwaysShowImage:
0149             return true;
0150         case Avatar.ImageMode.AdaptiveImageOrInitals:
0151             return avatarImage.status === Image.Ready;
0152         case Avatar.ImageMode.AlwaysShowInitials:
0153         default:
0154             return false;
0155         }
0156     }
0157 
0158     Item {
0159         id: clippedContent
0160 
0161         anchors.centerIn: parent
0162 
0163         width: root.__diameter
0164         height: root.__diameter
0165 
0166         Text {
0167             anchors.fill: parent
0168 
0169             visible: root.initialsMode === Avatar.InitialsMode.UseInitials &&
0170                     !root.__showImage &&
0171                     !Components.NameUtils.isStringUnsuitableForInitials(root.name) &&
0172                     root.width > Kirigami.Units.gridUnit
0173 
0174             text: Components.NameUtils.initialsFromString(root.name)
0175             color: root.color
0176 
0177             font {
0178                 // this ensures we don't get a both point and pixel size are set warning
0179                 pointSize: -1
0180                 pixelSize: Math.round((root.height - Kirigami.Units.largeSpacing) / 2)
0181             }
0182             fontSizeMode: Text.Fit
0183             verticalAlignment: Text.AlignVCenter
0184             horizontalAlignment: Text.AlignHCenter
0185         }
0186 
0187         Kirigami.Icon {
0188             id: avatarIcon
0189 
0190             anchors.fill: parent
0191             anchors.margins: Kirigami.Units.largeSpacing
0192 
0193             visible: !root.__showImage
0194                 && (root.initialsMode === Avatar.InitialsMode.UseIcon
0195                     || Components.NameUtils.isStringUnsuitableForInitials(root.name))
0196 
0197             color: root.__textColor
0198             source: "user"
0199         }
0200 
0201         Image {
0202             id: avatarImage
0203 
0204             anchors.fill: parent
0205 
0206             visible: root.__showImage
0207 
0208             fillMode: Image.PreserveAspectCrop
0209             mipmap: true
0210             sourceSize {
0211                 width: root.__diameter * root.Screen.devicePixelRatio
0212                 height: root.__diameter * root.Screen.devicePixelRatio
0213             }
0214         }
0215 
0216         layer {
0217             enabled: true
0218             effect: Kirigami.ShadowedTexture {
0219                 radius: root.__diameter
0220 
0221                 border {
0222                     width: root.__showImage ? 0 : 1.25
0223                     color: root.color
0224                 }
0225 
0226                 color: Kirigami.ColorUtils.tintWithAlpha(Kirigami.Theme.backgroundColor, border.color, 0.07)
0227             }
0228         }
0229     }
0230 }