File indexing completed on 2024-12-01 06:51:43
0001 /*************************************************************************** 0002 cmapelement.h 0003 ------------------- 0004 begin : Sat Mar 10 2001 0005 copyright : (C) 2001 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 #ifndef CMAPELEMENT_H 0019 #define CMAPELEMENT_H 0020 0021 #include <qpainter.h> 0022 #include <qpoint.h> 0023 #include <qrect.h> 0024 #include <qregion.h> 0025 #include <qobject.h> 0026 #include <qdom.h> 0027 #include <QList> 0028 0029 #include "kmemconfig.h" 0030 #include <kconfiggroup.h> 0031 0032 #include <kmuddy_export.h> 0033 0034 enum directionTyp { NORTH=0 , SOUTH=1 , WEST=2 , EAST=3 , NORTHWEST=4, NORTHEAST=5, SOUTHEAST=6, SOUTHWEST=7 , UP=8, DOWN=9, SPECIAL=30 }; 0035 //typedef enum direction_type directionTyp; 0036 0037 enum elementTyp { ROOM=0, PATH, TEXT, ZONE, OTHER }; 0038 //typedef enum element_type elementTyp; 0039 0040 /**The base class for all map elements 0041 *@author Kmud Developer Team 0042 */ 0043 0044 class CMapZone; 0045 class CMapManager; 0046 class CMapLevel; 0047 0048 class KMUDDY_EXPORT CMapElement : public QObject 0049 { 0050 Q_OBJECT 0051 public: 0052 CMapElement(CMapManager *manager,CMapLevel *level); 0053 CMapElement(CMapManager *manager,QRect rect,CMapLevel *level); 0054 ~CMapElement() override; 0055 0056 // Genomerty methods 0057 /** Used to set the position of the top left of the element */ 0058 void setLowPos(QPoint pos); 0059 QPoint getLowPos(void) { return position.topLeft(); } 0060 void setHighPos(QPoint pos) { position.setBottom(pos.y()); position.setRight(pos.x()); geometryChanged(); } 0061 QPoint getHighPos(void) { return position.bottomRight(); } 0062 void setSize(QSize size) { position.setSize(size); geometryChanged(); } 0063 void setWidth(int width) { position.setWidth(width); geometryChanged(); } 0064 void setHeight(int height) { position.setHeight(height); geometryChanged(); } 0065 QSize getSize(void) { return position.size(); } 0066 void setRect(QRect rect) { position = rect; geometryChanged(); } 0067 QRect getRect(void) { return position; } 0068 int getHighX() { return position.x()+position.width() -1; } 0069 int getHighY() { return position.y()+position.height() -1; } 0070 int getX() { return position.x(); } 0071 int getY() { return position.y(); } 0072 int getWidth() { return position.width(); } 0073 int getHeight() { return position.height(); } 0074 0075 /** Used to load the properties of the element from a list of properties */ 0076 virtual void loadProperties(KConfigGroup grp); 0077 /** Used to save the properties of the element to a list of properties */ 0078 virtual void saveProperties(KConfigGroup grp); 0079 0080 /** Used to save the element as an XML object 0081 * @param properties The XML object to save the properties too 0082 * @param doc The XML Document */ 0083 virtual void saveQDomElement(QDomDocument *doc,QDomElement *properties); 0084 /** Used to load the properties from a XML object 0085 * @param properties The XML object to load the properties from */ 0086 virtual void loadQDomElement(QDomElement *properties); 0087 0088 virtual elementTyp getElementType(void) { return OTHER ; } 0089 0090 // Painting methods 0091 /** Used to paint the element to the map */ 0092 virtual void paint(QPainter *p,CMapZone *currentZone); 0093 /** Used to paint the element while it is being dragged */ 0094 virtual void dragPaint(QPoint offset,QPainter *p,CMapZone *currentZone) =0; 0095 /** Used to paint the lower level repesnation of the element */ 0096 virtual void lowerPaint(QPainter *p,CMapZone *currentZone)=0; 0097 /** Used to paint the higher level repesnation of the element */ 0098 virtual void higherPaint(QPainter *p,CMapZone *currentZone)=0; 0099 /** This is used to paint a element while it is being resized 0100 * @param offset The offset of the mouse pointer since the start of the resize operation 0101 * @param p The painter to paint the element too 0102 * @param currentZone The current zone being viewed 0103 * @param resizeId The id of the resize handle being moved */ 0104 virtual void resizePaint(QPoint offset,QPainter *p,CMapZone *currentZone,int resizeId); 0105 0106 // Mouse functions 0107 /** Used to find out if the mouse is in the element */ 0108 virtual bool mouseInElement(QPoint mousePos); 0109 /** Used to find out if the mouse is in the resize box 0110 * @param mousePos The position of the mouse pointer 0111 * @param currentZone A pointer to the current zone 0112 * @return the ID of the resize box, or 0 if not in any */ 0113 virtual int mouseInResize(QPoint mousePos,CMapZone *currentZone); 0114 /** Used to find out if the element is in a rectangle */ 0115 virtual bool elementIn(QRect rect,CMapZone *currentZone); 0116 0117 /** This method is used to put a element in a selected state 0118 * @param sel The selected state to set the element too */ 0119 virtual void setSelected(bool sel); 0120 /** This method is used to get the selected state of the element 0121 * @return The selected state of the element */ 0122 virtual bool getSelected(void); 0123 0124 /** This method is used to set the elements edit mode state */ 0125 virtual void setEditMode(bool edit); 0126 /** This method is used to find out if the element is in edit mode */ 0127 virtual bool getEditMode(void); 0128 0129 /** This method is called to return a copy of the element */ 0130 virtual CMapElement *copy(void) =0; 0131 /** Used to move the element relative to it's current position */ 0132 virtual void moveBy(QPoint offset); 0133 0134 /** This method is used to set the level that the element is in */ 0135 virtual void setLevel(CMapLevel *level); 0136 /** This method is used to get the level that the element is in */ 0137 CMapLevel *getLevel(void); 0138 0139 CMapZone *getZone(void); 0140 /** This is used to resize the element */ 0141 virtual void resize(QPoint offset,int resizeId); 0142 /** This is called to set if the element should be painted */ 0143 void setDoPaint(bool paint) { doPaint = paint; } 0144 /** This is called to see if the element is to be painted */ 0145 bool getDoPaint(void) { return doPaint; } 0146 0147 signals: 0148 void deleteElement(CMapElement *,bool); 0149 0150 protected: 0151 /** Used to paint the element at a given location and size 0152 * @param p The painer to paint the element to 0153 * @param pos The position to paint the elmenetposition 0154 * @param size The size the element should be draw 0155 * @param zone The current zone being viewed */ 0156 virtual void paintElementResize(QPainter *p,QPoint pos,QSize size,CMapZone *zone)=0; 0157 /** This method is used to calculate the positions of the resize handles */ 0158 virtual void generateResizePositions(); 0159 /** Used to get the map manager */ 0160 CMapManager *getManager(void) { return mapManager; } 0161 void geometryChanged(void) { } 0162 /** This method is called when the element is put into edit mode */ 0163 virtual void editModeSetEvent(void) { } 0164 /** This method is called when the element looses it's edit mode */ 0165 virtual void editModeUnsetEvent(void) { } 0166 /** This method is used to paint the resize handles 0167 * @param p The painter used to do the painting 0168 * @param resizePos The positions of the handles to be painted */ 0169 void paintResizeHandles(QPainter *p,QList<QRect> &resizePos); 0170 /** 0171 * This is used to read a color value from a XML object 0172 * @param e The XML object 0173 * @param key The key of the color to read 0174 * @param defaultCol If the color is not found then this will be used 0175 * @return The Color value 0176 */ 0177 static QColor readColor(QDomElement *e,QString key,QColor defaultCol); 0178 0179 /** 0180 * This is used to write a color value to a XML object 0181 * @param doc The XML document 0182 * @param e The XML object 0183 * @param key The key of the color to write 0184 * @param col The color value to write 0185 */ 0186 static void writeColor(QDomDocument *doc,QDomElement *e,QString key,QColor col); 0187 0188 /** 0189 * This method is used to read a int value from a XML object 0190 * @param e The XML object 0191 * @param key The key of the int to read 0192 * @param defaultCol If the int is not found then this will be used 0193 * @return The int value 0194 */ 0195 static int readInt(QDomElement *e,QString key,int i); 0196 0197 /** 0198 * This metod is used to write a int value to a XML object 0199 * @param doc The XML document 0200 * @param e The XML object 0201 * @param key The key of the int to write 0202 * @param i The int value to write 0203 */ 0204 static void writeInt(QDomDocument *doc,QDomElement *e,QString key,int i); 0205 0206 /** 0207 * This method is used to read a boolean value from a XML object 0208 * @param e The XML object 0209 * @param key The key of the boolean to read 0210 * @param defaultCol If the boolean is not found then this will be used 0211 * @return The boolean value 0212 */ 0213 static bool readBool(QDomElement *e,QString key,bool b); 0214 0215 /** 0216 * This metod is used to write a boolean value to a XML object 0217 * @param e The XML object 0218 * @param key The key of the boolean to write 0219 * @param i The boolean value to write 0220 */ 0221 static void writeBool(QDomElement *e,QString key,bool b); 0222 0223 protected: 0224 /** This is used to store the cords of the resize handles */ 0225 QList<QRect> resizePos; 0226 0227 private: 0228 /** This method is used to calc the resize handle positions for a rectangle 0229 * object and add them to a list 0230 * @param rect The coridantes of the object 0231 * @param resizePos The list to add the positions too */ 0232 void generateResizePositions(QRect rect, QList<QRect> &resizePos); 0233 0234 void calcResizeCords(QSize *size,QPoint *pos,signed int *offsetx,signed int *offsety,QPoint *offset,int resizeId); 0235 0236 private: 0237 bool doPaint; 0238 // Position attrubites 0239 CMapLevel *mapLevel; 0240 QRect position; 0241 0242 // States 0243 bool selected,editing; 0244 CMapManager *mapManager; 0245 0246 }; 0247 0248 #endif