diff --git a/src/QGCFileDialog.cc b/src/QGCFileDialog.cc index 4f5b02335d6ec6156acc39ee5ae9f3516d553171..cd5ffae026fb516960ed132e135c9728f5c396fd 100644 --- a/src/QGCFileDialog.cc +++ b/src/QGCFileDialog.cc @@ -23,6 +23,7 @@ #include "QGCFileDialog.h" #include "QGCApplication.h" +#include "QRegularExpression.h" #include "MainWindow.h" #ifdef QT_DEBUG #include "UnitTest.h" @@ -105,9 +106,6 @@ QString QGCFileDialog::getSaveFileName(QWidget* parent, { QFileDialog dlg(parent, caption, dir, filter); dlg.setAcceptMode(QFileDialog::AcceptSave); - if (selectedFilter) { - dlg.selectNameFilter(*selectedFilter); - } if (options) { dlg.setOptions(options); } @@ -121,7 +119,45 @@ QString QGCFileDialog::getSaveFileName(QWidget* parent, while (true) { if (dlg.exec()) { if (dlg.selectedFiles().count()) { - return dlg.selectedFiles().first(); + QString result = dlg.selectedFiles().first(); + //-- If we don't care about the extension, just return it + if (!strict) { + return result; + } else { + //-- We must enforce the file extension + QFileInfo fi(result); + QString userSuffix(fi.suffix()); + if (_validateExtension(filter, userSuffix)) { + return result; + } +#if 0 //-- If we ever want to propose replacing the extension, we can guess the replacement below + //-- Do we have a default extension? + QString localDefaultSuffix; + if (!defaultSuffix) { + //-- We don't, so get the first one in the filter + localDefaultSuffix = _getFirstExtensionInFilter(filter); + defaultSuffix = &localDefaultSuffix; + } + Q_ASSERT(defaultSuffix->isEmpty() == false); + QString proposed = fi.completeBaseName(); + proposed += "."; + proposed += *defaultSuffix; +#endif + //-- Ask user what to do + QMessageBox msgBox( + QMessageBox::Critical, + tr("Invalid File Extension."), + tr("The given extension (.%1) is not a valid extension for this type of file.").arg(userSuffix), + QMessageBox::Cancel, + parent); + msgBox.setDefaultButton(QMessageBox::Cancel); + msgBox.setWindowModality(Qt::ApplicationModal); + QPushButton *retryButton = msgBox.addButton(tr("Retry"), QMessageBox::ActionRole); + msgBox.exec(); + if (msgBox.clickedButton() == retryButton) { + continue; + } + } } } break; @@ -130,6 +166,35 @@ QString QGCFileDialog::getSaveFileName(QWidget* parent, } } +/// @brief Make sure filename is using one of the valid extensions defined in the filter +bool QGCFileDialog::_validateExtension(const QString& filter, const QString& extension) { + QRegularExpression re("(\\*\\.\\w+)"); + QRegularExpressionMatchIterator i = re.globalMatch(filter); + while (i.hasNext()) { + QRegularExpressionMatch match = i.next(); + if (match.hasMatch()) { + //-- Compare "foo" with "*.foo" + if(extension == match.captured(0).mid(2)) { + return true; + } + } + } + return false; +} + +/// @brief Returns first extension found in filter +QString QGCFileDialog::_getFirstExtensionInFilter(const QString& filter) { + QRegularExpression re("(\\*\\.\\w+)"); + QRegularExpressionMatchIterator i = re.globalMatch(filter); + while (i.hasNext()) { + QRegularExpressionMatch match = i.next(); + if (match.hasMatch()) { + return match.captured(0).mid(1); + } + } + return QString(""); +} + /// @brief Validates and updates the parameters for the file dialog calls void QGCFileDialog::_validate(QString* selectedFilter, Options& options) { diff --git a/src/QGCFileDialog.h b/src/QGCFileDialog.h index d7a68227c06b510b99ad02f04a5b351c48b81602..a55c92332ceea5e476ec7e3d5542af5c85697821 100644 --- a/src/QGCFileDialog.h +++ b/src/QGCFileDialog.h @@ -125,7 +125,9 @@ private slots: int exec(void) { return QGCFileDialog::exec(); } private: - static void _validate(QString* selectedFilter, Options& options); + static void _validate(QString* selectedFilter, Options& options); + static bool _validateExtension(const QString& filter, const QString& extension); + static QString _getFirstExtensionInFilter(const QString& filter); };