-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmainsettings.cpp
297 lines (255 loc) · 11.8 KB
/
cmainsettings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "cmainsettings.h"
#include <map>
#include <QSettings>
#include <QHostAddress>
#include "cabstractguisettings.h"
#include "BotSocket/cabstractbotsocketsettings.h"
static const char *GUI_PREFIX = "GUI";
enum EN_GuiKeys
{
ENGK_FIRST = 0,
ENGK_MSAA = ENGK_FIRST,
//The LaserBot
ENGK_BOT_ANCHOR_X,
ENGK_BOT_ANCHOR_Y,
ENGK_BOT_ANCHOR_Z,
ENGK_BOT_LASER_X,
ENGK_BOT_LASER_Y,
ENGK_BOT_LASER_Z,
ENGK_BOT_COORD_TYPE,
//The Part
ENGK_PART_TR_X,
ENGK_PART_TR_Y,
ENGK_PART_TR_Z,
ENGK_PART_CENTER_X,
ENGK_PART_CENTER_Y,
ENGK_PART_CENTER_Z,
ENGK_PART_ROTATE_X,
ENGK_PART_ROTATE_Y,
ENGK_PART_ROTATE_Z,
ENGK_PART_SCALE,
//The Grip
ENGK_GRIP_VISIBLE,
ENGK_GRIP_TR_X,
ENGK_GRIP_TR_Y,
ENGK_GRIP_TR_Z,
ENGK_GRIP_CENTER_X,
ENGK_GRIP_CENTER_Y,
ENGK_GRIP_CENTER_Z,
ENGK_GRIP_ROTATE_X,
ENGK_GRIP_ROTATE_Y,
ENGK_GRIP_ROTATE_Z,
ENGK_GRIP_SCALE,
ENGK_LAST
};
typedef int TGuiKey;
static const std::map <TGuiKey, QString> guiKeyMap = {
{ ENGK_MSAA , "msaa" },
//The LaserBot
{ ENGK_BOT_ANCHOR_X , "bot_anch_x" },
{ ENGK_BOT_ANCHOR_Y , "bot_anch_y" },
{ ENGK_BOT_ANCHOR_Z , "bot_anch_z" },
{ ENGK_BOT_LASER_X , "bot_lsr_x" },
{ ENGK_BOT_LASER_Y , "bot_lsr_y" },
{ ENGK_BOT_LASER_Z , "bot_lsr_z" },
{ ENGK_BOT_COORD_TYPE, "bot_coord_type" },
//The Part
{ ENGK_PART_TR_X , "part_tr_x" },
{ ENGK_PART_TR_Y , "part_tr_y" },
{ ENGK_PART_TR_Z , "part_tr_z" },
{ ENGK_PART_CENTER_X , "part_center_x" },
{ ENGK_PART_CENTER_Y , "part_center_y" },
{ ENGK_PART_CENTER_Z , "part_center_z" },
{ ENGK_PART_ROTATE_X , "part_rotate_x" },
{ ENGK_PART_ROTATE_Y , "part_rotate_y" },
{ ENGK_PART_ROTATE_Z , "part_rotate_z" },
{ ENGK_PART_SCALE , "part_scale" },
//The Grip
{ ENGK_GRIP_VISIBLE , "grip_visible" },
{ ENGK_GRIP_TR_X , "grip_tr_x" },
{ ENGK_GRIP_TR_Y , "grip_tr_y" },
{ ENGK_GRIP_TR_Z , "grip_tr_z" },
{ ENGK_GRIP_CENTER_X , "grip_center_x" },
{ ENGK_GRIP_CENTER_Y , "grip_center_y" },
{ ENGK_GRIP_CENTER_Z , "grip_center_z" },
{ ENGK_GRIP_ROTATE_X , "grip_rotate_x" },
{ ENGK_GRIP_ROTATE_Y , "grip_rotate_y" },
{ ENGK_GRIP_ROTATE_Z , "grip_rotate_z" },
{ ENGK_GRIP_SCALE , "grip_scale" },
};
static const char *BOT_PREFIX = "BOT";
static const char *BOT_KEY_LCL_IP = "lcl_ip";
static const char *BOT_KEY_LCL_PORT = "lcl_port";
static const char *BOT_KEY_REM_IP = "rem_ip";
static const char *BOT_KEY_REM_PORT = "rem_port";
static const char *BOT_DEF_LCL_IP = "192.168.0.1";
static const uint16_t BOT_DEF_LCL_PORT = 1122;
static const char *BOT_DEF_REM_IP = "192.168.0.2";
static const uint16_t BOT_DEF_REM_PORT = 2211;
class CMainSettingsPrivate : public CAbstractGuiSettings, public CAbstractBotSocketSettings
{
friend class CMainSettings;
public:
CMainSettingsPrivate() :
CAbstractGuiSettings(),
CAbstractBotSocketSettings(),
settings(new QSettings()) {
}
~CMainSettingsPrivate() {
delete settings;
}
//CAbstractGuiSettings
//CAbstractGuiSettings
GUI_TYPES::TMSAA getMsaa() const final { return readGuiValue <GUI_TYPES::TMSAA> (ENGK_MSAA); }
void setMsaa(const GUI_TYPES::TMSAA value) final { writeGuiValue(ENGK_MSAA, value); }
//The LaserBot
double getBotAnchorX() const final { return readGuiValue <double> (ENGK_BOT_ANCHOR_X); }
double getBotAnchorY() const final { return readGuiValue <double> (ENGK_BOT_ANCHOR_Y); }
double getBotAnchorZ() const final { return readGuiValue <double> (ENGK_BOT_ANCHOR_Z); }
double getBotLaserX() const final { return readGuiValue <double> (ENGK_BOT_LASER_X); }
double getBotLaserY() const final { return readGuiValue <double> (ENGK_BOT_LASER_Y); }
double getBotLaserZ() const final { return readGuiValue <double> (ENGK_BOT_LASER_Z); }
GUI_TYPES::TCoordSystem getBotCoordType() const final {
return readGuiValue <GUI_TYPES::TCoordSystem> (ENGK_BOT_COORD_TYPE);
}
void setBotAnchorX(const double value) final { writeGuiValue(ENGK_BOT_ANCHOR_X, value); }
void setBotAnchorY(const double value) final { writeGuiValue(ENGK_BOT_ANCHOR_Y, value); }
void setBotAnchorZ(const double value) final { writeGuiValue(ENGK_BOT_ANCHOR_Z, value); }
void setBotLaserX(const double value) final { writeGuiValue(ENGK_BOT_LASER_X, value); }
void setBotLaserY(const double value) final { writeGuiValue(ENGK_BOT_LASER_Y, value); }
void setBotLaserZ(const double value) final { writeGuiValue(ENGK_BOT_LASER_Z, value); }
void setBotCoordType(const GUI_TYPES::TCoordSystem value) final {
writeGuiValue(ENGK_BOT_COORD_TYPE, value);
}
//The Part
double getPartTrX() const final { return readGuiValue <double> (ENGK_PART_TR_X); }
double getPartTrY() const final { return readGuiValue <double> (ENGK_PART_TR_Y); }
double getPartTrZ() const final { return readGuiValue <double> (ENGK_PART_TR_Z); }
double getPartCenterX() const final { return readGuiValue <double> (ENGK_PART_CENTER_X); }
double getPartCenterY() const final { return readGuiValue <double> (ENGK_PART_CENTER_Y); }
double getPartCenterZ() const final { return readGuiValue <double> (ENGK_PART_CENTER_Z); }
double getPartRotationX() const final { return readGuiValue <double> (ENGK_PART_ROTATE_X); }
double getPartRotationY() const final { return readGuiValue <double> (ENGK_PART_ROTATE_Y); }
double getPartRotationZ() const final { return readGuiValue <double> (ENGK_PART_ROTATE_Z); }
double getPartScale() const final { return readGuiValue <double> (ENGK_PART_SCALE); }
void setPartTrX(const double value) final { writeGuiValue(ENGK_PART_TR_X, value); }
void setPartTrY(const double value) final { writeGuiValue(ENGK_PART_TR_Y, value); }
void setPartTrZ(const double value) final { writeGuiValue(ENGK_PART_TR_Z, value); }
void setPartCenterX(const double value) final { writeGuiValue(ENGK_PART_CENTER_X, value); }
void setPartCenterY(const double value) final { writeGuiValue(ENGK_PART_CENTER_Y, value); }
void setPartCenterZ(const double value) final { writeGuiValue(ENGK_PART_CENTER_Z, value); }
void setPartRotationX(const double value) final { writeGuiValue(ENGK_PART_ROTATE_X, value); }
void setPartRotationY(const double value) final { writeGuiValue(ENGK_PART_ROTATE_Y, value); }
void setPartRotationZ(const double value) final { writeGuiValue(ENGK_PART_ROTATE_Z, value); }
void setPartScale(const double value) final { writeGuiValue(ENGK_PART_SCALE, value); }
//The Grip
bool isGripVisible() const final { return readGuiValue <bool> (ENGK_GRIP_VISIBLE); }
double getGripTrX() const final { return readGuiValue <double> (ENGK_GRIP_TR_X); }
double getGripTrY() const final { return readGuiValue <double> (ENGK_GRIP_TR_Y); }
double getGripTrZ() const final { return readGuiValue <double> (ENGK_GRIP_TR_Z); }
double getGripCenterX() const final { return readGuiValue <double> (ENGK_GRIP_CENTER_X); }
double getGripCenterY() const final { return readGuiValue <double> (ENGK_GRIP_CENTER_Y); }
double getGripCenterZ() const final { return readGuiValue <double> (ENGK_GRIP_CENTER_Z); }
double getGripRotationX() const final { return readGuiValue <double> (ENGK_GRIP_ROTATE_X); }
double getGripRotationY() const final { return readGuiValue <double> (ENGK_GRIP_ROTATE_Y); }
double getGripRotationZ() const final { return readGuiValue <double> (ENGK_GRIP_ROTATE_Z); }
double getGripScale() const final { return readGuiValue <double> (ENGK_GRIP_SCALE); }
void setGripVisible(const bool value) final { writeGuiValue(ENGK_GRIP_VISIBLE, value); }
void setGripTrX(const double value) final { writeGuiValue(ENGK_GRIP_TR_X, value); }
void setGripTrY(const double value) final { writeGuiValue(ENGK_GRIP_TR_Y, value); }
void setGripTrZ(const double value) final { writeGuiValue(ENGK_GRIP_TR_Z, value); }
void setGripCenterX(const double value) final { writeGuiValue(ENGK_GRIP_CENTER_X, value); }
void setGripCenterY(const double value) final { writeGuiValue(ENGK_GRIP_CENTER_Y, value); }
void setGripCenterZ(const double value) final { writeGuiValue(ENGK_GRIP_CENTER_Z, value); }
void setGripRotationX(const double value) final { writeGuiValue(ENGK_GRIP_ROTATE_X, value); }
void setGripRotationY(const double value) final { writeGuiValue(ENGK_GRIP_ROTATE_Y, value); }
void setGripRotationZ(const double value) final { writeGuiValue(ENGK_GRIP_ROTATE_Z, value); }
void setGripScale(const double value) final { writeGuiValue(ENGK_GRIP_SCALE, value); }
//CAbstractBotSocketSettings
uint32_t getLocalIpV4() const { return readIp(BOT_PREFIX, BOT_KEY_LCL_IP); }
uint16_t getLocalUdpPort() const { return readIp(BOT_PREFIX, BOT_KEY_LCL_PORT); }
uint32_t getRemoteBotIpV4() const { return readIp(BOT_PREFIX, BOT_KEY_REM_IP); }
uint16_t getRemoteBotUdpPort() const { return readIp(BOT_PREFIX, BOT_KEY_REM_PORT); }
private:
template <typename T>
T readGuiValue(const TGuiKey key) const {
T result = T();
auto it = guiKeyMap.find(key);
if (it != guiKeyMap.cend()) {
settings->beginGroup(GUI_PREFIX);
result = settings->value(it->second).value <T> ();
settings->endGroup();
}
return result;
}
template <typename T>
void writeGuiValue(const TGuiKey key, const T value) {
auto it = guiKeyMap.find(key);
if (it != guiKeyMap.cend()) {
settings->beginGroup(GUI_PREFIX);
settings->setValue(it->second, QVariant::fromValue <T> (value));
settings->endGroup();
}
}
uint32_t readIp(const char *group, const char *key) const {
QString txt;
settings->beginGroup(group);
txt = settings->value(key).toString();
settings->endGroup();
return QHostAddress(txt).toIPv4Address();
}
void writeIp(const char *group, const char *key, const uint32_t ip) {
settings->beginGroup(group);
settings->setValue(key, QHostAddress(ip).toString());
settings->endGroup();
}
uint16_t readPort(const char *group, const char *key) const {
uint16_t result = 0;
settings->beginGroup(group);
result = static_cast <uint16_t> (settings->value(key).toUInt());
settings->endGroup();
return result;
}
void writePort(const char *group, const char *key, const uint16_t port) {
settings->beginGroup(group);
settings->setValue(key, port);
settings->endGroup();
}
private:
QSettings *settings;
};
CMainSettings::CMainSettings() :
d_ptr(new CMainSettingsPrivate())
{
}
CMainSettings::~CMainSettings()
{
delete d_ptr;
}
void CMainSettings::setSettingsFName(const char *fname)
{
delete d_ptr->settings;
d_ptr->settings = new QSettings(QString::fromLocal8Bit(fname), QSettings::IniFormat);
d_ptr->settings->beginGroup(BOT_PREFIX);
if (!d_ptr->settings->contains(BOT_KEY_LCL_IP))
d_ptr->settings->setValue(BOT_KEY_LCL_IP, BOT_DEF_LCL_IP);
if (!d_ptr->settings->contains(BOT_KEY_LCL_PORT))
d_ptr->settings->setValue(BOT_KEY_LCL_PORT, BOT_DEF_LCL_PORT);
if (!d_ptr->settings->contains(BOT_KEY_REM_IP))
d_ptr->settings->setValue(BOT_KEY_REM_IP, BOT_DEF_REM_IP);
if (!d_ptr->settings->contains(BOT_KEY_REM_PORT))
d_ptr->settings->setValue(BOT_KEY_REM_PORT, BOT_DEF_REM_PORT);
d_ptr->settings->endGroup();
d_ptr->settings->beginGroup(GUI_PREFIX);
if (!d_ptr->settings->contains(guiKeyMap.at(ENGK_MSAA)))
d_ptr->settings->setValue(guiKeyMap.at(ENGK_MSAA), GUI_TYPES::ENMSAA_4);
d_ptr->settings->endGroup();
}
CAbstractGuiSettings &CMainSettings::guiSettings()
{
return *d_ptr;
}
CAbstractBotSocketSettings &CMainSettings::socketSettings()
{
return *d_ptr;
}