From 6274c05520736570aecb9d2ce955ae63eacc3542 Mon Sep 17 00:00:00 2001 From: fourtf Date: Tue, 13 Aug 2019 13:42:38 +0200 Subject: [PATCH] Fixed always loading 1x, 2x and 3x images --- src/messages/Image.cpp | 9 ++++++++- src/messages/Image.hpp | 4 +++- src/messages/ImageSet.cpp | 10 +++++----- src/messages/layouts/MessageLayoutElement.cpp | 4 ++-- src/singletons/TooltipPreviewImage.cpp | 2 +- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/messages/Image.cpp b/src/messages/Image.cpp index 8be4bd930..2bf4b68f1 100644 --- a/src/messages/Image.cpp +++ b/src/messages/Image.cpp @@ -275,7 +275,14 @@ const Url &Image::url() const return this->url_; } -boost::optional Image::pixmap() const +bool Image::loaded() const +{ + assertInGuiThread(); + + return bool(this->frames_->current()); +} + +boost::optional Image::pixmapOrLoad() const { assertInGuiThread(); diff --git a/src/messages/Image.hpp b/src/messages/Image.hpp index 8db607e42..e7f3f4ebd 100644 --- a/src/messages/Image.hpp +++ b/src/messages/Image.hpp @@ -53,7 +53,9 @@ public: static ImagePtr getEmpty(); const Url &url() const; - boost::optional pixmap() const; + bool loaded() const; + // either returns the current pixmap, or triggers loading it (lazy loading) + boost::optional pixmapOrLoad() const; qreal scale() const; bool isEmpty() const; int width() const; diff --git a/src/messages/ImageSet.cpp b/src/messages/ImageSet.cpp index 222f12203..cabaa6a00 100644 --- a/src/messages/ImageSet.cpp +++ b/src/messages/ImageSet.cpp @@ -84,16 +84,16 @@ const ImagePtr &ImageSet::getImage(float scale) const return this->imageX1_; }(); - // prefere other image if selected image is not loaded yet - if (result->pixmap()) + // prefer other image if selected image is not loaded yet + if (result->loaded()) return result; - else if (this->imageX1_->pixmap()) + else if (this->imageX1_->loaded()) return this->imageX1_; else if (this->imageX2_ && !this->imageX2_->isEmpty() && - this->imageX2_->pixmap()) + this->imageX2_->loaded()) return this->imageX2_; else if (this->imageX3_ && !this->imageX3_->isEmpty() && - this->imageX3_->pixmap()) + this->imageX3_->loaded()) return this->imageX3_; else return result; diff --git a/src/messages/layouts/MessageLayoutElement.cpp b/src/messages/layouts/MessageLayoutElement.cpp index a8bcc30c9..53095ccbf 100644 --- a/src/messages/layouts/MessageLayoutElement.cpp +++ b/src/messages/layouts/MessageLayoutElement.cpp @@ -120,7 +120,7 @@ void ImageLayoutElement::paint(QPainter &painter) return; } - auto pixmap = this->image_->pixmap(); + auto pixmap = this->image_->pixmapOrLoad(); if (pixmap && !this->image_->animated()) { // fourtf: make it use qreal values @@ -137,7 +137,7 @@ void ImageLayoutElement::paintAnimated(QPainter &painter, int yOffset) if (this->image_->animated()) { - if (auto pixmap = this->image_->pixmap()) + if (auto pixmap = this->image_->pixmapOrLoad()) { auto rect = this->getRect(); rect.moveTop(rect.y() + yOffset); diff --git a/src/singletons/TooltipPreviewImage.cpp b/src/singletons/TooltipPreviewImage.cpp index 99cd9548b..847cc3e6e 100644 --- a/src/singletons/TooltipPreviewImage.cpp +++ b/src/singletons/TooltipPreviewImage.cpp @@ -17,7 +17,7 @@ TooltipPreviewImage::TooltipPreviewImage() auto tooltipWidget = TooltipWidget::getInstance(); if (this->image_ && !tooltipWidget->isHidden()) { - auto pixmap = this->image_->pixmap(); + auto pixmap = this->image_->pixmapOrLoad(); if (pixmap) { tooltipWidget->setImage(*pixmap);