File indexing completed on 2024-05-19 05:42:14
0001 // ct_lvtmdl_packagetreemodel.cpp -*-C++-*- 0002 0003 /* 0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0005 // SPDX-License-Identifier: Apache-2.0 0006 // 0007 // Licensed under the Apache License, Version 2.0 (the "License"); 0008 // you may not use this file except in compliance with the License. 0009 // You may obtain a copy of the License at 0010 // 0011 // http://www.apache.org/licenses/LICENSE-2.0 0012 // 0013 // Unless required by applicable law or agreed to in writing, software 0014 // distributed under the License is distributed on an "AS IS" BASIS, 0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 // See the License for the specific language governing permissions and 0017 // limitations under the License. 0018 */ 0019 0020 #include <ct_lvtmdl_packagetreemodel.h> 0021 0022 #include <ct_lvtldr_nodestorage.h> 0023 #include <ct_lvtldr_packagenode.h> 0024 #include <ct_lvtldr_repositorynode.h> 0025 0026 #include <ct_lvtldr_lakosiannode.h> 0027 #include <ct_lvtmdl_modelhelpers.h> 0028 0029 namespace Codethink::lvtmdl { 0030 0031 using namespace lvtshr; 0032 using namespace lvtldr; 0033 0034 // -------------------------------------------- 0035 // struct PackageTreeModelPrivate 0036 // -------------------------------------------- 0037 struct PackageTreeModel::PackageTreeModelPrivate { 0038 NodeStorage& nodeStorage; 0039 0040 explicit PackageTreeModelPrivate(NodeStorage& nodeStorage): nodeStorage(nodeStorage) 0041 { 0042 } 0043 }; 0044 // -------------------------------------------- 0045 // class PackageTreeModel 0046 // -------------------------------------------- 0047 0048 PackageTreeModel::PackageTreeModel(NodeStorage& nodeStorage): 0049 d(std::make_unique<PackageTreeModel::PackageTreeModelPrivate>(nodeStorage)) 0050 { 0051 QObject::connect(&nodeStorage, &NodeStorage::nodeAdded, [this](LakosianNode *node, std::any) { // NOLINT 0052 reload(); 0053 (void) node; 0054 }); 0055 0056 QObject::connect(&nodeStorage, &NodeStorage::nodeRemoved, [this](LakosianNode *node) { 0057 reload(); 0058 (void) node; 0059 }); 0060 0061 QObject::connect(&nodeStorage, &NodeStorage::nodeNameChanged, [this](LakosianNode *node) { 0062 if (QStandardItem *item = itemForLakosianNode(node)) { 0063 item->setText(QString::fromStdString(node->name())); 0064 } 0065 }); 0066 } 0067 0068 PackageTreeModel::~PackageTreeModel() = default; 0069 0070 QStandardItem *PackageTreeModel::itemForLakosianNode(LakosianNode *node) 0071 { 0072 std::function<QStandardItem *(QStandardItem *)> recurseFnc = [&](QStandardItem *item) -> QStandardItem * { 0073 auto itemType = 0074 NodeType::toDiagramType(static_cast<NodeType::Enum>(item->data(ModelRoles::e_NodeType).toInt())); 0075 auto itemId = item->data(ModelRoles::e_Id).toLongLong(); 0076 if (node->type() == itemType && node->id() == itemId) { 0077 return item; 0078 } 0079 0080 for (int i = 0; i < item->rowCount(); ++i) { 0081 if (QStandardItem *foundItem = recurseFnc(item->child(i))) { 0082 return foundItem; 0083 } 0084 } 0085 0086 return nullptr; 0087 }; 0088 0089 return recurseFnc(invisibleRootItem()); 0090 } 0091 0092 void PackageTreeModel::reload() 0093 { 0094 QStandardItem *root = invisibleRootItem(); 0095 root->removeRows(0, root->rowCount()); 0096 0097 auto shouldLoadChildren = [](LakosianNode const& node) { 0098 // Will leave children of components (logical entities) to be lazy loaded 0099 return node.type() != lvtshr::DiagramType::ComponentType; 0100 }; 0101 0102 for (auto *topLvlEntity : d->nodeStorage.getTopLevelPackages()) { 0103 auto *package = dynamic_cast<PackageNode *>(topLvlEntity); 0104 if (package) { 0105 auto *item = ModelUtil::createTreeItemFromLakosianNode(*package, shouldLoadChildren); 0106 root->appendRow(item); 0107 continue; 0108 } 0109 0110 auto *repository = dynamic_cast<RepositoryNode *>(topLvlEntity); 0111 if (repository) { 0112 if (repository->name().empty()) { 0113 continue; 0114 } 0115 auto *item = ModelUtil::createTreeItemFromLakosianNode(*repository, shouldLoadChildren); 0116 root->appendRow(item); 0117 } 0118 } 0119 } 0120 0121 void PackageTreeModel::fetchMore(const QModelIndex& parent) 0122 { 0123 auto childItemsAlreadyLoaded = parent.data(ModelRoles::e_ChildItemsLoaded).toBool(); 0124 if (childItemsAlreadyLoaded) { 0125 return; 0126 } 0127 0128 auto qualifiedName = parent.data(ModelRoles::e_QualifiedName).toString().toStdString(); 0129 auto *parentNode = d->nodeStorage.findByQualifiedName(qualifiedName); 0130 if (!parentNode) { 0131 return; 0132 } 0133 0134 auto *parentItem = itemForLakosianNode(parentNode); 0135 if (!parentItem) { 0136 return; 0137 } 0138 0139 for (auto row = parentItem->rowCount() - 1; row >= 0; --row) { 0140 parentItem->removeRow(row); 0141 } 0142 ModelUtil::populateTreeItemChildren(*parentNode, *parentItem, [](LakosianNode const&) { 0143 return true; 0144 }); 0145 parentItem->setData(true, ModelRoles::e_ChildItemsLoaded); 0146 } 0147 0148 } // end namespace Codethink::lvtmdl