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. // 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); QImageReader reader(fileName, format);
static_cast<QImage&>(*this) = reader.read(); QImage res = reader.read();
if (isNull()) if (res.isNull())
qInfo() << "Error loading image" << fileName << (int)reader.error() << reader.errorString(); 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))); QUrl url = QUrl::fromUserInput(localFilePath(QString(picture->filename)));
if (url.isLocalFile()) { if(url.isLocalFile())
load(url.toLocalFile()); res = loadImage(url.toLocalFile());
if (isNull()) if (res.isNull()) {
qInfo() << "Failed loading picture" << url.toLocalFile();
else
qDebug() << "Loaded picture" << url.toLocalFile();
}
if (isNull()) {
// This did not load anything. Let's try to get the image from other sources // 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 // Let's try to load it locally via its hash
QString filename = localFilePath(picture->filename); QString filename = localFilePath(picture->filename);
@ -135,8 +131,8 @@ SHashedImage::SHashedImage(struct picture *picture) : QImage()
QtConcurrent::run(loadPicture, clone_picture(picture), true); QtConcurrent::run(loadPicture, clone_picture(picture), true);
} else { } else {
// Load locally from translated file name // Load locally from translated file name
load(filename); res = loadImage(filename);
if (!isNull()) { if (!res.isNull()) {
// Make sure the hash still matches the image file // Make sure the hash still matches the image file
qDebug() << "Loaded picture from translated filename" << filename; qDebug() << "Loaded picture from translated filename" << filename;
QtConcurrent::run(hashPicture, clone_picture(picture)); 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. // We loaded successfully. Now, make sure hash is up to date.
QtConcurrent::run(hashPicture, clone_picture(picture)); QtConcurrent::run(hashPicture, clone_picture(picture));
} }
return res;
} }

View file

@ -19,10 +19,6 @@ private:
struct picture *picture; struct picture *picture;
}; };
class SHashedImage : public QImage { QImage getHashedImage(struct picture *picture);
bool load(const QString &fileName, const char *format=nullptr);
public:
SHashedImage(struct picture *picture);
};
#endif // IMAGEDOWNLOADER_H #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. // Rescale in such a case to avoid resizing artifacts.
if (thumbnail.isNull() || (thumbnail.size().width() < maxSize && thumbnail.size().height() < maxSize)) { if (thumbnail.isNull() || (thumbnail.size().width() < maxSize && thumbnail.size().height() < maxSize)) {
qDebug() << "No thumbnail in cache for" << entry.filename; 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); addThumbnailToCache(thumbnail, entry);
} }