[processing] Add metadata setting for file destination parameters

to avoid file overwrite confirmation prompt

Fixes #37011
This commit is contained in:
Nyall Dawson 2020-06-08 10:42:29 +10:00
parent 50573d5e1f
commit e81af2c9e1
3 changed files with 31 additions and 2 deletions

View File

@ -3142,6 +3142,19 @@ class QgsProcessingParameterFileDestination : QgsProcessingDestinationParameter
A generic file based destination parameter, for specifying the destination path for a file (non-map layer)
created by the algorithm.
In some circumstances it is desirable to avoid the usual file overwriting confirmation prompt when
users select an existing destination file for this parameter type (e.g., for algorithms which
append to an existing destination file instead of overwriting them.). This can be done by setting
the widget wrapper metadata "dontconfirmoverwrite" option:
.. code-block:: python
param = QgsProcessingParameterFileDestination( 'OUTPUT', 'Destination file')
# don't show the file overwrite warning when users select a destination file:
param.setMetadata( {'widget_wrapper':
{ 'dontconfirmoverwrite': True }
})
.. versionadded:: 3.0
%End

View File

@ -2986,7 +2986,21 @@ class CORE_EXPORT QgsProcessingParameterRasterDestination : public QgsProcessing
* \ingroup core
* A generic file based destination parameter, for specifying the destination path for a file (non-map layer)
* created by the algorithm.
* \since QGIS 3.0
*
* In some circumstances it is desirable to avoid the usual file overwriting confirmation prompt when
* users select an existing destination file for this parameter type (e.g., for algorithms which
* append to an existing destination file instead of overwriting them.). This can be done by setting
* the widget wrapper metadata "dontconfirmoverwrite" option:
*
* * \code{.py}
* param = QgsProcessingParameterFileDestination( 'OUTPUT', 'Destination file')
* # don't show the file overwrite warning when users select a destination file:
* param.setMetadata( {'widget_wrapper':
* { 'dontconfirmoverwrite': True }
* })
* \endcode
*
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterFileDestination : public QgsProcessingDestinationParameter
{

View File

@ -404,7 +404,9 @@ void QgsProcessingLayerOutputDestinationWidget::selectFile()
else
path = settings.value( QStringLiteral( "/Processing/Configuration/OUTPUTS_FOLDER" ) ).toString();
QString filename = QFileDialog::getSaveFileName( this, tr( "Save file" ), path, fileFilter, &lastFilter );
const bool dontConfirmOverwrite = mParameter->metadata().value( QStringLiteral( "widget_wrapper" ) ).toMap().value( QStringLiteral( "dontconfirmoverwrite" ), false ).toBool();
QString filename = QFileDialog::getSaveFileName( this, tr( "Save file" ), path, fileFilter, &lastFilter, dontConfirmOverwrite ? QFileDialog::Options( QFileDialog::DontConfirmOverwrite ) : nullptr );
if ( !filename.isEmpty() )
{
mUseTemporary = false;