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

0001 // ct_lvtqtc_componententity.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_lvtqtc_componententity.h>
0021 
0022 #include <ct_lvtldr_lakosiannode.h>
0023 
0024 #include <ct_lvtqtc_logicalentity.h>
0025 
0026 #include <QBrush>
0027 #include <QPen>
0028 
0029 #include <preferences.h>
0030 
0031 namespace Codethink::lvtqtc {
0032 
0033 std::string nodeNameWithoutParentPrefix(lvtldr::LakosianNode *node, const std::string& name)
0034 {
0035     auto *parent = node->parent();
0036     if (!parent) {
0037         return name;
0038     }
0039 
0040     if (name.find(parent->name()) == 0) {
0041         auto newName = name;
0042         newName.erase(0, parent->name().size() + 1);
0043         return newName;
0044     }
0045 
0046     return name;
0047 }
0048 
0049 ComponentEntity::ComponentEntity(lvtldr::LakosianNode *node, lvtshr::LoaderInfo loaderInfo):
0050     LakosEntity(getUniqueId(node->id()), node, loaderInfo)
0051 {
0052     updateTooltip();
0053     ComponentEntity::setText(node->name());
0054     truncateTitle(EllipsisTextItem::Truncate::No);
0055 
0056     setFont(Preferences::componentFont());
0057     connect(Preferences::self(), &Preferences::componentFontChanged, this, [this] {
0058         setFont(Preferences::componentFont());
0059     });
0060 
0061     connect(Preferences::self(), &Preferences::hidePackagePrefixOnComponentsChanged, this, [this] {
0062         setText(name());
0063     });
0064 }
0065 
0066 void ComponentEntity::setText(const std::string& text)
0067 {
0068     auto newText =
0069         Preferences::hidePackagePrefixOnComponents() ? nodeNameWithoutParentPrefix(internalNode(), text) : text;
0070     LakosEntity::setText(newText);
0071 }
0072 
0073 void ComponentEntity::updateTooltip()
0074 {
0075     makeToolTip("(no namespace)");
0076 }
0077 
0078 std::string ComponentEntity::getUniqueId(const long long id)
0079 {
0080     return "source_component#" + std::to_string(id);
0081 }
0082 
0083 lvtshr::DiagramType ComponentEntity::instanceType() const
0084 {
0085     return lvtshr::DiagramType::ComponentType;
0086 }
0087 
0088 } // end namespace Codethink::lvtqtc