diff options
| author | Oliver Eftevaag <oliver.eftevaag@qt.io> | 2023-12-11 19:47:24 +0100 |
|---|---|---|
| committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-12-24 01:27:15 +0000 |
| commit | 6e22393819cb8c4d381a130bbab12464935dd936 (patch) | |
| tree | 7202e2f1426dcea0d01fe83263826e3f1749de79 /src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp | |
| parent | b1569d546515ebbf60067f713e03f6ac1d3770c7 (diff) | |
FileDialog: prompt the user when selecting an existing file
It is common for editing software to prompt the user when he/she wishes
to save the work done in the editor, and an existing file is selected in
the file dialog. This is an extra safety step, to hopefully prevent the
user from accidentally shooting himself/herself in the foot, by
overwriting an existing file and losing something valuable.
One of the available file dialog option is DontConfirmOverwrite.
Which according to the documentation, could be set in order to bypass
a confirmation which is supposed to show up by default.
But that weren't the case when using the non-native quick file dialog.
The FileDialog will now show that confirmation dialog as a popup
dialog, which popups up on top of the FileDialog, when selecting an
existing file using the SaveFile file mode.
The DontConfirmOverwrite option can now be used as intended, which will
make the FileDialog behave like it used to, when selecting an existing
file.
In additon, hitting enter while the file dialog list view has focus,
will now function the same as clicking the "Open" button. This was done
in order to prevent the user from being able to bypass the new
confirmation dialog.
Fixes: QTBUG-119916
Change-Id: I07710a7126c53f489fd5554ea21e7684244a93c1
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 1cc20d181bdee1131bf2eb191e7f8fe4e4927e03)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit d340459e7cd7b4ec9e7ae0e5166ad8879d22bdf7)
(cherry picked from commit 025c130c1ed67b9dbd96cacac2de80e90ea07e1b)
Diffstat (limited to 'src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp')
| -rw-r--r-- | src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp | 60 |
1 files changed, 56 insertions, 4 deletions
diff --git a/src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp b/src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp index 5b8affcf1b..d190d3dd73 100644 --- a/src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp +++ b/src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp @@ -238,14 +238,35 @@ void QQuickFileDialogImplPrivate::handleClick(QQuickAbstractButton *button) // Don't call accept(), because selecting a folder != accepting the dialog. } else { // Otherwise it's a file, so select it and close the dialog. - q->setSelectedFile(selectedFile); - q->accept(); - QQuickDialogPrivate::handleClick(button); - emit q->fileSelected(selectedFile); + + lastButtonClicked = button; + + // Unless it already exists... + const bool dontConfirmOverride = q->options()->testOption(QFileDialogOptions::DontConfirmOverwrite); + const bool isSaveMode = q->options()->fileMode() == QFileDialogOptions::AnyFile; + if (QQuickFileDialogImplAttached *attached = attachedOrWarn(); + attached && fileInfo.exists() && isSaveMode && !dontConfirmOverride) { + QQuickDialog *confirmationDialog = attached->overwriteConfirmationDialog(); + confirmationDialog->open(); + static_cast<QQuickDialogButtonBox *>(confirmationDialog->footer())->standardButton(QPlatformDialogHelper::Yes) + ->forceActiveFocus(Qt::PopupFocusReason); + } else { + selectFile(); + } } } } +void QQuickFileDialogImplPrivate::selectFile() +{ + Q_Q(QQuickFileDialogImpl); + Q_ASSERT(lastButtonClicked); + q->setSelectedFile(selectedFile); + q->accept(); + QQuickDialogPrivate::handleClick(lastButtonClicked); + emit q->fileSelected(selectedFile); +} + QQuickFileDialogImpl::QQuickFileDialogImpl(QObject *parent) : QQuickDialog(*(new QQuickFileDialogImplPrivate), parent) { @@ -473,6 +494,11 @@ void QQuickFileDialogImpl::setFileName(const QString &fileName) setSelectedFile(QUrl(currentFolder().path() + u'/' + fileName)); } +QString QQuickFileDialogImpl::currentFolderName() const +{ + return QDir(currentFolder().toLocalFile()).dirName(); +} + void QQuickFileDialogImpl::componentComplete() { Q_D(QQuickFileDialogImpl); @@ -756,6 +782,32 @@ void QQuickFileDialogImplAttached::setFileNameTextField(QQuickTextField *fileNam emit fileNameTextFieldChanged(); } +QQuickDialog *QQuickFileDialogImplAttached::overwriteConfirmationDialog() const +{ + Q_D(const QQuickFileDialogImplAttached); + return d->overwriteConfirmationDialog; +} + +void QQuickFileDialogImplAttached::setOverwriteConfirmationDialog(QQuickDialog *dialog) +{ + Q_D(QQuickFileDialogImplAttached); + if (dialog == d->overwriteConfirmationDialog) + return; + + QQuickFileDialogImpl *fileDialogImpl = qobject_cast<QQuickFileDialogImpl*>(parent()); + if (d->overwriteConfirmationDialog && fileDialogImpl) + QObjectPrivate::disconnect(d->overwriteConfirmationDialog, &QQuickDialog::accepted, + QQuickFileDialogImplPrivate::get(fileDialogImpl), &QQuickFileDialogImplPrivate::selectFile); + + d->overwriteConfirmationDialog = dialog; + + if (d->overwriteConfirmationDialog && fileDialogImpl) + QObjectPrivate::connect(d->overwriteConfirmationDialog, &QQuickDialog::accepted, + QQuickFileDialogImplPrivate::get(fileDialogImpl), &QQuickFileDialogImplPrivate::selectFile, Qt::QueuedConnection); + + emit overwriteConfirmationDialogChanged(); +} + QT_END_NAMESPACE #include "moc_qquickfiledialogimpl_p.cpp" |
