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

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2013-2015 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 "UndoSetNodePos.h"
0021 #include "Document.h"
0022 #include "Node.h"
0023 
0024 namespace tikz {
0025 namespace core {
0026 
0027 UndoSetNodePos::UndoSetNodePos(Document * doc)
0028     : UndoItem("Move Node", doc)
0029     , m_undoPos(doc)
0030     , m_redoPos(doc)
0031 {
0032 }
0033 
0034 UndoSetNodePos::UndoSetNodePos(Node * node,
0035                                const MetaPos & newPos,
0036                                Document * doc)
0037     : UndoItem("Move Node", doc)
0038     , m_nodeUid(node->uid())
0039     , m_undoPos(node->metaPos())
0040     , m_redoPos(newPos)
0041 {
0042 }
0043 
0044 UndoSetNodePos::~UndoSetNodePos()
0045 {
0046 }
0047 
0048 void UndoSetNodePos::undo()
0049 {
0050     auto node = m_nodeUid.entity<Node>();
0051     Q_ASSERT(node);
0052 
0053     node->setMetaPos(m_undoPos);
0054 }
0055 
0056 void UndoSetNodePos::redo()
0057 {
0058     auto node = m_nodeUid.entity<Node>();
0059     Q_ASSERT(node);
0060     node->setMetaPos(m_redoPos);
0061 }
0062 
0063 bool UndoSetNodePos::mergeWith(const UndoItem * command)
0064 {
0065     // only merge when command is of correct type
0066     auto other = static_cast<const UndoSetNodePos*>(command);
0067     if (m_nodeUid != other->m_nodeUid) {
0068         return false;
0069     }
0070 
0071     m_redoPos = other->m_redoPos;
0072     return true;
0073 }
0074 
0075 void UndoSetNodePos::loadData(const QJsonObject & json)
0076 {
0077     m_nodeUid = Uid(json["uid"].toString(), document());
0078     m_redoPos = MetaPos(json["pos"].toString(), document());
0079 }
0080 
0081 QJsonObject UndoSetNodePos::saveData() const
0082 {
0083     QJsonObject json;
0084     json["type"] = "node-set-pos";
0085     json["uid"] = m_nodeUid.toString();
0086     json["pos"] = m_redoPos.toString();
0087     return json;
0088 }
0089 
0090 }
0091 }
0092 
0093 // kate: indent-width 4; replace-tabs on;