File indexing completed on 2025-07-06 05:06: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 #include "mindmap/command/reparentingnodecommand.h"
0021 
0022 #include <QDebug>
0023 
0024 #include "mindmap/data/linkcontroller.h"
0025 #include "mindmap/data/mindnode.h"
0026 #include "mindmap/model/minditemmodel.h"
0027 
0028 namespace mindmap
0029 {
0030 ReparentingNodeCommand::ReparentingNodeCommand(MindItemModel* nodeModel, PositionedItem* newParent, const QString& id)
0031     : m_nodeModel(nodeModel), m_newParent(newParent)
0032 {
0033     m_mindNode= dynamic_cast<PositionedItem*>(m_nodeModel->item(id));
0034     if(m_mindNode)
0035         m_oldParent= m_mindNode->parentNode();
0036 
0037     if(m_oldParent)
0038     {
0039         auto links= m_oldParent->subLinks();
0040         auto idxLink= std::find_if(links.begin(), links.end(),
0041                                    [this](LinkController* link)
0042                                    {
0043                                        // qDebug() << "find if reparentingNode";
0044                                        return link->end() == m_mindNode.data();
0045                                    });
0046         if(idxLink != links.end())
0047             m_oldLink= (*idxLink);
0048     }
0049 }
0050 
0051 void ReparentingNodeCommand::undo()
0052 {
0053     qDebug() << "Reparenting undo:";
0054     if(m_mindNode.isNull() || m_oldLink.isNull() || m_newLink.isNull())
0055         return;
0056 
0057     m_newParent->removeLink(m_newLink);
0058     m_oldParent->addLink(m_oldLink);
0059     m_nodeModel->removeItem(m_newLink);
0060     m_nodeModel->appendItem({m_oldLink});
0061 }
0062 
0063 void ReparentingNodeCommand::redo()
0064 {
0065     qDebug() << "Reparenting redo:";
0066     if(m_mindNode.isNull() || m_oldLink.isNull())
0067         return;
0068 
0069     m_nodeModel->removeItem(m_oldLink);
0070     if(m_newLink.isNull())
0071     {
0072         m_newLink= new LinkController();
0073         m_newLink->setStart(m_newParent);
0074         m_newLink->setEnd(m_mindNode);
0075     }
0076 
0077     m_oldParent->removeLink(m_oldLink);
0078 
0079     m_nodeModel->appendItem({m_newLink});
0080 }
0081 } // namespace mindmap