From 6ee1a4238437d1a713340c19f21da891f04612ac Mon Sep 17 00:00:00 2001 From: dogmaphobic Date: Sun, 1 Feb 2015 20:20:50 -0500 Subject: [PATCH] Initial work on enforcing extension. --- src/QGCApplication.cc | 14 ++++--- src/QGCFileDialog.cc | 57 ++++++++++++++++------------- src/QGCFileDialog.h | 52 ++++++++++++++------------ src/ui/MainWindow.cc | 16 ++++---- src/ui/QGCBaseParamWidget.cc | 24 ++++++++---- src/ui/designer/QGCToolWidget.cc | 3 +- src/ui/linechart/LinechartWidget.cc | 3 +- 7 files changed, 97 insertions(+), 72 deletions(-) diff --git a/src/QGCApplication.cc b/src/QGCApplication.cc index e9f2f2559..7c1a60248 100644 --- a/src/QGCApplication.cc +++ b/src/QGCApplication.cc @@ -477,12 +477,14 @@ void QGCApplication::criticalMessageBoxOnMainThread(const QString& title, const void QGCApplication::saveTempFlightDataLogOnMainThread(QString tempLogfile) { QString defaultSuffix("mavlink"); - QString saveFilename = QGCFileDialog::getSaveFileName(MainWindow::instance(), - tr("Select file to save Flight Data Log"), - qgcApp()->mavlinkLogFilesLocation(), - tr("Flight Data Log (*.mavlink)"), - 0,0, - &defaultSuffix); + QString saveFilename = QGCFileDialog::getSaveFileName( + MainWindow::instance(), + tr("Select file to save Flight Data Log"), + qgcApp()->mavlinkLogFilesLocation(), + tr("Flight Data Log (*.mavlink)"), + 0,0, + &defaultSuffix, + true); if (!saveFilename.isEmpty()) { QFile::copy(tempLogfile, saveFilename); } diff --git a/src/QGCFileDialog.cc b/src/QGCFileDialog.cc index b3cb51805..4f5b02335 100644 --- a/src/QGCFileDialog.cc +++ b/src/QGCFileDialog.cc @@ -28,10 +28,11 @@ #include "UnitTest.h" #endif -QString QGCFileDialog::getExistingDirectory(QWidget* parent, - const QString& caption, - const QString& dir, - Options options) +QString QGCFileDialog::getExistingDirectory( + QWidget* parent, + const QString& caption, + const QString& dir, + Options options) { _validate(NULL, options); @@ -45,12 +46,13 @@ QString QGCFileDialog::getExistingDirectory(QWidget* parent, } } -QString QGCFileDialog::getOpenFileName(QWidget* parent, - const QString& caption, - const QString& dir, - const QString& filter, - QString* selectedFilter, - Options options) +QString QGCFileDialog::getOpenFileName( + QWidget* parent, + const QString& caption, + const QString& dir, + const QString& filter, + QString* selectedFilter, + Options options) { _validate(selectedFilter, options); @@ -64,12 +66,13 @@ QString QGCFileDialog::getOpenFileName(QWidget* parent, } } -QStringList QGCFileDialog::getOpenFileNames(QWidget* parent, - const QString& caption, - const QString& dir, - const QString& filter, - QString* selectedFilter, - Options options) +QStringList QGCFileDialog::getOpenFileNames( + QWidget* parent, + const QString& caption, + const QString& dir, + const QString& filter, + QString* selectedFilter, + Options options) { _validate(selectedFilter, options); @@ -84,12 +87,13 @@ QStringList QGCFileDialog::getOpenFileNames(QWidget* parent, } QString QGCFileDialog::getSaveFileName(QWidget* parent, - const QString& caption, - const QString& dir, - const QString& filter, - QString* selectedFilter, - Options options, - QString* defaultSuffix) + const QString& caption, + const QString& dir, + const QString& filter, + QString* selectedFilter, + Options options, + QString* defaultSuffix, + bool strict) { _validate(selectedFilter, options); @@ -114,10 +118,13 @@ QString QGCFileDialog::getSaveFileName(QWidget* parent, } dlg.setDefaultSuffix(*defaultSuffix); } - if (dlg.exec()) { - if (dlg.selectedFiles().count()) { - return dlg.selectedFiles().first(); + while (true) { + if (dlg.exec()) { + if (dlg.selectedFiles().count()) { + return dlg.selectedFiles().first(); + } } + break; } return QString(""); } diff --git a/src/QGCFileDialog.h b/src/QGCFileDialog.h index 7dea53cae..d7a68227c 100644 --- a/src/QGCFileDialog.h +++ b/src/QGCFileDialog.h @@ -50,10 +50,11 @@ public: @return The chosen path or \c QString("") if none. @sa QFileDialog::getExistingDirectory() */ - static QString getExistingDirectory(QWidget* parent = 0, - const QString& caption = QString(), - const QString& dir = QString(), - Options options = ShowDirsOnly); + static QString getExistingDirectory( + QWidget* parent = 0, + const QString& caption = QString(), + const QString& dir = QString(), + Options options = ShowDirsOnly); //! Static helper that invokes a File Open dialog where the user can select a file to be opened. /*! @@ -66,12 +67,13 @@ public: @return The full path and filename to be opened or \c QString("") if none. @sa QFileDialog::getOpenFileName() */ - static QString getOpenFileName(QWidget* parent = 0, - const QString& caption = QString(), - const QString& dir = QString(), - const QString& filter = QString(), - QString* selectedFilter = 0, - Options options = 0); + static QString getOpenFileName( + QWidget* parent = 0, + const QString& caption = QString(), + const QString& dir = QString(), + const QString& filter = QString(), + QString* selectedFilter = 0, + Options options = 0); //! Static helper that invokes a File Open dialog where the user can select one or more files to be opened. /*! @@ -84,12 +86,13 @@ public: @return A QStringList object containing zero or more files to be opened. @sa QFileDialog::getOpenFileNames() */ - static QStringList getOpenFileNames(QWidget* parent = 0, - const QString& caption = QString(), - const QString& dir = QString(), - const QString& filter = QString(), - QString* selectedFilter = 0, - Options options = 0); + static QStringList getOpenFileNames( + QWidget* parent = 0, + const QString& caption = QString(), + const QString& dir = QString(), + const QString& filter = QString(), + QString* selectedFilter = 0, + Options options = 0); //! Static helper that invokes a File Save dialog where the user can select a directory and enter a filename to be saved. /*! @@ -100,18 +103,21 @@ public: @param[out] selectedFilter **NOT IMPLEMENTED - Set to NULL** Returns the filter that the user selected in the file dialog. @param[in] options Set the various options that affect the look and feel of the dialog. @param[in] defaultSuffix Specifies a string that will be added to the filename if it has no suffix already. The suffix is typically used to indicate the file type (e.g. "txt" indicates a text file). + @param[in] strict Makes the default suffix mandatory. Only files with those extensions will be allowed. @return The full path and filename to be used to save the file or \c QString("") if none. @sa QFileDialog::getSaveFileName() @remark If a default suffix is given, it will be appended to the filename if the user does not enter one themselves. That is, if the user simply enters \e foo and the default suffix is set to \e bar, the returned filename will be \e foo.bar. However, if the user specifies a suffix, none will be added. That is, if the user enters \e foo.txt, that's what you will receive in return. */ - static QString getSaveFileName(QWidget* parent = 0, - const QString& caption = QString(), - const QString& dir = QString(), - const QString& filter = QString(), - QString* selectedFilter = 0, - Options options = 0, - QString* defaultSuffix = 0); + static QString getSaveFileName( + QWidget* parent = 0, + const QString& caption = QString(), + const QString& dir = QString(), + const QString& filter = QString(), + QString* selectedFilter = 0, + Options options = 0, + QString* defaultSuffix = 0, + bool strict = false); private slots: /// @brief The exec slot is private becasue when only want QGCFileDialog users to use the static methods. Otherwise it will break diff --git a/src/ui/MainWindow.cc b/src/ui/MainWindow.cc index 8e59022bd..ce0f31184 100644 --- a/src/ui/MainWindow.cc +++ b/src/ui/MainWindow.cc @@ -998,14 +998,14 @@ void MainWindow::startVideoCapture() { QString format("bmp"); QString initialPath = QDir::currentPath() + tr("/untitled.") + format; - - QString screenFileName = QGCFileDialog::getSaveFileName(this, tr("Save As"), - initialPath, - tr("%1 Files (*.%2);;All Files (*)") - .arg(format.toUpper()) - .arg(format), - 0,0, - &format); + QString screenFileName = QGCFileDialog::getSaveFileName( + this, tr("Save As"), + initialPath, + tr("%1 Files (*.%2);;All Files (*)") + .arg(format.toUpper()) + .arg(format), + 0,0, + &format); delete videoTimer; videoTimer = new QTimer(this); } diff --git a/src/ui/QGCBaseParamWidget.cc b/src/ui/QGCBaseParamWidget.cc index 2d8c96997..0e6824522 100644 --- a/src/ui/QGCBaseParamWidget.cc +++ b/src/ui/QGCBaseParamWidget.cc @@ -106,15 +106,23 @@ void QGCBaseParamWidget::saveParametersToFile() if (!mav) return; QString defaultSuffix("txt"); - QString fileName = QGCFileDialog::getSaveFileName(this, tr("Save File"), qgcApp()->savedParameterFilesLocation(), tr("Parameter File (*.txt)"), 0, 0, &defaultSuffix); - QFile file(fileName); - if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { - return; + QString fileName = QGCFileDialog::getSaveFileName( + this, + tr("Save Parameters"), + qgcApp()->savedParameterFilesLocation(), + tr("Parameter File (*.txt)"), + 0, 0, + &defaultSuffix, + true); + if (!fileName.isEmpty()) { + QFile file(fileName); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { + return; + } + QTextStream outstream(&file); + paramMgr->writeOnboardParamsToStream(outstream,mav->getUASName()); + file.close(); } - - QTextStream outstream(&file); - paramMgr->writeOnboardParamsToStream(outstream,mav->getUASName()); - file.close(); } diff --git a/src/ui/designer/QGCToolWidget.cc b/src/ui/designer/QGCToolWidget.cc index 61c565c77..7cc942ec3 100644 --- a/src/ui/designer/QGCToolWidget.cc +++ b/src/ui/designer/QGCToolWidget.cc @@ -578,7 +578,8 @@ void QGCToolWidget::exportWidget() QStandardPaths::writableLocation(QStandardPaths::DesktopLocation), tr("QGroundControl Widget (*%1)").arg(widgetFileExtension), 0,0, - &defaultSuffix); + &defaultSuffix, + true); //-- Note that if the user enters foo.bar, this will end up foo.bar.qgw if (!fileName.endsWith(widgetFileExtension)) { diff --git a/src/ui/linechart/LinechartWidget.cc b/src/ui/linechart/LinechartWidget.cc index 0975bb721..02a588759 100644 --- a/src/ui/linechart/LinechartWidget.cc +++ b/src/ui/linechart/LinechartWidget.cc @@ -439,7 +439,8 @@ QString LinechartWidget::getLogSaveFilename() QStandardPaths::writableLocation(QStandardPaths::DesktopLocation), tr("Logfile (*.log)"), 0,0, - &defaultSuffix); + &defaultSuffix, + true); return fileName; } -- 2.22.0