From 8885ef7e5aa66bc1a4e9541c46d90a46bdeca1b3 Mon Sep 17 00:00:00 2001 From: hjk Date: Wed, 11 Aug 2021 09:01:53 +0200 Subject: [PATCH] Utils: Pass dialog parent to Utils::* file dialog Amends 3edc5673b58e55. Turns out quite a few potential uses have other parents than ICore::dialogParent(). Use a nullptr parent to mean ICore::dialogParent() to keep the caller side simple. Change-Id: Icfe1daafd710ae273d286679e0c8e2a3a27da552 Reviewed-by: Christian Stenger --- src/libs/utils/fileutils.cpp | 36 ++++++++++--------- src/libs/utils/fileutils.h | 12 ++++--- src/plugins/coreplugin/documentmanager.cpp | 3 +- src/plugins/diffeditor/diffeditorplugin.cpp | 4 +-- src/plugins/perforce/perforceplugin.cpp | 2 +- src/plugins/projectexplorer/project.cpp | 1 + .../projectexplorer/projectexplorer.cpp | 7 ++-- src/plugins/projectexplorer/projectwindow.cpp | 4 +-- src/plugins/studiowelcome/examplecheckout.cpp | 6 ++-- src/plugins/vcsbase/vcsbaseplugin.cpp | 2 +- 10 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp index 589d3f307c3..60a2d6e62fa 100644 --- a/src/libs/utils/fileutils.cpp +++ b/src/libs/utils/fileutils.cpp @@ -429,18 +429,19 @@ void FileUtils::setDialogParentGetter(const std::function &getter) s_dialogParentGetter = getter; } -static QWidget *dialogParent() +static QWidget *dialogParent(QWidget *parent) { - return s_dialogParentGetter ? s_dialogParentGetter() : nullptr; + return parent ? parent : s_dialogParentGetter ? s_dialogParentGetter() : nullptr; } -FilePath FileUtils::getOpenFilePath(const QString &caption, +FilePath FileUtils::getOpenFilePath(QWidget *parent, + const QString &caption, const FilePath &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options) { - const QString result = QFileDialog::getOpenFileName(dialogParent(), + const QString result = QFileDialog::getOpenFileName(dialogParent(parent), caption, dir.toString(), filter, @@ -449,13 +450,14 @@ FilePath FileUtils::getOpenFilePath(const QString &caption, return FilePath::fromString(result); } -FilePath FileUtils::getSaveFilePath(const QString &caption, - const FilePath &dir, - const QString &filter, - QString *selectedFilter, - QFileDialog::Options options) +FilePath FileUtils::getSaveFilePath(QWidget *parent, + const QString &caption, + const FilePath &dir, + const QString &filter, + QString *selectedFilter, + QFileDialog::Options options) { - const QString result = QFileDialog::getSaveFileName(dialogParent(), + const QString result = QFileDialog::getSaveFileName(dialogParent(parent), caption, dir.toString(), filter, @@ -464,24 +466,26 @@ FilePath FileUtils::getSaveFilePath(const QString &caption, return FilePath::fromString(result); } -FilePath FileUtils::getExistingDirectory(const QString &caption, - const FilePath &dir, - QFileDialog::Options options) +FilePath FileUtils::getExistingDirectory(QWidget *parent, + const QString &caption, + const FilePath &dir, + QFileDialog::Options options) { - const QString result = QFileDialog::getExistingDirectory(dialogParent(), + const QString result = QFileDialog::getExistingDirectory(dialogParent(parent), caption, dir.toString(), options); return FilePath::fromString(result); } -FilePaths FileUtils::getOpenFilePaths(const QString &caption, +FilePaths FileUtils::getOpenFilePaths(QWidget *parent, + const QString &caption, const FilePath &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options) { - const QStringList result = QFileDialog::getOpenFileNames(dialogParent(), + const QStringList result = QFileDialog::getOpenFileNames(dialogParent(parent), caption, dir.toString(), filter, diff --git a/src/libs/utils/fileutils.h b/src/libs/utils/fileutils.h index d5bfee5f074..2a55f28f4a4 100644 --- a/src/libs/utils/fileutils.h +++ b/src/libs/utils/fileutils.h @@ -138,23 +138,27 @@ public: #ifdef QT_WIDGETS_LIB static void setDialogParentGetter(const std::function &getter); - static FilePath getOpenFilePath(const QString &caption = {}, + static FilePath getOpenFilePath(QWidget *parent, + const QString &caption, const FilePath &dir = {}, const QString &filter = {}, QString *selectedFilter = nullptr, QFileDialog::Options options = {}); - static FilePath getSaveFilePath(const QString &caption = {}, + static FilePath getSaveFilePath(QWidget *parent, + const QString &caption, const FilePath &dir = {}, const QString &filter = {}, QString *selectedFilter = nullptr, QFileDialog::Options options = {}); - static FilePath getExistingDirectory(const QString &caption = {}, + static FilePath getExistingDirectory(QWidget *parent, + const QString &caption, const FilePath &dir = {}, QFileDialog::Options options = QFileDialog::ShowDirsOnly); - static FilePaths getOpenFilePaths(const QString &caption = {}, + static FilePaths getOpenFilePaths(QWidget *parent, + const QString &caption, const FilePath &dir = {}, const QString &filter = {}, QString *selectedFilter = nullptr, diff --git a/src/plugins/coreplugin/documentmanager.cpp b/src/plugins/coreplugin/documentmanager.cpp index b636ce02d48..ba73bd32845 100644 --- a/src/plugins/coreplugin/documentmanager.cpp +++ b/src/plugins/coreplugin/documentmanager.cpp @@ -1039,8 +1039,7 @@ FilePaths DocumentManager::getOpenFileNames(const QString &filters, { const FilePath path = pathIn.isEmpty() ? FilePath::fromString(fileDialogInitialDirectory()) : pathIn; - const FilePaths files = FileUtils::getOpenFilePaths(tr("Open File"), - path, filters, + const FilePaths files = FileUtils::getOpenFilePaths(nullptr, tr("Open File"), path, filters, selectedFilter); if (!files.isEmpty()) setFileDialogLastVisitedDirectory(files.front().absolutePath().toString()); diff --git a/src/plugins/diffeditor/diffeditorplugin.cpp b/src/plugins/diffeditor/diffeditorplugin.cpp index 5fb59e4e9e4..624de7002b2 100644 --- a/src/plugins/diffeditor/diffeditorplugin.cpp +++ b/src/plugins/diffeditor/diffeditorplugin.cpp @@ -566,13 +566,13 @@ void DiffEditorPluginPrivate::diffOpenFiles() void DiffEditorPluginPrivate::diffExternalFiles() { - const FilePath filePath1 = FileUtils::getOpenFilePath(tr("Select First File for Diff")); + const FilePath filePath1 = FileUtils::getOpenFilePath(nullptr, tr("Select First File for Diff")); if (filePath1.isEmpty()) return; if (EditorManager::skipOpeningBigTextFile(filePath1)) return; - const FilePath filePath2 = FileUtils::getOpenFilePath(tr("Select Second File for Diff")); + const FilePath filePath2 = FileUtils::getOpenFilePath(nullptr, tr("Select Second File for Diff")); if (filePath2.isEmpty()) return; if (EditorManager::skipOpeningBigTextFile(filePath2)) diff --git a/src/plugins/perforce/perforceplugin.cpp b/src/plugins/perforce/perforceplugin.cpp index 081170e8df5..6b5a85d5941 100644 --- a/src/plugins/perforce/perforceplugin.cpp +++ b/src/plugins/perforce/perforceplugin.cpp @@ -885,7 +885,7 @@ void PerforcePluginPrivate::filelogCurrentFile() void PerforcePluginPrivate::filelogFile() { - const FilePath file = FileUtils::getOpenFilePath(tr("p4 filelog")); + const FilePath file = FileUtils::getOpenFilePath(nullptr, tr("p4 filelog")); if (!file.isEmpty()) filelog(file.parentDir(), file.fileName()); } diff --git a/src/plugins/projectexplorer/project.cpp b/src/plugins/projectexplorer/project.cpp index d336a45e418..07c78e61155 100644 --- a/src/plugins/projectexplorer/project.cpp +++ b/src/plugins/projectexplorer/project.cpp @@ -721,6 +721,7 @@ Utils::FilePath Project::projectDirectory(const Utils::FilePath &top) void Project::changeRootProjectDirectory() { Utils::FilePath rootPath = Utils::FileUtils::getExistingDirectory( + nullptr, tr("Select the Root Directory"), rootProjectDirectory(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index c89a330bc18..bb790b917cd 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -1997,7 +1997,8 @@ void ProjectExplorerPluginPrivate::loadAction() dir = isProject ? fn : QFileInfo(fn).absolutePath(); } - FilePath filePath = Utils::FileUtils::getOpenFilePath(tr("Load Project"), FilePath::fromString(dir), + FilePath filePath = Utils::FileUtils::getOpenFilePath(nullptr, + tr("Load Project"), FilePath::fromString(dir), dd->m_projectFilterString); if (filePath.isEmpty()) return; @@ -3578,7 +3579,7 @@ void ProjectExplorerPluginPrivate::addExistingProjects() QTC_ASSERT(projectNode, return); const FilePath dir = currentNode->directory(); FilePaths subProjectFilePaths = Utils::FileUtils::getOpenFilePaths( - tr("Choose Project File"), dir, + nullptr, tr("Choose Project File"), dir, projectNode->subProjectFileNamePatterns().join(";;")); if (!ProjectTree::hasNode(projectNode)) return; @@ -3616,7 +3617,7 @@ void ProjectExplorerPluginPrivate::handleAddExistingFiles() QTC_ASSERT(folderNode, return); const FilePaths filePaths = - Utils::FileUtils::getOpenFilePaths(tr("Add Existing Files"), node->directory()); + Utils::FileUtils::getOpenFilePaths(nullptr, tr("Add Existing Files"), node->directory()); if (filePaths.isEmpty()) return; diff --git a/src/plugins/projectexplorer/projectwindow.cpp b/src/plugins/projectexplorer/projectwindow.cpp index 793b2d26c86..4691cbd7d89 100644 --- a/src/plugins/projectexplorer/projectwindow.cpp +++ b/src/plugins/projectexplorer/projectwindow.cpp @@ -755,8 +755,8 @@ public: QTC_ASSERT(projectImporter, return); FilePath importDir = - Utils::FileUtils::getExistingDirectory(ProjectWindow::tr("Import Directory"), - project->projectDirectory()); + FileUtils::getExistingDirectory(nullptr, ProjectWindow::tr("Import Directory"), + project->projectDirectory()); Target *lastTarget = nullptr; BuildConfiguration *lastBc = nullptr; diff --git a/src/plugins/studiowelcome/examplecheckout.cpp b/src/plugins/studiowelcome/examplecheckout.cpp index 1782b817473..80e32d72639 100644 --- a/src/plugins/studiowelcome/examplecheckout.cpp +++ b/src/plugins/studiowelcome/examplecheckout.cpp @@ -41,6 +41,8 @@ #include #include +using namespace Utils; + ExampleCheckout::ExampleCheckout(QObject *) {} void ExampleCheckout::checkoutExample(const QUrl &url) @@ -239,8 +241,8 @@ QString FileExtractor::targetPath() const void FileExtractor::browse() { - const Utils::FilePath path = - Utils::FileUtils::getExistingDirectory(tr("Choose Directory"), m_targetPath); + const FilePath path = + FileUtils::getExistingDirectory(nullptr, tr("Choose Directory"), m_targetPath); if (!path.isEmpty()) m_targetPath = path; diff --git a/src/plugins/vcsbase/vcsbaseplugin.cpp b/src/plugins/vcsbase/vcsbaseplugin.cpp index 961b64caeae..b058600d0b2 100644 --- a/src/plugins/vcsbase/vcsbaseplugin.cpp +++ b/src/plugins/vcsbase/vcsbaseplugin.cpp @@ -644,7 +644,7 @@ void VcsBasePluginPrivate::createRepository() // Prompt for a directory that is not under version control yet QWidget *mw = ICore::dialogParent(); do { - directory = FileUtils::getExistingDirectory(tr("Choose Repository Directory"), directory); + directory = FileUtils::getExistingDirectory(nullptr, tr("Choose Repository Directory"), directory); if (directory.isEmpty()) return; const IVersionControl *managingControl = VcsManager::findVersionControlForDirectory(directory);