1 #ifndef Header_Config_Manager_Interface
2 #define Header_Config_Manager_Interface
3 
4 #include "mostQtHeaders.h"
5 
6 
7 typedef QMap<QString, QString> StringStringMap;
8 
9 #define PROPERTY_TYPE_FOREACH_MACRO_SIMPLE(M) \
10 	M(QVariant, PT_VARIANT) \
11 	M(int, PT_INT) \
12 	M(bool, PT_BOOL) \
13 	M(QString, PT_STRING) \
14 	M(QStringList, PT_STRINGLIST) \
15 	M(QDateTime, PT_DATETIME) \
16 	M(float, PT_FLOAT) \
17 	M(double, PT_DOUBLE) \
18 	M(QByteArray, PT_BYTEARRAY) \
19 	M(QList<QVariant>, PT_LIST)
20 
21 #define PROPERTY_TYPE_FOREACH_MACRO_COMPLEX(M) \
22 	M(StringStringMap, PT_MAP_STRING_STRING)
23 
24 #define PROPERTY_TYPE_FOREACH_MACRO(M) PROPERTY_TYPE_FOREACH_MACRO_SIMPLE(M) PROPERTY_TYPE_FOREACH_MACRO_COMPLEX(M)
25 
26 #define ENUMERATE_ID(dummy, ID) , ID
27 enum PropertyType {PT_VOID = 0 PROPERTY_TYPE_FOREACH_MACRO(ENUMERATE_ID) };
28 #undef ENUMERATE_ID
29 
30 #include <QString>
31 #include <QStringList>
32 #include <QDateTime>
33 #include <QVariant>
34 #include <QWidget>
35 #include <QByteArray>
36 
37 enum LinkOption {
38 	LO_NONE = 0,
39 	LO_UPDATE_ALL = 1, //if the property is changed (through the config manager), update all displaying objects
40 	LO_DIRECT_OVERRIDE = 2 //if one object is changed, update property (if not set, the property is updated if a majority of objects is changed)
41 };
42 Q_DECLARE_FLAGS(LinkOptions, LinkOption)
43 
44 
45 struct ManagedProperty {
46 	QString name;
47 	void *storage;
48 	PropertyType type;
49 	QVariant def;
50 	ptrdiff_t widgetOffset;
51 
52 	ManagedProperty();
53 #define CONSTRUCTOR(TYPE, dummy) \
54 	ManagedProperty(TYPE* storage, QVariant def = QVariant(), ptrdiff_t widgetOffset = 0); \
55 	static ManagedProperty fromValue(TYPE value);
56 	PROPERTY_TYPE_FOREACH_MACRO(CONSTRUCTOR)
57 #undef CONSTRUCTOR
58 
59 	QVariant valueToQVariant() const;
60 	void valueFromQVariant(const QVariant v);
61 	void writeToObject(QObject *w) const;
62 	bool readFromObject(const QObject *w);
63 	void deallocate();
64 };
65 
66 
67 class ConfigManagerInterface
68 {
69 public:
70 	//registers an option with the given name, type and default value, stored at the given pointer
71 	//this has to be called before the configmanager reads the settings, and the option (*storage) will
72 	//automatically be read/written from the ini file.
73 	virtual void registerOption(const QString &name, void *storage, PropertyType type, QVariant def) = 0;
74 #define VIRTUAL_REGISTER_OPTION(TYPE, dummy) \
75 	virtual void registerOption(const QString& name, TYPE* storage, QVariant def=QVariant()) = 0;
76 	PROPERTY_TYPE_FOREACH_MACRO(VIRTUAL_REGISTER_OPTION)
77 #undef VIRTUAL_REGISTER_OPTION
78 	//set an option as variant (don't use it in c++, since it is not type-safe)
79 	virtual void setOption(const QString &name, const QVariant &value) = 0;
80 	//get an option as variant (don't use it in c++, since it is not type-safe)
81 	virtual QVariant getOption(const QString &name, const QVariant &defaultValue = QVariant()) const = 0;
82 	//check if an option exists
83 	virtual bool existsOption(const QString &name) const = 0;
84 	//shows the value of an registered option in the passed widget
85 	//if the dialog containing widget is accepted (and not rejected), the value from the widget will be written to the option
86 	virtual void linkOptionToDialogWidget(const void *optionStorage, QWidget *widget) = 0;
87 	//Displays the option in the object.
88 	//Each option can be linked to several objects;
89 	//If the object representation changes => (If fullsync => the option and all linked objects are changed
90 	//                                         If !fullsync => the option is changed to the value of the majority of the objects)
91 	//Setting a link changes the object value to the current option value
92     virtual void linkOptionToObject(const void *optionStorage, QObject *widget, LinkOptions options = LO_NONE) = 0;
93 
94 	virtual void updateAllLinkedObjects(const void *optionStorage) = 0;
95 
96 	virtual QString parseDir(QString s) const = 0;
97 	virtual QStringList parseDirList(const QString &s) const = 0;
98 	virtual QString reverseParseDir(QString s) const = 0;
99 	virtual QString reverseParseDir(const QStringList &s) const = 0;
100 
101 	static ConfigManagerInterface *getInstance();
102 };
103 
104 #define CONFIG_DECLARE_OPTION_WITH_OBJECT(config, type, name, defaultvalue, configname, object) static type name; config->registerOption(configname, &name, defaultvalue); config->linkOptionToObject(&name, object, LinkOptions(LO_UPDATE_ALL | LO_DIRECT_OVERRIDE));
105 
106 #endif // CONFIGMANAGERINTERFACE_H
107