moment/src/qml/Base/HMxcImage.qml

68 lines
1.7 KiB
QML
Raw Normal View History

import QtQuick 2.12
HImage {
id: image
2019-11-07 06:47:18 +11:00
progressBar.indeterminate: isMxc
source: sourceOverride || (show ? cachedPath : "")
onWidthChanged: Qt.callLater(update)
onHeightChanged: Qt.callLater(update)
onVisibleChanged: Qt.callLater(update)
2019-11-05 01:46:06 +11:00
onMxcChanged: {
Qt.callLater(update)
2019-11-07 06:47:18 +11:00
if (isMxc) {
py.callCoro("mxc_to_http", [mxc], http => {
image.httpUrl = http || ""
})
2019-11-05 01:46:06 +11:00
} else {
httpUrl = mxc
}
}
property string mxc
property string sourceOverride: ""
property bool thumbnail: true
property var cryptDict: ({})
property bool show: false
property string cachedPath: ""
2019-11-05 01:46:06 +11:00
property string httpUrl: ""
2019-11-07 06:47:18 +11:00
readonly property bool isMxc: mxc.startsWith("mxc://")
function update() {
let w = sourceSize.width || width
let h = sourceSize.height || height
if (! image.mxc || w < 1 || h < 1 ) {
show = false
return
}
if (! image) return // if it was destroyed
2019-11-07 06:47:18 +11:00
if (! isMxc) {
2019-11-06 00:19:48 +11:00
if (source != mxc) source = mxc
show = image.visible
return
}
let method = image.thumbnail ? "get_thumbnail" : "get_media"
let args = image.thumbnail ?
[image.mxc, w, h, cryptDict] : [image.mxc, cryptDict]
py.callCoro("media_cache." + method, args, path => {
if (! image) return
2019-11-06 00:19:48 +11:00
if (image.cachedPath != path) image.cachedPath = path
image.broken = false
image.show = image.visible
}, () => {
image.broken = true
},
)
}
}