1 /*************************************************************************** 2 onu.h - description 3 ------------------- 4 begin : Wed Jul 18 2001 5 copyright : (C) 2001 by Gael de Chalendar 6 email : Gael.de.Chalendar@free.fr 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This program is free software; you can redistribute it and/or modify * 12 * it under the terms of the GNU General Public License as published by * 13 * the Free Software Foundation; either either version 2 14 of the License, or (at your option) any later version.of the License, or * 15 * (at your option) any later version. * 16 * * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20 * 02110-1301, USA 21 ***************************************************************************/ 22 #define KDE_NO_COMPAT 23 24 #ifndef KSIRKSKINEDITORONU_H 25 #define KSIRKSKINEDITORONU_H 26 27 #include "country.h" 28 #include "continent.h" 29 #include "nationality.h" 30 #include "spritetype.h" 31 32 #include <QPixmap> 33 #include <QFont> 34 #include <QSvgRenderer> 35 #include <QObject> 36 #include <QMap> 37 38 #define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API 39 #include <libkdegamesprivate/kgamesvgdocument.h> 40 41 class QGraphicsItem; 42 43 namespace KsirkSkinEditor 44 { 45 46 class Country; 47 class Goal; 48 /** 49 * Class ONU (Organisation des Nations Unies = UNO : United Nations 50 * Organization) is simply the list of the countries. The data definining 51 * each country is loaded from an XML configuration file located in the 52 * current skin data directory 53 */ 54 class ONU: public QObject 55 { 56 Q_OBJECT 57 58 public: 59 60 /** 61 * Constructor 62 * @param configFileName The name of the XML file defining this world. Built 63 * from the current skin dir and a default file name. 64 */ 65 explicit ONU(const QString& configFileName, QObject *parent); 66 67 /** Default destructor */ 68 ~ONU() override; 69 70 //{@ 71 /** 72 * Accessors 73 */ skin()74 inline const QString& skin() const {return m_skin;} name()75 inline const QString& name() const {return m_name;} setName(const QString & n)76 inline void setName(const QString& n) { if (m_name != n) {m_name = n; m_dirty = true;} } description()77 inline const QString& description() const {return m_description;} setDescription(const QString & d)78 inline void setDescription(const QString& d) { if (m_description != d) {m_description = d; m_dirty=true;} } configFileName()79 inline const QString& configFileName() const {return m_configFileName;} map()80 inline const QPixmap& map() const {return m_map;} snapshot()81 inline const QPixmap& snapshot() const {return m_snapshot;} width()82 inline unsigned int width() const {return m_width;} setWidth(unsigned int w)83 inline void setWidth(unsigned int w) { if (m_width != w) {m_width = w; m_dirty = true;} } height()84 inline unsigned int height() const {return m_height;} setHeight(unsigned int h)85 inline void setHeight(unsigned int h) { if (m_height != h) {m_height = h; m_dirty = true;} } poolIds()86 inline const QStringList& poolIds() const {return m_poolIds;} dirty()87 inline bool dirty() const {return m_dirty;} setDirty()88 inline void setDirty() {m_dirty = true;} 89 //@} 90 91 /** 92 * This method returns a pointer to the country that contains the given 93 * point. If there is no country there, the functions returns 0. 94 * @param point The point where to search for a country in the map mask 95 * @return The country at the given point or 0 if there is no country there. 96 */ 97 Country* countryAt(const QPointF& point); 98 99 /** 100 * Calls its reset method for each country 101 */ 102 void reset(); 103 104 /** 105 * Return the countries list 106 */ countries()107 inline QList<Country*>& countries() {return m_countries;} 108 109 /** 110 * Returns the nationalities list 111 */ nationalities()112 inline QList<Nationality*>& nationalities() {return m_nationalities;} 113 114 //@{ 115 /** Read property of QList<Continent*> continents. */ continents()116 inline QList<Continent*>& continents() {return m_continents;} continents()117 inline const QList<Continent*>& continents() const {return m_continents;} 118 //@} 119 goals()120 inline QList<Goal*>& goals() {return m_goals;} 121 122 /** 123 * Retrieves the continent with the given id 124 * @param id The id of the continent to retrieve 125 * @return A pointer to the retrieved continent or 0 if there is no 126 * continent with the given id. 127 */ 128 const Continent* continentWithId(const unsigned int id) const; 129 130 /** 131 * Returns the country named "name" ; 0 in case there is no such country. 132 * @param name The name of the country to retrieve. 133 * @return The country named name or 0 if there is no such country. 134 */ 135 Country* countryNamed(const QString& name); 136 137 /** 138 * Gets the number of countries in the world 139 * @return The number of countries in the world 140 */ 141 unsigned int getNbCountries() const; 142 143 /** 144 * Saves a XML representation of the world for game saving purpose 145 * @param xmlStream The stream to write on 146 */ 147 void saveXml(std::ostream& xmlStream); 148 149 /** 150 * Returns the nation named "name" ; 0 in case there is no such nation 151 * @param name The name of the nation to retrieve. 152 * @return The nation named name or 0 if there is no such nation. 153 */ 154 Nationality* nationNamed(const QString& name); 155 156 /** 157 * Returns the continent named "name" ; 0 in case there is no such continent 158 * @param name The name of the continent to retrieve. 159 * @return The continent named name or 0 if there is no such continent. 160 */ 161 Continent* continentNamed(const QString& name); 162 163 Nationality* nationalityNamed(const QString& name); 164 165 QSvgRenderer* renderer(); 166 mask()167 inline const QImage& mask() const {return m_countriesMask;} 168 169 KGameSvgDocument* svgDom(); 170 itemsMap()171 inline QMap<QGraphicsItem*, QPair<Country*, SpriteType> >& itemsMap() {return m_itemsMap;} 172 173 QGraphicsItem* itemFor(const Country* country, SpriteType spriteType); 174 175 QPixmap pixmapForId(const QString& id, int width, int height); 176 flagIcon()177 inline const QPixmap& flagIcon() const {return m_flagIcon;} infantryIcon()178 inline const QPixmap& infantryIcon() const {return m_infantryIcon;} cavalryIcon()179 inline const QPixmap& cavalryIcon() const {return m_cavalryIcon;} cannonIcon()180 inline const QPixmap& cannonIcon() const {return m_cannonIcon;} 181 182 void saveConfig(const QString& configFileName = QString()); 183 184 QFont foregroundFont(); 185 QFont backgroundFont(); 186 foregroundColor()187 QColor foregroundColor() {return QColor(m_font.foregroundColor);} backgroundColor()188 QColor backgroundColor() {return QColor(m_font.backgroundColor);} 189 190 void setFont(const QFont& font); 191 void setFontFgColor(const QColor& color); 192 void setFontBgColor(const QColor& color); 193 194 void createCountry(const QString& newCountryName); 195 void deleteCountry(Country* country); 196 197 void createContinent(const QString& newCountryName); 198 void deleteContinent(Continent* country); 199 200 void updateIcon(SpriteType type); 201 202 void createGoal(); 203 void deleteGoal(int g); 204 205 void createNationality(const QString& newNationalityName); 206 void deleteNationality(Nationality* nationality); 207 208 private: 209 /** 210 * All data that have to be stored about the font to display countries names 211 * in this world's skin 212 */ 213 struct FontDesc 214 { 215 QString family; 216 int size; 217 QFont::Weight weight; 218 bool italic; 219 QString foregroundColor; 220 QString backgroundColor; 221 }; 222 223 /** 224 * Build the map from it's stored image and the countries names 225 */ 226 void buildMap(); 227 228 void loadPoolIds(const QString& fileName); 229 230 QString m_configDir; 231 232 /** 233 * The name of the .desktop file containing the world's definition 234 */ 235 QString m_configFileName; 236 237 /** 238 * The displayable name of the skin 239 */ 240 QString m_name; 241 242 /** 243 * The displayable long description of the skin 244 */ 245 QString m_description; 246 247 /** 248 * The map used by this skin, built at the proper size from its SVG source 249 * and decorated with countries names 250 */ 251 QPixmap m_map; 252 253 /** 254 * A snaphsot of a running game with this skin. Used at skin choice time. 255 */ 256 QPixmap m_snapshot; 257 258 //@{ 259 /** 260 * The width and height of the map file (will be used as canvas size). These 261 * measures do not take into account the zoom factor. 262 */ 263 unsigned int m_width; 264 unsigned int m_height; 265 //@} 266 267 /** 268 * The list of countries 269 */ 270 QList<Country*> m_countries; 271 272 /** 273 * The list of nationalities 274 */ 275 QList<Nationality*> m_nationalities; 276 277 /** 278 * The continents of the world 279 */ 280 QList<Continent*> m_continents; 281 282 /** 283 * This image stores the mask that defines the countries of the world. 284 * The blue RGB component value of each pixel gives the index of the 285 * country in the countries list. 286 */ 287 QImage m_countriesMask; 288 289 /** 290 * The path to the skin ; relative to the ksirk data dir ; loaded from the 291 * XML file 292 */ 293 QString m_skin; 294 295 /** 296 * The description of the font used to draw countries names onto the map. 297 */ 298 FontDesc m_font; 299 300 /** 301 * This SVG renderer stores the SVG file of the map, renders it at the 302 * desired zoom factor and the result is used to build the map image. 303 */ 304 QSvgRenderer m_renderer; 305 306 KGameSvgDocument m_svgDom; 307 308 QMap<QGraphicsItem*, QPair<Country*, SpriteType> > m_itemsMap; 309 310 QPixmap m_flagIcon; 311 QPixmap m_infantryIcon; 312 QPixmap m_cavalryIcon; 313 QPixmap m_cannonIcon; 314 315 QString m_poolString; 316 317 QList<Goal*> m_goals; 318 319 QStringList m_poolIds; 320 321 bool m_dirty; 322 }; 323 324 } 325 #endif // ONU_H 326 327