From 7b3db8ee1dd5131011662ddbb2247d4cebef9e2a Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Fri, 17 Feb 2017 13:17:31 +0100 Subject: [PATCH] qbs: Move setup of Group node tree into QbsNodeTreeBuilder Move code to set up the tree of project nodes out of the project nodes themselves. This makes them easier to manage and will enable creator to change to a less update-intense project tree. Change-Id: I2d0702d257e87543f47ebfb456344423ebe4f871 Reviewed-by: Christian Kandeler --- src/plugins/qbsprojectmanager/qbsnodes.cpp | 72 ++----------- src/plugins/qbsprojectmanager/qbsnodes.h | 2 - .../qbsprojectmanager/qbsnodetreebuilder.cpp | 101 ++++++++++++++++++ .../qbsprojectmanager/qbsnodetreebuilder.h | 50 +++++++++ .../qbsprojectmanager/qbsprojectmanager.pro | 2 + .../qbsprojectmanager/qbsprojectmanager.qbs | 2 + 6 files changed, 165 insertions(+), 64 deletions(-) create mode 100644 src/plugins/qbsprojectmanager/qbsnodetreebuilder.cpp create mode 100644 src/plugins/qbsprojectmanager/qbsnodetreebuilder.h diff --git a/src/plugins/qbsprojectmanager/qbsnodes.cpp b/src/plugins/qbsprojectmanager/qbsnodes.cpp index 149fa1167e7..1cf39f00061 100644 --- a/src/plugins/qbsprojectmanager/qbsnodes.cpp +++ b/src/plugins/qbsprojectmanager/qbsnodes.cpp @@ -25,6 +25,7 @@ #include "qbsnodes.h" +#include "qbsnodetreebuilder.h" #include "qbsproject.h" #include "qbsprojectmanagerconstants.h" #include "qbsrunconfiguration.h" @@ -322,11 +323,8 @@ QbsGroupNode::QbsGroupNode(const qbs::GroupData &grp, const QString &productPath setIcon(m_groupIcon); - addNode(new QbsFileNode(Utils::FileName::fromString(grp.location().filePath()), - ProjectExplorer::FileType::Project, false, - grp.location().line())); - - updateQbsGroupData(grp, productPath, true); + m_productPath = productPath; + m_qbsGroupData = grp; } QList QbsGroupNode::supportedActions(ProjectExplorer::Node *node) const @@ -391,32 +389,6 @@ bool QbsGroupNode::renameFile(const QString &filePath, const QString &newFilePat prdNode->qbsProductData(), m_qbsGroupData); } -void QbsGroupNode::updateQbsGroupData(const qbs::GroupData &grp, const QString &productPath, - bool productIsEnabled) -{ - QTC_ASSERT(grp.isValid(), return); - - setEnabled(productIsEnabled && grp.isEnabled()); - - m_productPath = productPath; - m_qbsGroupData = grp; - - setAbsoluteFilePathAndLine(Utils::FileName::fromString(grp.location().filePath()), line()); - setDisplayName(grp.name()); - - QbsFileNode *idx = 0; - foreach (ProjectExplorer::FileNode *fn, fileNodes()) { - idx = dynamic_cast(fn); - if (idx) - break; - } - QTC_ASSERT(idx, return); - idx->setAbsoluteFilePathAndLine(Utils::FileName::fromString(grp.location().filePath()), - grp.location().line()); - - setupFiles(this, grp, grp.allFilePaths(), productPath, false); -} - void QbsGroupNode::setupFiles(ProjectExplorer::FolderNode *root, const qbs::GroupData &group, const QStringList &files, const QString &productPath, bool generated) { @@ -438,7 +410,7 @@ void QbsGroupNode::setupFiles(ProjectExplorer::FolderNode *root, const qbs::Grou QHash fileTypeHash; foreach (const qbs::ArtifactData &sa, group.allSourceArtifacts()) - fileTypeHash[sa.filePath()] = fileType(sa); + fileTypeHash[sa.filePath()] = Internal::QbsNodeTreeBuilder::fileType(sa); setupFolder(root, fileTypeHash, &tree, productPath, generated); } @@ -506,27 +478,6 @@ void QbsGroupNode::setupFolder(ProjectExplorer::FolderNode *root, } } -ProjectExplorer::FileType QbsGroupNode::fileType(const qbs::ArtifactData &artifact) -{ - QTC_ASSERT(artifact.isValid(), return ProjectExplorer::FileType::Unknown); - - if (artifact.fileTags().contains(QLatin1String("c")) - || artifact.fileTags().contains(QLatin1String("cpp")) - || artifact.fileTags().contains(QLatin1String("objc")) - || artifact.fileTags().contains(QLatin1String("objcpp"))) { - return ProjectExplorer::FileType::Source; - } - if (artifact.fileTags().contains(QLatin1String("hpp"))) - return ProjectExplorer::FileType::Header; - if (artifact.fileTags().contains(QLatin1String("qrc"))) - return ProjectExplorer::FileType::Resource; - if (artifact.fileTags().contains(QLatin1String("ui"))) - return ProjectExplorer::FileType::Form; - if (artifact.fileTags().contains(QLatin1String("scxml"))) - return ProjectExplorer::FileType::StateChart; - return ProjectExplorer::FileType::Unknown; -} - // -------------------------------------------------------------------- // QbsProductNode: // -------------------------------------------------------------------- @@ -619,7 +570,8 @@ void QbsProductNode::setQbsProductData(const qbs::Project &project, const qbs::P setEnabled(prd.isEnabled()); setDisplayName(QbsProject::productDisplayName(project, prd)); - setAbsoluteFilePathAndLine(Utils::FileName::fromString(prd.location().filePath()), line()); + setAbsoluteFilePathAndLine(Utils::FileName::fromString(prd.location().filePath()).parentDir(), + line()); const QString &productPath = QFileInfo(prd.location().filePath()).absolutePath(); // Find the QbsFileNode we added earlier: @@ -636,18 +588,14 @@ void QbsProductNode::setQbsProductData(const qbs::Project &project, const qbs::P foreach (const qbs::GroupData &grp, prd.groups()) { if (grp.name() == prd.name() && grp.location() == prd.location()) { // Set implicit product group right onto this node: - QbsGroupNode::setupFiles(this, grp, grp.allFilePaths(), productPath, false); + QbsNodeTreeBuilder::setupArtifacts(this, grp.allSourceArtifacts()); continue; } - addNode(new QbsGroupNode(grp, productPath)); + addNode(QbsNodeTreeBuilder::buildGroupNodeTree(grp, productPath, prd.isEnabled())); } - if (prd.isEnabled()) { - const QStringList generatedFiles - = Utils::transform(prd.generatedArtifacts(), &qbs::ArtifactData::filePath); - QbsGroupNode::setupFiles(m_generatedFilesNode, qbs::GroupData(), generatedFiles, - prd.buildDirectory(), true); - } + if (prd.isEnabled()) + QbsNodeTreeBuilder::setupArtifacts(this, prd.generatedArtifacts()); m_qbsProductData = prd; } diff --git a/src/plugins/qbsprojectmanager/qbsnodes.h b/src/plugins/qbsprojectmanager/qbsnodes.h index 1ea8edbef26..45f64da3eed 100644 --- a/src/plugins/qbsprojectmanager/qbsnodes.h +++ b/src/plugins/qbsprojectmanager/qbsnodes.h @@ -96,7 +96,6 @@ public: bool addFiles(const QStringList &filePaths, QStringList *notAdded = 0) override; bool removeFiles(const QStringList &filePaths, QStringList *notRemoved = 0) override; bool renameFile(const QString &filePath, const QString &newFilePath) override; - void updateQbsGroupData(const qbs::GroupData &grp, const QString &productPath, bool productIsEnabled); qbs::GroupData qbsGroupData() const { return m_qbsGroupData; } @@ -111,7 +110,6 @@ private: const QHash &fileTypeHash, const FileTreeNode *subFileTree, const QString &baseDir, bool generated); - static ProjectExplorer::FileType fileType(const qbs::ArtifactData &artifact); qbs::GroupData m_qbsGroupData; QString m_productPath; diff --git a/src/plugins/qbsprojectmanager/qbsnodetreebuilder.cpp b/src/plugins/qbsprojectmanager/qbsnodetreebuilder.cpp new file mode 100644 index 00000000000..768a8d1c08a --- /dev/null +++ b/src/plugins/qbsprojectmanager/qbsnodetreebuilder.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "qbsnodetreebuilder.h" + +#include +#include + +namespace { + +void setupArtifacts(ProjectExplorer::FolderNode *root, const QList &artifacts) +{ + QList fileNodes + = Utils::transform(artifacts, [](const qbs::ArtifactData &ad) { + const Utils::FileName path = Utils::FileName::fromString(ad.filePath()); + const ProjectExplorer::FileType type = + QbsProjectManager::Internal::QbsNodeTreeBuilder::fileType(ad); + const bool isGenerated = ad.isGenerated(); + return new ProjectExplorer::FileNode(path, type, isGenerated); + }); + + root->buildTree(fileNodes); + root->compress(); +} + +} // namespace + +namespace QbsProjectManager { +namespace Internal { + +ProjectExplorer::FileType QbsNodeTreeBuilder::fileType(const qbs::ArtifactData &artifact) +{ + QTC_ASSERT(artifact.isValid(), return ProjectExplorer::FileType::Unknown); + + if (artifact.fileTags().contains("c") + || artifact.fileTags().contains("cpp") + || artifact.fileTags().contains("objc") + || artifact.fileTags().contains("objcpp")) { + return ProjectExplorer::FileType::Source; + } + if (artifact.fileTags().contains("hpp")) + return ProjectExplorer::FileType::Header; + if (artifact.fileTags().contains("qrc")) + return ProjectExplorer::FileType::Resource; + if (artifact.fileTags().contains("ui")) + return ProjectExplorer::FileType::Form; + if (artifact.fileTags().contains("scxml")) + return ProjectExplorer::FileType::StateChart; + return ProjectExplorer::FileType::Unknown; +} + +QbsGroupNode *QbsNodeTreeBuilder::buildGroupNodeTree(const qbs::GroupData &grp, + const QString &productPath, + bool productIsEnabled) +{ + QTC_ASSERT(grp.isValid(), return nullptr); + + auto result = new QbsGroupNode(grp, productPath); + + result->setEnabled(productIsEnabled && grp.isEnabled()); + result->setAbsoluteFilePathAndLine( + Utils::FileName::fromString(grp.location().filePath()).parentDir(), -1); + result->setDisplayName(grp.name()); + result->addNode(new QbsFileNode(Utils::FileName::fromString(grp.location().filePath()), + ProjectExplorer::FileType::Project, false, + grp.location().line())); + + ::setupArtifacts(result, grp.allSourceArtifacts()); + + return result; +} + +void QbsNodeTreeBuilder::setupArtifacts(QbsBaseProjectNode *node, const QList &artifacts) +{ + ::setupArtifacts(node, artifacts); +} + +} // namespace Internal +} // namespace QbsProjectManager diff --git a/src/plugins/qbsprojectmanager/qbsnodetreebuilder.h b/src/plugins/qbsprojectmanager/qbsnodetreebuilder.h new file mode 100644 index 00000000000..b7001a09ab0 --- /dev/null +++ b/src/plugins/qbsprojectmanager/qbsnodetreebuilder.h @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include "qbsnodes.h" + +#include + +namespace QbsProjectManager { +namespace Internal { + +// ---------------------------------------------------------------------- +// QbsNodeTreeBuilder: +// ---------------------------------------------------------------------- + +class QbsNodeTreeBuilder +{ +public: + static ProjectExplorer::FileType fileType(const qbs::ArtifactData &artifact); + + static QbsGroupNode *buildGroupNodeTree(const qbs::GroupData &grp, const QString &productPath, + bool productIsEnabled); + static void setupArtifacts(QbsBaseProjectNode *node, const QList &artifacts); +}; + +} // namespace Internal +} // namespace QbsProjectManager diff --git a/src/plugins/qbsprojectmanager/qbsprojectmanager.pro b/src/plugins/qbsprojectmanager/qbsprojectmanager.pro index 1c82b79e7e9..de973943d69 100644 --- a/src/plugins/qbsprojectmanager/qbsprojectmanager.pro +++ b/src/plugins/qbsprojectmanager/qbsprojectmanager.pro @@ -29,6 +29,7 @@ HEADERS = \ qbsinfopage.h \ qbslogsink.h \ qbsnodes.h \ + qbsnodetreebuilder.h \ qbsparser.h \ qbspmlogging.h \ qbsprofilessettingspage.h \ @@ -53,6 +54,7 @@ SOURCES = \ qbsinfopage.cpp \ qbslogsink.cpp \ qbsnodes.cpp \ + qbsnodetreebuilder.cpp \ qbsparser.cpp \ qbspmlogging.cpp \ qbsprofilessettingspage.cpp \ diff --git a/src/plugins/qbsprojectmanager/qbsprojectmanager.qbs b/src/plugins/qbsprojectmanager/qbsprojectmanager.qbs index d833515490f..6cb3fae6c9e 100644 --- a/src/plugins/qbsprojectmanager/qbsprojectmanager.qbs +++ b/src/plugins/qbsprojectmanager/qbsprojectmanager.qbs @@ -84,6 +84,8 @@ QtcPlugin { "qbslogsink.h", "qbsnodes.cpp", "qbsnodes.h", + "qbsnodetreebuilder.cpp", + "qbsnodetreebuilder.h", "qbsparser.cpp", "qbsparser.h", "qbspmlogging.cpp",