mirror of
https://github.com/vcmi/vcmi.git
synced 2026-06-19 22:57:37 +02:00
Use local mod screenshots when possible
This commit is contained in:
@@ -1355,22 +1355,38 @@ void CModListView::loadScreenshots()
|
||||
|
||||
assert(ui->allModsView->currentIndex().isValid());
|
||||
|
||||
|
||||
if (!ui->allModsView->currentIndex().isValid())
|
||||
return;
|
||||
|
||||
ui->screenshotsList->clear();
|
||||
QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
|
||||
assert(modStateModel->isModExists(modName)); //should be filtered out by check above
|
||||
const auto localScreenshotsPath = QString{QLatin1String{"%1/%2/screenshots"}}.arg(CLauncherDirs::modsPath(), modName);
|
||||
|
||||
for(QString url : modStateModel->getMod(modName).getScreenshots())
|
||||
{
|
||||
// URL must be encoded to something else to get rid of symbols illegal in file names
|
||||
const auto hashed = QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5);
|
||||
const auto fileName = QString{QLatin1String{"%1.png"}}.arg(QLatin1String{hashed.toHex()});
|
||||
const auto originalFileName = QUrl{url}.fileName(QUrl::FullyDecoded);
|
||||
|
||||
const auto fullPath = QString{QLatin1String{"%1/%2"}}.arg(CLauncherDirs::downloadsPath(), fileName);
|
||||
QPixmap pixmap(fullPath);
|
||||
const QStringList fullPaths = {
|
||||
QString{QLatin1String{"%1/%2"}}.arg(localScreenshotsPath, originalFileName),
|
||||
QString{QLatin1String{"%1/%2"}}.arg(localScreenshotsPath, fileName),
|
||||
QString{QLatin1String{"%1/%2"}}.arg(CLauncherDirs::downloadsPath(), fileName)
|
||||
};
|
||||
|
||||
QPixmap pixmap;
|
||||
QString loadedPath;
|
||||
for(const auto & fullPath : fullPaths)
|
||||
{
|
||||
pixmap.load(fullPath);
|
||||
if(!pixmap.isNull())
|
||||
{
|
||||
loadedPath = fullPath;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(pixmap.isNull())
|
||||
{
|
||||
// image file not exists or corrupted - try to redownload
|
||||
@@ -1381,7 +1397,7 @@ void CModListView::loadScreenshots()
|
||||
// managed to load cached image
|
||||
QIcon icon(pixmap);
|
||||
auto * item = new QListWidgetItem(icon, QString(tr("Screenshot %1")).arg(ui->screenshotsList->count() + 1));
|
||||
item->setData(Qt::UserRole, fullPath);
|
||||
item->setData(Qt::UserRole, loadedPath);
|
||||
ui->screenshotsList->addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user