mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
Make it repaint after downloading the image
This commit is contained in:
parent
6223e3461e
commit
a56d6b084c
|
@ -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);
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
@ -13,6 +14,7 @@
|
|||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
@ -80,7 +82,7 @@ public:
|
|||
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;
|
||||
|
@ -111,6 +113,8 @@ private:
|
|||
// gui thread only
|
||||
std::unique_ptr<detail::Frames> frames_{};
|
||||
|
||||
std::function<void()> finishedLoadingCb_;
|
||||
|
||||
friend class ImageExpirationPool;
|
||||
};
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
@ -27,6 +34,14 @@ public:
|
|||
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;
|
||||
|
@ -56,7 +71,14 @@ public:
|
|||
}
|
||||
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;
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
namespace chatterino {
|
||||
|
||||
ImageUploaderPage::ImageUploaderPage()
|
||||
: imgDelegate_(new ImagePtrItemDelegate())
|
||||
{
|
||||
LayoutCreator<ImageUploaderPage> layoutCreator(this);
|
||||
auto tabs = layoutCreator.emplace<QTabWidget>();
|
||||
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue