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

0001 // ct_lvtqtc_undo_load_children.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_load_entity.h>
0021 
0022 using namespace Codethink::lvtqtc;
0023 
0024 struct UndoLoadEntity::Private {
0025     GraphicsScene *scene;
0026     lvtshr::UniqueId uuid;
0027     GraphicsScene::UnloadDepth type;
0028     QtcUtil::UndoActionType undoActionType;
0029     Private(GraphicsScene *scene,
0030             lvtshr::UniqueId uuid,
0031             GraphicsScene::UnloadDepth type,
0032             QtcUtil::UndoActionType undoActionType):
0033         scene(scene), uuid(uuid), type(type), undoActionType(undoActionType)
0034     {
0035     }
0036 };
0037 
0038 UndoLoadEntity::UndoLoadEntity(GraphicsScene *scene,
0039                                lvtshr::UniqueId entity_id,
0040                                GraphicsScene::UnloadDepth type,
0041                                QtcUtil::UndoActionType undoType,
0042                                QUndoCommand *parent):
0043     QUndoCommand(
0044         QObject::tr(undoType == QtcUtil::UndoActionType::e_Add
0045                         ? (type == GraphicsScene::UnloadDepth::Entity ? "Load Entity" : "Load Entity Children")
0046                         : (type == GraphicsScene::UnloadDepth::Entity ? "Unload Entity" : "Load Entity Children")),
0047         parent),
0048     d(std::make_unique<UndoLoadEntity::Private>(scene, entity_id, type, undoType))
0049 {
0050 }
0051 
0052 UndoLoadEntity::~UndoLoadEntity() = default;
0053 
0054 void UndoLoadEntity::undo()
0055 {
0056     if (d->undoActionType == QtcUtil::UndoActionType::e_Add) {
0057         d->scene->unloadEntity(d->uuid, d->type);
0058     } else {
0059         d->scene->loadEntity(d->uuid, d->type);
0060     }
0061 }
0062 
0063 void UndoLoadEntity::redo()
0064 {
0065     if (d->undoActionType == QtcUtil::UndoActionType::e_Add) {
0066         d->scene->loadEntity(d->uuid, d->type);
0067     } else {
0068         d->scene->unloadEntity(d->uuid, d->type);
0069     }
0070 }