moment/harmonyqml/components/base/Avatar.qml

54 lines
1.5 KiB
QML
Raw Normal View History

2019-03-22 14:28:14 +11:00
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.4
2019-04-29 01:01:38 +10:00
import "../base" as Base
2019-03-22 14:28:14 +11:00
Item {
property bool invisible: false
property var name: null // null, string or PyQtFuture
2019-03-22 14:28:14 +11:00
property var imageSource: null
property int dimmension: 48
readonly property string resolvedName:
! name ? "?" :
typeof(name) == "string" ? name :
(name.value ? name.value : "?")
id: root
2019-03-22 14:28:14 +11:00
width: dimmension
height: invisible ? 1 : dimmension
Rectangle {
id: letterRectangle
2019-03-22 14:28:14 +11:00
anchors.fill: parent
visible: ! invisible && imageSource === null
color: resolvedName === "?" ?
2019-04-29 01:01:38 +10:00
Base.HStyle.avatar.background.unknown :
Qt.hsla(
Backend.hueFromString(resolvedName),
Base.HStyle.avatar.background.saturation,
Base.HStyle.avatar.background.lightness,
Base.HStyle.avatar.background.alpha
)
2019-03-22 14:28:14 +11:00
HLabel {
2019-03-22 14:28:14 +11:00
anchors.centerIn: parent
text: resolvedName.charAt(0)
2019-04-29 01:01:38 +10:00
color: Base.HStyle.avatar.letter
2019-03-22 14:28:14 +11:00
font.pixelSize: letterRectangle.height / 1.4
}
}
Image {
id: avatarImage
2019-03-22 14:28:14 +11:00
anchors.fill: parent
visible: ! invisible && imageSource !== null
Component.onCompleted: if (imageSource) {source = imageSource}
asynchronous: true
mipmap: true
fillMode: Image.PreserveAspectCrop
sourceSize.width: root.dimmension
}
}