File indexing completed on 2024-05-19 05:42:20

0001 // ct_lvtqtc_lakosentitypluginutils.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_lvtldr_lakosiannode.h>
0021 #include <ct_lvtqtc_lakosentitypluginutils.h>
0022 #include <ct_lvtqtc_lakosrelation.h>
0023 
0024 namespace Codethink::lvtqtc {
0025 
0026 Entity createWrappedEntityFromLakosEntity(LakosEntity *e)
0027 {
0028     auto getName = [e]() {
0029         return e->name();
0030     };
0031     auto getQualifiedName = [e]() {
0032         return e->qualifiedName();
0033     };
0034     auto getType = [e]() {
0035         switch (e->instanceType()) {
0036         case lvtshr::DiagramType::ClassType:
0037             return EntityType::Unknown;
0038         case lvtshr::DiagramType::ComponentType:
0039             return EntityType::Component;
0040         case lvtshr::DiagramType::PackageType:
0041             if (e->internalNode()->isPackageGroup()) {
0042                 return EntityType::PackageGroup;
0043             }
0044             return EntityType::Package;
0045         case lvtshr::DiagramType::RepositoryType:
0046             return EntityType::Unknown;
0047         case lvtshr::DiagramType::FreeFunctionType:
0048             return EntityType::Unknown;
0049         case lvtshr::DiagramType::NoneType:
0050             return EntityType::Unknown;
0051         }
0052         return EntityType::Unknown;
0053     };
0054     auto setColor = [e](Color c) {
0055         e->setColor(QColor{c.r, c.g, c.b, c.a});
0056     };
0057     auto addHoverInfo = [e](std::string const& info) {
0058         e->setTooltipString(e->tooltipString() + "\n" + info);
0059     };
0060     auto getDependencies = [e]() {
0061         auto dependencies = std::vector<Entity>{};
0062         for (auto const& c : e->edgesCollection()) {
0063             for (auto const& r : c->relations()) {
0064                 dependencies.push_back(createWrappedEntityFromLakosEntity(r->to()));
0065             }
0066         }
0067         return dependencies;
0068     };
0069     auto unloadFromScene = [e]() {
0070         Q_EMIT e->unloadThis();
0071     };
0072     auto getDbChildrenQualifiedNames = [e]() {
0073         auto childrenQNames = std::vector<std::string>{};
0074         for (auto const& c : e->internalNode()->children()) {
0075             childrenQNames.emplace_back(c->qualifiedName());
0076         }
0077         return childrenQNames;
0078     };
0079     auto getParent = [e]() -> std::optional<Entity> {
0080         auto parent = dynamic_cast<LakosEntity *>(e->parentItem());
0081         if (!parent) {
0082             return std::nullopt;
0083         }
0084         return createWrappedEntityFromLakosEntity(parent);
0085     };
0086     auto setSelected = [e](bool v) {
0087         e->setSelected(v);
0088     };
0089     auto isSelected = [e]() -> bool {
0090         return e->isSelected();
0091     };
0092     return Entity{getName,
0093                   getQualifiedName,
0094                   getType,
0095                   setColor,
0096                   addHoverInfo,
0097                   getDependencies,
0098                   unloadFromScene,
0099                   getDbChildrenQualifiedNames,
0100                   getParent,
0101                   setSelected,
0102                   isSelected};
0103 }
0104 
0105 Edge createWrappedEdgeFromLakosEntity(LakosEntity *from, LakosEntity *to)
0106 {
0107     auto getRelation = [from, to]() -> LakosRelation * {
0108         if (!from || !to) {
0109             return nullptr;
0110         }
0111         auto& ecs = from->edgesCollection();
0112         auto foundToNode = std::find_if(ecs.begin(), ecs.end(), [to](auto const& ec) {
0113             return ec->to() == to;
0114         });
0115         if (foundToNode == ecs.end()) {
0116             return nullptr;
0117         }
0118         return (*foundToNode)->relations()[0];
0119     };
0120     auto setColor = [getRelation](Color c) {
0121         auto *relation = getRelation();
0122         if (!relation) {
0123             return;
0124         }
0125         relation->setColor(QColor{c.r, c.g, c.b, c.a});
0126     };
0127     auto setStyle = [getRelation](EdgeStyle s) {
0128         auto *relation = getRelation();
0129         if (!relation) {
0130             return;
0131         }
0132         auto qtStyle = [&]() {
0133             switch (s) {
0134             case EdgeStyle::SolidLine:
0135                 return Qt::PenStyle::SolidLine;
0136             case EdgeStyle::DashLine:
0137                 return Qt::PenStyle::DashLine;
0138             case EdgeStyle::DotLine:
0139                 return Qt::PenStyle::DotLine;
0140             case EdgeStyle::DashDotLine:
0141                 return Qt::PenStyle::DashDotLine;
0142             case EdgeStyle::DashDotDotLine:
0143                 return Qt::PenStyle::DashDotDotLine;
0144             }
0145             return Qt::PenStyle::NoPen;
0146         }();
0147         relation->setStyle(qtStyle);
0148     };
0149     return Edge{setColor, setStyle};
0150 }
0151 
0152 } // namespace Codethink::lvtqtc