Stellarium / stellarium

Stellarium is a free GPL software which renders realistic skies in real time with OpenGL. It is available for Linux/Unix, Windows and macOS. With Stellarium, you really see what you can see with your eyes, binoculars or a small telescope.
https://stellarium.org
GNU General Public License v2.0
7.76k stars 821 forks source link

screenshot file naming logic is bugged #1283

Closed axd1967 closed 2 years ago

axd1967 commented 4 years ago

This has been rejected as a bug in #1180 , yet I call on any developer that ever touches that code area, to review the code.

The code in question is following

https://github.com/Stellarium/stellarium/blob/23078c5912cd61129f72e1f4ba7489af38330299/src/StelMainView.cpp#L1680-L1685

This code contains internal logic errors as a result of a sloppy choice of magic numbers / hardcoded related constants.

Why limit to 10000? why not 10_0001? or 10_000_000? There is no immediate need for this limit. The full unsigned 32bit range is fine (although a time based range such as Unix time is a superior choice that would not break the existing implementation).

Meanwhile, the code is internally contradicting itself (counting to 100000 but limiting to a field width of 3 digits will cause sorting issues). The code will forcibly (and silently) overwrite an existing screenshot once rolling past 100000 . a while loop over an unsigned int makes this problem not go away, but far less likely (even if that also is Y2K reasoning).

The logic is also smelly: when the user moves some of the screenshot files, gaps are opened in the file names which will be filled by chronologically later screenshots, but alphabetically earlier (gap fillers). This will cause subtle sorting problems. (For me an indication that the use of an index is smelly. Call it an opinion...).

todo

A follow-on solution is to replace the numbering with Unix time, while searching for a solution (64 bit? ) for Epochalypse.

axd1967 commented 4 years ago

Additionally, I discovered in https://github.com/Stellarium/stellarium/issues/1180#issuecomment-699004723 that the index suffix is always added, also when invoking the screenshot function from scripting.

An improvement is to allow the caller to skip the free index seeking logic (eg a flag or new argument generate_index = true ?) as the index might no longer make sense.

Edit added: the index '000' (or maybe higher) is added only when overwrite=false.

Edit added: the same time stamp logic is used as default in the screen capture feature of KUbuntu ("spectacle").

axd1967 commented 3 years ago

for_wishlist

chrisx8 commented 2 years ago

@alex-w, what do you think if we just name screenshots by the current time, like stellarium-yyyyMMdd-hhmmss-ms?

Proposed change

A simple implementation of this could be like so:

// name screenshot files with current time
QString currentTime = QDateTime::currentDateTime().toString("yyyyMMdd-hhmmss-ms");
QFileInfo shotPath = QFileInfo(shotDir.filePath() + "/" + screenShotPrefix + currentTime + "." + screenShotFormat);

This would replace this code segment: https://github.com/Stellarium/stellarium/blob/966f54b393b619a7e9d6886289d31fdb94c1835a/src/StelMainView.cpp#L1714-L1719

Reasons

I think the current implementation is not a good idea. Here are some issues that I can think of:

IMO, The time-based naming is probably a better choice here, because:

Please let me know what do you think!

alex-w commented 2 years ago

I disagree. You may add it as an additional option to naming screenshot files (include updating the GUI), but not just replace the current method.

gzotti commented 2 years ago

It may indeed be good to detect the greatest used number and add there. And maybe allow a time-based strategy as alternative.

alex-w commented 2 years ago

Please check the latest stable version of Stellarium: https://github.com/Stellarium/stellarium/releases/latest