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 {
|
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)
|
NetworkRequest(url.string)
|
||||||
.concurrent()
|
.concurrent()
|
||||||
.cache()
|
.cache()
|
||||||
.onSuccess(
|
.onError([onError](const NetworkResult & /*result*/) {
|
||||||
[callback = std::move(callback), url](const NetworkResult &result) {
|
if (onError.has_value())
|
||||||
auto data = result.getData();
|
{
|
||||||
QBuffer buffer(&data);
|
(*onError)();
|
||||||
buffer.open(QIODevice::ReadOnly);
|
}
|
||||||
QImageReader reader(&buffer);
|
})
|
||||||
|
.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)
|
(*onError)();
|
||||||
<< "Can't read image file at" << url.string << ":"
|
|
||||||
<< reader.errorString();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
QImage image = reader.read();
|
QImage image = reader.read();
|
||||||
if (image.isNull())
|
if (image.isNull())
|
||||||
|
{
|
||||||
|
qCWarning(chatterinoImage)
|
||||||
|
<< "Failed reading image at" << url.string << ":"
|
||||||
|
<< reader.errorString();
|
||||||
|
if (onError.has_value())
|
||||||
{
|
{
|
||||||
qCWarning(chatterinoImage)
|
(*onError)();
|
||||||
<< "Failed reading image at" << url.string << ":"
|
|
||||||
<< reader.errorString();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
callback(QPixmap::fromImage(image));
|
callback(QPixmap::fromImage(image));
|
||||||
})
|
})
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,11 @@ namespace chatterino {
|
||||||
/**
|
/**
|
||||||
* Loads an image from url into a QPixmap. Allows for file:// protocol links. Uses cacheing.
|
* 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
|
} // namespace chatterino
|
||||||
|
|
Loading…
Reference in a new issue