| |
1 /* |
| |
2 * LDForge: LDraw parts authoring CAD |
| |
3 * Copyright (C) 2013, 2014 Santeri Piippo |
| |
4 * |
| |
5 * This program is free software: you can redistribute it and/or modify |
| |
6 * it under the terms of the GNU General Public License as published by |
| |
7 * the Free Software Foundation, either version 3 of the License, or |
| |
8 * (at your option) any later version. |
| |
9 * |
| |
10 * This program is distributed in the hope that it will be useful, |
| |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| |
13 * GNU General Public License for more details. |
| |
14 * |
| |
15 * You should have received a copy of the GNU General Public License |
| |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| |
17 */ |
| |
18 |
| |
19 #ifndef LDFORGE_CONFIG_H |
| |
20 #define LDFORGE_CONFIG_H |
| |
21 |
| |
22 #include "PropertyMacro.h" |
| |
23 #include "Types.h" |
| |
24 |
| |
25 // ============================================================================= |
| |
26 #include <QString> |
| |
27 #include <QVariant> |
| |
28 #include <QKeySequence> |
| |
29 class QSettings; |
| |
30 |
| |
31 #define MAX_INI_LINE 512 |
| |
32 #define MAX_CONFIG 512 |
| |
33 |
| |
34 #define cfg(T, NAME, DEFAULT) \ |
| |
35 Config::T##Type NAME; \ |
| |
36 T##Config config_##NAME (&NAME, #NAME, DEFAULT); |
| |
37 |
| |
38 #define extern_cfg(T, NAME) extern Config::T##Type NAME; |
| |
39 |
| |
40 // ========================================================= |
| |
41 class Config |
| |
42 { |
| |
43 PROPERTY (private, QString, Name, STR_OPS, STOCK_WRITE) |
| |
44 |
| |
45 public: |
| |
46 enum Type |
| |
47 { |
| |
48 EIntType, |
| |
49 EStringType, |
| |
50 EFloatType, |
| |
51 EBoolType, |
| |
52 EKeySequenceType, |
| |
53 EListType, |
| |
54 EVertexType, |
| |
55 }; |
| |
56 |
| |
57 using IntType = int; |
| |
58 using StringType = QString; |
| |
59 using FloatType = float; |
| |
60 using BoolType = bool; |
| |
61 using KeySequenceType = QKeySequence; |
| |
62 using ListType = QList<QVariant>; |
| |
63 using VertexType = Vertex; |
| |
64 |
| |
65 Config (QString name); |
| |
66 |
| |
67 virtual QVariant getDefaultAsVariant() const = 0; |
| |
68 virtual Type getType() const = 0; |
| |
69 virtual bool isDefault() const = 0; |
| |
70 virtual void loadFromVariant (const QVariant& val) = 0; |
| |
71 virtual void resetValue() = 0; |
| |
72 virtual QVariant toVariant() const = 0; |
| |
73 |
| |
74 // ------------------------------------------ |
| |
75 static bool load(); |
| |
76 static bool save(); |
| |
77 static void reset(); |
| |
78 static QString dirpath(); |
| |
79 static QString filepath (QString file); |
| |
80 |
| |
81 protected: |
| |
82 static void addToArray (Config* ptr); |
| |
83 }; |
| |
84 |
| |
85 // ============================================================================= |
| |
86 #define IMPLEMENT_CONFIG(NAME) \ |
| |
87 public: \ |
| |
88 using ValueType = Config::NAME##Type; \ |
| |
89 \ |
| |
90 NAME##Config (ValueType* valueptr, QString name, ValueType def) : \ |
| |
91 Config (name), \ |
| |
92 m_valueptr (valueptr), \ |
| |
93 m_default (def) \ |
| |
94 { \ |
| |
95 Config::addToArray (this); \ |
| |
96 *m_valueptr = def; \ |
| |
97 } \ |
| |
98 \ |
| |
99 inline ValueType getValue() const \ |
| |
100 { \ |
| |
101 return *m_valueptr; \ |
| |
102 } \ |
| |
103 \ |
| |
104 inline void setValue (ValueType val) \ |
| |
105 { \ |
| |
106 *m_valueptr = val; \ |
| |
107 } \ |
| |
108 \ |
| |
109 virtual Config::Type getType() const \ |
| |
110 { \ |
| |
111 return Config::E##NAME##Type; \ |
| |
112 } \ |
| |
113 \ |
| |
114 virtual void resetValue() \ |
| |
115 { \ |
| |
116 *m_valueptr = m_default; \ |
| |
117 } \ |
| |
118 \ |
| |
119 virtual const ValueType& getDefault() const \ |
| |
120 { \ |
| |
121 return m_default; \ |
| |
122 } \ |
| |
123 \ |
| |
124 virtual bool isDefault() const \ |
| |
125 { \ |
| |
126 return *m_valueptr == m_default; \ |
| |
127 } \ |
| |
128 \ |
| |
129 virtual void loadFromVariant (const QVariant& val) \ |
| |
130 { \ |
| |
131 *m_valueptr = val.value<ValueType>(); \ |
| |
132 } \ |
| |
133 \ |
| |
134 virtual QVariant toVariant() const \ |
| |
135 { \ |
| |
136 return QVariant::fromValue<ValueType> (*m_valueptr); \ |
| |
137 } \ |
| |
138 \ |
| |
139 virtual QVariant getDefaultAsVariant() const \ |
| |
140 { \ |
| |
141 return QVariant::fromValue<ValueType> (m_default); \ |
| |
142 } \ |
| |
143 \ |
| |
144 static NAME##Config* getByName (QString name); \ |
| |
145 \ |
| |
146 private: \ |
| |
147 ValueType* m_valueptr; \ |
| |
148 ValueType m_default; |
| |
149 |
| |
150 // ============================================================================= |
| |
151 class IntConfig : public Config |
| |
152 { |
| |
153 IMPLEMENT_CONFIG (Int) |
| |
154 }; |
| |
155 |
| |
156 // ============================================================================= |
| |
157 class StringConfig : public Config |
| |
158 { |
| |
159 IMPLEMENT_CONFIG (String) |
| |
160 }; |
| |
161 |
| |
162 // ============================================================================= |
| |
163 class FloatConfig : public Config |
| |
164 { |
| |
165 IMPLEMENT_CONFIG (Float) |
| |
166 }; |
| |
167 |
| |
168 // ============================================================================= |
| |
169 class BoolConfig : public Config |
| |
170 { |
| |
171 IMPLEMENT_CONFIG (Bool) |
| |
172 }; |
| |
173 |
| |
174 // ============================================================================= |
| |
175 class KeySequenceConfig : public Config |
| |
176 { |
| |
177 IMPLEMENT_CONFIG (KeySequence) |
| |
178 }; |
| |
179 |
| |
180 // ============================================================================= |
| |
181 class ListConfig : public Config |
| |
182 { |
| |
183 IMPLEMENT_CONFIG (List) |
| |
184 }; |
| |
185 |
| |
186 // ============================================================================= |
| |
187 class VertexConfig : public Config |
| |
188 { |
| |
189 IMPLEMENT_CONFIG (Vertex) |
| |
190 }; |
| |
191 |
| |
192 #endif // LDFORGE_CONFIG_H |