File indexing completed on 2024-12-08 06:46:37

0001 /***************************************************************************
0002                                cmapcmdelementdelete.cpp
0003                              -------------------
0004     begin                : Wed Feb 27 2002
0005     copyright            : (C) 2002 by Kmud Developer Team
0006     email                : kmud-devel@kmud.de
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "cmapcmdelementdelete.h"
0019 
0020 
0021 #include "cmapelement.h"
0022 #include "cmapmanager.h"
0023 #include "cmaplevel.h"
0024 #include "cmaproom.h"
0025 #include "cmappluginbase.h"
0026 #include "cmappath.h"
0027 #include "cmapcmdelementproperties.h"
0028 
0029 #include <KLocalizedString>
0030 
0031 CMapCmdElementDelete::CMapCmdElementDelete(CMapManager *mapManager,QString name,bool delOpsite) : CMapCommand(name),CMapElementUtil(mapManager)
0032 {
0033     manager = mapManager;
0034     properties = new KMemConfig;
0035     groups = 0;
0036     m_delOpsite = delOpsite;
0037 }
0038 
0039 CMapCmdElementDelete::~CMapCmdElementDelete()
0040 {
0041     delete properties;
0042 }
0043 
0044 void CMapCmdElementDelete::redo()
0045 {
0046   QStringList groupList = properties->groupList();
0047 
0048   for (QStringList::Iterator it = groupList.begin(); it != groupList.end(); ++it)
0049   {
0050     if (*it == "<default>") continue;
0051     CMapElement *element = manager->findElement(properties->group(*it));
0052     if (!element) properties->deleteGroup(*it);
0053   }
0054 
0055   for (QStringList::Iterator it = groupList.begin(); it != groupList.end(); ++it)
0056   {
0057     if (*it == "<default>") continue;
0058 
0059     // ignore elements that already got deleted - this is because deletion of multiple rooms can include the same exit twice
0060     // also mark them as ignored, so that undo doesn't create these twice
0061     KConfigGroup group = properties->group(*it);
0062     CMapElement *element = manager->findElement(group);
0063     if (!element) {
0064       group.writeEntry("Deleted", "1");
0065       continue;
0066     }
0067 
0068     for (CMapPluginBase *plugin : manager->getPluginList())
0069       plugin->beforeElementDeleted(element);
0070 
0071     deleteElement(properties->group(*it),m_delOpsite);
0072   }
0073 }
0074 
0075 void CMapCmdElementDelete::undo()
0076 {
0077   QStringList groupList = properties->groupList();
0078 
0079   for (QStringList::Iterator it = groupList.begin(); it != groupList.end(); ++it)
0080   {
0081     if (*it == "<default>") continue;
0082 
0083     KConfigGroup group = properties->group (*it);
0084     if (group.hasKey("Deleted")) continue;
0085 
0086     CMapElement *elm = createElement(group);
0087     elm->loadProperties(group);
0088 
0089     for (CMapPluginBase *plugin : manager->getPluginList())
0090       plugin->afterElementUndeleted(elm);
0091   }
0092 }
0093 
0094 void CMapCmdElementDelete::addElement(KMemConfig *newElementProperties)
0095 {
0096     QString grpName = QString::number(groups++);
0097     KConfigGroup grp = properties->group(grpName);
0098 
0099     newElementProperties->group("Properties").copyTo(&grp);
0100 }