Skip to content

Commit

Permalink
fix: improve automatic thumbnail choice (fix #3230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed Jul 1, 2024
1 parent 7f3be9c commit f95c659
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/gui-qml/src/models/qml-image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
QString QmlImage::smartPreviewUrl(int width, int height) const
{
if (m_profile->getSettings()->value("thumbnailSmartSize", true).toBool()) {
return m_image->mediaForSize(QSize(width, height)).url.toString();
return m_image->mediaForSize(QSize(width, height), true).url.toString();
} else {
return previewUrl();
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/src/tabs/image-preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ImagePreview::ImagePreview(QSharedPointer<Image> image, QWidget *container, Prof
if (m_profile->getSettings()->value("thumbnailSmartSize", true).toBool()) {
const qreal upscale = m_profile->getSettings()->value("thumbnailUpscale", 1.0).toDouble();
const int imageSize = qFloor(150 * upscale);
m_thumbnailUrl = image->mediaForSize(QSize(imageSize, imageSize)).url;
m_thumbnailUrl = image->mediaForSize(QSize(imageSize, imageSize), true).url;
} else {
m_thumbnailUrl = image->url(Image::Size::Thumbnail);
}
Expand Down Expand Up @@ -162,7 +162,7 @@ void ImagePreview::finishedLoadingPreview()
QPixmap thumbnail;
thumbnail.loadFromData(m_reply->readAll());
if (thumbnail.isNull()) {
log(QStringLiteral("One of the thumbnails is empty (`%1`).").arg(m_image->url(Image::Size::Thumbnail).toString()), Logger::Error);
log(QStringLiteral("One of the thumbnails is empty (`%1`).").arg(m_reply->url().toString()), Logger::Error);
finishedLoading();
return;
}
Expand Down
17 changes: 11 additions & 6 deletions src/lib/src/models/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ Image::Image(Site *site, QMap<QString, QString> details, QVariantMap identity, Q
const QString &urlKey = (prefix.isEmpty() ? "file_" : prefix) + "url";
is->url = details.contains(urlKey) ? removeCacheBuster(m_parentSite->fixUrl(details[urlKey])) : QString();

is->size = details.contains(prefix + "width") && details.contains(prefix + "height")
? QSize(details[prefix + "width"].toInt(), details[prefix + "height"].toInt())
: QSize();
const int width = details.value(prefix + "width", "0").toInt();
const int height = details.value(prefix + "height", "0").toInt();
is->size = width > 0 && height > 0 ? QSize(width, height) : QSize();
is->fileSize = details.contains(prefix + "file_size") ? details[prefix + "file_size"].toInt() : 0;

if (details.contains(prefix + "rect")) {
Expand Down Expand Up @@ -1364,16 +1364,21 @@ bool Image::isValid() const
* Find the biggest media available in this image under the given size. Defaults to the thumbnail if none is found.
*
* @param size The bounding size not to exceed.
* @param thumbnail If the media will be used as a thumbnail, its filetype should match the thumbnail filetype.
* @return The biggest media available in this image under this size.
*/
const ImageSize &Image::mediaForSize(const QSize &size)
const ImageSize &Image::mediaForSize(const QSize &size, bool thumbnail)
{
QSharedPointer<ImageSize> ret;

const QString thumbnailExt = getExtension(m_sizes[Size::Thumbnail]->url);

// Find the biggest media smaller than the given size
for (const QSharedPointer<ImageSize> &media : m_allSizes) {
if (media->size.width() <= size.width() && media->size.height() <= size.height() && (ret.isNull() || isBigger(media->size, ret->size))) {
ret = media;
if (media->size.isValid() && media->size.width() <= size.width() && media->size.height() <= size.height() && (ret.isNull() || isBigger(media->size, ret->size))) {
if (!thumbnail || getExtension(media->url) == thumbnailExt) {
ret = media;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/src/models/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Image : public QObject, public Downloadable
void setPromoteDetailParsWarn(bool);
bool isValid() const;
Profile *getProfile() const { return m_profile; }
const ImageSize &mediaForSize(const QSize &size);
const ImageSize &mediaForSize(const QSize &size, bool thumbnail = false);
QList<QPair<QString, int>> ugoiraFrameInformation() const;

// Preview pixmap store
Expand Down

0 comments on commit f95c659

Please sign in to comment.