[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klfconfigbase.h
Go to the documentation of this file.
1/***************************************************************************
2 * file klfconfigbase.h
3 * This file is part of the KLatexFormula Project.
4 * Copyright (C) 2011 by Philippe Faist
5 * philippe.faist at bluewin.ch
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22/* $Id$ */
23
24
25#ifndef KLFCONFIGBASE_H_
26#define KLFCONFIGBASE_H_
27
28#include <QString>
29#include <QVariant>
30#include <QObject>
31
32#include <klfdefs.h>
33#include <klfutil.h>
34
36{
37public:
39 virtual ~KLFConfigPropBase();
40
41 virtual QString propName() const { return pname; }
42 virtual QVariant toVariant() const = 0;
43
44 virtual bool setValue(const QVariant& newvalue) { Q_UNUSED(newvalue); return false; }
45
46 virtual QVariant defaultValueVariant() const { return QVariant(); }
47
48protected:
50};
51
52
54{
55public:
56 virtual bool okChangeProperty(KLFConfigPropBase *property, const QVariant& oldValue, const QVariant& newValue);
57
58 virtual void propertyChanged(KLFConfigPropBase *property, const QVariant& oldValue, const QVariant& newValue);
59
60 virtual void propertyValueRequested(const KLFConfigPropBase *property);
61
67 virtual void connectQObjectProperty(const QString& configPropertyName, QObject *object,
68 const QByteArray& objPropName);
69 virtual void disconnectQObjectProperty(const QString& configPropertyName, QObject *object,
70 const QByteArray& objPropName);
77 virtual void connectQObjectSlot(const QString& configPropertyName, QObject *object,
78 const QByteArray& slotMethodName);
79 virtual void disconnectQObjectSlot(const QString& configPropertyName, QObject *object,
80 const QByteArray& slotMethodName);
81
83 virtual void disconnectQObject(QObject * object);
84
85 virtual QStringList propertyList() const;
86 KLFConfigPropBase * property(const QString& name);
87
89 inline void registerConfigProp(KLFConfigPropBase *p) { pProperties.append(p); }
90
91protected:
92 enum ConnectionTarget { Property, Slot };
97 inline bool operator==(const ObjConnection& b) const {
98 return target == b.target && object == b.object && targetName == b.targetName;
99 }
100 };
102
103 void connectQObject(const QString& configPropertyName, QObject *object,
104 ConnectionTarget target, const QByteArray& targetName);
105 void disconnectQObject(const QString& configPropertyName, QObject *object,
106 ConnectionTarget target, const QByteArray& targetName);
107
109};
110
111
112
113
114template<class T>
116{
117public:
118 typedef T Type;
119
120 KLFConfigProp() : config(NULL), val(T()), defval(T()), isdefaultvaluedefinite(false) { }
121
122
123 operator Type () const
124 {
125 return value();
126 }
127 const Type operator()() const
128 {
129 return value();
130 }
131 const Type& operator=(const Type& newvalue)
132 {
133 setValue(newvalue);
134 return newvalue;
135 };
136 bool operator==(const Type& compareValue) const
137 {
138 return value() == compareValue;
139 }
140 bool operator!=(const Type& compareValue) const
141 {
142 return value() != compareValue;
143 }
144
145 bool setValue(const QVariant& newvalue)
146 {
148 }
149
150 bool setValue(const Type& newvalue)
151 {
152 Type oldvalue = value();
153 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") have not been initialized!", return false; ) ;
155 if (!config->okChangeProperty(this, vc.convert(oldvalue), vc.convert(newvalue))) {
156 return false;
157 }
158 val = newvalue;
159 valisset = true;
160 config->propertyChanged(this, vc.convert(oldvalue), vc.convert(newvalue));
161 return true;
162 }
163
165 {
166 return defval;
167 }
169 {
170 return isdefaultvaluedefinite;
171 }
172 Type value() const
173 {
174 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") have not been initialized!", return Type(); ) ;
175 config->propertyValueRequested(this);
176
177 if (valisset)
178 return val;
179 return defval;
180 }
181 bool hasValue() const
182 {
183 return valisset;
184 }
185
186 virtual QVariant toVariant() const
187 {
189 return v.convert(value());
190 }
191
193 {
195 return v.convert(defaultValue());
196 }
197
199 bool isDefaultValueDefinite = true)
200 {
201 KLF_ASSERT_NOT_NULL(confptr, "Cannot use a NULL config pointer!!", ; );
202 config = confptr;
203 config->registerConfigProp(this);
204 pname = propName;
205 defval = defaultValue;
206 valisset = false;
207 val = Type();
208 isdefaultvaluedefinite = isDefaultValueDefinite;
209 }
210
212 {
213 defval = defaultValue;
214 isdefaultvaluedefinite = true;
215 }
216
218 {
219 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") are not initialized!", return; ) ;
220
221 config->connectQObjectProperty(pname, object, propName) ;
222 }
224 {
225 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") are not initialized!", return; ) ;
226
227 config->disconnectQObjectProperty(pname, object, propName) ;
228 }
229
230 void connectQObjectSlot(QObject *object, const QByteArray& slotName)
231 {
232 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") are not initialized!", return; ) ;
233
234 config->connectQObjectSlot(pname, object, slotName) ;
235 }
236 void disconnectQObjectSlot(QObject *object, const QByteArray& slotName)
237 {
238 KLF_ASSERT_NOT_NULL(config, "we ("<<pname<<") are not initialized!", return; ) ;
239
240 config->disconnectQObjectSlot(pname, object, slotName) ;
241 }
242
243private:
244 KLFConfigBase *config;
245
246 bool valisset;
247 Type val;
248 Type defval;
253 bool isdefaultvaluedefinite;
254};
255
256#define KLFCONFIGPROP_INIT_CONFIG(configptr) KLFConfigBase *__klfconfigprop_configbase = (configptr) ;
257#define KLFCONFIGPROP_INIT(var, defval) (var).initialize(__klfconfigprop_configbase, #var, (defval))
258#define KLFCONFIGPROP_INIT_DEFNOTDEF(var, defval) (var).initialize(__klfconfigprop_configbase, #var, (defval), false)
259
260
261
262
263#endif
virtual void propertyChanged(KLFConfigPropBase *property, const QVariant &oldValue, const QVariant &newValue)
virtual void connectQObjectProperty(const QString &configPropertyName, QObject *object, const QByteArray &objPropName)
virtual void propertyValueRequested(const KLFConfigPropBase *property)
QList< KLFConfigPropBase * > pProperties
virtual void connectQObjectSlot(const QString &configPropertyName, QObject *object, const QByteArray &slotMethodName)
virtual void disconnectQObjectSlot(const QString &configPropertyName, QObject *object, const QByteArray &slotMethodName)
virtual bool okChangeProperty(KLFConfigPropBase *property, const QVariant &oldValue, const QVariant &newValue)
QHash< QString, QList< ObjConnection > > pObjConnections
virtual void disconnectQObjectProperty(const QString &configPropertyName, QObject *object, const QByteArray &objPropName)
void registerConfigProp(KLFConfigPropBase *p)
virtual QString propName() const
virtual bool setValue(const QVariant &newvalue)
virtual QVariant defaultValueVariant() const
virtual QVariant toVariant() const =0
Type value() const
virtual QVariant toVariant() const
bool setValue(const Type &newvalue)
bool operator!=(const Type &compareValue) const
bool operator==(const Type &compareValue) const
void setDefaultValue(const Type &defaultValue)
void connectQObjectSlot(QObject *object, const QByteArray &slotName)
void initialize(KLFConfigBase *confptr, const QString &propName, const Type &defaultValue, bool isDefaultValueDefinite=true)
void connectQObjectProperty(QObject *object, const QByteArray &propName)
Type defaultValue() const
bool defaultValueDefinite() const
bool setValue(const QVariant &newvalue)
virtual QVariant defaultValueVariant() const
void disconnectQObjectProperty(QObject *object, const QByteArray &propName)
void disconnectQObjectSlot(QObject *object, const QByteArray &slotName)
const Type operator()() const
bool hasValue() const
const Type & operator=(const Type &newvalue)
#define KLF_ASSERT_NOT_NULL(ptr, msg, failaction)
Asserting Non-NULL pointers (NON-FATAL)
Definition klfdebug.h:210
Base declarations for klatexformula and some utilities.
#define KLF_EXPORT
Definition klfdefs.h:41
bool operator==(const ObjConnection &b) const
static QVariant convert(const T &value)
Definition klfutil.h:947

Generated by doxygen 1.9.8