Skip to content

Commit

Permalink
Fix Stellarium#1283: properly detect largest screenshot number (Stell…
Browse files Browse the repository at this point in the history
…arium#2379)

* Fix Stellarium#1283: detect largest screenshot number

* Fix Stellarium#1283: fix typo

* Fix screenshot number detection when there are non-Stellarium files

Fix a bug that causes screenshot number detection to fail when there are
non-Stellarium screenshot files (files that doesn't match Stellarium
screenshot patterns) in the screenshot directory.

* fix segfault when screenshot dir is empty

* Code cleanup for 41d3182
  • Loading branch information
chrisx8 authored Apr 5, 2022
1 parent f378380 commit 7076774
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/StelMainView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1711,11 +1711,37 @@ void StelMainView::doScreenshot(void)
}
else
{
for (int j=0; j<100000; ++j)
// build filter for file list, so we only select Stellarium screenshot files (prefix*.format)
QString shotFilePattern = QString("%1*.%2").arg(screenShotPrefix, screenShotFormat);
QStringList fileNameFilters(shotFilePattern);
// get highest-numbered file in screenshot directory
QDir dir(shotDir.filePath());
QStringList existingFiles = dir.entryList(fileNameFilters);

// screenshot number - default to 1 for empty directory
int shotNum = 1;
if (!existingFiles.empty())
{
shotPath = QFileInfo(shotDir.filePath() + "/" + screenShotPrefix + QString("%1").arg(j, 3, 10, QLatin1Char('0')) + "." + screenShotFormat);
if (!shotPath.exists())
break;
// already have screenshots, find largest number
QString lastFileName = existingFiles[existingFiles.size() - 1];

// extract number from highest-numbered file name
QString lastShotNumString = lastFileName.replace(screenShotPrefix, "").replace("." + screenShotFormat, "");
// new screenshot number = start at highest number
shotNum = lastShotNumString.toInt() + 1;
}

// build new screenshot path: "path/prefix-num.format"
// num is at least 3 characters
QString shotNumString = QString::number(shotNum).rightJustified(3, '0');
QString shotPathString = QString("%1/%2%3.%4").arg(shotDir.filePath(), screenShotPrefix, shotNumString, screenShotFormat);
shotPath = QFileInfo(shotPathString);
// validate if new screenshot number is valid (non-existent)
while (shotPath.exists()) {
shotNum++;
shotNumString = QString::number(shotNum).rightJustified(3, '0');
shotPathString = QString("%1/%2%3.%4").arg(shotDir.filePath(), screenShotPrefix, shotNumString, screenShotFormat);
shotPath = QFileInfo(shotPathString);
}
}

Expand Down

0 comments on commit 7076774

Please sign in to comment.