From fdf92b67b5a19c8214a38a209bb73b7954ce3bd3 Mon Sep 17 00:00:00 2001 From: hjk Date: Wed, 27 Jul 2022 14:18:48 +0200 Subject: [PATCH] ProjectExplorer: Use FilePath for Recent projects Change-Id: Ifaf893358d4fbe20696b39b795434cb51a3e29d4 Reviewed-by: Eike Ziller --- .../projectexplorer/projectexplorer.cpp | 81 +++++++++---------- src/plugins/projectexplorer/projectexplorer.h | 7 +- .../projectexplorer/projectwelcomepage.cpp | 20 ++--- .../projectexplorer/projectwelcomepage.h | 4 +- src/plugins/projectexplorer/session.cpp | 1 + .../studiowelcome/studiowelcomeplugin.cpp | 37 +++++---- 6 files changed, 76 insertions(+), 74 deletions(-) diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 9b9fe05b709..41740260f3e 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -470,7 +470,7 @@ public: QPair buildSettingsEnabledForSession(); QPair buildSettingsEnabled(const Project *pro); - void addToRecentProjects(const QString &fileName, const QString &displayName); + void addToRecentProjects(const FilePath &filePath, const QString &displayName); void startRunControl(RunControl *runControl); void showOutputPaneForRunControl(RunControl *runControl); @@ -515,8 +515,8 @@ public: void updateRecentProjectMenu(); void clearRecentProjects(); - void openRecentProject(const QString &fileName); - void removeFromRecentProjects(const QString &fileName, const QString &displayName); + void openRecentProject(const FilePath &filePath); + void removeFromRecentProjects(const FilePath &filePath); void updateUnloadProjectMenu(); using EnvironmentGetter = std::function(const Project *project)>; void openTerminalHere(const EnvironmentGetter &env); @@ -627,7 +627,7 @@ public: RecentProjectsEntries m_recentProjects; // pair of filename, displayname static const int m_maxRecentProjects = 25; - QString m_lastOpenDirectory; + FilePath m_lastOpenDirectory; QPointer m_defaultRunConfiguration; QPointer m_delayedRunConfiguration; QString m_projectFilterString; @@ -1677,7 +1677,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er .toStringList(); if (fileNames.size() == displayNames.size()) { for (int i = 0; i < fileNames.size(); ++i) { - dd->m_recentProjects.append(qMakePair(fileNames.at(i), displayNames.at(i))); + dd->m_recentProjects.append({FilePath::fromUserInput(fileNames.at(i)), displayNames.at(i)}); } } @@ -2129,18 +2129,18 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er void ProjectExplorerPluginPrivate::loadAction() { - QString dir = dd->m_lastOpenDirectory; + FilePath dir = dd->m_lastOpenDirectory; // for your special convenience, we preselect a pro file if it is // the current file if (const IDocument *document = EditorManager::currentDocument()) { - const QString fn = document->filePath().toString(); + const FilePath fn = document->filePath(); const bool isProject = dd->m_profileMimeTypes.contains(document->mimeType()); - dir = isProject ? fn : QFileInfo(fn).absolutePath(); + dir = isProject ? fn : fn.absolutePath(); } - FilePath filePath = Utils::FileUtils::getOpenFilePath(nullptr, - tr("Load Project"), FilePath::fromString(dir), + FilePath filePath = Utils::FileUtils::getOpenFilePath(ICore::dialogParent(), + tr("Load Project"), dir, dd->m_projectFilterString); if (filePath.isEmpty()) return; @@ -2199,7 +2199,7 @@ void ProjectExplorerPlugin::unloadProject(Project *project) if (projectExplorerSettings().closeSourceFilesWithProject && !dd->closeAllFilesInProject(project)) return; - dd->addToRecentProjects(project->projectFilePath().toString(), project->displayName()); + dd->addToRecentProjects(project->projectFilePath(), project->displayName()); SessionManager::removeProject(project); dd->updateActions(); @@ -2420,7 +2420,7 @@ void ProjectExplorerPluginPrivate::savePersistentSettings() RecentProjectsEntries::const_iterator it, end; end = dd->m_recentProjects.constEnd(); for (it = dd->m_recentProjects.constBegin(); it != end; ++it) { - fileNames << (*it).first; + fileNames << (*it).first.toUserOutput(); displayNames << (*it).second; } @@ -2483,9 +2483,9 @@ void ProjectExplorerPluginPrivate::savePersistentSettings() } } -void ProjectExplorerPlugin::openProjectWelcomePage(const QString &fileName) +void ProjectExplorerPlugin::openProjectWelcomePage(const FilePath &filePath) { - OpenProjectResult result = openProject(FilePath::fromUserInput(fileName)); + OpenProjectResult result = openProject(filePath); if (!result) showOpenProjectError(result); } @@ -2496,7 +2496,7 @@ ProjectExplorerPlugin::OpenProjectResult ProjectExplorerPlugin::openProject(cons Project *project = result.project(); if (!project) return result; - dd->addToRecentProjects(filePath.toString(), project->displayName()); + dd->addToRecentProjects(filePath, project->displayName()); SessionManager::setStartupProject(project); return result; } @@ -2853,7 +2853,7 @@ void ProjectExplorerPluginPrivate::buildQueueFinished(bool success) RecentProjectsEntries ProjectExplorerPluginPrivate::recentProjects() const { return Utils::filtered(dd->m_recentProjects, [](const RecentProjectsEntry &p) { - return QFileInfo(p.first).isFile(); + return p.first.isFile(); }); } @@ -3290,7 +3290,7 @@ void ProjectExplorerPluginPrivate::projectRemoved(Project *pro) void ProjectExplorerPluginPrivate::projectDisplayNameChanged(Project *pro) { - addToRecentProjects(pro->projectFilePath().toString(), pro->displayName()); + addToRecentProjects(pro->projectFilePath(), pro->displayName()); updateActions(); } @@ -3425,24 +3425,22 @@ void ProjectExplorerPluginPrivate::doUpdateRunActions() emit m_instance->runActionsUpdated(); } -void ProjectExplorerPluginPrivate::addToRecentProjects(const QString &fileName, const QString &displayName) +void ProjectExplorerPluginPrivate::addToRecentProjects(const FilePath &filePath, const QString &displayName) { - if (fileName.isEmpty()) + if (filePath.isEmpty()) return; - QString prettyFileName(QDir::toNativeSeparators(fileName)); RecentProjectsEntries::iterator it; for (it = m_recentProjects.begin(); it != m_recentProjects.end();) - if ((*it).first == prettyFileName) + if ((*it).first == filePath) it = m_recentProjects.erase(it); else ++it; if (m_recentProjects.count() > m_maxRecentProjects) m_recentProjects.removeLast(); - m_recentProjects.prepend(qMakePair(prettyFileName, displayName)); - QFileInfo fi(prettyFileName); - m_lastOpenDirectory = fi.absolutePath(); + m_recentProjects.prepend(qMakePair(filePath, displayName)); + m_lastOpenDirectory = filePath.absolutePath(); emit m_instance->recentProjectsChanged(); } @@ -3460,7 +3458,6 @@ void ProjectExplorerPluginPrivate::updateUnloadProjectMenu() void ProjectExplorerPluginPrivate::updateRecentProjectMenu() { - using StringPairListConstIterator = RecentProjectsEntries::const_iterator; ActionContainer *aci = ActionManager::actionContainer(Constants::M_RECENTPROJECTS); QMenu *menu = aci->menu(); menu->clear(); @@ -3468,18 +3465,18 @@ void ProjectExplorerPluginPrivate::updateRecentProjectMenu() int acceleratorKey = 1; const RecentProjectsEntries projects = recentProjects(); //projects (ignore sessions, they used to be in this list) - const StringPairListConstIterator end = projects.constEnd(); - for (StringPairListConstIterator it = projects.constBegin(); it != end; ++it, ++acceleratorKey) { - const QString fileName = it->first; - if (fileName.endsWith(QLatin1String(".qws"))) + for (const RecentProjectsEntry &item : projects) { + const FilePath &filePath = item.first; + if (filePath.endsWith(QLatin1String(".qws"))) continue; const QString actionText = ActionManager::withNumberAccelerator( - Utils::withTildeHomePath(fileName), acceleratorKey); + Utils::withTildeHomePath(filePath.toUserOutput()), acceleratorKey); QAction *action = menu->addAction(actionText); - connect(action, &QAction::triggered, this, [this, fileName] { - openRecentProject(fileName); + connect(action, &QAction::triggered, this, [this, filePath] { + openRecentProject(filePath); }); + ++acceleratorKey; } const bool hasRecentProjects = !projects.empty(); menu->setEnabled(hasRecentProjects); @@ -3501,21 +3498,22 @@ void ProjectExplorerPluginPrivate::clearRecentProjects() updateWelcomePage(); } -void ProjectExplorerPluginPrivate::openRecentProject(const QString &fileName) +void ProjectExplorerPluginPrivate::openRecentProject(const FilePath &filePath) { - if (!fileName.isEmpty()) { + if (!filePath.isEmpty()) { ProjectExplorerPlugin::OpenProjectResult result - = ProjectExplorerPlugin::openProject(FilePath::fromUserInput(fileName)); + = ProjectExplorerPlugin::openProject(filePath); if (!result) ProjectExplorerPlugin::showOpenProjectError(result); } } -void ProjectExplorerPluginPrivate::removeFromRecentProjects(const QString &fileName, - const QString &displayName) +void ProjectExplorerPluginPrivate::removeFromRecentProjects(const FilePath &filePath) { - QTC_ASSERT(!fileName.isEmpty() && !displayName.isEmpty(), return); - QTC_CHECK(m_recentProjects.removeOne(RecentProjectsEntry(fileName, displayName))); + QTC_ASSERT(!filePath.isEmpty(), return); + QTC_CHECK(Utils::eraseOne(m_recentProjects, [filePath](const RecentProjectsEntry &entry) { + return entry.first == filePath; + })); } void ProjectExplorerPluginPrivate::invalidateProject(Project *project) @@ -4349,10 +4347,9 @@ void ProjectExplorerPlugin::clearRecentProjects() dd->clearRecentProjects(); } -void ProjectExplorerPlugin::removeFromRecentProjects(const QString &fileName, - const QString &displayName) +void ProjectExplorerPlugin::removeFromRecentProjects(const FilePath &filePath) { - dd->removeFromRecentProjects(fileName, displayName); + dd->removeFromRecentProjects(filePath); } void ProjectExplorerPlugin::updateRunActions() diff --git a/src/plugins/projectexplorer/projectexplorer.h b/src/plugins/projectexplorer/projectexplorer.h index cc9a2aac914..a19508d3108 100644 --- a/src/plugins/projectexplorer/projectexplorer.h +++ b/src/plugins/projectexplorer/projectexplorer.h @@ -40,7 +40,6 @@ class QThreadPool; QT_END_NAMESPACE namespace Core { -class IMode; class OutputWindow; } // Core @@ -64,7 +63,7 @@ class MiniProjectTargetSelector; class ProjectExplorerSettings; } -using RecentProjectsEntry = QPair; +using RecentProjectsEntry = QPair; using RecentProjectsEntries = QList; class PROJECTEXPLORER_EXPORT ProjectExplorerPlugin : public ExtensionSystem::IPlugin @@ -122,7 +121,7 @@ public: static OpenProjectResult openProject(const Utils::FilePath &filePath); static OpenProjectResult openProjects(const Utils::FilePaths &filePaths); static void showOpenProjectError(const OpenProjectResult &result); - static void openProjectWelcomePage(const QString &fileName); + static void openProjectWelcomePage(const Utils::FilePath &filePath); static void unloadProject(Project *project); static bool saveModifiedFiles(); @@ -187,7 +186,7 @@ public: static void activateProjectPanel(Utils::Id panelId); static void clearRecentProjects(); - static void removeFromRecentProjects(const QString &fileName, const QString &displayName); + static void removeFromRecentProjects(const Utils::FilePath &filePath); static void updateRunActions(); diff --git a/src/plugins/projectexplorer/projectwelcomepage.cpp b/src/plugins/projectexplorer/projectwelcomepage.cpp index 574496da069..5550b151c82 100644 --- a/src/plugins/projectexplorer/projectwelcomepage.cpp +++ b/src/plugins/projectexplorer/projectwelcomepage.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -92,9 +93,9 @@ QVariant ProjectModel::data(const QModelIndex &index, int role) const return data.second; case Qt::ToolTipRole: case FilePathRole: - return data.first; + return data.first.toVariant(); case PrettyFilePathRole: - return Utils::withTildeHomePath(data.first); + return Utils::withTildeHomePath(data.first.toUserOutput()); // FIXME: FilePath::displayName() ? case ShortcutRole: { const Id projectBase = PROJECT_BASE_ID; if (Command *cmd = ActionManager::command(projectBase.withSuffix(index.row() + 1))) @@ -162,9 +163,9 @@ void ProjectWelcomePage::openSessionAt(int index) void ProjectWelcomePage::openProjectAt(int index) { QTC_ASSERT(m_projectModel, return); - const QString projectFile = m_projectModel->data(m_projectModel->index(index, 0), - ProjectModel::FilePathRole).toString(); - ProjectExplorerPlugin::openProjectWelcomePage(projectFile); + const QVariant projectFile = m_projectModel->data(m_projectModel->index(index, 0), + ProjectModel::FilePathRole); + ProjectExplorerPlugin::openProjectWelcomePage(FilePath::fromVariant(projectFile)); } void ProjectWelcomePage::createActions() @@ -529,8 +530,8 @@ public: const QMouseEvent *mouseEvent = static_cast(ev); const Qt::MouseButtons button = mouseEvent->button(); if (button == Qt::LeftButton) { - const QString projectFile = idx.data(ProjectModel::FilePathRole).toString(); - ProjectExplorerPlugin::openProjectWelcomePage(projectFile); + const QVariant projectFile = idx.data(ProjectModel::FilePathRole); + ProjectExplorerPlugin::openProjectWelcomePage(FilePath::fromVariant(projectFile)); return true; } if (button == Qt::RightButton) { @@ -540,9 +541,8 @@ public: const auto projectModel = qobject_cast(model); contextMenu.addAction(action); connect(action, &QAction::triggered, [idx, projectModel](){ - const QString projectFile = idx.data(ProjectModel::FilePathRole).toString(); - const QString displayName = idx.data(Qt::DisplayRole).toString(); - ProjectExplorerPlugin::removeFromRecentProjects(projectFile, displayName); + const QVariant projectFile = idx.data(ProjectModel::FilePathRole); + ProjectExplorerPlugin::removeFromRecentProjects(FilePath::fromVariant(projectFile)); projectModel->resetProjects(); }); contextMenu.addSeparator(); diff --git a/src/plugins/projectexplorer/projectwelcomepage.h b/src/plugins/projectexplorer/projectwelcomepage.h index dca26f3e6b3..ad0c81ba415 100644 --- a/src/plugins/projectexplorer/projectwelcomepage.h +++ b/src/plugins/projectexplorer/projectwelcomepage.h @@ -29,6 +29,8 @@ #include +#include + #include namespace ProjectExplorer { @@ -74,7 +76,7 @@ public slots: void openProject(); signals: - void requestProject(const QString &project); + void requestProject(const Utils::FilePath &project); void manageSessions(); private: diff --git a/src/plugins/projectexplorer/session.cpp b/src/plugins/projectexplorer/session.cpp index b8e69272c9b..94be9716a98 100644 --- a/src/plugins/projectexplorer/session.cpp +++ b/src/plugins/projectexplorer/session.cpp @@ -47,6 +47,7 @@ #include #include +#include #include #include diff --git a/src/plugins/studiowelcome/studiowelcomeplugin.cpp b/src/plugins/studiowelcome/studiowelcomeplugin.cpp index 7498cf40b2b..7e2dec67cb7 100644 --- a/src/plugins/studiowelcome/studiowelcomeplugin.cpp +++ b/src/plugins/studiowelcome/studiowelcomeplugin.cpp @@ -80,6 +80,9 @@ #include #include +using namespace ProjectExplorer; +using namespace Utils; + namespace StudioWelcome { namespace Internal { @@ -231,8 +234,8 @@ public: return; m_blockOpenRecent = true; - const QString projectFile = data(index(row, 0), ProjectModel::FilePathRole).toString(); - if (QFileInfo::exists(projectFile)) + const FilePath projectFile = FilePath::fromVariant(data(index(row, 0), ProjectModel::FilePathRole)); + if (projectFile.exists()) ProjectExplorer::ProjectExplorerPlugin::openProjectWelcomePage(projectFile); resetProjects(); @@ -260,7 +263,7 @@ public: if (!explicitQmlproject.isEmpty()) projectFile = exampleFolder + explicitQmlproject; - ProjectExplorer::ProjectExplorerPlugin::openProjectWelcomePage(projectFile); + ProjectExplorer::ProjectExplorerPlugin::openProjectWelcomePage(FilePath::fromString(projectFile)); const QString qmlFile = QFileInfo(projectFile).dir().absolutePath() + "/" + formFile; @@ -283,7 +286,7 @@ public: Q_UNUSED(completeBaseName) const Utils::FilePath projectFile = Core::ICore::resourcePath("examples") / example / example + ".qmlproject"; - ProjectExplorer::ProjectExplorerPlugin::openProjectWelcomePage(projectFile.toString()); + ProjectExplorer::ProjectExplorerPlugin::openProjectWelcomePage(projectFile); const Utils::FilePath qmlFile = Core::ICore::resourcePath("examples") / example / formFile; @@ -328,10 +331,10 @@ int ProjectModel::rowCount(const QModelIndex &) const return ProjectExplorer::ProjectExplorerPlugin::recentProjects().count(); } -QString getQDSVersion(const QString &projectFilePath) +static QString getQDSVersion(const FilePath &projectFilePath) { const QString qdsVersion = QmlProjectManager::ProjectFileContentTools::qdsVersion( - Utils::FilePath::fromString(projectFilePath)); + projectFilePath); return ProjectModel::tr("Created with Qt Design Studio version: %1").arg(qdsVersion); } @@ -347,11 +350,11 @@ static QString fromCamelCase(const QString &s) { return result; } -static QString resolutionFromConstants(const QString &projectFilePath) +static QString resolutionFromConstants(const FilePath &projectFilePath) { QmlProjectManager::ProjectFileContentTools::Resolution res = QmlProjectManager::ProjectFileContentTools::resolutionFromConstants( - Utils::FilePath::fromString(projectFilePath)); + projectFilePath); if (res.width > 0 && res.height > 0) return ProjectModel::tr("Resolution: %1x%2").arg(res.width).arg(res.height); @@ -359,25 +362,25 @@ static QString resolutionFromConstants(const QString &projectFilePath) return {}; } -static QString description(const QString &projectFilePath) +static QString description(const FilePath &projectFilePath) { const QString created = ProjectModel::tr("Created: %1").arg( - QFileInfo(projectFilePath).fileTime(QFileDevice::FileBirthTime).toString()); + projectFilePath.toFileInfo().fileTime(QFileDevice::FileBirthTime).toString()); const QString lastEdited = ProjectModel::tr("Last Edited: %1").arg( - QFileInfo(projectFilePath).fileTime(QFileDevice::FileModificationTime).toString()); + projectFilePath.toFileInfo().fileTime(QFileDevice::FileModificationTime).toString()); - return fromCamelCase(QFileInfo(projectFilePath).baseName()) + "\n\n" + created + "\n" + lastEdited + return fromCamelCase(projectFilePath.baseName()) + "\n\n" + created + "\n" + lastEdited + "\n" + resolutionFromConstants(projectFilePath) + "\n" + getQDSVersion(projectFilePath); } -static QString tags(const QString &projectFilePath) +static QString tags(const FilePath &projectFilePath) { QStringList ret; const QString defaultReturn = "content/App.qml"; Utils::FileReader reader; - if (!reader.fetch(Utils::FilePath::fromString(projectFilePath))) + if (!reader.fetch(projectFilePath)) return defaultReturn; const QByteArray data = reader.data(); @@ -403,13 +406,13 @@ QVariant ProjectModel::data(const QModelIndex &index, int role) const return data.second; break; case FilePathRole: - return data.first; + return data.first.toVariant(); case PrettyFilePathRole: - return Utils::withTildeHomePath(QFileInfo(data.first).dir().absolutePath()); + return Utils::withTildeHomePath(data.first.absolutePath().toUserOutput()); case PreviewUrl: return QVariant(QStringLiteral("image://project_preview/") + QmlProjectManager::ProjectFileContentTools::appQmlFile( - Utils::FilePath::fromString(data.first))); + data.first)); case TagData: return tags(data.first); case Description: