File indexing completed on 2024-12-01 06:51:43

0001 /***************************************************************************
0002                                cmapelementutil.cpp
0003                              -------------------
0004     begin                : Thu Jan 10 2002
0005     copyright            : (C) 2002 by Kmud Developer Team
0006                            (C) 2014-2015 by Tomas Mecir <mecirt@gmail.com>
0007     email                : kmud-devel@kmud.de
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *   This program is free software; you can redistribute it and/or modify  *
0013  *   it under the terms of the GNU General Public License as published by  *
0014  *   the Free Software Foundation; either version 2 of the License, or     *
0015  *   (at your option) any later version.                                   *
0016  *                                                                         *
0017  ***************************************************************************/
0018 
0019 #include "cmapelementutil.h"
0020 
0021 #include "cmapmanager.h"
0022 #include "cmaplevel.h"
0023 #include "cmaproom.h"
0024 #include "cmaptext.h"
0025 #include "cmapzone.h"
0026 #include "cmappath.h"
0027 #include "cmapelement.h"
0028 #include "cmapview.h"
0029 
0030 #include "dialogs/dlgmappathproperties.h"
0031 
0032 CMapElementUtil::CMapElementUtil(CMapManager *mapManager)
0033 {
0034     manager = mapManager;
0035 }
0036 
0037 CMapElementUtil::~CMapElementUtil()
0038 {
0039 }
0040 
0041 void CMapElementUtil::deleteElement(KConfigGroup grp,bool delOpsite)
0042 {
0043     CMapElement *element = manager->findElement(grp);
0044 
0045     if (element)
0046     {
0047         switch (element->getElementType())
0048         {
0049             case PATH : deletePath((CMapPath *)element,delOpsite); break;
0050             case ZONE : delete ((CMapZone *)element); break;
0051             case TEXT : delete ((CMapText *)element); break;
0052             case ROOM : delete ((CMapRoom *)element); break;
0053             default: break;
0054         }
0055     }
0056 }
0057 
0058 CMapElement *CMapElementUtil::createElement(KConfigGroup grp)
0059 {
0060     CMapElement *result = nullptr;
0061     QString text;
0062 
0063     elementTyp type  = (elementTyp)grp.readEntry("Type",(int)OTHER);
0064     
0065     if (type!=OTHER)
0066     {
0067         if  (type==PATH)
0068         {
0069             CMapLevel *srcLevel = manager->findLevel(grp.readEntry("SrcLevel",-1));
0070             CMapLevel *destLevel = manager->findLevel(grp.readEntry("DestLevel",-1));
0071             CMapRoom *srcRoom = srcLevel->findRoom(grp.readEntry("SrcRoom",-1));
0072             CMapRoom *destRoom = destLevel->findRoom(grp.readEntry("DestRoom",-1));
0073 
0074             directionTyp srcDir  = (directionTyp)grp.readEntry("SrcDir",0);
0075             directionTyp destDir = (directionTyp)grp.readEntry("DestDir",0);
0076             bool twoWay = grp.readEntry("PathTwoWay", manager->getMapData()->defaultPathTwoWay);
0077 
0078             result = new CMapPath(manager,srcRoom,srcDir,destRoom,destDir,twoWay);
0079 
0080             ((CMapPath *)result)->loadProperties(grp);
0081         }
0082         else
0083         {   
0084             CMapLevel *level = nullptr;
0085             if (grp.hasKey("Level"))
0086             {
0087                 level = manager->findLevel(grp.readEntry("Level",-1));
0088             }
0089 
0090             QPoint lowPos(-5,-5);
0091             lowPos.setX(grp.readEntry("X",-5));
0092             lowPos.setY(grp.readEntry("Y",-5));
0093 
0094             switch (type)
0095             {
0096                 case ROOM : result = createRoom(manager,lowPos,level);
0097                             if (result)
0098                             {
0099                                 if (!grp.hasKey("RoomID"))
0100                                 {
0101                                     grp.writeEntry("RoomID",((CMapRoom *)result)->getRoomID());
0102                                 }
0103                                 else
0104                                 {
0105                                     ((CMapRoom *)result)->setRoomID(grp.readEntry("RoomID",-1));
0106                                 }
0107                             }
0108                             break;
0109                 case TEXT : text = grp.readEntry("Text","");
0110                             if (grp.hasKey("Font") && grp.hasKey("Color"))
0111                             {
0112                                 QColor color=Qt::black;
0113                                 color=grp.readEntry("Color",color);
0114                                 QFont font = manager->getMapData()->defaultTextFont;
0115                                 font = grp.readEntry("Font",font);
0116                                                 result = createText(manager, lowPos, level, text, font, color);
0117                             }
0118                             else
0119                             {
0120                                                 result = createText(manager, lowPos, level, text);
0121                             }
0122                         if (!grp.hasKey("TextID"))
0123                             grp.writeEntry("TextID",((CMapText *)result)->getTextID());                                 
0124                         else
0125                             ((CMapText *)result)->setTextID(grp.readEntry("TextID",-1));
0126                             break;
0127                 default : ;
0128             }
0129 
0130             // FIXME_jp: This should problay load the elements propties into the new element.
0131             //           Calls to loadProperties else were in the code will not then be need after
0132             //           creating the new element
0133         }
0134     }
0135 
0136     return result;
0137 }
0138 
0139 /** Used to create a new room */
0140 CMapRoom *CMapElementUtil::createRoom(CMapManager *manager, QPoint pos, CMapLevel *level)
0141 {
0142   if ((!level) || level->findElementAt(pos))
0143     return nullptr;
0144 
0145   QRect rect(pos,manager->getMapData()->gridSize);
0146 
0147   CMapRoom *room = new CMapRoom(manager,rect,level);
0148 
0149   return room;
0150 }
0151 
0152 CMapText *CMapElementUtil::createText(CMapManager *manager, QPoint pos, CMapLevel *level, QString str)
0153 {
0154   return new CMapText(str, manager, pos, level);
0155 }
0156 
0157 CMapText *CMapElementUtil::createText(CMapManager *manager, QPoint pos, CMapLevel *level, QString str, QFont f, QColor col)
0158 {
0159   return new CMapText(str, f, col, manager, pos, level);
0160 }
0161 
0162 /** Delete a path map element */
0163 void CMapElementUtil::deletePath(CMapPath *path,bool delOpsite)
0164 {
0165   if (!delOpsite) path->setOpsitePath(nullptr);
0166   delete path;
0167 }