diff --git a/src/QGCApplication.cc b/src/QGCApplication.cc index 8c6ad72..b9bf6ba 100644 --- a/src/QGCApplication.cc +++ b/src/QGCApplication.cc @@ -476,14 +476,12 @@ 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, - &defaultSuffix); + "mavlink"); if (!saveFilename.isEmpty()) { QFile::copy(tempLogfile, saveFilename); } diff --git a/src/QGCFileDialog.cc b/src/QGCFileDialog.cc index 45a934e..5888dde 100644 --- a/src/QGCFileDialog.cc +++ b/src/QGCFileDialog.cc @@ -89,14 +89,14 @@ QString QGCFileDialog::getSaveFileName( const QString& caption, const QString& dir, const QString& filter, - Options options, - QString* defaultSuffix) + const QString& defaultSuffix, + Options options) { _validate(options); #ifdef QT_DEBUG if (qgcApp()->runningUnitTests()) { - return UnitTest::_getSaveFileName(parent, caption, dir, filter, options, defaultSuffix); + return UnitTest::_getSaveFileName(parent, caption, dir, filter, defaultSuffix, options); } else #endif { @@ -105,12 +105,13 @@ QString QGCFileDialog::getSaveFileName( if (options) { dlg.setOptions(options); } - if (defaultSuffix) { + if (!defaultSuffix.isEmpty()) { + QString suffixCopy(defaultSuffix); //-- Make sure dot is not present - if (defaultSuffix->startsWith(".")) { - defaultSuffix->remove(0,1); + if (suffixCopy.startsWith(".")) { + suffixCopy.remove(0,1); } - dlg.setDefaultSuffix(*defaultSuffix); + dlg.setDefaultSuffix(suffixCopy); } if (dlg.exec()) { if (dlg.selectedFiles().count()) { diff --git a/src/QGCFileDialog.h b/src/QGCFileDialog.h index 0351da9..86069ad 100644 --- a/src/QGCFileDialog.h +++ b/src/QGCFileDialog.h @@ -31,10 +31,21 @@ /// @author Don Gagne /*! - Subclass of QFileDialog which re-implements the static public functions. The reason for this - is that the QFileDialog implementations of these use the native os dialogs. On OSX these - these can intermittently hang. So instead here we use the native dialogs. It also allows - use to catch these dialogs for unit testing. + Subclass of QFileDialog which re-implements the static public functions. The reason for this + is that the QFileDialog implementations of these use the native os dialogs. On OSX these + these can intermittently hang. So instead here we use the native dialogs. It also allows + use to catch these dialogs for unit testing. + @remark If you need to know what type of file was returned by these functions, you can use something like: + @code{.cpp} + QString filename = QGCFileDialog::getSaveFileName(this, tr("Save File"), "~/", "Foo files (*.foo);;All Files (*.*)", "foo"); + if (!filename.isEmpty()) { + QFileInfo fi(filename); + QString fileExtension(fi.suffix()); + if (fileExtension == QString("foo")) { + // do something + } + } + @endcode */ class QGCFileDialog : public QFileDialog { @@ -96,8 +107,8 @@ public: @param[in] caption The caption displayed at the top of the dialog. @param[in] dir The initial directory shown to the user. @param[in] filter The filter used for selecting the file type. - @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] options Set the various options that affect the look and feel of the dialog. @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, @@ -108,9 +119,9 @@ public: const QString& caption = QString(), const QString& dir = QString(), const QString& filter = QString(), - Options options = 0, - QString* defaultSuffix = 0); - + const QString& defaultSuffix = QString(), + Options options = 0); + private slots: /// @brief The exec slot is private becasue we only want QGCFileDialog users to use the static methods. Otherwise it will break /// unit testing. diff --git a/src/qgcunittest/UnitTest.cc b/src/qgcunittest/UnitTest.cc index d8e9c15..98fb249 100644 --- a/src/qgcunittest/UnitTest.cc +++ b/src/qgcunittest/UnitTest.cc @@ -340,8 +340,8 @@ QString UnitTest::_getSaveFileName( const QString& caption, const QString& dir, const QString& filter, - QFileDialog::Options options, - QString* defaultSuffix) + const QString& defaultSuffix, + QFileDialog::Options options) { Q_UNUSED(parent); Q_UNUSED(caption); @@ -349,8 +349,8 @@ QString UnitTest::_getSaveFileName( Q_UNUSED(filter); Q_UNUSED(options); - if(defaultSuffix) - Q_ASSERT(defaultSuffix->startsWith(".") == false); + if(!defaultSuffix.isEmpty()) + Q_ASSERT(defaultSuffix.startsWith(".") == false); return _fileDialogResponseSingle(getSaveFileName); } diff --git a/src/qgcunittest/UnitTest.h b/src/qgcunittest/UnitTest.h index ce50b5b..bbc78ac 100644 --- a/src/qgcunittest/UnitTest.h +++ b/src/qgcunittest/UnitTest.h @@ -143,9 +143,9 @@ private: const QString& caption, const QString& dir, const QString& filter, - QFileDialog::Options options, - QString* defaultSuffix); - + const QString& defaultSuffix, + QFileDialog::Options options); + static QString _fileDialogResponseSingle(enum FileDialogType type); // This allows the private calls to the file dialog methods diff --git a/src/ui/MainWindow.cc b/src/ui/MainWindow.cc index f943edb..d5bc4f9 100644 --- a/src/ui/MainWindow.cc +++ b/src/ui/MainWindow.cc @@ -846,8 +846,7 @@ void MainWindow::startVideoCapture() tr("%1 Files (*.%2);;All Files (*)") .arg(format.toUpper()) .arg(format), - 0, - &format); + format); delete videoTimer; videoTimer = new QTimer(this); } diff --git a/src/ui/QGCBaseParamWidget.cc b/src/ui/QGCBaseParamWidget.cc index ac39a1c..a70db99 100644 --- a/src/ui/QGCBaseParamWidget.cc +++ b/src/ui/QGCBaseParamWidget.cc @@ -105,8 +105,7 @@ void QGCBaseParamWidget::saveParametersToFile() { if (!mav) return; - QString defaultSuffix("txt"); - QString fileName = QGCFileDialog::getSaveFileName(this, tr("Save Parameters"), qgcApp()->savedParameterFilesLocation(), tr("Parameter File (*.txt)"), 0, &defaultSuffix); + QString fileName = QGCFileDialog::getSaveFileName(this, tr("Save Parameters"), qgcApp()->savedParameterFilesLocation(), tr("Parameter File (*.txt)"), "txt"); QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { return; diff --git a/src/ui/QGCDataPlot2D.cc b/src/ui/QGCDataPlot2D.cc index 236f134..1f5318e 100644 --- a/src/ui/QGCDataPlot2D.cc +++ b/src/ui/QGCDataPlot2D.cc @@ -115,12 +115,10 @@ void QGCDataPlot2D::loadFile(QString file) */ QString QGCDataPlot2D::getSavePlotFilename() { - QString defaultSuffix("pdf"); QString fileName = QGCFileDialog::getSaveFileName( this, "Export File Name", QStandardPaths::writableLocation(QStandardPaths::DesktopLocation), "PDF Documents (*.pdf);;SVG Images (*.svg)", - 0, - &defaultSuffix); + "pdf"); return fileName; } @@ -690,12 +688,10 @@ bool QGCDataPlot2D::linearRegression(double *x, double *y, int n, double *a, dou void QGCDataPlot2D::saveCsvLog() { - QString defaultSuffix("csv"); QString fileName = QGCFileDialog::getSaveFileName( this, "Export CSV File Name", QStandardPaths::writableLocation(QStandardPaths::DesktopLocation), "CSV file (*.csv);;Text file (*.txt)", - 0, - &defaultSuffix); + "csv"); if (fileName.isEmpty()) return; //User cancelled diff --git a/src/ui/WaypointList.cc b/src/ui/WaypointList.cc index 30e7abe..d5091f3 100644 --- a/src/ui/WaypointList.cc +++ b/src/ui/WaypointList.cc @@ -217,8 +217,7 @@ void WaypointList::setUAS(UASInterface* uas) void WaypointList::saveWaypoints() { - QString defaultSuffix("txt"); - QString fileName = QGCFileDialog::getSaveFileName(this, tr("Save File"), "./waypoints.txt", tr("Waypoint File (*.txt)"), 0, &defaultSuffix); + QString fileName = QGCFileDialog::getSaveFileName(this, tr("Save File"), "./waypoints.txt", tr("Waypoint File (*.txt)"), "txt"); WPM->saveWaypoints(fileName); } diff --git a/src/ui/designer/QGCToolWidget.cc b/src/ui/designer/QGCToolWidget.cc index 34e94f9..be7bd47 100644 --- a/src/ui/designer/QGCToolWidget.cc +++ b/src/ui/designer/QGCToolWidget.cc @@ -571,14 +571,12 @@ void QGCToolWidget::widgetRemoved() void QGCToolWidget::exportWidget() { - QString defaultSuffix("qgw"); const QString widgetFileExtension(".qgw"); QString fileName = QGCFileDialog::getSaveFileName( this, tr("Specify Widget File Name"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation), tr("QGroundControl Widget (*%1)").arg(widgetFileExtension), - 0, - &defaultSuffix); + "qgw"); //-- 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 fa39e9b..252277a 100644 --- a/src/ui/linechart/LinechartWidget.cc +++ b/src/ui/linechart/LinechartWidget.cc @@ -433,13 +433,11 @@ void LinechartWidget::refresh() QString LinechartWidget::getLogSaveFilename() { - QString defaultSuffix("log"); QString fileName = QGCFileDialog::getSaveFileName(this, tr("Specify Log File Name"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation), tr("Log file (*.log)"), - 0, - &defaultSuffix); + "log"); return fileName; }