C

Qt Quick Ultralite image_cache Example

// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial
import QtQuick 2.15

Rectangle {
    color: "black"
    property int reloadInSeconds: 15
    property int reloadInMs: 1000 * reloadInSeconds
    property int showCountDownSeconds: 3
    property int showCountDownMs: 1000 * showCountDownSeconds
    property int remainingSeconds: 0
    property int showDescriptionMs: 1000 * 5

    ListView {
        anchors.fill: parent
        orientation: Qt.Horizontal
        model: ImageModel { id: myImageModel }
        spacing: 4
        delegate: Image {
            anchors.fill: parent
            fillMode: Image.Pad
            source: model.imagePath
        }
    }

    Rectangle {
        id: infoBox
        color: "white"
        width: parent.width
        height: infoBoxText.height + 4
        anchors.verticalCenter: parent.verticalCenter
        visible: false

        Text {
            id: infoBoxText
            anchors.centerIn: parent
        }
    }

    Timer {
        id: reloadImagesTimer
        interval: reloadInMs - showCountDownMs
        // Start the timer when the application starts.
        running: true
        onTriggered: {
            // We unfortunately do not have Timer::triggeredOnStart
            // in qul at the moment, therefore we init the values here.
            remainingSeconds = showCountDownSeconds
            infoBoxText.text = "Will reload in " + remainingSeconds + "..."
            infoBox.visible = true
            countDownTextVisible.start()
        }
    }

    Timer {
        id: countDownTextVisible
        interval: 1000
        // Repeat every second, so we can update the countdown text.
        repeat: true
        onTriggered: {
            remainingSeconds -= 1
            infoBoxText.text = "Will reload in " + remainingSeconds + "..."
            if (remainingSeconds == 0) {
                infoBox.visible = false;
                myImageModel.swapImages();
                countDownTextVisible.stop()
                reloadImagesTimer.start()
            }
        }
    }

    Timer {
        id: infoBoxHideTimer
        onTriggered: infoBox.visible = false
    }

    Component.onCompleted: {
        infoBoxText.text = "Swipe to view photos.\nContent reloads every " + reloadInSeconds + " seconds."
        infoBoxHideTimer.interval = showDescriptionMs
        infoBox.visible = true
        infoBoxHideTimer.start()
    }
}