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_package.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 UndoAddPackage::UndoAddPackage(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 Package '%1'").arg(QString::fromStdString(name)));
0055     }
0056 
0057     if (undoActionType == QtcUtil::UndoActionType::e_Remove) {
0058         setText(QObject::tr("Remove Package '%1'").arg(QString::fromStdString(name)));
0059     }
0060 }
0061 
0062 UndoAddPackage::~UndoAddPackage() = default;
0063 
0064 void UndoAddPackage::removeEntity()
0065 {
0066     auto *node = m_storage.findByQualifiedName(lvtshr::DiagramType::PackageType, m_addInfo.qualifiedName);
0067     if (m_scene) {
0068         auto *visualEntity = m_scene->findLakosEntityFromUid(node->uid());
0069         if (visualEntity) {
0070             m_addInfo.pos = visualEntity->pos();
0071         }
0072     }
0073     m_addInfo.currentNotes = node->notes();
0074     m_storage.removePackage(node).expect("Unexpected error on UndoRedo action");
0075 
0076     if (m_scene) {
0077         m_scene->updateBoundingRect();
0078     }
0079 }
0080 
0081 void UndoAddPackage::addEntity()
0082 {
0083     auto *parent = m_addInfo.parentQualifiedName.empty()
0084         ? nullptr
0085         : m_storage.findByQualifiedName(lvtshr::DiagramType::PackageType, m_addInfo.parentQualifiedName);
0086     auto result = m_storage.addPackage(m_addInfo.name, m_addInfo.qualifiedName, parent, m_scene ? m_scene : std::any());
0087     assert(!result.has_error());
0088 
0089     auto *package = result.value();
0090     package->setNotes(m_addInfo.currentNotes);
0091 
0092     if (m_scene) {
0093         m_scene->setEntityPos(package->uid(), m_addInfo.pos);
0094         m_scene->updateBoundingRect();
0095     }
0096 }