mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Dive pictures: Make failure of loading images less noisy
For debug reasons, failure to load the original image was spilled to the console, even if the local file was then found. Only print a message, when also the local image failed loading. This needed a bit of code reshuffling. To know when to print a failed-loading message, the URL is now checked at the Thumbnailer level, not the ImageDownloader level. The ImageDownloader is passed the URL and the original filename (if different). The image is loaded from the URL, but the signals send the original filename, so that the thumbnail can be associated to the proper image. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
be361c5cfb
commit
4c19d3da1d
2 changed files with 14 additions and 28 deletions
|
@ -29,16 +29,8 @@ ImageDownloader::ImageDownloader()
|
|||
connect(&manager, &QNetworkAccessManager::finished, this, &ImageDownloader::saveImage);
|
||||
}
|
||||
|
||||
void ImageDownloader::load(QString filename)
|
||||
void ImageDownloader::load(QUrl url, QString filename)
|
||||
{
|
||||
QUrl url = QUrl::fromUserInput(filename);
|
||||
|
||||
// If this is a file, we tried previously -> don't bother trying it again
|
||||
if (url.scheme() == "file" || !url.isValid()) {
|
||||
emit failed(filename);
|
||||
return;
|
||||
}
|
||||
|
||||
QNetworkRequest request(url);
|
||||
request.setAttribute(QNetworkRequest::User, filename);
|
||||
manager.get(request);
|
||||
|
@ -73,21 +65,11 @@ void ImageDownloader::saveImage(QNetworkReply *reply)
|
|||
reply->deleteLater();
|
||||
}
|
||||
|
||||
static void loadPicture(QString filename)
|
||||
static void loadPicture(QUrl url, QString filename)
|
||||
{
|
||||
// This has to be done in UI main thread, because QNetworkManager refuses
|
||||
// to treat requests from other threads.
|
||||
QMetaObject::invokeMethod(ImageDownloader::instance(), "load", Qt::AutoConnection, Q_ARG(QString, filename));
|
||||
}
|
||||
|
||||
// Overwrite QImage::load() so that we can perform better error reporting.
|
||||
static QImage loadImage(const QString &fileName, const char *format = nullptr)
|
||||
{
|
||||
QImageReader reader(fileName, format);
|
||||
QImage res = reader.read();
|
||||
if (res.isNull())
|
||||
qInfo() << "Error loading image" << fileName << (int)reader.error() << reader.errorString();
|
||||
return res;
|
||||
QMetaObject::invokeMethod(ImageDownloader::instance(), "load", Qt::AutoConnection, Q_ARG(QUrl, url), Q_ARG(QString, filename));
|
||||
}
|
||||
|
||||
// Returns: thumbnail, still loading
|
||||
|
@ -98,26 +80,30 @@ static std::pair<QImage,bool> getHashedImage(const QString &file_in, bool tryDow
|
|||
bool stillLoading = false;
|
||||
QUrl url = QUrl::fromUserInput(localFilePath(file));
|
||||
if (url.isLocalFile())
|
||||
thumb = loadImage(url.toLocalFile());
|
||||
thumb.load(url.toLocalFile());
|
||||
if (!thumb.isNull()) {
|
||||
// We loaded successfully. Now, make sure hash is up to date.
|
||||
hashPicture(file);
|
||||
} else if (tryDownload) {
|
||||
// This did not load anything. Let's try to get the image from other sources
|
||||
QString filenameLocal = localFilePath(qPrintable(file));
|
||||
qDebug() << QStringLiteral("Translated filename: %1 -> %2").arg(file, filenameLocal);
|
||||
QString filenameLocal = localFilePath(file);
|
||||
// Load locally from translated file name if it is different
|
||||
if (filenameLocal != file)
|
||||
thumb = loadImage(filenameLocal);
|
||||
thumb.load(filenameLocal);
|
||||
if (!thumb.isNull()) {
|
||||
// Make sure the hash still matches the image file
|
||||
hashPicture(filenameLocal);
|
||||
} else {
|
||||
// Interpret filename as URL
|
||||
loadPicture(filenameLocal);
|
||||
stillLoading = true;
|
||||
QUrl url = QUrl::fromUserInput(filenameLocal);
|
||||
if (!url.isLocalFile() && url.isValid()) {
|
||||
loadPicture(url, file);
|
||||
stillLoading = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (thumb.isNull() && !stillLoading)
|
||||
qInfo() << "Error loading image" << file << "[local:" << localFilePath(file) << "]";
|
||||
return { thumb, stillLoading };
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ public:
|
|||
static ImageDownloader *instance();
|
||||
ImageDownloader();
|
||||
public slots:
|
||||
void load(QString filename);
|
||||
void load(QUrl url, QString filename);
|
||||
signals:
|
||||
void loaded(QString filename);
|
||||
void failed(QString filename);
|
||||
|
|
Loading…
Add table
Reference in a new issue