Skip to content

Commit

Permalink
[file widget] add a save file mode to allow file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Dec 7, 2017
1 parent bac80aa commit cddca2f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/gui/qgsfilewidget.cpp
Expand Up @@ -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( ) )
Expand All @@ -283,6 +294,7 @@ void QgsFileWidget::openFileDialog()
switch ( mStorageMode )
{
case GetFile:
case SaveFile:
settings.setValue( QStringLiteral( "UI/lastFileNameWidgetDir" ), QFileInfo( fileName ).absolutePath() );
break;
case GetDirectory:
Expand Down
13 changes: 13 additions & 0 deletions src/gui/qgsfilewidget.h
Expand Up @@ -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
};

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit cddca2f

Please sign in to comment.