File indexing completed on 2024-05-12 15:26:38
0001 /*************************************************************************** 0002 File : aspectcommands.h 0003 Project : LabPlot 0004 -------------------------------------------------------------------- 0005 Copyright : (C) 2007-2010 by Knut Franke (knut.franke@gmx.de) 0006 Copyright : (C) 2007-2009 Tilman Benkert(thzs@gmx.net) 0007 Copyright : (C) 2013-2017 by Alexander Semke (alexander.semke@web.de) 0008 Description : Undo commands used by AbstractAspect. 0009 Only meant to be used within AbstractAspect.cpp 0010 0011 ***************************************************************************/ 0012 0013 /*************************************************************************** 0014 * * 0015 * This program is free software; you can redistribute it and/or modify * 0016 * it under the terms of the GNU General Public License as published by * 0017 * the Free Software Foundation; either version 2 of the License, or * 0018 * (at your option) any later version. * 0019 * * 0020 * This program is distributed in the hope that it will be useful, * 0021 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0023 * GNU General Public License for more details. * 0024 * * 0025 * You should have received a copy of the GNU General Public License * 0026 * along with this program; if not, write to the Free Software * 0027 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0028 * Boston, MA 02110-1301 USA * 0029 * * 0030 ***************************************************************************/ 0031 #ifndef ASPECTCOMMANDS_H 0032 #define ASPECTCOMMANDS_H 0033 0034 #include "AspectPrivate.h" 0035 #include <QUndoCommand> 0036 #include <KLocalizedString> 0037 0038 class AspectChildRemoveCmd : public QUndoCommand { 0039 public: 0040 AspectChildRemoveCmd(AbstractAspectPrivate* target, AbstractAspect* child) 0041 : m_target(target), m_child(child), m_index(-1) { 0042 setText(i18n("%1: remove %2", m_target->m_name, m_child->name())); 0043 } 0044 0045 ~AspectChildRemoveCmd() override { 0046 //TODO: this makes labplot crashing on project close/save. 0047 // if (m_removed) 0048 // delete m_child; 0049 } 0050 0051 // calling redo transfers ownership of m_child to the undo command 0052 void redo() override { 0053 AbstractAspect* nextSibling; 0054 if (m_child == m_target->m_children.last()) 0055 nextSibling = nullptr; 0056 else 0057 nextSibling = m_target->m_children.at(m_target->indexOfChild(m_child) + 1); 0058 0059 emit m_target->q->aspectAboutToBeRemoved(m_child); 0060 0061 //emit the "about to be removed" signal also for all children of the aspect being currently removed 0062 for (auto* child : m_child->children<AbstractAspect>(AbstractAspect::ChildIndexFlag::Recursive | AbstractAspect::ChildIndexFlag::IncludeHidden)) 0063 m_child->aspectAboutToBeRemoved(child); 0064 0065 m_index = m_target->removeChild(m_child); 0066 emit m_target->q->aspectRemoved(m_target->q, nextSibling, m_child); 0067 // m_removed = true; 0068 } 0069 0070 // calling undo transfers ownership of m_child back to its parent aspect 0071 void undo() override { 0072 Q_ASSERT(m_index != -1); // m_child must be a child of m_target->q 0073 0074 emit m_target->q->aspectAboutToBeAdded(m_target->q, nullptr, m_child); 0075 m_target->insertChild(m_index, m_child); 0076 emit m_target->q->aspectAdded(m_child); 0077 // m_removed = false; 0078 } 0079 0080 protected: 0081 AbstractAspectPrivate* m_target; 0082 AbstractAspect* m_child; 0083 int m_index; 0084 // bool m_removed{false}; 0085 }; 0086 0087 class AspectChildAddCmd : public AspectChildRemoveCmd { 0088 public: 0089 AspectChildAddCmd(AbstractAspectPrivate* target, AbstractAspect* child, int index) 0090 : AspectChildRemoveCmd(target, child) { 0091 setText(i18n("%1: add %2", m_target->m_name, m_child->name())); 0092 m_index = index; 0093 // m_removed = true; 0094 } 0095 0096 void redo() override { 0097 AspectChildRemoveCmd::undo(); 0098 } 0099 0100 void undo() override { 0101 AspectChildRemoveCmd::redo(); 0102 } 0103 }; 0104 0105 class AspectChildReparentCmd : public QUndoCommand { 0106 public: 0107 AspectChildReparentCmd(AbstractAspectPrivate* target, AbstractAspectPrivate* new_parent, 0108 AbstractAspect* child, int new_index) 0109 : m_target(target), m_new_parent(new_parent), m_child(child), m_new_index(new_index) { 0110 setText(i18n("%1: move %2 to %3.", m_target->m_name, m_child->name(), m_new_parent->m_name)); 0111 } 0112 0113 // calling redo transfers ownership of m_child to the new parent aspect 0114 void redo() override { 0115 emit m_child->aspectAboutToBeRemoved(m_child); 0116 m_index = m_target->removeChild(m_child); 0117 m_new_parent->insertChild(m_new_index, m_child); 0118 emit m_child->aspectAdded(m_child); 0119 } 0120 0121 // calling undo transfers ownership of m_child back to its previous parent aspect 0122 void undo() override { 0123 Q_ASSERT(m_index != -1); 0124 emit m_child->aspectAboutToBeRemoved(m_child); 0125 m_new_parent->removeChild(m_child); 0126 m_target->insertChild(m_index, m_child); 0127 emit m_child->aspectAdded(m_child); 0128 } 0129 0130 protected: 0131 AbstractAspectPrivate * m_target; 0132 AbstractAspectPrivate * m_new_parent; 0133 AbstractAspect* m_child; 0134 int m_index{-1}; 0135 int m_new_index; 0136 }; 0137 0138 #endif