Source: fun/PropertyList.h
|
|
|
|
// ***************************************************************************
// PropertyList.h - description
// -------------------
// begin : Thu Oct 5 2000
// Copyright (C) 2000 Stephan Beal & Rusty Ballinger
// stephan@wanderinghorse.net & rusty@sgi.com
// ***************************************************************************
// * *
// * This program is free software; you can redistribute it and/or modify *
// * it under the terms of the GNU General Public License as published by *
// * the Free Software Foundation; either version 2 of the License, or *
// * (at your option) any later version. *
// * *
// ***************************************************************************
#ifndef PROPERTYLIST_H
#define PROPERTYLIST_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace fun {
/**
* This class provides a generic hashtable-like interface for perstantly storing
* arbitrary key/value pairs.
*/
class PropertyList : public QObject, public Serializable
{
Q_OBJECT
public:
PropertyList();
~PropertyList();
/**
* Returns the value corresponding to the given key. If the value has
* not been set, the given default value is returned.
*/
QString getString(const QString &key, QString defaultVal = QString()) const;
/**
* This is the same as calling getString().
*/
QString get(const QString &key, QString defaultVal = QString()) const;
/**
* Returns the value corresponding to the given key. If the value has
* not been set or can't be converted to an int, the given default value
* is returned. If you need to know whether a value was set but couldn't
* be converted, use isSet() and isInt().
*/
int getInt(const QString &key, int defaultVal = 0) const;
/**
* Returns the value corresponding to the given key. If the value has
* not been set or can't be converted to a bool, the given default value
* is returned. (See isBool() for the rules.) If you need to know
* whether a value was set but couldn't be converted, use isSet() and
* isBool().
*/
bool getBool(const QString &key, bool defaultVal = false) const;
/**
* Returns the value corresponding to the given key. If the value has
* not been set or can't be converted to a double, the given default
* value is returned. (See isDouble() for the rules.) If you need to
* know whether a value was set but couldn't be converted, use isSet()
* and isDouble().
*/
double getDouble(const QString &key, double defaultVal = 0.0) const;
/**
* Returns the value corresponding to the given key. If the value has
* not been set or can't be converted to a color, the given default
* value is returned. (See isColor() for the rules.) If you need to
* know whether a value was set but couldn't be converted, use isSet()
* and isColor().
*/
QColor getColor(const QString &key, QColor defaultVal = QColor()) const;
/**
* Returns the QPoint stored in the given key.
*/
virtual QPoint getPoint( const QString &key, const QPoint &dflt );
/**
* A convenience wrapper around getPoint().
*/
virtual QSize getSize( const QString &key, const QSize &dflt );
/**
* Returns true if a value has been supplied for the given key, false
* otherwise. Note that a key can be supplied with a null value.
*/
bool isSet(const QString &key) const;
/**
* Returns true if the value has been set for the given key and can
* be converted to an int. This uses QString::toInt() to determine
* whether the value can be converted.
*/
bool isInt(const QString &key) const;
/**
* Returns true if the value has been set for the given key and can
* be converted to a bool. (True if it's "true", "false", "on", "off",
* "yes", "no", "t", "f", "y", "n", or an integer.)
*/
bool isBool(const QString &key) const;
/**
* Returns true if the value has been set for the given key and can
* be converted to a double. This uses QString::toDouble() to determine
* whether the value can be converted.
*/
bool isDouble(const QString &key) const;
/**
* Returns true if the value has been set for the given key and can
* be converted to a color. See QColor::setNamedColor() for the
* rules.
*/
bool isColor(const QString &key) const;
/**
* Gets an element's comment.
*/
QString getComment(const QString &key) const;
/**
* Returns true if this ConfigFile has been successfully loaded, or
* if at least one value has been set().
*/
virtual bool isLoaded() const { return !data.isEmpty(); }
/**
* Returns true if any of the configuration values have been changed
* since the file was last loaded or saved.
*/
virtual bool needsSave() const { return changed; }
const QValueList &keys();
public slots:
/**
* Forces the object to emit a signalChange( QString() ) with
* an empty String. Receivers can decide if they want to react
* to a global update or not.
*/
void slotEmitUpdate();
/**
* Sets the given element to the given string value.
*/
void set(const QString &key, const QString &val, const QString &comment = QString() );
/**
* Sets the given element to the given int value.
*/
void set(const QString &key, int val);
/**
* Sets the given element to the given bool value.
*/
void set(const QString &key, bool val);
/**
* Sets the given element to the given double value.
*/
void set(const QString &key, double val);
/**
* Sets the given element to the given color value. This will be stored
* in the format #rrggbb.
*/
void set(const QString &key, const QColor &val);
/**
* Puts a QPoint in the list, in the format "x,y".
*/
virtual void set( const QString &key, const QPoint &val );
/**
* A convenience wrapper around set( QString, QPoint ).
*/
virtual void set( const QString &key, const QSize &dflt );
/**
* Removes the given element.
*/
void unset(const QString &key);
/**
* Sets the comment which will be printed above an element. Don't
* include comment characters; they will be added by saveToText()
* or saveToXML().
*/
void setComment(const QString &key, const QString &comment);
/**
* Sets the "needs save" flag to the given value. (The only reason for
* this is that saveToXML and saveToText are const methods, so they
* can't change the flag; after saving it, setNeedsSave() yourself.)
* Too bogus?
*/
void setNeedsSave(bool needsSave) { changed = needsSave; }
/**
* This take a different ConfigFile and merges in any new values
* and keys.
* This can be used to easily allow gamesets to modify the global
* config settings. My idea is to have a qub.ini in any given dir,
* and have DirView pull that into GConfigFile::global()->mergeIn().
* This function will return false when es Fehler gab.
*/
virtual bool mergeIn( const PropertyList *config );
/**
* Tells the object to restore it's state from the given Deserializer.
*/
virtual void deserialize(const Deserializer &ser);
/**
* Tells the instance to save it's state to the given Serializer object.
*/
virtual void serialize(Serializer &ser) const;
signals:
/**
* Emitted when set() is called.
*/
virtual void signalChange( const QString & );
protected: // I hate to make this protected, but I'm too tired to deal with making accessors...
struct Entry
{
QString val;
QString cmnt;
};
QDict data;
typedef QValueList QVStringList;
QVStringList order;
private:
mutable bool changed; // mutable gets around the whole problem listed in setNeedsSave(); that code needs to be revisited.
};
//} // namespace qub
class KeyValuePair : public QObject, public Serializable
{
Q_OBJECT
public:
KeyValuePair( QString key=0, QString val=0, QString cmnt=0 );
~KeyValuePair();
QString key();
QString val();
QString comment();
virtual void deserialize(const Deserializer &node);
virtual void serialize(Serializer &node) const;
private:
QString k;
QString v;
QString c;
}; // end class KeyValuePair
}; // namespace fun
#endif
Generated by: stephan on cheyenne on Mon Aug 11 14:06:52 2003, using kdoc 2.0a54. |