From cddca2f8501ab1c98ec776316a7c5cba9f384757 Mon Sep 17 00:00:00 2001 From: nirvn Date: Thu, 7 Dec 2017 11:22:03 +0700 Subject: [PATCH] [file widget] add a save file mode to allow file creation --- src/gui/qgsfilewidget.cpp | 12 ++++++++++++ src/gui/qgsfilewidget.h | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/gui/qgsfilewidget.cpp b/src/gui/qgsfilewidget.cpp index 960bd3ba132..df599d13ede 100644 --- a/src/gui/qgsfilewidget.cpp +++ b/src/gui/qgsfilewidget.cpp @@ -262,6 +262,17 @@ void QgsFileWidget::openFileDialog() title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a directory" ); fileName = QFileDialog::getExistingDirectory( this, title, QFileInfo( oldPath ).absoluteFilePath(), QFileDialog::ShowDirsOnly ); break; + case SaveFile: + title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Create or select a file" ); + if ( !confirmOverwrite() ) + { + fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, nullptr, QFileDialog::DontConfirmOverwrite ); + } + else + { + fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter ); + } + break; } if ( fileName.isEmpty() && fileNames.isEmpty( ) ) @@ -283,6 +294,7 @@ void QgsFileWidget::openFileDialog() switch ( mStorageMode ) { case GetFile: + case SaveFile: settings.setValue( QStringLiteral( "UI/lastFileNameWidgetDir" ), QFileInfo( fileName ).absolutePath() ); break; case GetDirectory: diff --git a/src/gui/qgsfilewidget.h b/src/gui/qgsfilewidget.h index 1c81aa523db..f1dcaf76d19 100644 --- a/src/gui/qgsfilewidget.h +++ b/src/gui/qgsfilewidget.h @@ -65,6 +65,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget GetFile, //! Select a single file GetDirectory, //! Select a directory GetMultipleFiles, //! Select multiple files + SaveFile, //! Select a single new or pre-existing file }; /** @@ -120,6 +121,17 @@ class GUI_EXPORT QgsFileWidget : public QWidget */ void setFilter( const QString &filter ); + /** + * Sets whether a confirmation to overwrite an existing file will appear + * \param confirmOverwrite If set to true, an overwrite confirmation will be shown + */ + void setConfirmOverwrite( bool confirmOverwrite ) { mConfirmOverwrite = confirmOverwrite; } + + /** + * Returns whether a confirmation will be shown when overwriting an existing file + */ + bool confirmOverwrite() const { return mConfirmOverwrite; } + //! determines if the tool button is shown bool fileWidgetButtonVisible() const; //! determines if the tool button is shown @@ -173,6 +185,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget QString mDialogTitle; QString mFilter; QString mDefaultRoot; + bool mConfirmOverwrite = true; StorageMode mStorageMode = GetFile; RelativeStorage mRelativeStorage = Absolute;