src/config.h

changeset 436
4268a5507725
parent 421
7d26db0be944
child 461
fbcc91ae1dd2
equal deleted inserted replaced
435:bc3a51394953 436:4268a5507725
22 #include "common.h" 22 #include "common.h"
23 23
24 // ============================================================================= 24 // =============================================================================
25 #include <QString> 25 #include <QString>
26 #include <QKeySequence> 26 #include <QKeySequence>
27 class QSettings;
27 28
28 typedef QChar qchar; 29 typedef QChar qchar;
29 typedef QString str; 30 typedef QString str;
30 31
31 #define MAX_INI_LINE 512 32 #define MAX_INI_LINE 512
32 #define MAX_CONFIG 512 33 #define MAX_CONFIG 512
33 34
34 #define cfg(T, NAME, DEFAULT) T##config NAME (DEFAULT, #NAME) 35 #define cfg(T, NAME, DEFAULT) T##config NAME (DEFAULT, #NAME, #DEFAULT)
35 #define extern_cfg(T, NAME) extern T##config NAME 36 #define extern_cfg(T, NAME) extern T##config NAME
36 37
37 // ========================================================= 38 // =========================================================
38 class config { 39 class config {
39 public: 40 public:
44 Type_float, 45 Type_float,
45 Type_bool, 46 Type_bool,
46 Type_keyseq, 47 Type_keyseq,
47 }; 48 };
48 49
50 config (const char* defstring) : m_defstring (defstring) {}
49 const char* name; 51 const char* name;
50 52
51 virtual Type getType() const { 53 virtual Type getType() const {
52 return Type_none; 54 return Type_none;
53 } 55 }
54 56
57 str toString() const;
58 str defaultString() const;
55 virtual void resetValue() {} 59 virtual void resetValue() {}
56 virtual bool isDefault() const { 60 virtual void loadFromConfig (const QSettings* cfg) { (void) cfg; }
57 return false; 61 virtual bool isDefault() const { return false; }
58 } 62
59
60 // ------------------------------------------ 63 // ------------------------------------------
61 static bool load(); 64 static bool load();
62 static bool save(); 65 static bool save();
63 static void reset(); 66 static void reset();
64 static str dirpath(); 67 static str dirpath();
65 static str filepath(); 68 static str filepath (str file);
69
70 private:
71 const char* m_defstring;
66 }; 72 };
67 73
68 void addConfig (config* ptr); 74 void addConfig (config* ptr);
69 75
70 // ============================================================================= 76 // =============================================================================
111 class T##config : public config 117 class T##config : public config
112 118
113 #define IMPLEMENT_CONFIG(T) \ 119 #define IMPLEMENT_CONFIG(T) \
114 T value, defval; \ 120 T value, defval; \
115 \ 121 \
116 T##config (T _defval, const char* _name) { \ 122 T##config (T _defval, const char* _name, const char* defstring) : config (defstring) { \
117 value = defval = _defval; \ 123 value = defval = _defval; \
118 name = _name; \ 124 name = _name; \
119 addConfig (this); \ 125 addConfig (this); \
120 } \ 126 } \
121 operator const T&() const { \ 127 operator const T&() const { \
127 virtual void resetValue() { \ 133 virtual void resetValue() { \
128 value = defval; \ 134 value = defval; \
129 } \ 135 } \
130 virtual bool isDefault() const { \ 136 virtual bool isDefault() const { \
131 return value == defval; \ 137 return value == defval; \
132 } 138 } \
139 virtual void loadFromConfig (const QSettings* cfg) override;
133 140
134 // ============================================================================= 141 // =============================================================================
135 CONFIGTYPE (int) { 142 CONFIGTYPE (int) {
136 public: 143 public:
137 IMPLEMENT_CONFIG (int) 144 IMPLEMENT_CONFIG (int)

mercurial