QFileDialog: split some code out of accept() to smaller functions

Easier to reason about.

Change-Id: I4f0351f405517cee522fc2159eb4014963a8946c
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Ahmad Samir 2022-10-15 02:49:20 +02:00
parent 91d1ec3589
commit 7396f36e42
2 changed files with 33 additions and 18 deletions

View File

@ -2667,6 +2667,32 @@ void QFileDialog::done(int result)
d->signalToDisconnectOnClose.clear(); d->signalToDisconnectOnClose.clear();
} }
bool QFileDialogPrivate::itemAlreadyExists(const QString &fileName)
{
#if QT_CONFIG(messagebox)
Q_Q(QFileDialog);
const QString msg = QFileDialog::tr("%1 already exists.\nDo you want to replace it?").arg(fileName);
using B = QMessageBox;
const auto res = B::warning(q, q->windowTitle(), msg, B::Yes | B::No, B::No);
return res == B::Yes;
#endif
return false;
}
void QFileDialogPrivate::itemNotFound(const QString &fileName, QFileDialog::FileMode mode)
{
#if QT_CONFIG(messagebox)
Q_Q(QFileDialog);
const QString message = mode == QFileDialog::Directory
? QFileDialog::tr("%1\nDirectory not found.\n"
"Please verify the correct directory name was given.")
: QFileDialog::tr("%1\nFile not found.\nPlease verify the "
"correct file name was given.");
QMessageBox::warning(q, q->windowTitle(), message.arg(fileName));
#endif // QT_CONFIG(messagebox)
}
/*! /*!
\reimp \reimp
*/ */
@ -2697,18 +2723,15 @@ void QFileDialog::accept()
return; return;
} }
switch (fileMode()) { const auto mode = fileMode();
switch (mode) {
case Directory: { case Directory: {
QString fn = files.first(); QString fn = files.first();
QFileInfo info(fn); QFileInfo info(fn);
if (!info.exists()) if (!info.exists())
info = QFileInfo(d->getEnvironmentVariable(fn)); info = QFileInfo(d->getEnvironmentVariable(fn));
if (!info.exists()) { if (!info.exists()) {
#if QT_CONFIG(messagebox) d->itemNotFound(info.fileName(), mode);
QString message = tr("%1\nDirectory not found.\nPlease verify the "
"correct directory name was given.");
QMessageBox::warning(this, windowTitle(), message.arg(info.fileName()));
#endif // QT_CONFIG(messagebox)
return; return;
} }
if (info.isDir()) { if (info.isDir()) {
@ -2736,17 +2759,11 @@ void QFileDialog::accept()
if (!info.exists() || testOption(DontConfirmOverwrite) || acceptMode() == AcceptOpen) { if (!info.exists() || testOption(DontConfirmOverwrite) || acceptMode() == AcceptOpen) {
d->emitFilesSelected(QStringList(fn)); d->emitFilesSelected(QStringList(fn));
QDialog::accept(); QDialog::accept();
#if QT_CONFIG(messagebox)
} else { } else {
if (QMessageBox::warning(this, windowTitle(), if (d->itemAlreadyExists(info.fileName())) {
tr("%1 already exists.\nDo you want to replace it?")
.arg(info.fileName()),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
== QMessageBox::Yes) {
d->emitFilesSelected(QStringList(fn)); d->emitFilesSelected(QStringList(fn));
QDialog::accept(); QDialog::accept();
} }
#endif
} }
return; return;
} }
@ -2758,11 +2775,7 @@ void QFileDialog::accept()
if (!info.exists()) if (!info.exists())
info = QFileInfo(d->getEnvironmentVariable(file)); info = QFileInfo(d->getEnvironmentVariable(file));
if (!info.exists()) { if (!info.exists()) {
#if QT_CONFIG(messagebox) d->itemNotFound(info.fileName(), mode);
QString message = tr("%1\nFile not found.\nPlease verify the "
"correct file name was given.");
QMessageBox::warning(this, windowTitle(), message.arg(info.fileName()));
#endif // QT_CONFIG(messagebox)
return; return;
} }
if (info.isDir()) { if (info.isDir()) {

View File

@ -259,6 +259,8 @@ private:
virtual void helperPrepareShow(QPlatformDialogHelper *) override; virtual void helperPrepareShow(QPlatformDialogHelper *) override;
virtual void helperDone(QDialog::DialogCode, QPlatformDialogHelper *) override; virtual void helperDone(QDialog::DialogCode, QPlatformDialogHelper *) override;
void itemNotFound(const QString &fileName, QFileDialog::FileMode mode);
bool itemAlreadyExists(const QString &fileName);
Q_DISABLE_COPY_MOVE(QFileDialogPrivate) Q_DISABLE_COPY_MOVE(QFileDialogPrivate)
}; };