From 5d372cfda3770207ce95e6d64ac2ee141681421b Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 4 Mar 2018 16:40:06 +0100 Subject: 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 --- core/imagedownloader.cpp | 28 ++++++++++++---------------- core/imagedownloader.h | 6 +----- qt-models/divepicturemodel.cpp | 2 +- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/core/imagedownloader.cpp b/core/imagedownloader.cpp index 8610d3454..772365d38 100644 --- a/core/imagedownloader.cpp +++ b/core/imagedownloader.cpp @@ -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(*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; } - diff --git a/core/imagedownloader.h b/core/imagedownloader.h index 04846a9cc..2a3d95df4 100644 --- a/core/imagedownloader.h +++ b/core/imagedownloader.h @@ -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 diff --git a/qt-models/divepicturemodel.cpp b/qt-models/divepicturemodel.cpp index e92a16bd0..6a90c1431 100644 --- a/qt-models/divepicturemodel.cpp +++ b/qt-models/divepicturemodel.cpp @@ -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); } -- cgit v1.2.3-70-g09d2