qt: add cache property to RoundImage

This commit is contained in:
Pierre Lamot 2024-03-28 16:20:04 +01:00 committed by Steve Lhomme
parent d28d827465
commit 3144941432
3 changed files with 32 additions and 1 deletions

View File

@ -231,6 +231,11 @@ RoundImageRequest::~RoundImageRequest()
g_imageCache.removeRequest(m_key);
}
void RoundImageRequest::saveInCache()
{
m_saveInCache = true;
}
void RoundImageRequest::handleImageResponseFinished()
{
m_cancelOnDelete = false;
@ -255,7 +260,8 @@ void RoundImageRequest::handleImageResponseFinished()
image.setDevicePixelRatio(m_dpr);
g_imageCache.insert(m_key, new QImage(image), image.sizeInBytes());
if (m_saveInCache)
g_imageCache.insert(m_key, new QImage(image), image.sizeInBytes());
emit requestCompleted(RoundImage::Status::Ready, image);
}
@ -416,6 +422,11 @@ RoundImage::Status RoundImage::status() const
return m_status;
}
bool RoundImage::cache() const
{
return m_cache;
}
void RoundImage::setSource(const QUrl& source)
{
if (m_source == source)
@ -435,6 +446,14 @@ void RoundImage::setRadius(qreal radius)
emit radiusChanged(m_radius);
}
void RoundImage::setCache(bool cache)
{
if (m_cache == cache)
return;
m_cache = cache;
emit cacheChanged();
}
void RoundImage::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value)
{
if (change == QQuickItem::ItemDevicePixelRatioHasChanged)
@ -479,6 +498,9 @@ void RoundImage::load()
return;
}
if (m_cache)
m_activeImageResponse->saveInCache();
connect(m_activeImageResponse.get(), &RoundImageRequest::requestCompleted, this, &RoundImage::onRequestCompleted);
//at this point m_activeImageResponse is either in Loading or Error status
onRequestCompleted(RoundImage::Loading, {});

View File

@ -42,6 +42,8 @@ class RoundImage : public QQuickItem
Q_PROPERTY(Status status READ status NOTIFY statusChanged FINAL)
Q_PROPERTY(bool cache READ cache WRITE setCache NOTIFY cacheChanged FINAL)
public:
enum Status
{
@ -61,15 +63,18 @@ public:
QUrl source() const;
qreal radius() const;
Status status() const;
bool cache() const;
public slots:
void setSource(const QUrl& source);
void setRadius(qreal radius);
void setCache(bool cache);
signals:
void sourceChanged(const QUrl&);
void radiusChanged(qreal);
void statusChanged();
void cacheChanged();;
protected:
void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) override;
@ -96,6 +101,7 @@ private:
QUrl m_source;
qreal m_radius = 0.0;
qreal m_dpr = 1.0; // device pixel ratio
bool m_cache = true;
QImage m_roundImage;
std::shared_ptr<RoundImageRequest> m_activeImageResponse;

View File

@ -78,6 +78,8 @@ public:
void handleImageResponseFinished();
void saveInCache();
signals:
void requestCompleted(RoundImage::Status status, QImage image);
@ -89,6 +91,7 @@ private:
qreal m_dpr;
QQuickImageResponse* m_imageResponse = nullptr;
RoundImage::Status m_status = RoundImage::Status::Loading;
bool m_saveInCache = false;
};
/**