Add support for setting custom covers on folders

This commit is contained in:
Luis Ángel San Martín
2025-05-08 22:00:55 +02:00
parent f0b9d45033
commit b976b7f809
17 changed files with 263 additions and 34 deletions

18
common/cover_utils.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "cover_utils.h"
bool YACReader::saveCover(const QString &path, const QImage &cover)
{
QImage scaled;
if (cover.width() > cover.height()) {
scaled = cover.scaledToWidth(640, Qt::SmoothTransformation);
} else {
auto aspectRatio = static_cast<double>(cover.width()) / static_cast<double>(cover.height());
auto maxAllowedAspectRatio = 0.5;
if (aspectRatio < maxAllowedAspectRatio) { // cover is too tall, e.g. webtoon
scaled = cover.scaledToHeight(960, Qt::SmoothTransformation);
} else {
scaled = cover.scaledToWidth(480, Qt::SmoothTransformation);
}
}
return scaled.save(path, 0, 75);
}

9
common/cover_utils.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef COVER_UTILS_H
#define COVER_UTILS_H
#include <QImage>
namespace YACReader {
bool saveCover(const QString &path, const QImage &image);
}
#endif // COVER_UTILS_H

View File

@ -109,6 +109,7 @@ void iterate(const QModelIndex &index,
const QAbstractItemModel *model,
const std::function<bool(const QModelIndex &)> &iteration);
// TODO: remove all the dataPath variants and always use the root folder of a library `libraryPath` to get all the paths.
struct LibraryPaths {
LibraryPaths() = delete; // Prevent instantiation
@ -137,14 +138,34 @@ struct LibraryPaths {
return QDir(libraryCoversFolderPath(libraryPath)).filePath(coverFileName(hash));
}
static QString libraryCustomFoldersCoverPath(const QString &libraryPath) // libraryPath + /.yacreaderlibrary/covers/folders
{
return QDir(libraryCoversFolderPath(libraryPath)).filePath("folders");
}
static QString libraryCustomFoldersCoverPathFromLibraryDataPath(const QString &libraryDataPath)
{
return QDir(libraryCoversPathFromLibraryDataPath(libraryDataPath)).filePath("folders");
}
static QString customFolderCoverPath(const QString &libraryPath, const QString &folderId)
{
return QDir(libraryCustomFoldersCoverPath(libraryPath)).filePath(coverFileName(folderId));
}
static QString customFolderCoverPathFromDataPath(const QString &libraryDataPath, const QString &folderId)
{
return QDir(libraryCustomFoldersCoverPathFromLibraryDataPath(libraryDataPath)).filePath(coverFileName(folderId));
}
static QString coverPathFromLibraryDataPath(const QString &libraryDataPath, const QString &hash) // libraryDataPath + /covers/hash + .jpg
{
return QDir(libraryCoversPathFromLibraryDataPath(libraryDataPath)).filePath(coverFileName(hash));
}
static QString coverFileName(const QString &hash) // hash + .jpg
static QString coverFileName(const QString &id) // id + .jpg (it can be a comic hash or a folder id)
{
return hash + ".jpg";
return id + ".jpg";
}
static QString coverPathWithFileName(const QString &libraryPath, const QString &fileName) // libraryPath + /.yacreaderlibrary/covers/hash + fileName