diff --git a/src/plugins/resourceeditor/resourcenode.cpp b/src/plugins/resourceeditor/resourcenode.cpp index a619f0b6c1b..63a534c4d6e 100644 --- a/src/plugins/resourceeditor/resourcenode.cpp +++ b/src/plugins/resourceeditor/resourcenode.cpp @@ -27,15 +27,15 @@ #include "resourceeditorconstants.h" #include "qrceditor/resourcefile_p.h" -#include - #include #include #include -#include #include +#include +#include +#include #include #include @@ -121,102 +121,89 @@ ResourceTopLevelNode::~ResourceTopLevelNode() void ResourceTopLevelNode::addInternalNodes() { - QMap> nodesToAdd; - QMap> foldersToAddToPrefix; - ResourceFile file(filePath().toString(), m_contents); - if (file.load() == Core::IDocument::OpenResult::Success) { - QMap prefixNodes; - QMap folderNodes; + if (file.load() != Core::IDocument::OpenResult::Success) + return; - int prfxcount = file.prefixCount(); - 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(prefixNodes[prefixId]); + QMap folderNodes; - QSet fileNames; - int filecount = file.fileCount(i); - for (int j = 0; j < filecount; ++j) { - const QString &fileName = file.file(i, j); - QString alias = file.alias(i, j); - if (fileNames.contains(fileName)) { - // The file name is duplicated, skip it - // Note: this is wrong, but the qrceditor doesn't allow it either - // only aliases need to be unique - } else { - 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; - } - } + int prfxcount = file.prefixCount(); + 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 (!folderNodes.contains(prefixId)) { + ProjectExplorer::FolderNode *fn = new ResourceFolderNode(file.prefix(i), file.lang(i), this); + addNode(fn); + folderNodes.insert(prefixId, fn); } - } + ResourceFolderNode *currentPrefixNode = static_cast(folderNodes[prefixId]); - const QList fnodes = folderNodes(); - for (FolderNode *sfn : fnodes) { - ResourceFolderNode *srn = static_cast(sfn); - PrefixFolderLang folderId(srn->prefix(), QString(), srn->lang()); - const QList nodes = nodesToAdd[folderId]; - for (Node *n : nodes) - srn->addNode(n); - const QList sfnodes = sfn->folderNodes(); - for (FolderNode *ssfn : sfnodes) { - SimpleResourceFolderNode *sssn = static_cast(ssfn); - sssn->addFilesAndSubfolders(nodesToAdd, srn->prefix(), srn->lang()); + QSet fileNames; + int filecount = file.fileCount(i); + for (int j = 0; j < filecount; ++j) { + const QString &fileName = file.file(i, j); + if (fileNames.contains(fileName)) { + // The file name is duplicated, skip it + // Note: this is wrong, but the qrceditor doesn't allow it either + // only aliases need to be unique + continue; + } + + 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; } - -void SimpleResourceFolderNode::addFilesAndSubfolders(const QMap> &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(subNode); - sn->addFilesAndSubfolders(nodesToAdd, prefix, lang); - } -} diff --git a/src/plugins/resourceeditor/resourcenode.h b/src/plugins/resourceeditor/resourcenode.h index e8d480122eb..233da99ca2d 100644 --- a/src/plugins/resourceeditor/resourcenode.h +++ b/src/plugins/resourceeditor/resourcenode.h @@ -122,8 +122,6 @@ public: const QString &prefix, const QString &lang, Utils::FileName absolutePath, ResourceTopLevelNode *topLevel, ResourceFolderNode *prefixNode); QList supportedActions(ProjectExplorer::Node *node) const; - void addFilesAndSubfolders(const QMap > &nodesToAdd, - const QString &prefix, const QString &lang); bool addFiles(const QStringList &filePaths, QStringList *notAdded); bool removeFiles(const QStringList &filePaths, QStringList *notRemoved); bool renameFile(const QString &filePath, const QString &newFilePath);