aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp
diff options
context:
space:
mode:
authorOliver Eftevaag <oliver.eftevaag@qt.io>2023-12-11 19:47:24 +0100
committerOliver Eftevaag <oliver.eftevaag@qt.io>2023-12-22 04:12:14 +0100
commit1cc20d181bdee1131bf2eb191e7f8fe4e4927e03 (patch)
treea3dc98e24a64b2b9e8d85ca8f73a1c9dac721f8d /src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp
parent958de6c485f7c9f1bd8d4e1eee879c8bad3298bc (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 Pick-to: 6.7 6.6 6.5 Change-Id: I07710a7126c53f489fd5554ea21e7684244a93c1 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp')
-rw-r--r--src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp60
1 files changed, 56 insertions, 4 deletions
diff --git a/src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp b/src/quickdialogs/quickdialogsquickimpl/qquickfiledialogimpl.cpp
index 977f864b6a..c2f5531b9a 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);
@@ -771,6 +797,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"