File indexing completed on 2024-12-15 04:01:01
0001 /* 0002 * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best> 0003 * 0004 * SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #pragma once 0008 0009 #include <QUndoCommand> 0010 0011 #include "model/property/object_list_property.hpp" 0012 0013 namespace glaxnimate::command { 0014 0015 template<class ItemT, class PropT = model::ObjectListProperty<ItemT>> 0016 class AddObject : public QUndoCommand 0017 { 0018 public: 0019 AddObject( 0020 PropT* object_parent, 0021 std::unique_ptr<ItemT> object, 0022 int position = -1, 0023 QUndoCommand* parent = nullptr, 0024 const QString& name = {} 0025 ) 0026 : QUndoCommand(name.isEmpty() ? i18n("Create %1", object->object_name()) : name, parent), 0027 object_parent(object_parent), 0028 object_(std::move(object)), 0029 position(position == -1 ? object_parent->size() : position) 0030 {} 0031 0032 void undo() override 0033 { 0034 object_ = object_parent->remove(position); 0035 } 0036 0037 void redo() override 0038 { 0039 object_parent->insert(std::move(object_), position); 0040 } 0041 0042 ItemT* object() const 0043 { 0044 if ( object_ ) 0045 return object_.get(); 0046 return (*object_parent)[position]; 0047 } 0048 0049 private: 0050 PropT* object_parent; 0051 std::unique_ptr<ItemT> object_; 0052 int position; 0053 }; 0054 0055 0056 template<class ItemT, class PropT = model::ObjectListProperty<ItemT>> 0057 class RemoveObject : public QUndoCommand 0058 { 0059 public: 0060 RemoveObject(ItemT* object, PropT* object_parent, QUndoCommand* parent = nullptr) 0061 : QUndoCommand(i18n("Remove %1", object->object_name()), parent), 0062 object_parent(object_parent), 0063 position(object_parent->index_of(object, -1)) 0064 {} 0065 0066 RemoveObject(int index, PropT* object_parent, QUndoCommand* parent = nullptr) 0067 : QUndoCommand(i18n("Remove %1", (*object_parent)[index]->object_name()), parent), 0068 object_parent(object_parent), 0069 position(index) 0070 {} 0071 0072 0073 void undo() override 0074 { 0075 object_parent->insert(std::move(object), position); 0076 } 0077 0078 void redo() override 0079 { 0080 object = object_parent->remove(position); 0081 } 0082 0083 private: 0084 PropT* object_parent; 0085 std::unique_ptr<ItemT> object; 0086 int position; 0087 }; 0088 0089 0090 template<class ItemT, class PropT = model::ObjectListProperty<ItemT>> 0091 class MoveObject : public QUndoCommand 0092 { 0093 public: 0094 MoveObject( 0095 ItemT* object, 0096 PropT* parent_before, 0097 PropT* parent_after, 0098 int position_after, 0099 QUndoCommand* parent = nullptr 0100 ) 0101 : QUndoCommand(i18n("Move Object"), parent), 0102 parent_before(parent_before), 0103 position_before(parent_before->index_of(object, -1)), 0104 parent_after(parent_after), 0105 position_after(position_after) 0106 {} 0107 0108 void undo() override 0109 { 0110 if ( parent_before == parent_after ) 0111 parent_before->move(position_before, position_after); 0112 else if ( auto object = parent_after->remove(position_after) ) 0113 parent_before->insert(std::move(object), position_before); 0114 } 0115 0116 void redo() override 0117 { 0118 if ( parent_before == parent_after ) 0119 parent_before->move(position_before, position_after); 0120 else if ( auto object = parent_before->remove(position_before) ) 0121 parent_after->insert(std::move(object), position_after); 0122 } 0123 0124 private: 0125 PropT* parent_before; 0126 int position_before; 0127 PropT* parent_after; 0128 int position_after; 0129 }; 0130 0131 0132 } // namespace glaxnimate::command