src/configuration.h

changeset 971
c00f9665a9f8
parent 970
c8aae45afd85
child 972
a34b73114823
equal deleted inserted replaced
970:c8aae45afd85 971:c00f9665a9f8
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 - 2015 Teemu 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 class AbstractConfigEntry;
28
29 namespace Config
30 {
31 void Initialize();
32 bool Load();
33 bool Save();
34 void ResetToDefaults();
35 QString DirectoryPath();
36 QString FilePath (QString file);
37 QSettings* SettingsObject();
38 QList<AbstractConfigEntry*> const& AllConfigEntries();
39 AbstractConfigEntry* FindByName (QString const& name);
40 }
41
42 class AbstractConfigEntry
43 {
44 PROPERTY (private, QString, name, setName, STOCK_WRITE)
45
46 public:
47 enum Type
48 {
49 EIntType,
50 EStringType,
51 EFloatType,
52 EBoolType,
53 EKeySequenceType,
54 EListType,
55 EVertexType,
56 };
57
58 using IntType = int;
59 using StringType = QString;
60 using FloatType = float;
61 using BoolType = bool;
62 using KeySequenceType = QKeySequence;
63 using ListType = QList<QVariant>;
64 using VertexType = Vertex;
65
66 AbstractConfigEntry (QString name);
67
68 virtual QVariant getDefaultAsVariant() const = 0;
69 virtual Type getType() const = 0;
70 virtual bool isDefault() const = 0;
71 virtual void loadFromVariant (const QVariant& val) = 0;
72 virtual void resetValue() = 0;
73 virtual QVariant toVariant() const = 0;
74 };
75
76 #define IMPLEMENT_CONFIG(NAME) \
77 public: \
78 using ValueType = AbstractConfigEntry::NAME##Type; \
79 \
80 NAME##ConfigEntry (ValueType* valueptr, QString name, ValueType def) : \
81 AbstractConfigEntry (name), \
82 m_valueptr (valueptr), \
83 m_default (def) \
84 { \
85 *m_valueptr = def; \
86 } \
87 \
88 inline ValueType getValue() const \
89 { \
90 return *m_valueptr; \
91 } \
92 \
93 inline void setValue (ValueType val) \
94 { \
95 *m_valueptr = val; \
96 } \
97 \
98 virtual AbstractConfigEntry::Type getType() const \
99 { \
100 return AbstractConfigEntry::E##NAME##Type; \
101 } \
102 \
103 virtual void resetValue() \
104 { \
105 *m_valueptr = m_default; \
106 } \
107 \
108 virtual const ValueType& getDefault() const \
109 { \
110 return m_default; \
111 } \
112 \
113 virtual bool isDefault() const \
114 { \
115 return *m_valueptr == m_default; \
116 } \
117 \
118 virtual void loadFromVariant (const QVariant& val); \
119 \
120 virtual QVariant toVariant() const \
121 { \
122 return QVariant::fromValue<ValueType> (*m_valueptr); \
123 } \
124 \
125 virtual QVariant getDefaultAsVariant() const \
126 { \
127 return QVariant::fromValue<ValueType> (m_default); \
128 } \
129 \
130 static NAME##ConfigEntry* getByName (QString name); \
131 \
132 private: \
133 ValueType* m_valueptr; \
134 ValueType m_default;
135
136 class IntConfigEntry : public AbstractConfigEntry
137 {
138 IMPLEMENT_CONFIG (Int)
139 };
140
141 class StringConfigEntry : public AbstractConfigEntry
142 {
143 IMPLEMENT_CONFIG (String)
144 };
145
146 class FloatConfigEntry : public AbstractConfigEntry
147 {
148 IMPLEMENT_CONFIG (Float)
149 };
150
151 class BoolConfigEntry : public AbstractConfigEntry
152 {
153 IMPLEMENT_CONFIG (Bool)
154 };
155
156 class KeySequenceConfigEntry : public AbstractConfigEntry
157 {
158 IMPLEMENT_CONFIG (KeySequence)
159 };
160
161 class ListConfigEntry : public AbstractConfigEntry
162 {
163 IMPLEMENT_CONFIG (List)
164 };
165
166 class VertexConfigEntry : public AbstractConfigEntry
167 {
168 IMPLEMENT_CONFIG (Vertex)
169 };

mercurial