[file widget] add a save file mode to allow file creation

This commit is contained in:
nirvn 2017-12-07 11:22:03 +07:00
parent bac80aa93a
commit cddca2f850
2 changed files with 25 additions and 0 deletions

View File

@ -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:

View File

@ -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;