File indexing completed on 2024-05-19 05:41:04

0001 /***************************************************************************
0002  *  Copyright (C) 2019 by Renaud Guezennec                                 *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program 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 General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #include "mindmap/command/dragnodecommand.h"
0022 #include "mindmap/data/mindmaptypes.h"
0023 #include "mindmap/data/mindnode.h"
0024 namespace mindmap
0025 {
0026 
0027 DragNodeCommand::DragNodeCommand(const QPointF& motion, const std::vector<QPointer<PositionedItem>>& selection)
0028     : m_motion(motion), m_mindNodes(selection)
0029 {
0030 }
0031 
0032 void DragNodeCommand::undo()
0033 {
0034     std::for_each(m_mindNodes.begin(), m_mindNodes.end(), [this](const QPointer<PositionedItem>& node) {
0035         if(node.isNull())
0036             return;
0037         node->translate(m_motion * -1);
0038     });
0039 }
0040 
0041 void DragNodeCommand::redo()
0042 {
0043     std::for_each(m_mindNodes.begin(), m_mindNodes.end(), [this](const QPointer<PositionedItem>& node) {
0044         if(node.isNull())
0045             return;
0046         if(node->isDragged())
0047             return;
0048         node->translate(m_motion);
0049     });
0050 }
0051 
0052 int DragNodeCommand::id() const
0053 {
0054     return static_cast<int>(mindmap::CommandType::Drag);
0055 }
0056 
0057 bool DragNodeCommand::mergeWith(const QUndoCommand* other)
0058 {
0059     if(other->id() != id())
0060         return false;
0061     auto command= dynamic_cast<const DragNodeCommand*>(other);
0062 
0063     if(nullptr == command)
0064         return false;
0065 
0066     if(command->getSelection() != getSelection())
0067         return false;
0068 
0069     m_motion+= command->getMotion();
0070     return true;
0071 }
0072 
0073 const QPointF& DragNodeCommand::getMotion() const
0074 {
0075     return m_motion;
0076 }
0077 const std::vector<QPointer<PositionedItem>> DragNodeCommand::getSelection() const
0078 {
0079     return m_mindNodes;
0080 }
0081 } // namespace mindmap