Add custom image functionality for inline mod buttons. (#5369)

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
Mm2PL
2024-05-11 12:54:27 +02:00
committed by GitHub
parent 321d881bfe
commit c3b84cb4b6
15 changed files with 396 additions and 89 deletions
+48
View File
@@ -0,0 +1,48 @@
#include "util/LoadPixmap.hpp"
#include "common/network/NetworkRequest.hpp"
#include "common/network/NetworkResult.hpp"
#include "common/QLogging.hpp"
#include <QBuffer>
#include <QImageReader>
#include <QLoggingCategory>
#include <QPixmap>
namespace chatterino {
void loadPixmapFromUrl(const Url &url, std::function<void(QPixmap)> &&callback)
{
NetworkRequest(url.string)
.concurrent()
.cache()
.onSuccess(
[callback = std::move(callback), url](const NetworkResult &result) {
auto data = result.getData();
QBuffer buffer(&data);
buffer.open(QIODevice::ReadOnly);
QImageReader reader(&buffer);
if (!reader.canRead() || reader.size().isEmpty())
{
qCWarning(chatterinoImage)
<< "Can't read image file at" << url.string << ":"
<< reader.errorString();
return;
}
QImage image = reader.read();
if (image.isNull())
{
qCWarning(chatterinoImage)
<< "Failed reading image at" << url.string << ":"
<< reader.errorString();
return;
}
callback(QPixmap::fromImage(image));
})
.execute();
}
} // namespace chatterino
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "common/Aliases.hpp"
#include <QPixmap>
namespace chatterino {
/**
* Loads an image from url into a QPixmap. Allows for file:// protocol links. Uses cacheing.
*
* @param callback The callback you will get the pixmap by. It will be invoked concurrently with no guarantees on which thread.
*/
void loadPixmapFromUrl(const Url &url, std::function<void(QPixmap)> &&callback);
} // namespace chatterino