Spectral/qml/component/ImageStatus.qml

83 lines
2.2 KiB
QML
Raw Normal View History

2018-07-12 01:44:41 +00:00
import QtQuick 2.9
import QtQuick.Controls 2.2
2018-02-23 14:39:14 +00:00
import QtGraphicalEffects 1.0
2018-07-12 01:44:41 +00:00
import QtQuick.Controls.Material 2.2
2018-02-23 14:39:14 +00:00
2018-03-04 12:05:09 +00:00
Item {
2018-03-04 14:40:48 +00:00
property bool opaqueBackground: false
2018-07-09 02:45:26 +00:00
property bool round: true
property string source: ""
property string displayText: ""
readonly property bool showImage: source
readonly property bool showInitial: !showImage && displayText
2018-03-02 15:05:32 +00:00
id: item
2018-03-04 12:05:09 +00:00
Rectangle {
width: item.width
height: item.width
2018-07-09 02:45:26 +00:00
radius: round ? item.width / 2 : 0
2018-03-04 12:05:09 +00:00
color: "white"
2018-03-04 14:40:48 +00:00
visible: opaqueBackground
2018-03-04 12:05:09 +00:00
}
Image {
id: avatar
width: item.width
height: item.width
2018-07-09 02:45:26 +00:00
visible: showImage
source: item.source
2018-02-23 14:39:14 +00:00
2018-03-04 12:05:09 +00:00
mipmap: true
layer.enabled: true
fillMode: Image.PreserveAspectCrop
sourceSize.width: item.width
2018-02-23 14:39:14 +00:00
2018-03-04 12:05:09 +00:00
layer.effect: OpacityMask {
maskSource: Item {
2018-02-23 14:39:14 +00:00
width: avatar.width
height: avatar.width
2018-03-04 12:05:09 +00:00
Rectangle {
anchors.centerIn: parent
width: avatar.width
height: avatar.width
2018-07-09 02:45:26 +00:00
radius: round? avatar.width / 2 : 0
2018-03-04 12:05:09 +00:00
}
2018-02-23 14:39:14 +00:00
}
}
}
2018-07-09 02:45:26 +00:00
Label {
anchors.fill: parent
color: "white"
visible: showInitial
text: showInitial ? getInitials(displayText)[0] : ""
font.pixelSize: 22
font.bold: true
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
background: Rectangle {
anchors.fill: parent
radius: round? width / 2 : 0
color: showInitial ? stringToColor(displayText) : Material.accent
}
}
function getInitials(text) {
return text.toUpperCase().replace(/[^a-zA-Z- ]/g, "").match(/\b\w/g);
}
function stringToColor(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
var colour = '#';
for (var j = 0; j < 3; j++) {
var value = (hash >> (j * 8)) & 0xFF;
2018-07-09 02:45:26 +00:00
colour += ('00' + value.toString(16)).substr(-2);
}
return colour;
}
2018-02-23 14:39:14 +00:00
}