From 7396f36e42df0d7d8b33ef43f888c695ecb277cf Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sat, 15 Oct 2022 02:49:20 +0200 Subject: QFileDialog: split some code out of accept() to smaller functions Easier to reason about. Change-Id: I4f0351f405517cee522fc2159eb4014963a8946c Reviewed-by: Volker Hilsheimer --- src/widgets/dialogs/qfiledialog.cpp | 49 +++++++++++++++++++++++-------------- src/widgets/dialogs/qfiledialog_p.h | 2 ++ 2 files changed, 33 insertions(+), 18 deletions(-) (limited to 'src/widgets/dialogs') diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index 6974edcaec..d007f1afc9 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -2667,6 +2667,32 @@ void QFileDialog::done(int result) 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 */ @@ -2697,18 +2723,15 @@ void QFileDialog::accept() return; } - switch (fileMode()) { + const auto mode = fileMode(); + switch (mode) { case Directory: { QString fn = files.first(); QFileInfo info(fn); if (!info.exists()) info = QFileInfo(d->getEnvironmentVariable(fn)); if (!info.exists()) { -#if QT_CONFIG(messagebox) - 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) + d->itemNotFound(info.fileName(), mode); return; } if (info.isDir()) { @@ -2736,17 +2759,11 @@ void QFileDialog::accept() if (!info.exists() || testOption(DontConfirmOverwrite) || acceptMode() == AcceptOpen) { d->emitFilesSelected(QStringList(fn)); QDialog::accept(); -#if QT_CONFIG(messagebox) } else { - if (QMessageBox::warning(this, windowTitle(), - tr("%1 already exists.\nDo you want to replace it?") - .arg(info.fileName()), - QMessageBox::Yes | QMessageBox::No, QMessageBox::No) - == QMessageBox::Yes) { + if (d->itemAlreadyExists(info.fileName())) { d->emitFilesSelected(QStringList(fn)); QDialog::accept(); } -#endif } return; } @@ -2758,11 +2775,7 @@ void QFileDialog::accept() if (!info.exists()) info = QFileInfo(d->getEnvironmentVariable(file)); if (!info.exists()) { -#if QT_CONFIG(messagebox) - 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) + d->itemNotFound(info.fileName(), mode); return; } if (info.isDir()) { diff --git a/src/widgets/dialogs/qfiledialog_p.h b/src/widgets/dialogs/qfiledialog_p.h index 3bd2fb99c2..9a12574a78 100644 --- a/src/widgets/dialogs/qfiledialog_p.h +++ b/src/widgets/dialogs/qfiledialog_p.h @@ -259,6 +259,8 @@ private: virtual void helperPrepareShow(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) }; -- cgit v1.2.3