Utils: Pass dialog parent to Utils::* file dialog

Amends 3edc5673b5.

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 <christian.stenger@qt.io>
This commit is contained in:
hjk
2021-08-11 09:01:53 +02:00
parent 7726267d07
commit 8885ef7e5a
10 changed files with 44 additions and 33 deletions

View File

@@ -429,18 +429,19 @@ void FileUtils::setDialogParentGetter(const std::function<QWidget *()> &getter)
s_dialogParentGetter = 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 FilePath &dir,
const QString &filter, const QString &filter,
QString *selectedFilter, QString *selectedFilter,
QFileDialog::Options options) QFileDialog::Options options)
{ {
const QString result = QFileDialog::getOpenFileName(dialogParent(), const QString result = QFileDialog::getOpenFileName(dialogParent(parent),
caption, caption,
dir.toString(), dir.toString(),
filter, filter,
@@ -449,13 +450,14 @@ FilePath FileUtils::getOpenFilePath(const QString &caption,
return FilePath::fromString(result); return FilePath::fromString(result);
} }
FilePath FileUtils::getSaveFilePath(const QString &caption, FilePath FileUtils::getSaveFilePath(QWidget *parent,
const FilePath &dir, const QString &caption,
const QString &filter, const FilePath &dir,
QString *selectedFilter, const QString &filter,
QFileDialog::Options options) QString *selectedFilter,
QFileDialog::Options options)
{ {
const QString result = QFileDialog::getSaveFileName(dialogParent(), const QString result = QFileDialog::getSaveFileName(dialogParent(parent),
caption, caption,
dir.toString(), dir.toString(),
filter, filter,
@@ -464,24 +466,26 @@ FilePath FileUtils::getSaveFilePath(const QString &caption,
return FilePath::fromString(result); return FilePath::fromString(result);
} }
FilePath FileUtils::getExistingDirectory(const QString &caption, FilePath FileUtils::getExistingDirectory(QWidget *parent,
const FilePath &dir, const QString &caption,
QFileDialog::Options options) const FilePath &dir,
QFileDialog::Options options)
{ {
const QString result = QFileDialog::getExistingDirectory(dialogParent(), const QString result = QFileDialog::getExistingDirectory(dialogParent(parent),
caption, caption,
dir.toString(), dir.toString(),
options); options);
return FilePath::fromString(result); return FilePath::fromString(result);
} }
FilePaths FileUtils::getOpenFilePaths(const QString &caption, FilePaths FileUtils::getOpenFilePaths(QWidget *parent,
const QString &caption,
const FilePath &dir, const FilePath &dir,
const QString &filter, const QString &filter,
QString *selectedFilter, QString *selectedFilter,
QFileDialog::Options options) QFileDialog::Options options)
{ {
const QStringList result = QFileDialog::getOpenFileNames(dialogParent(), const QStringList result = QFileDialog::getOpenFileNames(dialogParent(parent),
caption, caption,
dir.toString(), dir.toString(),
filter, filter,

View File

@@ -138,23 +138,27 @@ public:
#ifdef QT_WIDGETS_LIB #ifdef QT_WIDGETS_LIB
static void setDialogParentGetter(const std::function<QWidget *()> &getter); static void setDialogParentGetter(const std::function<QWidget *()> &getter);
static FilePath getOpenFilePath(const QString &caption = {}, static FilePath getOpenFilePath(QWidget *parent,
const QString &caption,
const FilePath &dir = {}, const FilePath &dir = {},
const QString &filter = {}, const QString &filter = {},
QString *selectedFilter = nullptr, QString *selectedFilter = nullptr,
QFileDialog::Options options = {}); QFileDialog::Options options = {});
static FilePath getSaveFilePath(const QString &caption = {}, static FilePath getSaveFilePath(QWidget *parent,
const QString &caption,
const FilePath &dir = {}, const FilePath &dir = {},
const QString &filter = {}, const QString &filter = {},
QString *selectedFilter = nullptr, QString *selectedFilter = nullptr,
QFileDialog::Options options = {}); QFileDialog::Options options = {});
static FilePath getExistingDirectory(const QString &caption = {}, static FilePath getExistingDirectory(QWidget *parent,
const QString &caption,
const FilePath &dir = {}, const FilePath &dir = {},
QFileDialog::Options options = QFileDialog::ShowDirsOnly); QFileDialog::Options options = QFileDialog::ShowDirsOnly);
static FilePaths getOpenFilePaths(const QString &caption = {}, static FilePaths getOpenFilePaths(QWidget *parent,
const QString &caption,
const FilePath &dir = {}, const FilePath &dir = {},
const QString &filter = {}, const QString &filter = {},
QString *selectedFilter = nullptr, QString *selectedFilter = nullptr,

View File

@@ -1039,8 +1039,7 @@ FilePaths DocumentManager::getOpenFileNames(const QString &filters,
{ {
const FilePath path = pathIn.isEmpty() ? FilePath::fromString(fileDialogInitialDirectory()) const FilePath path = pathIn.isEmpty() ? FilePath::fromString(fileDialogInitialDirectory())
: pathIn; : pathIn;
const FilePaths files = FileUtils::getOpenFilePaths(tr("Open File"), const FilePaths files = FileUtils::getOpenFilePaths(nullptr, tr("Open File"), path, filters,
path, filters,
selectedFilter); selectedFilter);
if (!files.isEmpty()) if (!files.isEmpty())
setFileDialogLastVisitedDirectory(files.front().absolutePath().toString()); setFileDialogLastVisitedDirectory(files.front().absolutePath().toString());

View File

@@ -566,13 +566,13 @@ void DiffEditorPluginPrivate::diffOpenFiles()
void DiffEditorPluginPrivate::diffExternalFiles() 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()) if (filePath1.isEmpty())
return; return;
if (EditorManager::skipOpeningBigTextFile(filePath1)) if (EditorManager::skipOpeningBigTextFile(filePath1))
return; 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()) if (filePath2.isEmpty())
return; return;
if (EditorManager::skipOpeningBigTextFile(filePath2)) if (EditorManager::skipOpeningBigTextFile(filePath2))

View File

@@ -885,7 +885,7 @@ void PerforcePluginPrivate::filelogCurrentFile()
void PerforcePluginPrivate::filelogFile() void PerforcePluginPrivate::filelogFile()
{ {
const FilePath file = FileUtils::getOpenFilePath(tr("p4 filelog")); const FilePath file = FileUtils::getOpenFilePath(nullptr, tr("p4 filelog"));
if (!file.isEmpty()) if (!file.isEmpty())
filelog(file.parentDir(), file.fileName()); filelog(file.parentDir(), file.fileName());
} }

View File

@@ -721,6 +721,7 @@ Utils::FilePath Project::projectDirectory(const Utils::FilePath &top)
void Project::changeRootProjectDirectory() void Project::changeRootProjectDirectory()
{ {
Utils::FilePath rootPath = Utils::FileUtils::getExistingDirectory( Utils::FilePath rootPath = Utils::FileUtils::getExistingDirectory(
nullptr,
tr("Select the Root Directory"), tr("Select the Root Directory"),
rootProjectDirectory(), rootProjectDirectory(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);

View File

@@ -1997,7 +1997,8 @@ void ProjectExplorerPluginPrivate::loadAction()
dir = isProject ? fn : QFileInfo(fn).absolutePath(); 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); dd->m_projectFilterString);
if (filePath.isEmpty()) if (filePath.isEmpty())
return; return;
@@ -3578,7 +3579,7 @@ void ProjectExplorerPluginPrivate::addExistingProjects()
QTC_ASSERT(projectNode, return); QTC_ASSERT(projectNode, return);
const FilePath dir = currentNode->directory(); const FilePath dir = currentNode->directory();
FilePaths subProjectFilePaths = Utils::FileUtils::getOpenFilePaths( FilePaths subProjectFilePaths = Utils::FileUtils::getOpenFilePaths(
tr("Choose Project File"), dir, nullptr, tr("Choose Project File"), dir,
projectNode->subProjectFileNamePatterns().join(";;")); projectNode->subProjectFileNamePatterns().join(";;"));
if (!ProjectTree::hasNode(projectNode)) if (!ProjectTree::hasNode(projectNode))
return; return;
@@ -3616,7 +3617,7 @@ void ProjectExplorerPluginPrivate::handleAddExistingFiles()
QTC_ASSERT(folderNode, return); QTC_ASSERT(folderNode, return);
const FilePaths filePaths = 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()) if (filePaths.isEmpty())
return; return;

View File

@@ -755,8 +755,8 @@ public:
QTC_ASSERT(projectImporter, return); QTC_ASSERT(projectImporter, return);
FilePath importDir = FilePath importDir =
Utils::FileUtils::getExistingDirectory(ProjectWindow::tr("Import Directory"), FileUtils::getExistingDirectory(nullptr, ProjectWindow::tr("Import Directory"),
project->projectDirectory()); project->projectDirectory());
Target *lastTarget = nullptr; Target *lastTarget = nullptr;
BuildConfiguration *lastBc = nullptr; BuildConfiguration *lastBc = nullptr;

View File

@@ -41,6 +41,8 @@
#include <QQuickItem> #include <QQuickItem>
#include <QQuickWidget> #include <QQuickWidget>
using namespace Utils;
ExampleCheckout::ExampleCheckout(QObject *) {} ExampleCheckout::ExampleCheckout(QObject *) {}
void ExampleCheckout::checkoutExample(const QUrl &url) void ExampleCheckout::checkoutExample(const QUrl &url)
@@ -239,8 +241,8 @@ QString FileExtractor::targetPath() const
void FileExtractor::browse() void FileExtractor::browse()
{ {
const Utils::FilePath path = const FilePath path =
Utils::FileUtils::getExistingDirectory(tr("Choose Directory"), m_targetPath); FileUtils::getExistingDirectory(nullptr, tr("Choose Directory"), m_targetPath);
if (!path.isEmpty()) if (!path.isEmpty())
m_targetPath = path; m_targetPath = path;

View File

@@ -644,7 +644,7 @@ void VcsBasePluginPrivate::createRepository()
// Prompt for a directory that is not under version control yet // Prompt for a directory that is not under version control yet
QWidget *mw = ICore::dialogParent(); QWidget *mw = ICore::dialogParent();
do { do {
directory = FileUtils::getExistingDirectory(tr("Choose Repository Directory"), directory); directory = FileUtils::getExistingDirectory(nullptr, tr("Choose Repository Directory"), directory);
if (directory.isEmpty()) if (directory.isEmpty())
return; return;
const IVersionControl *managingControl = VcsManager::findVersionControlForDirectory(directory); const IVersionControl *managingControl = VcsManager::findVersionControlForDirectory(directory);