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

0001 // ct_lvtqtc_undo_add_component.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_undo_add_component.h>
0021 
0022 #include <ct_lvtldr_lakosiannode.h>
0023 #include <ct_lvtldr_nodestorage.h>
0024 #include <ct_lvtqtc_graphicsscene.h>
0025 #include <ct_lvtqtc_graphicsview.h>
0026 #include <ct_lvtqtc_isa.h>
0027 #include <ct_lvtqtc_lakosentity.h>
0028 
0029 #include <ct_lvtqtc_packageentity.h>
0030 
0031 #include <ct_lvtshr_graphenums.h>
0032 
0033 #include <optional>
0034 
0035 #include <QPointF>
0036 
0037 using namespace Codethink::lvtqtc;
0038 using namespace Codethink;
0039 
0040 namespace Codethink::lvtldr {
0041 class LakosianNode;
0042 }
0043 
0044 UndoAddComponent::UndoAddComponent(GraphicsScene *scene,
0045                                    const QPointF& pos,
0046                                    const std::string& name,
0047                                    const std::string& qualifiedName,
0048                                    const std::string& parentQualifiedName,
0049                                    QtcUtil::UndoActionType undoActionType,
0050                                    lvtldr::NodeStorage& storage):
0051     UndoAddEntityBase{scene, pos, name, qualifiedName, parentQualifiedName, undoActionType, storage}
0052 {
0053     if (undoActionType == QtcUtil::UndoActionType::e_Add) {
0054         setText(QObject::tr("Add Component '%1'").arg(QString::fromStdString(name)));
0055     }
0056 
0057     if (undoActionType == QtcUtil::UndoActionType::e_Remove) {
0058         setText(QObject::tr("Remove Component '%1'").arg(QString::fromStdString(name)));
0059     }
0060 }
0061 
0062 UndoAddComponent::~UndoAddComponent() = default;
0063 
0064 void UndoAddComponent::removeEntity()
0065 {
0066     auto *node = m_storage.findByQualifiedName(lvtshr::DiagramType::ComponentType, m_addInfo.qualifiedName);
0067     m_addInfo.currentNotes = node->notes();
0068 
0069     if (m_scene) {
0070         LakosEntity *visualEntity = m_scene->findLakosEntityFromUid(node->uid());
0071         m_addInfo.pos = visualEntity->pos();
0072     }
0073     m_storage.removeComponent(node).expect("Unexpected error on UndoRedo action");
0074 
0075     if (m_scene) {
0076         m_scene->updateBoundingRect();
0077     }
0078 }
0079 
0080 void UndoAddComponent::addEntity()
0081 {
0082     auto *parent = m_storage.findByQualifiedName(lvtshr::DiagramType::PackageType, m_addInfo.parentQualifiedName);
0083     auto result = m_storage.addComponent(m_addInfo.name, m_addInfo.qualifiedName, parent);
0084     assert(!result.has_error());
0085     auto *newComponent = result.value();
0086     newComponent->setNotes(m_addInfo.currentNotes);
0087 
0088     if (m_scene) {
0089         m_scene->setEntityPos(newComponent->uid(), m_addInfo.pos);
0090     }
0091 }