subsurface/core/imagedownloader.h
Berthold Stoeger 3967b1fd4d Dive pictures: Introduce thumbnailer class
Create a new class, which performs all thumbnailing code.
This is mostly code reshuffling. Thumbnails are extracted
either from a cache or thumbnail calculation is started in
a worker thread.

Since getHashedImage() is called from a worker thread it
makes no sense to call subfunctions in yet another worker
thread. Remove these calls.

In contrast to the previous code, on error the background
thread produces a failure image, but it is not yet shown.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-05-13 13:52:35 -07:00

49 lines
1.1 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#ifndef IMAGEDOWNLOADER_H
#define IMAGEDOWNLOADER_H
#include <QImage>
#include <QFuture>
#include <QNetworkReply>
#include <QThreadPool>
class ImageDownloader : public QObject {
Q_OBJECT
public:
ImageDownloader(const QString &filename);
void load(bool fromHash);
private:
bool loadFromUrl(const QUrl &); // return true on success
void saveImage(QNetworkReply *reply, bool &success);
QString filename;
};
class PictureEntry;
class Thumbnailer : public QObject {
Q_OBJECT
public:
static Thumbnailer *instance();
// Schedule a thumbnail for fetching or calculation.
// Returns a placehlder thumbnail. The actual thumbnail will be sent
// via a signal later.
QImage fetchThumbnail(PictureEntry &entry, int size);
// If we change dive, clear all unfinished thumbnail creations
void clearWorkQueue();
signals:
void thumbnailChanged(QString filename, QImage thumbnail);
private:
Thumbnailer();
void processItem(QString filename, int size);
mutable QMutex lock;
QThreadPool pool;
QMap<QString,QFuture<void>> workingOn;
};
QImage getHashedImage(const QString &filename);
#endif // IMAGEDOWNLOADER_H