ProjectExplorer: Use FilePath for Recent projects

Change-Id: Ifaf893358d4fbe20696b39b795434cb51a3e29d4
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2022-07-27 14:18:48 +02:00
parent d7aeb21bfb
commit fdf92b67b5
6 changed files with 76 additions and 74 deletions

View File

@@ -470,7 +470,7 @@ public:
QPair<bool, QString> buildSettingsEnabledForSession();
QPair<bool, QString> 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<Utils::optional<Environment>(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<RunConfiguration> m_defaultRunConfiguration;
QPointer<RunConfiguration> 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()

View File

@@ -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<QString, QString>;
using RecentProjectsEntry = QPair<Utils::FilePath, QString>;
using RecentProjectsEntries = QList<RecentProjectsEntry>;
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();

View File

@@ -42,6 +42,7 @@
#include <utils/icon.h>
#include <utils/qtcassert.h>
#include <utils/stringutils.h>
#include <utils/qtcassert.h>
#include <utils/theme/theme.h>
#include <QAbstractItemDelegate>
@@ -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<QMouseEvent *>(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<ProjectModel *>(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();

View File

@@ -29,6 +29,8 @@
#include <coreplugin/iwelcomepage.h>
#include <utils/filepath.h>
#include <QAbstractListModel>
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:

View File

@@ -47,6 +47,7 @@
#include <texteditor/texteditor.h>
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <utils/stylehelper.h>
#include <utils/qtcassert.h>

View File

@@ -80,6 +80,9 @@
#include <algorithm>
#include <memory>
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: