00001 // *************************************************************************** 00002 // PropertyList.h - description 00003 // ------------------- 00004 // begin : Thu Oct 5 2000 00005 // Copyright (C) 2000 Stephan Beal & Rusty Ballinger 00006 // stephan@wanderinghorse.net & rusty@sgi.com 00007 // *************************************************************************** 00008 // * * 00009 // * This program is free software; you can redistribute it and/or modify * 00010 // * it under the terms of the GNU General Public License as published by * 00011 // * the Free Software Foundation; either version 2 of the License, or * 00012 // * (at your option) any later version. * 00013 // * * 00014 // *************************************************************************** 00015 00016 00017 #ifndef PROPERTYLIST_H 00018 #define PROPERTYLIST_H 00019 #include <qobject.h> 00020 00021 #include <qobject.h> 00022 #include <qdict.h> 00023 #include <qcolor.h> 00024 #include <qtextstream.h> 00025 #include <qvaluelist.h> 00026 00027 #include <fun/fun.h> 00028 #include <fun/Serializer.h> 00029 #include <fun/Deserializer.h> 00030 00031 namespace fun { 00032 /** 00033 * This class provides a generic hashtable-like interface for perstantly storing 00034 * arbitrary key/value pairs. 00035 */ 00036 00037 class PropertyList : public QObject, public Serializable 00038 { 00039 Q_OBJECT 00040 public: 00041 PropertyList(); 00042 ~PropertyList(); 00043 /** 00044 * Returns the value corresponding to the given key. If the value has 00045 * not been set, the given default value is returned. 00046 */ 00047 QString getString(const QString &key, QString defaultVal = QString()) const; 00048 /** 00049 * This is the same as calling getString(). 00050 */ 00051 QString get(const QString &key, QString defaultVal = QString()) const; 00052 00053 /** 00054 * Returns the value corresponding to the given key. If the value has 00055 * not been set or can't be converted to an int, the given default value 00056 * is returned. If you need to know whether a value was set but couldn't 00057 * be converted, use isSet() and isInt(). 00058 */ 00059 int getInt(const QString &key, int defaultVal = 0) const; 00060 00061 /** 00062 * Returns the value corresponding to the given key. If the value has 00063 * not been set or can't be converted to a bool, the given default value 00064 * is returned. (See isBool() for the rules.) If you need to know 00065 * whether a value was set but couldn't be converted, use isSet() and 00066 * isBool(). 00067 */ 00068 bool getBool(const QString &key, bool defaultVal = false) const; 00069 00070 /** 00071 * Returns the value corresponding to the given key. If the value has 00072 * not been set or can't be converted to a double, the given default 00073 * value is returned. (See isDouble() for the rules.) If you need to 00074 * know whether a value was set but couldn't be converted, use isSet() 00075 * and isDouble(). 00076 */ 00077 double getDouble(const QString &key, double defaultVal = 0.0) const; 00078 00079 /** 00080 * Returns the value corresponding to the given key. If the value has 00081 * not been set or can't be converted to a color, the given default 00082 * value is returned. (See isColor() for the rules.) If you need to 00083 * know whether a value was set but couldn't be converted, use isSet() 00084 * and isColor(). 00085 */ 00086 QColor getColor(const QString &key, QColor defaultVal = QColor()) const; 00087 00088 00089 /** 00090 * Returns the QPoint stored in the given key. 00091 */ 00092 virtual QPoint getPoint( const QString &key, const QPoint &dflt ); 00093 00094 /** 00095 * A convenience wrapper around getPoint(). 00096 */ 00097 virtual QSize getSize( const QString &key, const QSize &dflt ); 00098 00099 /** 00100 * Returns true if a value has been supplied for the given key, false 00101 * otherwise. Note that a key can be supplied with a null value. 00102 */ 00103 bool isSet(const QString &key) const; 00104 00105 /** 00106 * Returns true if the value has been set for the given key and can 00107 * be converted to an int. This uses QString::toInt() to determine 00108 * whether the value can be converted. 00109 */ 00110 bool isInt(const QString &key) const; 00111 00112 /** 00113 * Returns true if the value has been set for the given key and can 00114 * be converted to a bool. (True if it's "true", "false", "on", "off", 00115 * "yes", "no", "t", "f", "y", "n", or an integer.) 00116 */ 00117 bool isBool(const QString &key) const; 00118 00119 /** 00120 * Returns true if the value has been set for the given key and can 00121 * be converted to a double. This uses QString::toDouble() to determine 00122 * whether the value can be converted. 00123 */ 00124 bool isDouble(const QString &key) const; 00125 00126 /** 00127 * Returns true if the value has been set for the given key and can 00128 * be converted to a color. See QColor::setNamedColor() for the 00129 * rules. 00130 */ 00131 bool isColor(const QString &key) const; 00132 00133 /** 00134 * Gets an element's comment. 00135 */ 00136 QString getComment(const QString &key) const; 00137 00138 /** 00139 * Returns true if this ConfigFile has been successfully loaded, or 00140 * if at least one value has been set(). 00141 */ 00142 virtual bool isLoaded() const { return !data.isEmpty(); } 00143 00144 /** 00145 * Returns true if any of the configuration values have been changed 00146 * since the file was last loaded or saved. 00147 */ 00148 virtual bool needsSave() const { return changed; } 00149 00150 const QValueList<QString> &keys(); 00151 00152 public slots: 00153 /** 00154 * Forces the object to emit a signalChange( QString() ) with 00155 * an empty String. Receivers can decide if they want to react 00156 * to a global update or not. 00157 */ 00158 void slotEmitUpdate(); 00159 /** 00160 * Sets the given element to the given string value. 00161 */ 00162 void set(const QString &key, const QString &val, const QString &comment = QString() ); 00163 /** 00164 * Sets the given element to the given int value. 00165 */ 00166 void set(const QString &key, int val); 00167 /** 00168 * Sets the given element to the given bool value. 00169 */ 00170 void set(const QString &key, bool val); 00171 /** 00172 * Sets the given element to the given double value. 00173 */ 00174 void set(const QString &key, double val); 00175 /** 00176 * Sets the given element to the given color value. This will be stored 00177 * in the format #rrggbb. 00178 */ 00179 void set(const QString &key, const QColor &val); 00180 00181 00182 /** 00183 * Puts a QPoint in the list, in the format "x,y". 00184 */ 00185 virtual void set( const QString &key, const QPoint &val ); 00186 /** 00187 * A convenience wrapper around set( QString, QPoint ). 00188 */ 00189 virtual void set( const QString &key, const QSize &dflt ); 00190 00191 00192 /** 00193 * Removes the given element. 00194 */ 00195 void unset(const QString &key); 00196 00197 00198 00199 /** 00200 * Sets the comment which will be printed above an element. Don't 00201 * include comment characters; they will be added by saveToText() 00202 * or saveToXML(). 00203 */ 00204 void setComment(const QString &key, const QString &comment); 00205 00206 /** 00207 * Sets the "needs save" flag to the given value. (The only reason for 00208 * this is that saveToXML and saveToText are const methods, so they 00209 * can't change the flag; after saving it, setNeedsSave() yourself.) 00210 * Too bogus? 00211 */ 00212 void setNeedsSave(bool needsSave) { changed = needsSave; } 00213 00214 00215 /** 00216 * This take a different ConfigFile and merges in any new values 00217 * and keys. 00218 * This can be used to easily allow gamesets to modify the global 00219 * config settings. My idea is to have a qub.ini in any given dir, 00220 * and have DirView pull that into GConfigFile::global()->mergeIn(). 00221 * This function will return false when es Fehler gab. 00222 */ 00223 virtual bool mergeIn( const PropertyList *config ); 00224 00225 /** 00226 * Tells the object to restore it's state from the given Deserializer. 00227 */ 00228 virtual void deserialize(const Deserializer &ser); 00229 00230 /** 00231 * Tells the instance to save it's state to the given Serializer object. 00232 */ 00233 virtual void serialize(Serializer &ser) const; 00234 00235 00236 00237 signals: 00238 /** 00239 * Emitted when set() is called. 00240 */ 00241 virtual void signalChange( const QString & ); 00242 00243 protected: // I hate to make this protected, but I'm too tired to deal with making accessors... 00244 struct Entry 00245 { 00246 QString val; 00247 QString cmnt; 00248 }; 00249 QDict<Entry> data; 00250 typedef QValueList<QString> QVStringList; 00251 QVStringList order; 00252 00253 private: 00254 mutable bool changed; // mutable gets around the whole problem listed in setNeedsSave(); that code needs to be revisited. 00255 00256 }; 00257 //} // namespace qub 00258 00259 class KeyValuePair : public QObject, public Serializable 00260 { 00261 Q_OBJECT 00262 public: 00263 KeyValuePair( QString key=0, QString val=0, QString cmnt=0 ); 00264 ~KeyValuePair(); 00265 QString key(); 00266 QString val(); 00267 QString comment(); 00268 00269 00270 virtual void deserialize(const Deserializer &node); 00271 virtual void serialize(Serializer &node) const; 00272 00273 private: 00274 QString k; 00275 QString v; 00276 QString c; 00277 00278 }; // end class KeyValuePair 00279 00280 }; // namespace fun 00281 #endif