File indexing completed on 2024-05-12 04:35:03

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2018 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "UndoDeleteEntity.h"
0021 #include "Entity.h"
0022 #include "Document.h"
0023 
0024 namespace tikz {
0025 namespace core {
0026 
0027 UndoDeleteEntity::UndoDeleteEntity(Document * doc)
0028     : UndoItem("Delete Entity", doc)
0029 {
0030 }
0031 
0032 UndoDeleteEntity::UndoDeleteEntity(const Uid & uid, Document * doc)
0033     : UndoItem("Delete Entity", doc)
0034     , m_uid(uid)
0035     , m_entityType(uid.entityType())
0036 {
0037     // get entity to save data
0038     auto entity = m_uid.entity();
0039     Q_ASSERT(entity);
0040 
0041     // save properties
0042     m_data = entity->save();
0043 }
0044 
0045 UndoDeleteEntity::~UndoDeleteEntity() = default;
0046 
0047 void UndoDeleteEntity::undo()
0048 {
0049     document()->createEntity(m_uid, m_entityType);
0050 
0051     auto entity = m_uid.entity();
0052     Q_ASSERT(entity);
0053 
0054     ConfigTransaction transaction(entity);
0055     entity->load(m_data);
0056 }
0057 
0058 void UndoDeleteEntity::redo()
0059 {
0060     document()->deleteEntity(m_uid);
0061 }
0062 
0063 void UndoDeleteEntity::loadData(const QJsonObject & json)
0064 {
0065     m_uid = Uid(json["uid"].toString(), document());
0066     m_entityType = toEntityType(json["type"].toString());
0067     m_data = json["data"].toObject();
0068 }
0069 
0070 QJsonObject UndoDeleteEntity::saveData() const
0071 {
0072     QJsonObject json;
0073     json["uid"] = m_uid.toString();
0074     json["type"] = toString(m_entityType);
0075     json["data"] = m_data;
0076     return json;
0077 }
0078 
0079 }
0080 }
0081 
0082 // kate: indent-width 4; replace-tabs on;