-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[FIX] Save dialogs: Avoid weird behavior with extensions on macOS #7195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
markotoplak
merged 1 commit into
biolab:master
from
janezd:fix-save-extensions-on-macos
Dec 19, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ | |
|
|
||
| _userhome = os.path.expanduser(f"~{os.sep}") | ||
|
|
||
| _IS_DARWIN = sys.platform == "darwin" | ||
| _IS_WIN32 = sys.platform == "win32" | ||
|
|
||
| class OWSaveBase(widget.OWWidget, openclass=True): | ||
| """ | ||
|
|
@@ -300,7 +302,7 @@ def initial_start_dir(self): | |
| Return either the current file's path, the last directory or home. | ||
| """ | ||
| if self.filename and os.path.exists(os.path.split(self.filename)[0]): | ||
| return self.filename | ||
| return os.path.splitext(self.filename)[0] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same change as in the base class. See whether this is OK for Windows, too. |
||
| else: | ||
| return self.last_dir or _userhome | ||
|
|
||
|
|
@@ -353,39 +355,30 @@ def migrate_settings(cls, settings, version): | |
|
|
||
| # As of Qt 5.9, QFileDialog.setDefaultSuffix does not support double | ||
| # suffixes, not even in non-native dialogs. We handle each OS separately. | ||
| if sys.platform in ("darwin", "win32"): | ||
| # macOS and Windows native dialogs do not correctly handle double | ||
| # extensions. We thus don't pass any suffixes to the dialog and add | ||
| # the correct suffix after closing the dialog and only then check | ||
| # if the file exists and ask whether to override. | ||
| # It is a bit confusing that the user does not see the final name in the | ||
| # dialog, but I see no better solution. | ||
| if _IS_DARWIN or _IS_WIN32: | ||
| def get_save_filename(self): # pragma: no cover | ||
| if sys.platform == "darwin": | ||
| def remove_star(filt): | ||
| return filt.replace(" (*.", " (.") | ||
| else: | ||
| def remove_star(filt): | ||
| return filt | ||
|
|
||
| no_ext_filters = {remove_star(f): f for f in self.valid_filters()} | ||
| filename = self.initial_start_dir() | ||
| while True: | ||
| dlg = QFileDialog( | ||
| None, "Save File", filename, ";;".join(no_ext_filters)) | ||
| None, "Save File", filename, ";;".join(self.valid_filters())) | ||
| dlg.setAcceptMode(dlg.AcceptSave) | ||
| dlg.selectNameFilter(remove_star(self.default_valid_filter())) | ||
| dlg.setOption(QFileDialog.DontConfirmOverwrite) | ||
| dlg.selectNameFilter(self.default_valid_filter()) | ||
| # MacOs (currently) ignores DontConfirmOverwrite | ||
| # Let us not set it, so we know it's not set in the future | ||
| if _IS_WIN32: | ||
| dlg.setOption(QFileDialog.DontConfirmOverwrite) | ||
| if dlg.exec() == QFileDialog.Rejected: | ||
| return "", "" | ||
| filename = dlg.selectedFiles()[0] | ||
| selected_filter = no_ext_filters[dlg.selectedNameFilter()] | ||
| selected_filter = dlg.selectedNameFilter() | ||
| filename = self._replace_extension( | ||
| filename, self._extension_from_filter(selected_filter)) | ||
| if not os.path.exists(filename) or QMessageBox.question( | ||
| if (not os.path.exists(filename) | ||
| or _IS_DARWIN # MacOs already asked for confirmation | ||
| or QMessageBox.question( | ||
| self, "Overwrite file?", | ||
| f"File {os.path.split(filename)[1]} already exists.\n" | ||
| "Overwrite?") == QMessageBox.Yes: | ||
| "Overwrite?") == QMessageBox.Yes): | ||
| return filename, selected_filter | ||
|
|
||
| else: # Linux and any unknown platforms | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change might affect Windows. We need to check and, if necessary, do this only for macOS.