ResourceEditor: Simplify Resource tree construction

Instead of keeping track which nodes need to be created, create
the tree on-the-fly directly.

Change-Id: Iebb00c385f2abe42c7fd04547a7af85ad8f8f87b
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
hjk
2017-02-10 13:54:33 +01:00
parent ba058f3a29
commit 1400522408
2 changed files with 81 additions and 107 deletions

View File

@@ -27,15 +27,15 @@
#include "resourceeditorconstants.h" #include "resourceeditorconstants.h"
#include "qrceditor/resourcefile_p.h" #include "qrceditor/resourcefile_p.h"
#include <utils/fileutils.h>
#include <coreplugin/documentmanager.h> #include <coreplugin/documentmanager.h>
#include <coreplugin/fileiconprovider.h> #include <coreplugin/fileiconprovider.h>
#include <qmljstools/qmljstoolsconstants.h> #include <qmljstools/qmljstoolsconstants.h>
#include <utils/mimetypes/mimedatabase.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcassert.h>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDir> #include <QDir>
@@ -121,102 +121,89 @@ ResourceTopLevelNode::~ResourceTopLevelNode()
void ResourceTopLevelNode::addInternalNodes() void ResourceTopLevelNode::addInternalNodes()
{ {
QMap<PrefixFolderLang, QList<ProjectExplorer::Node *>> nodesToAdd;
QMap<PrefixFolderLang, QList<ProjectExplorer::FolderNode *>> foldersToAddToPrefix;
ResourceFile file(filePath().toString(), m_contents); ResourceFile file(filePath().toString(), m_contents);
if (file.load() == Core::IDocument::OpenResult::Success) { if (file.load() != Core::IDocument::OpenResult::Success)
QMap<PrefixFolderLang, ProjectExplorer::FolderNode *> prefixNodes; return;
QMap<PrefixFolderLang, ProjectExplorer::FolderNode *> folderNodes;
int prfxcount = file.prefixCount(); QMap<PrefixFolderLang, ProjectExplorer::FolderNode *> folderNodes;
for (int i = 0; i < prfxcount; ++i) {
const QString &prefix = file.prefix(i);
const QString &lang = file.lang(i);
// ensure that we don't duplicate prefixes
PrefixFolderLang prefixId(prefix, QString(), lang);
if (!prefixNodes.contains(prefixId)) {
ProjectExplorer::FolderNode *fn = new ResourceFolderNode(file.prefix(i), file.lang(i), this);
addNode(fn);
prefixNodes.insert(prefixId, fn);
}
ResourceFolderNode *currentPrefixNode = static_cast<ResourceFolderNode*>(prefixNodes[prefixId]);
QSet<QString> fileNames; int prfxcount = file.prefixCount();
int filecount = file.fileCount(i); for (int i = 0; i < prfxcount; ++i) {
for (int j = 0; j < filecount; ++j) { const QString &prefix = file.prefix(i);
const QString &fileName = file.file(i, j); const QString &lang = file.lang(i);
QString alias = file.alias(i, j); // ensure that we don't duplicate prefixes
if (fileNames.contains(fileName)) { PrefixFolderLang prefixId(prefix, QString(), lang);
// The file name is duplicated, skip it if (!folderNodes.contains(prefixId)) {
// Note: this is wrong, but the qrceditor doesn't allow it either ProjectExplorer::FolderNode *fn = new ResourceFolderNode(file.prefix(i), file.lang(i), this);
// only aliases need to be unique addNode(fn);
} else { folderNodes.insert(prefixId, fn);
if (alias.isEmpty())
alias = filePath().toFileInfo().absoluteDir().relativeFilePath(fileName);
QString prefixWithSlash = prefix;
if (!prefixWithSlash.endsWith(QLatin1Char('/')))
prefixWithSlash.append(QLatin1Char('/'));
const QString fullPath = QDir::cleanPath(alias);
QStringList pathList = fullPath.split(QLatin1Char('/'));
const QString displayName = pathList.last();
pathList.removeLast(); // remove file name
bool parentIsPrefix = true;
QString parentFolderName;
PrefixFolderLang folderId(prefix, QString(), lang);
QStringList currentPathList;
foreach (const QString &pathElement, pathList) {
currentPathList << pathElement;
const QString folderName = currentPathList.join(QLatin1Char('/'));
folderId = PrefixFolderLang(prefix, folderName, lang);
if (!folderNodes.contains(folderId)) {
const QString absoluteFolderName
= filePath().toFileInfo().absoluteDir().absoluteFilePath(
currentPathList.join(QLatin1Char('/')));
const Utils::FileName folderPath
= Utils::FileName::fromString(absoluteFolderName);
ProjectExplorer::FolderNode *newNode
= new SimpleResourceFolderNode(folderName, pathElement,
prefix, lang, folderPath,
this, currentPrefixNode);
if (parentIsPrefix) {
foldersToAddToPrefix[prefixId] << newNode;
nodesToAdd[prefixId] << newNode;
} else {
PrefixFolderLang parentFolderId(prefix, parentFolderName, lang);
nodesToAdd[parentFolderId] << newNode;
}
folderNodes.insert(folderId, newNode);
}
parentIsPrefix = false;
parentFolderName = folderName;
}
const QString qrcPath = QDir::cleanPath(prefixWithSlash + alias);
fileNames.insert(fileName);
auto rn = new ResourceFileNode(Utils::FileName::fromString(fileName),
qrcPath, displayName);
nodesToAdd[folderId] << rn;
}
}
} }
} ResourceFolderNode *currentPrefixNode = static_cast<ResourceFolderNode*>(folderNodes[prefixId]);
const QList<FolderNode *> fnodes = folderNodes(); QSet<QString> fileNames;
for (FolderNode *sfn : fnodes) { int filecount = file.fileCount(i);
ResourceFolderNode *srn = static_cast<ResourceFolderNode *>(sfn); for (int j = 0; j < filecount; ++j) {
PrefixFolderLang folderId(srn->prefix(), QString(), srn->lang()); const QString &fileName = file.file(i, j);
const QList<ProjectExplorer::Node *> nodes = nodesToAdd[folderId]; if (fileNames.contains(fileName)) {
for (Node *n : nodes) // The file name is duplicated, skip it
srn->addNode(n); // Note: this is wrong, but the qrceditor doesn't allow it either
const QList<FolderNode *> sfnodes = sfn->folderNodes(); // only aliases need to be unique
for (FolderNode *ssfn : sfnodes) { continue;
SimpleResourceFolderNode *sssn = static_cast<SimpleResourceFolderNode *>(ssfn); }
sssn->addFilesAndSubfolders(nodesToAdd, srn->prefix(), srn->lang());
QString alias = file.alias(i, j);
if (alias.isEmpty())
alias = filePath().toFileInfo().absoluteDir().relativeFilePath(fileName);
QString prefixWithSlash = prefix;
if (!prefixWithSlash.endsWith(QLatin1Char('/')))
prefixWithSlash.append(QLatin1Char('/'));
const QString fullPath = QDir::cleanPath(alias);
QStringList pathList = fullPath.split(QLatin1Char('/'));
const QString displayName = pathList.last();
pathList.removeLast(); // remove file name
bool parentIsPrefix = true;
QString parentFolderName;
PrefixFolderLang folderId(prefix, QString(), lang);
QStringList currentPathList;
foreach (const QString &pathElement, pathList) {
currentPathList << pathElement;
const QString folderName = currentPathList.join(QLatin1Char('/'));
folderId = PrefixFolderLang(prefix, folderName, lang);
if (!folderNodes.contains(folderId)) {
const QString absoluteFolderName
= filePath().toFileInfo().absoluteDir().absoluteFilePath(
currentPathList.join(QLatin1Char('/')));
const Utils::FileName folderPath
= Utils::FileName::fromString(absoluteFolderName);
ProjectExplorer::FolderNode *newNode
= new SimpleResourceFolderNode(folderName, pathElement,
prefix, lang, folderPath,
this, currentPrefixNode);
folderNodes.insert(folderId, newNode);
PrefixFolderLang thisPrefixId = prefixId;
if (!parentIsPrefix)
thisPrefixId = PrefixFolderLang(prefix, parentFolderName, lang);
FolderNode *fn = folderNodes[thisPrefixId];
QTC_CHECK(fn);
if (fn)
fn->addNode(newNode);
}
parentIsPrefix = false;
parentFolderName = folderName;
}
const QString qrcPath = QDir::cleanPath(prefixWithSlash + alias);
fileNames.insert(fileName);
FolderNode *fn = folderNodes[folderId];
QTC_CHECK(fn);
if (fn)
fn->addNode(new ResourceFileNode(Utils::FileName::fromString(fileName),
qrcPath, displayName));
} }
} }
} }
@@ -651,14 +638,3 @@ ResourceFolderNode *SimpleResourceFolderNode::prefixNode() const
{ {
return m_prefixNode; return m_prefixNode;
} }
void SimpleResourceFolderNode::addFilesAndSubfolders(const QMap<PrefixFolderLang, QList<Node *>> &nodesToAdd,
const QString &prefix, const QString &lang)
{
for (Node *node : nodesToAdd.value(PrefixFolderLang(prefix, m_folderName, lang)))
addNode(node);
foreach (FolderNode* subNode, folderNodes()) {
SimpleResourceFolderNode* sn = static_cast<SimpleResourceFolderNode*>(subNode);
sn->addFilesAndSubfolders(nodesToAdd, prefix, lang);
}
}

View File

@@ -122,8 +122,6 @@ public:
const QString &prefix, const QString &lang, Utils::FileName absolutePath, const QString &prefix, const QString &lang, Utils::FileName absolutePath,
ResourceTopLevelNode *topLevel, ResourceFolderNode *prefixNode); ResourceTopLevelNode *topLevel, ResourceFolderNode *prefixNode);
QList<ProjectExplorer::ProjectAction> supportedActions(ProjectExplorer::Node *node) const; QList<ProjectExplorer::ProjectAction> supportedActions(ProjectExplorer::Node *node) const;
void addFilesAndSubfolders(const QMap<PrefixFolderLang, QList<Node *> > &nodesToAdd,
const QString &prefix, const QString &lang);
bool addFiles(const QStringList &filePaths, QStringList *notAdded); bool addFiles(const QStringList &filePaths, QStringList *notAdded);
bool removeFiles(const QStringList &filePaths, QStringList *notRemoved); bool removeFiles(const QStringList &filePaths, QStringList *notRemoved);
bool renameFile(const QString &filePath, const QString &newFilePath); bool renameFile(const QString &filePath, const QString &newFilePath);