00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef KLFCONFIGBASE_H_
00026 #define KLFCONFIGBASE_H_
00027
00028 #include <QString>
00029 #include <QVariant>
00030 #include <QObject>
00031
00032 #include <klfdefs.h>
00033 #include <klfutil.h>
00034
00035 class KLF_EXPORT KLFConfigPropBase
00036 {
00037 public:
00038 KLFConfigPropBase();
00039 virtual ~KLFConfigPropBase();
00040
00041 virtual QString propName() const { return pname; }
00042 virtual QVariant toVariant() const = 0;
00043
00044 virtual bool setValue(const QVariant& newvalue) { Q_UNUSED(newvalue); return false; }
00045
00046 virtual QVariant defaultValueVariant() const { return QVariant(); }
00047
00048 protected:
00049 QString pname;
00050 };
00051
00052
00053 class KLF_EXPORT KLFConfigBase
00054 {
00055 public:
00056 virtual bool okChangeProperty(KLFConfigPropBase *property, const QVariant& oldValue, const QVariant& newValue);
00057
00058 virtual void propertyChanged(KLFConfigPropBase *property, const QVariant& oldValue, const QVariant& newValue);
00059
00060 virtual void propertyValueRequested(const KLFConfigPropBase *property);
00061
00067 virtual void connectQObjectProperty(const QString& configPropertyName, QObject *object,
00068 const QByteArray& objPropName);
00069 virtual void disconnectQObjectProperty(const QString& configPropertyName, QObject *object,
00070 const QByteArray& objPropName);
00077 virtual void connectQObjectSlot(const QString& configPropertyName, QObject *object,
00078 const QByteArray& slotMethodName);
00079 virtual void disconnectQObjectSlot(const QString& configPropertyName, QObject *object,
00080 const QByteArray& slotMethodName);
00081
00083 virtual void disconnectQObject(QObject * object);
00084
00085 virtual QStringList propertyList() const;
00086 KLFConfigPropBase * property(const QString& name);
00087
00089 inline void registerConfigProp(KLFConfigPropBase *p) { pProperties.append(p); }
00090
00091 protected:
00092 enum ConnectionTarget { Property, Slot };
00093 struct ObjConnection {
00094 ConnectionTarget target;
00095 QObject *object;
00096 QByteArray targetName;
00097 inline bool operator==(const ObjConnection& b) const {
00098 return target == b.target && object == b.object && targetName == b.targetName;
00099 }
00100 };
00101 QHash<QString, QList<ObjConnection> > pObjConnections;
00102
00103 void connectQObject(const QString& configPropertyName, QObject *object,
00104 ConnectionTarget target, const QByteArray& targetName);
00105 void disconnectQObject(const QString& configPropertyName, QObject *object,
00106 ConnectionTarget target, const QByteArray& targetName);
00107
00108 QList<KLFConfigPropBase*> pProperties;
00109 };
00110
00111
00112
00113
00114 template<class T>
00115 class KLFConfigProp : public KLFConfigPropBase
00116 {
00117 public:
00118 typedef T Type;
00119
00120 KLFConfigProp() : config(NULL), val(T()), defval(T()), isdefaultvaluedefinite(false) { }
00121
00122
00123 operator Type () const
00124 {
00125 return value();
00126 }
00127 const Type operator()() const
00128 {
00129 return value();
00130 }
00131 const Type& operator=(const Type& newvalue)
00132 {
00133 setValue(newvalue);
00134 return newvalue;
00135 };
00136 bool operator==(const Type& compareValue) const
00137 {
00138 return value() == compareValue;
00139 }
00140 bool operator!=(const Type& compareValue) const
00141 {
00142 return value() != compareValue;
00143 }
00144
00145 bool setValue(const QVariant& newvalue)
00146 {
00147 return setValue(KLFVariantConverter<T>::recover(newvalue));
00148 }
00149
00150 bool setValue(const Type& newvalue)
00151 {
00152 Type oldvalue = value();
00153 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") have not been initialized!", return false; ) ;
00154 KLFVariantConverter<Type> vc;
00155 if (!config->okChangeProperty(this, vc.convert(oldvalue), vc.convert(newvalue))) {
00156 return false;
00157 }
00158 val = newvalue;
00159 valisset = true;
00160 config->propertyChanged(this, vc.convert(oldvalue), vc.convert(newvalue));
00161 return true;
00162 }
00163
00164 Type defaultValue() const
00165 {
00166 return defval;
00167 }
00168 bool defaultValueDefinite() const
00169 {
00170 return isdefaultvaluedefinite;
00171 }
00172 Type value() const
00173 {
00174 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") have not been initialized!", return Type(); ) ;
00175 config->propertyValueRequested(this);
00176
00177 if (valisset)
00178 return val;
00179 return defval;
00180 }
00181 bool hasValue() const
00182 {
00183 return valisset;
00184 }
00185
00186 virtual QVariant toVariant() const
00187 {
00188 KLFVariantConverter<Type> v;
00189 return v.convert(value());
00190 }
00191
00192 virtual QVariant defaultValueVariant() const
00193 {
00194 KLFVariantConverter<Type> v;
00195 return v.convert(defaultValue());
00196 }
00197
00198 void initialize(KLFConfigBase *confptr, const QString& propName, const Type& defaultValue,
00199 bool isDefaultValueDefinite = true)
00200 {
00201 KLF_ASSERT_NOT_NULL(confptr, "Cannot use a NULL config pointer!!", ; );
00202 config = confptr;
00203 config->registerConfigProp(this);
00204 pname = propName;
00205 defval = defaultValue;
00206 valisset = false;
00207 val = Type();
00208 isdefaultvaluedefinite = isDefaultValueDefinite;
00209 }
00210
00211 void setDefaultValue(const Type& defaultValue)
00212 {
00213 defval = defaultValue;
00214 isdefaultvaluedefinite = true;
00215 }
00216
00217 void connectQObjectProperty(QObject *object, const QByteArray& propName)
00218 {
00219 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") are not initialized!", return; ) ;
00220
00221 config->connectQObjectProperty(pname, object, propName) ;
00222 }
00223 void disconnectQObjectProperty(QObject *object, const QByteArray& propName)
00224 {
00225 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") are not initialized!", return; ) ;
00226
00227 config->disconnectQObjectProperty(pname, object, propName) ;
00228 }
00229
00230 void connectQObjectSlot(QObject *object, const QByteArray& slotName)
00231 {
00232 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") are not initialized!", return; ) ;
00233
00234 config->connectQObjectSlot(pname, object, slotName) ;
00235 }
00236 void disconnectQObjectSlot(QObject *object, const QByteArray& slotName)
00237 {
00238 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") are not initialized!", return; ) ;
00239
00240 config->disconnectQObjectSlot(pname, object, slotName) ;
00241 }
00242
00243 private:
00244 KLFConfigBase *config;
00245
00246 bool valisset;
00247 Type val;
00248 Type defval;
00253 bool isdefaultvaluedefinite;
00254 };
00255
00256 #define KLFCONFIGPROP_INIT_CONFIG(configptr) KLFConfigBase *__klfconfigprop_configbase = (configptr) ;
00257 #define KLFCONFIGPROP_INIT(var, defval) (var).initialize(__klfconfigprop_configbase, #var, (defval))
00258 #define KLFCONFIGPROP_INIT_DEFNOTDEF(var, defval) (var).initialize(__klfconfigprop_configbase, #var, (defval), false)
00259
00260
00261
00262
00263 #endif