From da75676ce85ea1466dd6bdae6e5bfe47a401b71e Mon Sep 17 00:00:00 2001 From: Pieter Hijma Date: Fri, 9 Aug 2024 12:30:22 +0200 Subject: [PATCH] Gui: Fix #15203 filename extension export --- src/Gui/FileDialog.cpp | 18 ++++++++++++++++-- src/Gui/FileDialog.h | 2 ++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Gui/FileDialog.cpp b/src/Gui/FileDialog.cpp index 2c96211a2d..1b35178970 100644 --- a/src/Gui/FileDialog.cpp +++ b/src/Gui/FileDialog.cpp @@ -153,6 +153,17 @@ void FileDialog::accept() QFileDialog::accept(); } +void FileDialog::getPossibleSuffixes(QStringList& possibleSuffixes, const QRegularExpression& rx, + const QString* suffixDescriptions) +{ + QRegularExpressionMatchIterator i = rx.globalMatch(*suffixDescriptions); + while (i.hasNext()) { + QRegularExpressionMatch match = i.next(); + QString suffix = match.captured(1); + possibleSuffixes << suffix; + } +} + /** * This is a convenience static function that will return a file name selected by the user. The file does not have to exist. */ @@ -177,7 +188,7 @@ QString FileDialog::getSaveFileName (QWidget * parent, const QString & caption, // get the suffix for the filter: use the selected filter if there is one, // otherwise find the first valid suffix in the complete list of filters const QString *filterToSearch; - if (selectedFilter) { + if (selectedFilter && !selectedFilter->isEmpty()) { filterToSearch = selectedFilter; } else { @@ -186,6 +197,8 @@ QString FileDialog::getSaveFileName (QWidget * parent, const QString & caption, QRegularExpression rx; rx.setPattern(QLatin1String(R"(\s(\(\*\.\w{1,})\W)")); + QStringList possibleSuffixes; + getPossibleSuffixes(possibleSuffixes, rx, filterToSearch); auto match = rx.match(*filterToSearch); if (match.hasMatch()) { int index = match.capturedStart(); @@ -194,8 +207,9 @@ QString FileDialog::getSaveFileName (QWidget * parent, const QString & caption, int offsetStart = 3; int offsetEnd = 4; QString suffix = filterToSearch->mid(index + offsetStart, length - offsetEnd); - if (fi.suffix().isEmpty()) + if (fi.suffix().isEmpty() || !possibleSuffixes.contains(fi.suffix())) { dirName += suffix; + } } } diff --git a/src/Gui/FileDialog.h b/src/Gui/FileDialog.h index 62f0ddfb83..a581889fb8 100644 --- a/src/Gui/FileDialog.h +++ b/src/Gui/FileDialog.h @@ -89,6 +89,8 @@ private: bool hasSuffix(const QString&) const; static QList fetchSidebarUrls(); static QString workingDirectory; + static void getPossibleSuffixes(QStringList& possibleSuffixes, const QRegularExpression& rx, + const QString* suffixDescriptions); }; // ----------------------------------------------------------------------