-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsettingseditor.cpp
337 lines (273 loc) · 9.7 KB
/
settingseditor.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "settingseditor.h"
#include "qtpropertymanager.h"
#include "qtvariantproperty.h"
#include "qttreepropertybrowser.h"
#include "tvseg/util/arrayio.h"
#include <boost/foreach.hpp>
#include <QVBoxLayout>
namespace tvseg_ui {
namespace {
using namespace tvseg::settings;
typedef QPair<QString, QVariant> AttributePair;
typedef QList<AttributePair> AttributeList;
QPair<QVariant, AttributeList> anyToVariant(const boost::any &value)
{
QVariant var;
AttributeList attributes;
// bool
if (var.isNull()) {
const bool* p = boost::any_cast<bool>(&value);
if (p) {
var = *p;
}
}
// std::string
if (var.isNull()) {
const std::string* p = boost::any_cast<std::string>(&value);
if (p) {
var = QString::fromStdString(*p);
}
}
// double
if (var.isNull()) {
const double* p = boost::any_cast<double>(&value);
if (p) {
var = *p;
}
}
// int
if (var.isNull()) {
const int* p = boost::any_cast<int>(&value);
if (p) {
var = *p;
}
}
// uint -> int
if (var.isNull()) {
const unsigned int* p = boost::any_cast<unsigned int>(&value);
if (p) {
var = static_cast<int>(*p);
attributes.append(AttributePair("minimum", 0));
}
}
// float -> double
if (var.isNull()) {
const float* p = boost::any_cast<float>(&value);
if (p) {
var = static_cast<double>(*p);
attributes.append(AttributePair("decimals", 8));
}
}
// std::vector<float> -> string
if (var.isNull()) {
const float_array_t *p = boost::any_cast<float_array_t>(&value);
if (p) {
std::ostringstream ss;
ss << *p;
var = QString::fromStdString(ss.str());
}
}
// std::vector<uint> -> string
if (var.isNull()) {
const uint_array_t *p = boost::any_cast<uint_array_t>(&value);
if (p) {
std::ostringstream ss;
ss << *p;
var = QString::fromStdString(ss.str());
}
}
return QPair<QVariant, AttributeList>(var, attributes);
}
bool variantAssignAny(boost::any &value, const QVariant &var)
{
// bool
if (boost::any_cast<bool>(&value)) {
if (var.type() == QVariant::Bool) {
value = var.toBool();
return true;
} else {
LWARNING << "variantAssignAny: variant type '" << var.typeName() << "' cannot be assigned to bool value";
return false;
}
}
// std::string
if (boost::any_cast<std::string>(&value)) {
if (var.type() == QVariant::String) {
value = var.toString().toStdString();
return true;
} else {
LWARNING << "variantAssignAny: variant type '" << var.typeName() << "' cannot be assigned to std::string value";
return false;
}
}
// double
if (boost::any_cast<double>(&value)) {
if (var.type() == QVariant::Double) {
value = var.toDouble();
return true;
} else {
LWARNING << "variantAssignAny: variant type '" << var.typeName() << "' cannot be assigned to double value";
return false;
}
}
// int
if (boost::any_cast<int>(&value)) {
if (var.type() == QVariant::Int) {
value = var.toInt();
return true;
} else {
LWARNING << "variantAssignAny: variant type '" << var.typeName() << "' cannot be assigned to int value";
return false;
}
}
// uint -> int
if (boost::any_cast<unsigned int>(&value)) {
if (var.type() == QVariant::Int) {
if (var.toInt() < 0) {
LWARNING << "variantAssignAny: variant value " << var.toInt() << " for unsigned int is negative. Using 0.";
}
value = static_cast<unsigned int>(std::max(0, var.toInt()));
return true;
} else {
LWARNING << "variantAssignAny: variant type '" << var.typeName() << "' cannot be assigned to unsigned int value";
return false;
}
}
// float -> double
if (boost::any_cast<float>(&value)) {
if (var.type() == QVariant::Double) {
value = static_cast<float>(var.toDouble());
return true;
} else {
LWARNING << "variantAssignAny: variant type '" << var.typeName() << "' cannot be assigned to float value";
return false;
}
}
// std::vector<float> -> string
if (boost::any_cast<float_array_t>(&value)) {
if (var.type() == QVariant::String) {
float_array_t tmp;
std::istringstream ss(var.toString().toStdString());
ss >> tmp;
value = tmp;
return true;
} else {
LWARNING << "variantAssignAny: variant type '" << var.typeName() << "' cannot be assigned to float_array_t value";
return false;
}
}
// std::vector<uint> -> string
if (boost::any_cast<uint_array_t>(&value)) {
if (var.type() == QVariant::String) {
uint_array_t tmp;
std::istringstream ss(var.toString().toStdString());
ss >> tmp;
value = tmp;
return true;
} else {
LWARNING << "variantAssignAny: variant type '" << var.typeName() << "' cannot be assigned to uint_array_t value";
return false;
}
}
// conversion not implemented
return false;
}
QtVariantProperty* createProperty(QtVariantPropertyManager* variantManager, SettingsEditor::EntryPtr entry, QString name)
{
QPair<QVariant, AttributeList> pair = anyToVariant(entry->value());
QVariant &var = pair.first;
AttributeList &attributes = pair.second;
// not implemented
if (var.isNull()) {
LWARNING << "Could not add property '" << name << "' of type '" << entry->value().type().name() << "' to editor. Conversion not implemented.";
return NULL;
}
QtVariantProperty* property = variantManager->addProperty(var.type(), name);
if (!property) {
LWARNING << "Could not add property '" << name << "' of type '" << entry->value().type().name() << "' to editor. Not supported by PropertyBrowser.";
return NULL;
}
property->setValue(var);
foreach(const AttributePair &p, attributes) {
variantManager->setAttribute(property, p.first, p.second);
}
return property;
}
} // namespace
SettingsEditor::SettingsEditor(QWidget *parent) :
QWidget(parent),
variantManager_(new QtVariantPropertyManager(this)),
variantFactory_(new QtVariantEditorFactory(this)),
variantEditor_(new QtTreePropertyBrowser(this))
{
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(variantEditor_);
layout->setContentsMargins(0,0,0,0);
setLayout(layout);
variantEditor_->setFactoryForManager(variantManager_, variantFactory_);
variantEditor_->setPropertiesWithoutValueMarked(true);
variantEditor_->setRootIsDecorated(false);
variantEditor_->setResizeMode(QtTreePropertyBrowser::Interactive);
connect(variantManager_, SIGNAL(valueChanged(QtProperty*,QVariant)), this, SLOT(updateEntry(QtProperty*,QVariant)));
connect(variantManager_, SIGNAL(valueChanged(QtProperty*,QVariant)), this, SIGNAL(valueChanged(QtProperty*,QVariant)));
}
QVector<QtProperty*> SettingsEditor::addEntries(const entry_map_type &entries, const std::string &groupName)
{
QtProperty *topItem = variantManager_->addProperty(QtVariantPropertyManager::groupTypeId(), QString::fromStdString(groupName));
QVector<QtProperty*> addedProperties;
BOOST_FOREACH(entry_map_type::value_type p, entries) {
std::string name = p.first;
EntryPtr entry = p.second;
QtVariantProperty* item = createProperty(variantManager_, entry, QString::fromStdString(name));
if (item) {
topItem->addSubProperty(item);
callbackEntryMap_[item] = entry;
addedProperties.push_back(item);
}
}
variantEditor_->addProperty(topItem);
return addedProperties;
}
void SettingsEditor::clear()
{
variantManager_->clear();
variantEditor_->clear();
callbackEntryMap_.clear();
}
void SettingsEditor::updateFromSettings()
{
foreach(QtVariantProperty* key, callbackEntryMap_.keys()) {
updateFromEntry(key, callbackEntryMap_[key]);
}
}
void SettingsEditor::updateFromEntry(QtVariantProperty *property, const EntryPtr &entry)
{
QPair<QVariant, AttributeList> pair = anyToVariant(entry->value());
QVariant &var = pair.first;
AttributeList &attributes = pair.second;
// not implemented
if (var.isNull()) {
LWARNING << "Could not update property '" << property->propertyName() << "' of type '" << entry->value().type().name() << "'. Conversion not implemented.";
return;
}
property->setValue(var);
foreach(const AttributePair &p, attributes) {
variantManager_->setAttribute(property, p.first, p.second);
}
}
void SettingsEditor::updateEntry(QtProperty *property, QVariant value)
{
QtVariantProperty *vp = static_cast<QtVariantProperty*>(property);
QMap<QtVariantProperty*, EntryPtr>::const_iterator iter = callbackEntryMap_.find(vp);
if (iter != callbackEntryMap_.end()) {
boost::any oldValue = iter.value()->value();
if (!variantAssignAny(oldValue, value)) {
LWARNING << "Cannot convert variant to any for setting'" << property->propertyName() << "'";
} else {
if (!iter.value()->setValue(oldValue)) {
LWARNING << "Cannot update entry from setting '" << property->propertyName() << "'";
}
}
}
}
} // namespace tvseg_ui