Dive pictures: turn SHashedImage class into getHashedImage() function

SHashedImage was a subclass of QImage, which fetched the image according
to the filename hashes. Turn this into a function, as this is much more
idiomatic and flexible.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-03-04 16:40:06 +01:00 committed by Dirk Hohndel
parent 630862971f
commit 5d372cfda3
3 changed files with 14 additions and 22 deletions

View file

@ -104,26 +104,22 @@ static void loadPicture(struct picture *picture, bool fromHash)
}
// Overwrite QImage::load() so that we can perform better error reporting.
bool SHashedImage::load(const QString &fileName, const char *format)
static QImage loadImage(const QString &fileName, const char *format = nullptr)
{
QImageReader reader(fileName, format);
static_cast<QImage&>(*this) = reader.read();
if (isNull())
QImage res = reader.read();
if (res.isNull())
qInfo() << "Error loading image" << fileName << (int)reader.error() << reader.errorString();
return !isNull();
return res;
}
SHashedImage::SHashedImage(struct picture *picture) : QImage()
QImage getHashedImage(struct picture *picture)
{
QImage res;
QUrl url = QUrl::fromUserInput(localFilePath(QString(picture->filename)));
if (url.isLocalFile()) {
load(url.toLocalFile());
if (isNull())
qInfo() << "Failed loading picture" << url.toLocalFile();
else
qDebug() << "Loaded picture" << url.toLocalFile();
}
if (isNull()) {
if(url.isLocalFile())
res = loadImage(url.toLocalFile());
if (res.isNull()) {
// This did not load anything. Let's try to get the image from other sources
// Let's try to load it locally via its hash
QString filename = localFilePath(picture->filename);
@ -135,8 +131,8 @@ SHashedImage::SHashedImage(struct picture *picture) : QImage()
QtConcurrent::run(loadPicture, clone_picture(picture), true);
} else {
// Load locally from translated file name
load(filename);
if (!isNull()) {
res = loadImage(filename);
if (!res.isNull()) {
// Make sure the hash still matches the image file
qDebug() << "Loaded picture from translated filename" << filename;
QtConcurrent::run(hashPicture, clone_picture(picture));
@ -150,5 +146,5 @@ SHashedImage::SHashedImage(struct picture *picture) : QImage()
// We loaded successfully. Now, make sure hash is up to date.
QtConcurrent::run(hashPicture, clone_picture(picture));
}
return res;
}

View file

@ -19,10 +19,6 @@ private:
struct picture *picture;
};
class SHashedImage : public QImage {
bool load(const QString &fileName, const char *format=nullptr);
public:
SHashedImage(struct picture *picture);
};
QImage getHashedImage(struct picture *picture);
#endif // IMAGEDOWNLOADER_H

View file

@ -65,7 +65,7 @@ static void scaleImages(PictureEntry &entry, int maxSize)
// Rescale in such a case to avoid resizing artifacts.
if (thumbnail.isNull() || (thumbnail.size().width() < maxSize && thumbnail.size().height() < maxSize)) {
qDebug() << "No thumbnail in cache for" << entry.filename;
thumbnail = SHashedImage(entry.picture).scaled(maxSize, maxSize, Qt::KeepAspectRatio);
thumbnail = getHashedImage(entry.picture).scaled(maxSize, maxSize, Qt::KeepAspectRatio);
addThumbnailToCache(thumbnail, entry);
}