Skip to content

Commit a712417

Browse files
committed
Update save dialog suffix when the format filter changes
ShowSaveFileDialog set the default suffix once from the configured extension and never listened to QFileDialog::filterSelected. Picking another image format in the file type dropdown therefore had no effect on the saved file: a name typed without an extension still got the configured suffix, and with a native dialog such as GTK's the extension already present in the name was not swapped either, so it had to be edited by hand. Connect filterSelected, derive the suffix from the selected mime type and make it the new default suffix. If the current name already ends in one of the offered image formats, rename it to the new suffix as well; the Qt widget dialog does that on its own, native dialogs do not. Fixes #4708
1 parent 24ce091 commit a712417

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/utils/screenshotsaver.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,34 @@ QString ShowSaveFileDialog(const QString& title, const QString& directory)
104104
QMimeDatabase().mimeTypeForFile("image." + suffix).name();
105105
dialog.selectMimeTypeFilter(defaultMimeType);
106106
dialog.setDefaultSuffix(suffix);
107+
// Follow the format picked in the file type dropdown: use its suffix
108+
// when the typed name has none, and swap the extension of a name that
109+
// already has one (the Qt dialog does this itself, GTK's does not).
110+
QObject::connect(&dialog,
111+
&QFileDialog::filterSelected,
112+
&dialog,
113+
[&dialog](const QString&) {
114+
QString newSuffix =
115+
QMimeDatabase()
116+
.mimeTypeForName(dialog.selectedMimeTypeFilter())
117+
.preferredSuffix();
118+
if (newSuffix.isEmpty()) {
119+
return;
120+
}
121+
dialog.setDefaultSuffix(newSuffix);
122+
QStringList files = dialog.selectedFiles();
123+
if (files.isEmpty()) {
124+
return;
125+
}
126+
QFileInfo info(files.constFirst());
127+
QString oldSuffix = info.suffix().toLower();
128+
if (oldSuffix != newSuffix &&
129+
QImageWriter::supportedImageFormats().contains(
130+
oldSuffix.toLatin1())) {
131+
dialog.selectFile(info.dir().filePath(
132+
info.completeBaseName() + "." + newSuffix));
133+
}
134+
});
107135
if (dialog.exec() == QDialog::Accepted) {
108136
return dialog.selectedFiles().constFirst();
109137
} else {

0 commit comments

Comments
 (0)