C

Qt Quick Ultralite image_loading Example

/****************************************************************************** ** ** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Ultralite module. ** ** $QT_BEGIN_LICENSE:COMM$ ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** $QT_END_LICENSE$ ** ******************************************************************************/
#include "myimageloader.h" #include <qul/application.h> #include <platforminterface/allocator.h> Qul::SharedImage MyImageProvider::requestImage(const char *imageName, size_t imageNameLength) { if (imageNameLength < 8) return {}; // Parse the image name to determine the colors to use for the image const int colors[3] = {imageName[5] - '0', imageName[6] - '0', imageName[7] - '0'}; // Create the image and mark it as being-written-to Qul::Image image(120, 120, Qul::PixelFormat_RGB16); Qul::SharedImage sharedImage(image); image.beginWrite(); // Start the image loading process. // // Here, we start a timer that fills the image data step by step. // When multithreading is available, this could start a real asynchronous task. // // When done, post an event to trigger endWrite(). // (postEvent() is safe to call asynchronously, while endWrite() is not) int loadSteps = 3; int currentLoadStep = 0; m_asyncLoadTimer.setInterval(300); m_asyncLoadTimer.onTimeout([image = std::move(image), loadSteps, currentLoadStep, this, colors]() mutable { // Fill a segment of the image with a solid color auto pixelBegin = reinterpret_cast<uint16_t *>( image.bits() + image.bytesPerLine() * image.height() * currentLoadStep / loadSteps); auto pixelEnd = reinterpret_cast<uint16_t *>( image.bits() + image.bytesPerLine() * image.height() * (currentLoadStep + 1) / loadSteps); const uint32_t color = (0xFF / 2 * colors[currentLoadStep]) << (currentLoadStep * 8); const uint16_t colorRGB16 = ((color >> 3) & 0x001f) | ((color >> 5) & 0x07e0) | ((color >> 8) & 0xf800); std::fill(pixelBegin, pixelEnd, colorRGB16); currentLoadStep++; if (currentLoadStep == loadSteps) { m_asyncLoadTimer.stop(); postEvent(Qul::PlatformInterface::qul_new<MyImageLoadedEvent>(image)); } }); m_asyncLoadTimer.start(); // Return the unfinished image. Since beginWrite() was called on it, nothing // will be drawn to the screen yet. return sharedImage; } void MyImageProvider::onEvent(MyImageLoadedEvent *const &event) { // Causes the finished image to become visible. event->sharedImage.image()->endWrite(); Qul::PlatformInterface::qul_delete(event); } void MyImageProvider::onEventDiscarded(MyImageLoadedEvent *const &event) { // Causes the finished image to become visible. event->sharedImage.image()->endWrite(); Qul::PlatformInterface::qul_delete(event); }