mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-15 11:38:49 -07:00
[PictureLoader] Allow saving downloaded images to local storage and not just the QNetworkManager cache.
Took 1 hour 11 minutes Took 4 seconds Took 25 seconds
This commit is contained in:
parent
338c56678a
commit
64885f4c20
6 changed files with 138 additions and 5 deletions
|
|
@ -150,9 +150,10 @@ void CardPictureLoader::getPixmap(QPixmap &pixmap, const ExactCard &card, QSize
|
|||
|
||||
void CardPictureLoader::imageLoaded(const ExactCard &card, const QImage &image)
|
||||
{
|
||||
QPixmap finalPixmap;
|
||||
|
||||
if (image.isNull()) {
|
||||
qCDebug(CardPictureLoaderLog) << "Caching NULL pixmap for" << card.getName();
|
||||
QPixmapCache::insert(card.getPixmapCacheKey(), QPixmap());
|
||||
} else {
|
||||
if (card.getInfo().getUiAttributes().upsideDownArt) {
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 0))
|
||||
|
|
@ -160,12 +161,18 @@ void CardPictureLoader::imageLoaded(const ExactCard &card, const QImage &image)
|
|||
#else
|
||||
QImage mirrorImage = image.mirrored(true, true);
|
||||
#endif
|
||||
QPixmapCache::insert(card.getPixmapCacheKey(), QPixmap::fromImage(mirrorImage));
|
||||
finalPixmap = QPixmap::fromImage(mirrorImage);
|
||||
} else {
|
||||
QPixmapCache::insert(card.getPixmapCacheKey(), QPixmap::fromImage(image));
|
||||
finalPixmap = QPixmap::fromImage(image);
|
||||
}
|
||||
}
|
||||
|
||||
QPixmapCache::insert(card.getPixmapCacheKey(), finalPixmap);
|
||||
|
||||
if (SettingsCache::instance().getSaveCardImagesToLocalStorage()) {
|
||||
saveCardImageToLocalStorage(card, finalPixmap);
|
||||
}
|
||||
|
||||
// imageLoaded should only be reached if the exactCard isn't already in cache.
|
||||
// (plus there's a deduplication mechanism in CardPictureLoaderWorker)
|
||||
// It should be safe to connect the CardInfo here without worrying about redundant connections.
|
||||
|
|
@ -175,6 +182,75 @@ void CardPictureLoader::imageLoaded(const ExactCard &card, const QImage &image)
|
|||
card.emitPixmapUpdated();
|
||||
}
|
||||
|
||||
void CardPictureLoader::saveCardImageToLocalStorage(const ExactCard &card, const QPixmap &pixmap)
|
||||
{
|
||||
if (pixmap.isNull() || !card) {
|
||||
return;
|
||||
}
|
||||
|
||||
const QString picsRoot = SettingsCache::instance().getPicsPath();
|
||||
const QString scheme = SettingsCache::instance().getLocalCardImageStorageNamingScheme();
|
||||
|
||||
if (picsRoot.isEmpty() || scheme.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Base directory: <picsPath>/downloadedPics
|
||||
QDir baseDir(picsRoot);
|
||||
if (!baseDir.exists("downloadedPics")) {
|
||||
baseDir.mkpath("downloadedPics");
|
||||
}
|
||||
baseDir.cd("downloadedPics");
|
||||
|
||||
// Collect card metadata
|
||||
const QString cardName = card.getInfo().getCorrectedName();
|
||||
|
||||
QString setName;
|
||||
QString collectorNumber;
|
||||
QString uuid;
|
||||
|
||||
PrintingInfo printing = card.getPrinting();
|
||||
if (printing.getSet()) {
|
||||
setName = printing.getSet()->getCorrectedShortName();
|
||||
collectorNumber = printing.getProperty("num");
|
||||
uuid = printing.getUuid();
|
||||
}
|
||||
|
||||
// Build path from scheme
|
||||
QString relativePath = scheme;
|
||||
|
||||
relativePath.replace("{name}", cardName);
|
||||
relativePath.replace("{set}", setName);
|
||||
relativePath.replace("{collector}", collectorNumber);
|
||||
relativePath.replace("{uuid}", uuid);
|
||||
relativePath.replace("{ext}", "png");
|
||||
|
||||
// Normalize slashes
|
||||
relativePath = QDir::cleanPath(relativePath);
|
||||
|
||||
QFileInfo outInfo(baseDir.filePath(relativePath));
|
||||
QDir outDir = outInfo.dir();
|
||||
|
||||
// Ensure directory exists
|
||||
if (!outDir.exists()) {
|
||||
if (!baseDir.mkpath(outDir.path())) {
|
||||
qCWarning(CardPictureLoaderLog) << "Failed to create directory for downloaded card image:" << outDir.path();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Do not overwrite existing files
|
||||
if (outInfo.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Save image
|
||||
QImage image = pixmap.toImage();
|
||||
if (!image.save(outInfo.absoluteFilePath(), "PNG")) {
|
||||
qCWarning(CardPictureLoaderLog) << "Failed to save card image to" << outInfo.absoluteFilePath();
|
||||
}
|
||||
}
|
||||
|
||||
void CardPictureLoader::clearPixmapCache()
|
||||
{
|
||||
QPixmapCache::clear();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue