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

0001 // ct_lvtqtc_undo_reparent_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_reparent_entity.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 
0028 using namespace Codethink::lvtldr;
0029 using namespace Codethink::lvtqtc;
0030 
0031 struct UndoReparentEntity::Private {
0032     NodeStorage& nodeStorage;
0033     LakosianNode *entity = nullptr;
0034     LakosianNode *oldParent = nullptr;
0035     LakosianNode *newParent = nullptr;
0036     std::string oldName;
0037     std::string newName;
0038 };
0039 
0040 UndoReparentEntity::UndoReparentEntity(NodeStorage& nodeStorage,
0041                                        LakosianNode *entity,
0042                                        LakosianNode *oldParent,
0043                                        LakosianNode *newParent,
0044                                        std::string const& oldName,
0045                                        std::string const& newName):
0046     d(std::make_unique<UndoReparentEntity::Private>(
0047         Private{nodeStorage, entity, oldParent, newParent, oldName, newName}))
0048 {
0049     setText(QObject::tr("Reparent Entity"));
0050 }
0051 
0052 UndoReparentEntity::~UndoReparentEntity() = default;
0053 
0054 void UndoReparentEntity::undo()
0055 {
0056     d->nodeStorage.reparentEntity(d->entity, d->oldParent).expect("Unexpected error on undo/redo");
0057     d->entity->setName(d->oldName);
0058 }
0059 
0060 void UndoReparentEntity::redo()
0061 {
0062     // Qt calls redo() when pushing the UndoCommand for the first time, but we do not want it to be run, as the first
0063     // run can lead to failures.
0064     if (isFirstRun) {
0065         isFirstRun = false;
0066         return;
0067     }
0068 
0069     d->nodeStorage.reparentEntity(d->entity, d->newParent).expect("Unexpected error on undo/redo");
0070     d->entity->setName(d->newName);
0071 }