Commit c7b0163f authored by Tobias Hunger's avatar Tobias Hunger
Browse files

ProjectExplorer: Make FolderNodes a bit easier to work with



Add some methods to look up different kinds of nodes in the
project tree.

Change-Id: Ia91844b45c2a124dc01771297e1f5c414d84e7cb
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 67e011ba
......@@ -331,6 +331,35 @@ QList<FileNode*> FolderNode::fileNodes() const
return m_fileNodes;
}
FileNode *FolderNode::fileNode(const Utils::FileName &file) const
{
return Utils::findOrDefault(m_fileNodes, [&file](const FileNode *fn) {
return fn->filePath() == file;
});
}
FileNode *FolderNode::recursiveFileNode(const Utils::FileName &file) const
{
Utils::FileName dir = file.parentDir();
const QDir thisDir(filePath().toString());
QString relativePath = thisDir.relativeFilePath(dir.toString());
if (relativePath == ".")
relativePath.clear();
QStringList parts = relativePath.split('/', QString::SkipEmptyParts);
const ProjectExplorer::FolderNode *parent = this;
foreach (const QString &part, parts) {
dir.appendPath(part);
// Find folder in subFolders
parent = Utils::findOrDefault(parent->folderNodes(), [&dir](const FolderNode *fn) {
return fn->filePath() == dir;
});
if (!parent)
return nullptr;
}
return parent->fileNode(file);
}
QList<FileNode *> FolderNode::recursiveFileNodes() const
{
QList<FileNode *> result = fileNodes();
......@@ -344,34 +373,35 @@ QList<FolderNode*> FolderNode::folderNodes() const
return m_folderNodes;
}
FolderNode *FolderNode::findOrCreateFolderNode(const QString &directory)
FolderNode *FolderNode::folderNode(const Utils::FileName &directory) const
{
return Utils::findOrDefault(m_folderNodes, [&directory](const FolderNode *fn) {
return fn->filePath() == directory;
});
}
FolderNode *FolderNode::recursiveFindOrCreateFolderNode(const QString &directory)
{
Utils::FileName path = filePath();
QDir parentDir(path.toString());
QString relativePath = parentDir.relativeFilePath(directory);
if (relativePath == ".")
relativePath.clear();
QStringList parts = relativePath.split('/', QString::SkipEmptyParts);
ProjectExplorer::FolderNode *parent = this;
foreach (const QString &part, parts) {
path.appendPath(part);
// Find folder in subFolders
bool found = false;
foreach (ProjectExplorer::FolderNode *folder, parent->folderNodes()) {
if (folder->filePath() == path) {
// yeah found something :)
parent = folder;
found = true;
break;
}
}
if (!found) {
FolderNode *next = parent->folderNode(path);
if (!next) {
// No FolderNode yet, so create it
auto tmp = new ProjectExplorer::FolderNode(path);
tmp->setDisplayName(part);
parent->addFolderNodes(QList<ProjectExplorer::FolderNode *>({ tmp }));
parent = tmp;
next = tmp;
}
parent = next;
}
return parent;
}
......@@ -397,7 +427,7 @@ void FolderNode::buildTree(QList<FileNode *> &files)
foreach (ProjectExplorer::FileNode *fn, added) {
// Get relative path to rootNode
QString parentDir = fn->filePath().toFileInfo().absolutePath();
ProjectExplorer::FolderNode *folder = findOrCreateFolderNode(parentDir);
ProjectExplorer::FolderNode *folder = recursiveFindOrCreateFolderNode(parentDir);
addedFolderMapping[folder] << fn;
}
......
......@@ -184,9 +184,12 @@ public:
QIcon icon() const;
QList<FileNode *> fileNodes() const;
FileNode *fileNode(const Utils::FileName &file) const;
FileNode *recursiveFileNode(const Utils::FileName &file) const;
QList<FileNode *> recursiveFileNodes() const;
QList<FolderNode *> folderNodes() const;
FolderNode *findOrCreateFolderNode(const QString &directory);
FolderNode *folderNode(const Utils::FileName &directory) const;
FolderNode *recursiveFindOrCreateFolderNode(const QString &directory);
void buildTree(QList<FileNode *> &files);
virtual void accept(NodesVisitor *visitor);
......
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