From b9865ed4f1eaccad3063e726409ea221a7a67441 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 13 Sep 2015 21:25:18 +0200 Subject: [PATCH] + fixes #0002209: Files that have a dot in name do not get the extension --- src/Gui/FileDialog.cpp | 21 +++++++++++++++++++-- src/Gui/FileDialog.h | 3 +++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Gui/FileDialog.cpp b/src/Gui/FileDialog.cpp index 2372dad2e..39aff4a0a 100644 --- a/src/Gui/FileDialog.cpp +++ b/src/Gui/FileDialog.cpp @@ -69,6 +69,21 @@ void FileDialog::onSelectedFilter(const QString& filter) } } +bool FileDialog::hasSuffix(const QString& ext) const +{ + QRegExp rx(QString::fromLatin1("\\*.(%1)\\W").arg(ext)); + rx.setCaseSensitivity(Qt::CaseInsensitive); + QStringList filters = nameFilters(); + for (QStringList::iterator it = filters.begin(); it != filters.end(); ++it) { + QString str = *it; + if (rx.indexIn(str) != -1) { + return true; + } + } + + return false; +} + void FileDialog::accept() { // When saving to a file make sure that the entered filename ends with the selected @@ -78,8 +93,10 @@ void FileDialog::accept() if (!files.isEmpty()) { QString ext = this->defaultSuffix(); QString file = files.front(); - QFileInfo fi(file); - if (!ext.isEmpty() && fi.suffix().isEmpty()) { + QString suffix = QFileInfo(file).suffix(); + // #0001928: do not add a suffix if a file with suffix is entered + // #0002209: make sure that the entered suffix is part of one of the filters + if (!ext.isEmpty() && (suffix.isEmpty() || !hasSuffix(suffix))) { file = QString::fromLatin1("%1.%2").arg(file).arg(ext); // That's the built-in line edit QLineEdit* fileNameEdit = this->findChild(QString::fromLatin1("fileNameEdit")); diff --git a/src/Gui/FileDialog.h b/src/Gui/FileDialog.h index b03279d32..1ba5714e2 100644 --- a/src/Gui/FileDialog.h +++ b/src/Gui/FileDialog.h @@ -62,6 +62,9 @@ public: void accept(); +private: + bool hasSuffix(const QString&) const; + private Q_SLOTS: void onSelectedFilter(const QString&); };