1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-08 22:26:51 +02:00

Try to fix crash on loading screenshots without active mod

This commit is contained in:
Ivan Savenko
2025-01-18 21:28:35 +00:00
parent 232e70c03a
commit 83155c2755

View File

@@ -953,38 +953,38 @@ void CModListView::on_tabWidget_currentChanged(int index)
void CModListView::loadScreenshots()
{
if(ui->tabWidget->currentIndex() == 2)
{
if(!ui->allModsView->currentIndex().isValid())
{
// select the first mod, so we can access its data
ui->allModsView->setCurrentIndex(filterModel->index(0, 0));
}
if(ui->tabWidget->currentIndex() != 2)
return;
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
ui->screenshotsList->clear();
QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
assert(modStateModel->isModExists(modName)); //should be filtered out by check above
for(QString url : modStateModel->getMod(modName).getScreenshots())
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 fullPath = QString{QLatin1String{"%1/%2"}}.arg(CLauncherDirs::downloadsPath(), fileName);
QPixmap pixmap(fullPath);
if(pixmap.isNull())
{
// 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 fullPath = QString{QLatin1String{"%1/%2"}}.arg(CLauncherDirs::downloadsPath(), fileName);
QPixmap pixmap(fullPath);
if(pixmap.isNull())
{
// image file not exists or corrupted - try to redownload
downloadFile(fileName, url, tr("screenshots"));
}
else
{
// managed to load cached image
QIcon icon(pixmap);
auto * item = new QListWidgetItem(icon, QString(tr("Screenshot %1")).arg(ui->screenshotsList->count() + 1));
ui->screenshotsList->addItem(item);
}
// image file not exists or corrupted - try to redownload
downloadFile(fileName, url, tr("screenshots"));
}
else
{
// managed to load cached image
QIcon icon(pixmap);
auto * item = new QListWidgetItem(icon, QString(tr("Screenshot %1")).arg(ui->screenshotsList->count() + 1));
ui->screenshotsList->addItem(item);
}
}
}