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

0001 // ct_lvtqtc_undo_add_entity.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_logicalentity.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_lvtshr_graphenums.h>
0030 
0031 #include <QPointF>
0032 
0033 using namespace Codethink::lvtqtc;
0034 using namespace Codethink::lvtshr;
0035 using namespace Codethink;
0036 using lvtshr::UDTKind;
0037 
0038 namespace Codethink::lvtldr {
0039 class LakosianNode;
0040 }
0041 
0042 UndoAddLogicalEntity::UndoAddLogicalEntity(GraphicsScene *scene,
0043                                            const QPointF& pos,
0044                                            const std::string& name,
0045                                            const std::string& qualifiedName,
0046                                            const std::string& parentQualifiedName,
0047                                            QtcUtil::UndoActionType undoActionType,
0048                                            lvtldr::NodeStorage& storage):
0049     UndoAddEntityBase{scene, pos, name, qualifiedName, parentQualifiedName, undoActionType, storage}
0050 {
0051     if (undoActionType == QtcUtil::UndoActionType::e_Add) {
0052         setText(QObject::tr("Add Logical Entity '%1'").arg(QString::fromStdString(name)));
0053     }
0054 
0055     if (undoActionType == QtcUtil::UndoActionType::e_Remove) {
0056         setText(QObject::tr("Remove Logical Entity '%1'").arg(QString::fromStdString(name)));
0057     }
0058 }
0059 
0060 UndoAddLogicalEntity::~UndoAddLogicalEntity() = default;
0061 
0062 void UndoAddLogicalEntity::removeEntity()
0063 {
0064     auto *node = m_storage.findByQualifiedName(lvtshr::DiagramType::ClassType, m_addInfo.qualifiedName);
0065     if (m_scene) {
0066         auto *visualEntity = m_scene->findLakosEntityFromUid(node->uid());
0067         if (visualEntity) {
0068             m_addInfo.pos = visualEntity->pos();
0069         }
0070     }
0071     m_addInfo.currentNotes = node->notes();
0072     m_storage.removeLogicalEntity(node).expect("Unexpected error trying to remove node on undo/redo");
0073     if (m_scene) {
0074         m_scene->updateBoundingRect();
0075     }
0076 }
0077 
0078 void UndoAddLogicalEntity::addEntity()
0079 {
0080     auto *parent = m_storage.findByQualifiedName(lvtshr::DiagramType::ComponentType, m_addInfo.parentQualifiedName);
0081     auto result = m_storage.addLogicalEntity(m_addInfo.name, m_addInfo.qualifiedName, parent, UDTKind::Class);
0082     assert(!result.has_error());
0083     auto *node = result.value();
0084 
0085     node->setNotes(m_addInfo.currentNotes);
0086     if (m_scene) {
0087         m_scene->setEntityPos(node->uid(), m_addInfo.pos);
0088         m_scene->updateBoundingRect();
0089     }
0090 }