src/configuration.h

changeset 655
b376645315ab
child 739
152b33a6d51b
equal deleted inserted replaced
654:a74f2ff353b8 655:b376645315ab
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 #pragma once
20 #include <QString>
21 #include <QVariant>
22 #include <QKeySequence>
23 #include "macros.h"
24 #include "basics.h"
25
26 class QSettings;
27
28 #define MAX_INI_LINE 512
29 #define MAX_CONFIG 512
30
31 #define cfg(T, NAME, DEFAULT) \
32 Config::T##Type NAME; \
33 T##Config config_##NAME (&NAME, #NAME, DEFAULT);
34
35 #define extern_cfg(T, NAME) extern Config::T##Type NAME;
36
37 // =========================================================
38 class Config
39 {
40 PROPERTY (private, QString, name, setName, STOCK_WRITE)
41
42 public:
43 enum Type
44 {
45 EIntType,
46 EStringType,
47 EFloatType,
48 EBoolType,
49 EKeySequenceType,
50 EListType,
51 EVertexType,
52 };
53
54 using IntType = int;
55 using StringType = QString;
56 using FloatType = float;
57 using BoolType = bool;
58 using KeySequenceType = QKeySequence;
59 using ListType = QList<QVariant>;
60 using VertexType = Vertex;
61
62 Config (QString name);
63
64 virtual QVariant getDefaultAsVariant() const = 0;
65 virtual Type getType() const = 0;
66 virtual bool isDefault() const = 0;
67 virtual void loadFromVariant (const QVariant& val) = 0;
68 virtual void resetValue() = 0;
69 virtual QVariant toVariant() const = 0;
70
71 // ------------------------------------------
72 static bool load();
73 static bool save();
74 static void reset();
75 static QString dirpath();
76 static QString filepath (QString file);
77
78 protected:
79 static void addToArray (Config* ptr);
80 };
81
82 // =============================================================================
83 #define IMPLEMENT_CONFIG(NAME) \
84 public: \
85 using ValueType = Config::NAME##Type; \
86 \
87 NAME##Config (ValueType* valueptr, QString name, ValueType def) : \
88 Config (name), \
89 m_valueptr (valueptr), \
90 m_default (def) \
91 { \
92 Config::addToArray (this); \
93 *m_valueptr = def; \
94 } \
95 \
96 inline ValueType getValue() const \
97 { \
98 return *m_valueptr; \
99 } \
100 \
101 inline void setValue (ValueType val) \
102 { \
103 *m_valueptr = val; \
104 } \
105 \
106 virtual Config::Type getType() const \
107 { \
108 return Config::E##NAME##Type; \
109 } \
110 \
111 virtual void resetValue() \
112 { \
113 *m_valueptr = m_default; \
114 } \
115 \
116 virtual const ValueType& getDefault() const \
117 { \
118 return m_default; \
119 } \
120 \
121 virtual bool isDefault() const \
122 { \
123 return *m_valueptr == m_default; \
124 } \
125 \
126 virtual void loadFromVariant (const QVariant& val) \
127 { \
128 *m_valueptr = val.value<ValueType>(); \
129 } \
130 \
131 virtual QVariant toVariant() const \
132 { \
133 return QVariant::fromValue<ValueType> (*m_valueptr); \
134 } \
135 \
136 virtual QVariant getDefaultAsVariant() const \
137 { \
138 return QVariant::fromValue<ValueType> (m_default); \
139 } \
140 \
141 static NAME##Config* getByName (QString name); \
142 \
143 private: \
144 ValueType* m_valueptr; \
145 ValueType m_default;
146
147 // =============================================================================
148 //
149 class IntConfig : public Config
150 {
151 IMPLEMENT_CONFIG (Int)
152 };
153
154 // =============================================================================
155 //
156 class StringConfig : public Config
157 {
158 IMPLEMENT_CONFIG (String)
159 };
160
161 // =============================================================================
162 //
163 class FloatConfig : public Config
164 {
165 IMPLEMENT_CONFIG (Float)
166 };
167
168 // =============================================================================
169 //
170 class BoolConfig : public Config
171 {
172 IMPLEMENT_CONFIG (Bool)
173 };
174
175 // =============================================================================
176 //
177 class KeySequenceConfig : public Config
178 {
179 IMPLEMENT_CONFIG (KeySequence)
180 };
181
182 // =============================================================================
183 //
184 class ListConfig : public Config
185 {
186 IMPLEMENT_CONFIG (List)
187 };
188
189 // =============================================================================
190 //
191 class VertexConfig : public Config
192 {
193 IMPLEMENT_CONFIG (Vertex)
194 };

mercurial