mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Add onError callback to load pixmap code
This commit is contained in:
parent
2e60142a47
commit
6c11b2400a
2 changed files with 40 additions and 22 deletions
|
@ -11,37 +11,52 @@
|
|||
|
||||
namespace chatterino {
|
||||
|
||||
void loadPixmapFromUrl(const Url &url, std::function<void(QPixmap)> &&callback)
|
||||
void loadPixmapFromUrl(const Url &url, std::function<void(QPixmap)> &&onSuccess,
|
||||
const std::optional<std::function<void()>> &onError)
|
||||
{
|
||||
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);
|
||||
.onError([onError](const NetworkResult & /*result*/) {
|
||||
if (onError.has_value())
|
||||
{
|
||||
(*onError)();
|
||||
}
|
||||
})
|
||||
.onSuccess([callback = std::move(onSuccess), onError,
|
||||
url](const NetworkResult &result) {
|
||||
auto data = result.getData();
|
||||
QBuffer buffer(&data);
|
||||
buffer.open(QIODevice::ReadOnly);
|
||||
QImageReader reader(&buffer);
|
||||
|
||||
if (!reader.canRead() || reader.size().isEmpty())
|
||||
if (!reader.canRead() || reader.size().isEmpty())
|
||||
{
|
||||
qCWarning(chatterinoImage)
|
||||
<< "Can't read image file at" << url.string << ":"
|
||||
<< reader.errorString();
|
||||
if (onError.has_value())
|
||||
{
|
||||
qCWarning(chatterinoImage)
|
||||
<< "Can't read image file at" << url.string << ":"
|
||||
<< reader.errorString();
|
||||
return;
|
||||
(*onError)();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
QImage image = reader.read();
|
||||
if (image.isNull())
|
||||
QImage image = reader.read();
|
||||
if (image.isNull())
|
||||
{
|
||||
qCWarning(chatterinoImage)
|
||||
<< "Failed reading image at" << url.string << ":"
|
||||
<< reader.errorString();
|
||||
if (onError.has_value())
|
||||
{
|
||||
qCWarning(chatterinoImage)
|
||||
<< "Failed reading image at" << url.string << ":"
|
||||
<< reader.errorString();
|
||||
return;
|
||||
(*onError)();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
callback(QPixmap::fromImage(image));
|
||||
})
|
||||
callback(QPixmap::fromImage(image));
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
|
|
|
@ -8,8 +8,11 @@ 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.
|
||||
* @param onSuccess The callback you will get the pixmap by. It will be invoked concurrently with no guarantees on which thread.
|
||||
* @param onError The callback that will be called if the request fails or the image cannot be loaded.
|
||||
*/
|
||||
void loadPixmapFromUrl(const Url &url, std::function<void(QPixmap)> &&callback);
|
||||
void loadPixmapFromUrl(
|
||||
const Url &url, std::function<void(QPixmap)> &&onSuccess,
|
||||
const std::optional<std::function<void()>> &onError = {});
|
||||
|
||||
} // namespace chatterino
|
||||
|
|
Loading…
Reference in a new issue