From 0894fe0e730db325dab6f22893d04ed80ca7bce1 Mon Sep 17 00:00:00 2001 From: Pieter Hijma Date: Sun, 8 Sep 2024 16:19:16 +0200 Subject: [PATCH] Core: Fix duplicated extensions (see #16299) --- src/Gui/FileDialog.cpp | 36 ++++++++++++++++-------------------- src/Gui/FileDialog.h | 3 +-- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/Gui/FileDialog.cpp b/src/Gui/FileDialog.cpp index ced4054b81..978f825dd1 100644 --- a/src/Gui/FileDialog.cpp +++ b/src/Gui/FileDialog.cpp @@ -153,14 +153,19 @@ void FileDialog::accept() QFileDialog::accept(); } -void FileDialog::getPossibleSuffixes(QStringList& possibleSuffixes, const QRegularExpression& rx, - const QString* suffixDescriptions) +void FileDialog::getSuffixesDescription(QStringList& suffixes, const QString* suffixDescriptions) { + QRegularExpression rx; + // start the raw string with a ( + // match a *, a . and at least one word character (a-z, A-Z, 0-9, _) with \*\.\w+ + // end the raw string with a ) + rx.setPattern(QLatin1String(R"(\*\.\w+)")); + QRegularExpressionMatchIterator i = rx.globalMatch(*suffixDescriptions); while (i.hasNext()) { QRegularExpressionMatch match = i.next(); - QString suffix = match.captured(1); - possibleSuffixes << suffix; + QString suffix = match.captured(0); + suffixes << suffix; } } @@ -195,22 +200,13 @@ QString FileDialog::getSaveFileName (QWidget * parent, const QString & caption, filterToSearch = &filter; } - 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(); - int length = match.capturedLength(); - // get the suffix with the leading dot but ignore the surrounding ' (*' and ')' - int offsetStart = 3; - int offsetEnd = 4; - QString suffix = filterToSearch->mid(index + offsetStart, length - offsetEnd); - QString fiSuffix = QLatin1String("*.") + fi.suffix(); // To match with possibleSuffixes - if (fi.suffix().isEmpty() || !possibleSuffixes.contains(fiSuffix)) { - dirName += suffix; - } + QStringList filterSuffixes; + getSuffixesDescription(filterSuffixes, filterToSearch); + QString fiSuffix = QLatin1String("*.") + fi.suffix(); // To match with filterSuffixes + if (fi.suffix().isEmpty() || !filterSuffixes.contains(fiSuffix)) { + // there is no suffix or not a suffix that matches the filter, so + // default to the first suffix of the filter + dirName += filterSuffixes[0].mid(1); } } diff --git a/src/Gui/FileDialog.h b/src/Gui/FileDialog.h index a581889fb8..14680a7048 100644 --- a/src/Gui/FileDialog.h +++ b/src/Gui/FileDialog.h @@ -89,8 +89,7 @@ private: bool hasSuffix(const QString&) const; static QList fetchSidebarUrls(); static QString workingDirectory; - static void getPossibleSuffixes(QStringList& possibleSuffixes, const QRegularExpression& rx, - const QString* suffixDescriptions); + static void getSuffixesDescription(QStringList& suffixes, const QString* suffixDescriptions); }; // ----------------------------------------------------------------------