moment/src/gui/Base/HMxcImage.qml

68 lines
1.8 KiB
QML
Raw Normal View History

2019-12-19 22:46:16 +11:00
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12
import "../PythonBridge"
HImage {
id: image
inderterminateProgressBar: isMxc
source: sourceOverride || (show ? cachedPath : "")
onWidthChanged: Qt.callLater(update)
onHeightChanged: Qt.callLater(update)
onVisibleChanged: Qt.callLater(update)
onMxcChanged: Qt.callLater(update)
Component.onDestruction: if (getFuture) getFuture.cancel()
property string mxc
property string title
property string sourceOverride: ""
property bool thumbnail: true
property var cryptDict: ({})
property bool show: false
property string cachedPath: ""
property Future getFuture: null
2019-11-07 06:47:18 +11:00
readonly property bool isMxc: mxc.startsWith("mxc://")
function update() {
if (! py) return // component was destroyed
2020-03-08 19:46:20 +11:00
const w = sourceSize.width || width
const h = sourceSize.height || height
if (! image.mxc || w < 1 || h < 1 ) {
show = false
return
}
2019-11-07 06:47:18 +11:00
if (! isMxc) {
2019-12-10 02:35:50 +11:00
if (source !== mxc) source = mxc
2019-11-06 00:19:48 +11:00
show = image.visible
return
}
2020-03-08 19:46:20 +11:00
const method = image.thumbnail ? "get_thumbnail" : "get_media"
const args = image.thumbnail ?
[image.mxc, image.title, w, h, cryptDict] :
[image.mxc, image.title, cryptDict]
getFuture = py.callCoro("media_cache." + method, args, path => {
if (! image) return
2019-12-10 02:35:50 +11:00
if (image.cachedPath !== path) image.cachedPath = path
image.broken = false
image.show = image.visible
}, (type, args, error, traceback) => {
print(`Error retrieving ${mxc} (${title}): ${type} ${args}`)
if (image) image.broken = true
},
)
}
}