File indexing completed on 2024-05-12 05:40:24

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 "additemcommand.h"
0021 
0022 #include "mindmap/data/linkcontroller.h"
0023 #include "mindmap/model/minditemmodel.h"
0024 
0025 #include <QDebug>
0026 
0027 namespace mindmap
0028 {
0029 AddItemCommand::AddItemCommand(mindmap::MindItemModel* nodeModel, MindItem::Type type, const QString& idParent,
0030                                QPointF pos)
0031     : m_nodeModel(nodeModel), m_idParent(idParent), m_type(type), m_pos(pos)
0032 {
0033     setText(tr("Add item of type: %1 to mindmap").arg(type));
0034 }
0035 
0036 void AddItemCommand::undo()
0037 {
0038     m_nodeModel->removeItem(m_mindItem);
0039     m_nodeModel->removeItem(m_link);
0040 }
0041 
0042 void AddItemCommand::redo()
0043 {
0044     if(m_mindItem.isNull())
0045     {
0046         auto pair= m_nodeModel->addItem(m_idParent, m_type);
0047         if(!m_pos.isNull())
0048         {
0049             auto p= dynamic_cast<PositionedItem*>(pair.first);
0050             if(p)
0051                 p->setPosition(m_pos);
0052         }
0053         m_mindItem= pair.first;
0054         m_link= pair.second;
0055     }
0056     else
0057     {
0058         m_nodeModel->appendItem({m_mindItem.data(), m_link});
0059     }
0060 }
0061 
0062 AddLinkCommand::AddLinkCommand(MindItemModel* nodeModel, const QString& idStart, const QString& idEnd)
0063     : m_nodeModel(nodeModel), m_idStart(idStart), m_idEnd(idEnd)
0064 {
0065     setText(tr("Add link"));
0066 }
0067 
0068 void AddLinkCommand::undo()
0069 {
0070     m_nodeModel->removeItem(m_link);
0071 }
0072 
0073 void AddLinkCommand::redo()
0074 {
0075     if(m_link.isNull())
0076     {
0077         auto [item, link]= m_nodeModel->addItem({}, MindItem::LinkType);
0078         m_link= link;
0079         m_link->setStart(m_nodeModel->positionItem(m_idStart));
0080         m_link->setEnd(m_nodeModel->positionItem(m_idEnd));
0081     }
0082     else
0083     {
0084         m_nodeModel->appendItem({m_link});
0085     }
0086 }
0087 
0088 } // namespace mindmap