Commit 3af13723 authored by Tobias Hunger's avatar Tobias Hunger

ProjectExplorer: No more pointer to the parent project node

That information is used rarely, and is not too expensive to regenerate,
so there is no need to store and manage it for every node.

Change-Id: I2261853431cd4328ec447031de3b9f5d5347e796
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 65bc23ba
......@@ -2914,7 +2914,7 @@ void ProjectExplorerPluginPrivate::updateContextMenuActions()
Node *currentNode = ProjectTree::currentNode();
if (currentNode && currentNode->parentProjectNode()) {
if (currentNode && currentNode->managingProject()) {
QList<ProjectAction> actions = currentNode->supportedActions(currentNode);
if (ProjectNode *pn = currentNode->asProjectNode()) {
......@@ -3082,7 +3082,7 @@ void ProjectExplorerPlugin::addExistingFiles(FolderNode *folderNode, const QStri
if (!notAdded.isEmpty()) {
const QString message = tr("Could not add following files to project %1:")
.arg(folderNode->parentProjectNode()->displayName()) + QLatin1Char('\n');
.arg(folderNode->managingProject()->displayName()) + QLatin1Char('\n');
const QStringList nativeFiles
= Utils::transform(notAdded,
[](const QString &f) { return QDir::toNativeSeparators(f); });
......@@ -3100,10 +3100,10 @@ void ProjectExplorerPluginPrivate::removeProject()
Node *node = ProjectTree::currentNode();
if (!node)
return;
ProjectNode *subProjectNode = node->parentProjectNode();
ProjectNode *subProjectNode = node->managingProject();
if (!subProjectNode)
return;
ProjectNode *projectNode = subProjectNode->parentFolderNode()->asProjectNode();
ProjectNode *projectNode = subProjectNode->managingProject();
if (projectNode) {
RemoveFileDialog removeFileDialog(subProjectNode->filePath().toString(), ICore::mainWindow());
removeFileDialog.setDeleteFileVisible(false);
......@@ -3157,7 +3157,7 @@ void ProjectExplorerPluginPrivate::removeFile()
QMessageBox::warning(ICore::mainWindow(), tr("Removing File Failed"),
tr("Could not remove file %1 from project %2.")
.arg(QDir::toNativeSeparators(filePath))
.arg(folderNode->parentProjectNode()->displayName()));
.arg(folderNode->managingProject()->displayName()));
if (!deleteFile)
return;
}
......@@ -3251,7 +3251,7 @@ void ProjectExplorerPlugin::renameFile(Node *node, const QString &newFilePath)
{
const QString oldFilePath = node->filePath().toFileInfo().absoluteFilePath();
FolderNode *folderNode = node->parentFolderNode();
const QString projectFileName = folderNode->parentProjectNode()->filePath().fileName();
const QString projectFileName = folderNode->managingProject()->filePath().fileName();
if (oldFilePath == newFilePath)
return;
......
......@@ -98,7 +98,12 @@ NodeType Node::nodeType() const
*/
ProjectNode *Node::parentProjectNode() const
{
return m_projectNode;
if (!m_parentFolderNode)
return nullptr;
auto pn = m_parentFolderNode->asProjectNode();
if (pn)
return pn;
return m_parentFolderNode->parentProjectNode();
}
/*!
......@@ -109,6 +114,19 @@ FolderNode *Node::parentFolderNode() const
return m_parentFolderNode;
}
ProjectNode *Node::managingProject()
{
if (asSessionNode())
return nullptr;
ProjectNode *pn = parentProjectNode();
return pn ? pn : asProjectNode(); // projects manage themselves...
}
const ProjectNode *Node::managingProject() const
{
return const_cast<Node *>(this)->managingProject();
}
/*!
The path of the file or folder in the filesystem the node represents.
*/
......@@ -155,11 +173,6 @@ void Node::setEnabled(bool enabled)
emitNodeUpdated();
}
void Node::setProjectNode(ProjectNode *project)
{
m_projectNode = project;
}
void Node::emitNodeUpdated()
{
if (parentFolderNode())
......@@ -496,36 +509,41 @@ QString FolderNode::addFileFilter() const
bool FolderNode::addFiles(const QStringList &filePaths, QStringList *notAdded)
{
if (parentProjectNode())
return parentProjectNode()->addFiles(filePaths, notAdded);
ProjectNode *pn = managingProject();
if (pn)
return pn->addFiles(filePaths, notAdded);
return false;
}
bool FolderNode::removeFiles(const QStringList &filePaths, QStringList *notRemoved)
{
if (parentProjectNode())
return parentProjectNode()->removeFiles(filePaths, notRemoved);
ProjectNode *pn = managingProject();
if (pn)
return pn->removeFiles(filePaths, notRemoved);
return false;
}
bool FolderNode::deleteFiles(const QStringList &filePaths)
{
if (parentProjectNode())
return parentProjectNode()->deleteFiles(filePaths);
ProjectNode *pn = managingProject();
if (pn)
return pn->deleteFiles(filePaths);
return false;
}
bool FolderNode::canRenameFile(const QString &filePath, const QString &newFilePath)
{
if (parentProjectNode())
return parentProjectNode()->canRenameFile(filePath, newFilePath);
ProjectNode *pn = managingProject();
if (pn)
return pn->canRenameFile(filePath, newFilePath);
return false;
}
bool FolderNode::renameFile(const QString &filePath, const QString &newFilePath)
{
if (parentProjectNode())
return parentProjectNode()->renameFile(filePath, newFilePath);
ProjectNode *pn = managingProject();
if (pn)
return pn->renameFile(filePath, newFilePath);
return false;
}
......@@ -545,7 +563,8 @@ FolderNode::AddNewInformation FolderNode::addNewInformation(const QStringList &f
void FolderNode::addFileNodes(const QList<FileNode *> &files)
{
Q_ASSERT(parentProjectNode());
Q_ASSERT(managingProject());
if (files.isEmpty())
return;
......@@ -556,7 +575,6 @@ void FolderNode::addFileNodes(const QList<FileNode *> &files)
qDebug("File node has already a parent folder"));
file->setParentFolderNode(this);
file->setProjectNode(parentProjectNode());
// Now find the correct place to insert file
if (m_fileNodes.count() == 0
|| m_fileNodes.last() < file) {
......@@ -581,7 +599,7 @@ void FolderNode::addFileNodes(const QList<FileNode *> &files)
void FolderNode::removeFileNodes(const QList<FileNode *> &files)
{
Q_ASSERT(parentProjectNode());
Q_ASSERT(managingProject());
if (files.isEmpty())
return;
......@@ -612,7 +630,7 @@ void FolderNode::removeFileNodes(const QList<FileNode *> &files)
*/
void FolderNode::addFolderNodes(const QList<FolderNode*> &subFolders)
{
Q_ASSERT(parentProjectNode());
Q_ASSERT(managingProject());
if (subFolders.isEmpty())
return;
......@@ -622,7 +640,6 @@ void FolderNode::addFolderNodes(const QList<FolderNode*> &subFolders)
QTC_ASSERT(!folder->parentFolderNode(),
qDebug("Project node has already a parent folder"));
folder->setParentFolderNode(this);
folder->setProjectNode(parentProjectNode());
// Find the correct place to insert
if (m_folderNodes.count() == 0
......@@ -651,7 +668,7 @@ void FolderNode::addFolderNodes(const QList<FolderNode*> &subFolders)
*/
void FolderNode::removeFolderNodes(const QList<FolderNode*> &subFolders)
{
Q_ASSERT(parentProjectNode());
Q_ASSERT(managingProject());
if (subFolders.isEmpty())
return;
......@@ -720,8 +737,6 @@ int VirtualFolderNode::priority() const
ProjectNode::ProjectNode(const Utils::FileName &projectFilePath) :
FolderNode(projectFilePath, NodeType::Project)
{
// project node "manages" itself
setProjectNode(this);
setDisplayName(projectFilePath.fileName());
}
......
......@@ -109,8 +109,16 @@ class PROJECTEXPLORER_EXPORT Node
public:
virtual ~Node() = default;
NodeType nodeType() const;
ProjectNode *parentProjectNode() const; // managing project
ProjectNode *parentProjectNode() const; // parent project, will be nullptr for the top-level project
FolderNode *parentFolderNode() const; // parent folder or project
ProjectNode *managingProject(); // project managing this node.
// result is nullptr if node is the SessionNode
// or node if node is a ProjectNode directly below SessionNode
// or node->parentProjectNode() for all other cases.
const ProjectNode *managingProject() const; // see above.
const Utils::FileName &filePath() const; // file system path
int line() const;
virtual QString displayName() const;
......@@ -140,14 +148,12 @@ public:
protected:
Node(NodeType nodeType, const Utils::FileName &filePath, int line = -1);
void setProjectNode(ProjectNode *project);
void setParentFolderNode(FolderNode *parentFolder);
void emitNodeSortKeyAboutToChange();
void emitNodeSortKeyChanged();
private:
ProjectNode *m_projectNode = nullptr;
FolderNode *m_parentFolderNode = nullptr;
Utils::FileName m_filePath;
int m_line;
......
......@@ -87,7 +87,7 @@ QIcon QbsProductNode::m_productIcon;
static QbsProjectNode *parentQbsProjectNode(ProjectExplorer::Node *node)
{
for (ProjectExplorer::FolderNode *pn = node->parentProjectNode(); pn; pn = pn->parentFolderNode()) {
for (ProjectExplorer::FolderNode *pn = node->managingProject(); pn; pn = pn->parentProjectNode()) {
QbsProjectNode *prjNode = dynamic_cast<QbsProjectNode *>(pn);
if (prjNode)
return prjNode;
......@@ -719,7 +719,7 @@ void QbsProductNode::setQbsProductData(const qbs::Project &project, const qbs::P
QList<ProjectExplorer::RunConfiguration *> QbsProductNode::runConfigurations() const
{
QList<ProjectExplorer::RunConfiguration *> result;
QbsProjectNode *pn = dynamic_cast<QbsProjectNode *>(parentProjectNode());
auto pn = dynamic_cast<const QbsProjectNode *>(managingProject());
if (!isEnabled() || !pn || m_qbsProductData.targetExecutable().isEmpty())
return result;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment