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>
This commit is contained in:
Berthold Stoeger 2018-03-10 14:15:50 +01:00 committed by Dirk Hohndel
parent f60343eebb
commit 3967b1fd4d
4 changed files with 141 additions and 68 deletions

View file

@ -5,6 +5,7 @@
#include <QImage>
#include <QFuture>
#include <QNetworkReply>
#include <QThreadPool>
class ImageDownloader : public QObject {
Q_OBJECT
@ -18,6 +19,31 @@ private:
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