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
|
#include <QScopedPointer>
#include <QSettings>
#include "qmpsettingsro.hpp"
qmpSettingsRO::qmpSettingsRO()
{
}
void qmpSettingsRO::registerOptionInt(std::string tab,std::string desc,std::string key,int min,int max,int defaultval)
{
Q_UNUSED(tab)
optionlist.push_back(key);
options[key]=qmpOptionR(desc,qmpOptionR::ParameterType::parameter_int,defaultval,min,max);
}
int qmpSettingsRO::getOptionInt(std::string key)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_int)
return settings.value(QString(key.c_str()),options[key].defaultval).toInt();
return options[key].defaultval.toInt();
}
void qmpSettingsRO::setOptionInt(std::string key,int val)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_int)
settings.insert(QString(key.c_str()),val);
}
void qmpSettingsRO::registerOptionUint(std::string tab,std::string desc,std::string key,unsigned min,unsigned max,unsigned defaultval)
{
Q_UNUSED(tab)
optionlist.push_back(key);
options[key]=qmpOptionR(desc,qmpOptionR::ParameterType::parameter_uint,defaultval,min,max);
}
unsigned qmpSettingsRO::getOptionUint(std::string key)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_uint)
return settings.value(QString(key.c_str()),options[key].defaultval).toUInt();
return options[key].defaultval.toUInt();
}
void qmpSettingsRO::setOptionUint(std::string key,unsigned val)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_uint)
settings.insert(QString(key.c_str()),val);
}
void qmpSettingsRO::registerOptionBool(std::string tab,std::string desc,std::string key,bool defaultval)
{
Q_UNUSED(tab)
optionlist.push_back(key);
options[key]=qmpOptionR(desc,qmpOptionR::ParameterType::parameter_bool,defaultval);
}
bool qmpSettingsRO::getOptionBool(std::string key)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_bool)
return settings.value(QString(key.c_str()),options[key].defaultval).toBool();
return options[key].defaultval.toBool();
}
void qmpSettingsRO::setOptionBool(std::string key,bool val)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_bool)
settings.insert(QString(key.c_str()),val);
}
void qmpSettingsRO::registerOptionDouble(std::string tab,std::string desc,std::string key,double min,double max,double defaultval)
{
Q_UNUSED(tab)
optionlist.push_back(key);
options[key]=qmpOptionR(desc,qmpOptionR::ParameterType::parameter_double,defaultval,min,max);
}
double qmpSettingsRO::getOptionDouble(std::string key)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_double)
return settings.value(QString(key.c_str()),options[key].defaultval).toDouble();
return options[key].defaultval.toDouble();
}
void qmpSettingsRO::setOptionDouble(std::string key,double val)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_double)
settings.insert(QString(key.c_str()),val);
}
void qmpSettingsRO::registerOptionString(std::string tab,std::string desc,std::string key,std::string defaultval,bool is_url)
{
Q_UNUSED(tab)
optionlist.push_back(key);
options[key]=qmpOptionR(desc,
is_url?qmpOptionR::ParameterType::parameter_url:qmpOptionR::ParameterType::parameter_str,
QString(defaultval.c_str()));
}
std::string qmpSettingsRO::getOptionString(std::string key)
{
if(options.find(key)!=options.end()&&
(options[key].type==qmpOptionR::ParameterType::parameter_str||options[key].type==qmpOptionR::ParameterType::parameter_url))
return settings.value(QString(key.c_str()),options[key].defaultval).toString().toStdString();
return options[key].defaultval.toString().toStdString();
}
void qmpSettingsRO::setOptionString(std::string key,std::string val)
{
if(options.find(key)!=options.end()&&
(options[key].type==qmpOptionR::ParameterType::parameter_str||options[key].type==qmpOptionR::ParameterType::parameter_url))
settings.insert(QString(key.c_str()),QString(val.c_str()));
}
void qmpSettingsRO::registerOptionEnumInt(std::string tab,std::string desc,std::string key,std::vector<std::string> enumlist,int defaultval)
{
Q_UNUSED(tab)
optionlist.push_back(key);
options[key]=qmpOptionR(desc,qmpOptionR::ParameterType::parameter_enum,defaultval);
options[key].enumlist=enumlist;
}
int qmpSettingsRO::getOptionEnumInt(std::string key)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_enum)
{
std::string curitm=settings.value(QString(key.c_str()),options[key].defaultval).toString().toStdString();
auto curidx=std::find(options[key].enumlist.begin(),options[key].enumlist.end(),curitm);
if(curidx!=options[key].enumlist.end())
return static_cast<int>(curidx-options[key].enumlist.begin());
else
{
return options[key].defaultval.toInt();
}
}
return options[key].defaultval.toInt();
}
std::string qmpSettingsRO::getOptionEnumIntOptName(std::string key)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_enum)
{
std::string curitm=settings.value(QString(key.c_str()),options[key].defaultval).toString().toStdString();
auto curidx=std::find(options[key].enumlist.begin(),options[key].enumlist.end(),curitm);
if(curidx!=options[key].enumlist.end())
return curitm;
else
{
return options[key].enumlist[static_cast<size_t>(options[key].defaultval.toInt())];
}
}
return options[key].enumlist[static_cast<size_t>(options[key].defaultval.toInt())];
}
void qmpSettingsRO::setOptionEnumInt(std::string key,int val)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_enum)
{
if(static_cast<size_t>(val)<options[key].enumlist.size())
settings.insert(QString(key.c_str()),QString(options[key].enumlist[static_cast<size_t>(val)].c_str()));
}
}
void qmpSettingsRO::setOptionEnumIntOptName(std::string key,std::string valname)
{
if(options.find(key)!=options.end()&&options[key].type==qmpOptionR::ParameterType::parameter_enum)
{
auto curidx=std::find(options[key].enumlist.begin(),options[key].enumlist.end(),valname);
if(curidx!=options[key].enumlist.end())
settings.insert(QString(key.c_str()),QString(valname.c_str()));
}
}
void qmpSettingsRO::load(const char *path)
{
QScopedPointer<QSettings> qsettings(new QSettings(path,QSettings::Format::IniFormat));
settings.clear();
for(QString&k:qsettings->allKeys())
{
settings.insert(k,qsettings->value(k));
}
}
void qmpSettingsRO::setopt(std::string key, std::string val)
{
if(options.find(key)==options.end())
{
std::string nkey="Visualization/"+key;
if(options.find(nkey)==options.end())
{
qDebug("invalid option key %s",key.c_str());
return;
}
else key=nkey;
}
char *rptr;
switch(options[key].type)
{
case qmpOptionR::ParameterType::parameter_int:
{
long long v=strtoll(val.c_str(),&rptr,10);
if(rptr==val.c_str()||v>INT_MAX||v<INT_MIN)
qDebug("invalid value for option %s",key.c_str());
setOptionInt(key,static_cast<int>(v));
}
break;
case qmpOptionR::ParameterType::parameter_uint:
{
long long v=strtoll(val.c_str(),&rptr,10);
if(rptr==val.c_str()||v>UINT32_MAX||v<0)
qDebug("invalid value for option %s",key.c_str());
setOptionUint(key,static_cast<uint32_t>(v));
}
break;
case qmpOptionR::ParameterType::parameter_double:
{
errno=0;
double v=strtod(val.c_str(),&rptr);
if(rptr==val.c_str()||errno)
qDebug("invalid value for option %s",key.c_str());
setOptionDouble(key,v);
}
break;
case qmpOptionR::ParameterType::parameter_bool:
{
if(val!="true"&&val!="false")
qDebug("invalid value for option %s",key.c_str());
setOptionBool(key,val=="true");
}
break;
case qmpOptionR::ParameterType::parameter_str:
case qmpOptionR::ParameterType::parameter_url:
setOptionString(key,val);
break;
case qmpOptionR::ParameterType::parameter_enum:
setOptionEnumIntOptName(key,val);
break;
default:
break;
}
}
|