Skip to content

Commit

Permalink
Make it repaint after downloading the image
Browse files Browse the repository at this point in the history
  • Loading branch information
Mm2PL committed Dec 2, 2023
1 parent 6223e34 commit a56d6b0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
21 changes: 20 additions & 1 deletion src/messages/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <functional>
#include <queue>
#include <thread>
#include <utility>

// Duration between each check of every Image instance
const auto IMAGE_POOL_CLEANUP_INTERVAL = std::chrono::minutes(1);
Expand Down Expand Up @@ -423,7 +424,7 @@ bool Image::loaded() const
return this->frames_->current().has_value();
}

std::optional<QPixmap> Image::pixmapOrLoad() const
std::optional<QPixmap> Image::pixmapOrLoad(std::function<void()> cb)
{
assertInGuiThread();

Expand All @@ -432,6 +433,10 @@ std::optional<QPixmap> Image::pixmapOrLoad() const
// See src/messages/layouts/MessageLayoutElement.cpp ImageLayoutElement::paint, for example.
this->lastUsed_ = std::chrono::steady_clock::now();

if (cb != nullptr)
{
this->finishedLoadingCb_ = std::move(cb);
}
this->load();

return this->frames_->current();
Expand Down Expand Up @@ -574,6 +579,20 @@ void Image::actuallyLoad()

return true;
})
.finally([weak]() {
postToThread([weak]() {
auto shared = weak.lock();
if (!shared)
{
return;
}

if (shared->finishedLoadingCb_ != nullptr)
{
shared->finishedLoadingCb_();
}
});
})
.execute();
}

Expand Down
6 changes: 5 additions & 1 deletion src/messages/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "common/Aliases.hpp"
#include "common/Common.hpp"
#include "common/NetworkResult.hpp"

#include <boost/variant.hpp>
#include <pajlada/signals/signal.hpp>
Expand All @@ -13,6 +14,7 @@

#include <atomic>
#include <chrono>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -80,7 +82,7 @@ class Image : public std::enable_shared_from_this<Image>
const Url &url() const;
bool loaded() const;
// either returns the current pixmap, or triggers loading it (lazy loading)
std::optional<QPixmap> pixmapOrLoad() const;
std::optional<QPixmap> pixmapOrLoad(std::function<void()> cb = nullptr);
void load() const;
qreal scale() const;
bool isEmpty() const;
Expand Down Expand Up @@ -111,6 +113,8 @@ class Image : public std::enable_shared_from_this<Image>
// gui thread only
std::unique_ptr<detail::Frames> frames_{};

std::function<void()> finishedLoadingCb_;

friend class ImageExpirationPool;
};

Expand Down
26 changes: 24 additions & 2 deletions src/widgets/listview/ImagePtrItemDelegate.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
#pragma once

#include "common/QLogging.hpp"
#include "messages/Image.hpp"
#include "messages/ImageSet.hpp"

#include <QModelIndex>
#include <qnamespace.h>
#include <QPainter>
#include <QStyledItemDelegate>
#include <Qt>
#include <QTableView>

namespace chatterino {
class ImagePtrItemDelegate : public QStyledItemDelegate
{
std::map<QString, ImagePtr> ownedImages_;
QTableView *view_;

public:
ImagePtrItemDelegate(QTableView *view)
: view_(view)
{
}
static constexpr auto IMAGE_URL_ROLE = Qt::UserRole + 1;

void paint(QPainter *painter, const QStyleOptionViewItem &option,
Expand All @@ -27,6 +34,14 @@ class ImagePtrItemDelegate : public QStyledItemDelegate
auto opt = img->pixmapOrLoad();
if (!opt) // wait for next time
{
if (img->isEmpty())
{
painter->drawText(option.rect, "[Error]");
}
else
{
painter->drawText(option.rect, "Loading");
}
return;
}
auto pixmap = *opt;
Expand Down Expand Up @@ -56,7 +71,14 @@ class ImagePtrItemDelegate : public QStyledItemDelegate
}
auto img = Image::fromUrl(Url{url});

img->pixmapOrLoad();
img->pixmapOrLoad([this, index]() {
// wait for it to parse
QTimer::singleShot(100, [this, index]() {
this->view_->repaint();
this->view_->update(index);
});
});

// You cannot stop me, clang-tidy
auto *bleh = const_cast<ImagePtrItemDelegate *>(this);
bleh->ownedImages_[url] = img;
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/settingspages/ImageUploaderPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace chatterino {

ImageUploaderPage::ImageUploaderPage()
: imgDelegate_(new ImagePtrItemDelegate())
{
LayoutCreator<ImageUploaderPage> layoutCreator(this);
auto tabs = layoutCreator.emplace<QTabWidget>();
Expand All @@ -42,6 +41,7 @@ ImageUploaderPage::ImageUploaderPage()

auto *view = layout.emplace<QTableView>().getElement();
view->setModel(model);
this->imgDelegate_ = new ImagePtrItemDelegate(view);

view->setItemDelegateForColumn(0, this->imgDelegate_);
view->setSelectionMode(QAbstractItemView::SingleSelection);
Expand Down

0 comments on commit a56d6b0

Please sign in to comment.