Core: Use FilePath in some functions related to opening files

Change-Id: I9610855a914d315d7934996c755fb69ad399320f
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2021-07-23 10:33:02 +02:00
parent dbdea46f66
commit 906cfb060b
13 changed files with 46 additions and 40 deletions

View File

@@ -279,8 +279,10 @@ QObject *CorePlugin::remoteCommand(const QStringList & /* options */,
});
return nullptr;
}
const FilePaths filePaths = Utils::transform(args, FilePath::fromString);
IDocument *res = MainWindow::openFiles(
args, ICore::OpenFilesFlags(ICore::SwitchMode | ICore::CanContainLineAndColumnNumbers | ICore::SwitchSplitIfAlreadyVisible),
filePaths,
ICore::OpenFilesFlags(ICore::SwitchMode | ICore::CanContainLineAndColumnNumbers | ICore::SwitchSplitIfAlreadyVisible),
workingDirectory);
m_mainWindow->raiseWindow();
return res;

View File

@@ -44,7 +44,6 @@
#include <coreplugin/editormanager/ieditorfactory.h>
#include <coreplugin/editormanager/iexternaleditor.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/algorithm.h>
@@ -1034,17 +1033,17 @@ void DocumentManager::showFilePropertiesDialog(const FilePath &filePath)
dialog in if that is not overridden by the user's policy.
*/
QStringList DocumentManager::getOpenFileNames(const QString &filters,
const QString &pathIn,
FilePaths DocumentManager::getOpenFileNames(const QString &filters,
const FilePath &pathIn,
QString *selectedFilter)
{
const QString &path = pathIn.isEmpty() ? fileDialogInitialDirectory() : pathIn;
const QStringList files = QFileDialog::getOpenFileNames(ICore::dialogParent(),
tr("Open File"),
const FilePath path = pathIn.isEmpty() ? FilePath::fromString(fileDialogInitialDirectory())
: pathIn;
const FilePaths files = FileUtils::getOpenFilePaths(tr("Open File"),
path, filters,
selectedFilter);
if (!files.isEmpty())
setFileDialogLastVisitedDirectory(QFileInfo(files.front()).absolutePath());
setFileDialogLastVisitedDirectory(files.front().absolutePath().toString());
return files;
}

View File

@@ -84,8 +84,8 @@ public:
static QString allDocumentFactoryFiltersString(QString *allFilesFilter);
static QStringList getOpenFileNames(const QString &filters,
const QString &path = QString(),
static Utils::FilePaths getOpenFileNames(const QString &filters,
const Utils::FilePath &path = {},
QString *selectedFilter = nullptr);
static QString getSaveFileName(const QString &title,
const QString &pathIn,

View File

@@ -3190,13 +3190,13 @@ void EditorManager::addCloseEditorListener(const std::function<bool (IEditor *)>
/*!
Asks the user for a list of files to open and returns the choice.
\sa DocumentManager::getOpenFileNames()
\sa DocumentManager::getOpenFilePaths()
*/
QStringList EditorManager::getOpenFileNames()
FilePaths EditorManager::getOpenFilePaths()
{
QString selectedFilter;
const QString &fileFilters = DocumentManager::allDocumentFactoryFiltersString(&selectedFilter);
return DocumentManager::getOpenFileNames(fileFilters, QString(), &selectedFilter);
return DocumentManager::getOpenFileNames(fileFilters, {}, &selectedFilter);
}
static QString makeTitleUnique(QString *titlePattern)

View File

@@ -118,7 +118,7 @@ public:
static bool openExternalEditor(const Utils::FilePath &filePath, Utils::Id editorId);
static void addCloseEditorListener(const std::function<bool(IEditor *)> &listener);
static QStringList getOpenFileNames();
static Utils::FilePaths getOpenFilePaths();
static IDocument *currentDocument();
static IEditor *currentEditor();

View File

@@ -804,14 +804,14 @@ void ICore::registerWindow(QWidget *window, const Context &context)
}
/*!
Opens files using \a arguments and \a flags like it would be
Opens files using \a filePaths and \a flags like it would be
done if they were given to \QC on the command line, or
they were opened via \uicontrol File > \uicontrol Open.
*/
void ICore::openFiles(const QStringList &arguments, ICore::OpenFilesFlags flags)
void ICore::openFiles(const FilePaths &filePaths, ICore::OpenFilesFlags flags)
{
MainWindow::openFiles(arguments, flags);
MainWindow::openFiles(filePaths, flags);
}
/*!

View File

@@ -133,7 +133,7 @@ public:
StopOnLoadFail = 4,
SwitchSplitIfAlreadyVisible = 8
};
static void openFiles(const QStringList &fileNames, OpenFilesFlags flags = None);
static void openFiles(const Utils::FilePaths &filePaths, OpenFilesFlags flags = None);
static void addPreCloseListener(const std::function<bool()> &listener);

View File

@@ -390,7 +390,7 @@ void MainWindow::openDroppedFiles(const QList<DropSupport::FileSpec> &files)
{
raiseWindow();
QStringList filePaths = Utils::transform(files, &DropSupport::FileSpec::filePath);
openFiles(filePaths, ICore::SwitchMode);
openFiles(Utils::transform(filePaths, &FilePath::fromString), ICore::SwitchMode);
}
IContext *MainWindow::currentContextObject() const
@@ -852,7 +852,7 @@ void MainWindow::registerModeSelectorStyleActions()
void MainWindow::openFile()
{
openFiles(EditorManager::getOpenFileNames(), ICore::SwitchMode);
openFiles(EditorManager::getOpenFilePaths(), ICore::SwitchMode);
}
static IDocumentFactory *findDocumentFactory(const QList<IDocumentFactory*> &fileFactories,
@@ -866,7 +866,7 @@ static IDocumentFactory *findDocumentFactory(const QList<IDocumentFactory*> &fil
/*!
* \internal
* Either opens \a fileNames with editors or loads a project.
* Either opens \a filePaths with editors or loads a project.
*
* \a flags can be used to stop on first failure, indicate that a file name
* might include line numbers and/or switch mode to edit mode.
@@ -879,14 +879,15 @@ static IDocumentFactory *findDocumentFactory(const QList<IDocumentFactory*> &fil
*
* \sa IPlugin::remoteArguments()
*/
IDocument *MainWindow::openFiles(const QStringList &fileNames,
IDocument *MainWindow::openFiles(const FilePaths &filePaths,
ICore::OpenFilesFlags flags,
const QString &workingDirectory)
{
const QList<IDocumentFactory*> documentFactories = IDocumentFactory::allDocumentFactories();
IDocument *res = nullptr;
for (const QString &fileName : fileNames) {
for (const FilePath &filePath : filePaths) {
const QString fileName = filePath.toString();
const QDir workingDir(workingDirectory.isEmpty() ? QDir::currentPath() : workingDirectory);
const QFileInfo fi(workingDir, fileName);
const QString absoluteFilePath = fi.absoluteFilePath();
@@ -947,15 +948,16 @@ void MainWindow::exit()
void MainWindow::openFileWith()
{
foreach (const QString &fileName, EditorManager::getOpenFileNames()) {
const FilePaths filePaths = EditorManager::getOpenFilePaths();
for (const FilePath &filePath : filePaths) {
bool isExternal;
const Id editorId = EditorManagerPrivate::getOpenWithEditorId(fileName, &isExternal);
const Id editorId = EditorManagerPrivate::getOpenWithEditorId(filePath.toString(), &isExternal);
if (!editorId.isValid())
continue;
if (isExternal)
EditorManager::openExternalEditor(FilePath::fromString(fileName), editorId);
EditorManager::openExternalEditor(filePath, editorId);
else
EditorManagerPrivate::openEditorWith(FilePath::fromString(fileName), editorId);
EditorManagerPrivate::openEditorWith(filePath, editorId);
}
}

View File

@@ -89,7 +89,7 @@ public:
void addContextObject(IContext *context);
void removeContextObject(IContext *context);
static IDocument *openFiles(const QStringList &fileNames,
static IDocument *openFiles(const Utils::FilePaths &filePaths,
ICore::OpenFilesFlags flags,
const QString &workingDirectory = QString());

View File

@@ -75,6 +75,8 @@
#include <QToolButton>
#include <QVBoxLayout>
using namespace Utils;
const int PATH_ROLE = Qt::UserRole;
const int ID_ROLE = Qt::UserRole + 1;
const int SORT_ROLE = Qt::UserRole + 2;
@@ -679,7 +681,7 @@ void FolderNavigationWidget::openProjectsInDirectory(const QModelIndex &index)
{
const QStringList projectFiles = projectsInDirectory(index);
if (!projectFiles.isEmpty())
Core::ICore::openFiles(projectFiles);
Core::ICore::openFiles(Utils::transform(projectFiles, &FilePath::fromString));
}
void FolderNavigationWidget::createNewFolder(const QModelIndex &parent)

View File

@@ -2566,7 +2566,8 @@ void ProjectExplorerPluginPrivate::restoreSession()
dd->m_arguments = arguments;
// delay opening projects from the command line even more
QTimer::singleShot(0, m_instance, []() {
ICore::openFiles(dd->m_arguments, ICore::OpenFilesFlags(ICore::CanContainLineAndColumnNumbers | ICore::SwitchMode));
ICore::openFiles(Utils::transform(dd->m_arguments, &FilePath::fromString),
ICore::OpenFilesFlags(ICore::CanContainLineAndColumnNumbers | ICore::SwitchMode));
emit m_instance->finishedInitialization();
});
updateActions();
@@ -4075,10 +4076,10 @@ bool ProjectExplorerPlugin::isProjectFile(const FilePath &filePath)
void ProjectExplorerPlugin::openOpenProjectDialog()
{
const QString path = DocumentManager::useProjectsDirectory()
? DocumentManager::projectsDirectory().toString()
: QString();
const QStringList files = DocumentManager::getOpenFileNames(dd->m_projectFilterString, path);
const FilePath path = DocumentManager::useProjectsDirectory()
? DocumentManager::projectsDirectory()
: FilePath();
const FilePaths files = DocumentManager::getOpenFileNames(dd->m_projectFilterString, path);
if (!files.isEmpty())
ICore::openFiles(files, ICore::SwitchMode);
}

View File

@@ -207,7 +207,7 @@ void ExamplesWelcomePage::openProject(const ExampleItem *item)
ProjectExplorerPlugin::OpenProjectResult result =
ProjectExplorerPlugin::openProject(FilePath::fromString(proFile));
if (result) {
ICore::openFiles(filesToOpen);
ICore::openFiles(Utils::transform(filesToOpen, &FilePath::fromString));
ModeManager::activateMode(Core::Constants::MODE_EDIT);
QUrl docUrl = QUrl::fromUserInput(item->docUrl);
if (docUrl.isValid())

View File

@@ -399,7 +399,7 @@ bool WelcomeMode::openDroppedFiles(const QList<QUrl> &urls)
const QList<QUrl> localUrls = Utils::filtered(urls, &QUrl::isLocalFile);
if (!localUrls.isEmpty()) {
QTimer::singleShot(0, [localUrls]() {
ICore::openFiles(Utils::transform(localUrls, &QUrl::toLocalFile), ICore::SwitchMode);
ICore::openFiles(Utils::transform(localUrls, &FilePath::fromUrl), ICore::SwitchMode);
});
return true;
}