Allow true/false for boolean settings.

master
Torsten Paul 2015-01-10 00:54:13 +01:00
parent 670d9a0833
commit 1ae9ed5ada
1 changed files with 19 additions and 5 deletions

View File

@ -31,6 +31,7 @@
#include <QKeyEvent> #include <QKeyEvent>
#include <QSettings> #include <QSettings>
#include <QStatusBar> #include <QStatusBar>
#include <boost/algorithm/string.hpp>
#include "GeometryCache.h" #include "GeometryCache.h"
#include "AutoUpdater.h" #include "AutoUpdater.h"
#include "feature.h" #include "feature.h"
@ -49,18 +50,27 @@ class SettingsReader : public Settings::Visitor
{ {
QSettings settings; QSettings settings;
const Value getValue(const Settings::SettingsEntry& entry, const std::string& value) const { const Value getValue(const Settings::SettingsEntry& entry, const std::string& value) const {
if (value.empty()) { std::string trimmed_value(value);
boost::trim(trimmed_value);
if (trimmed_value.empty()) {
return entry.defaultValue(); return entry.defaultValue();
} }
try { try {
switch (entry.defaultValue().type()) { switch (entry.defaultValue().type()) {
case Value::STRING: case Value::STRING:
return Value(value); return Value(trimmed_value);
case Value::NUMBER: case Value::NUMBER:
return Value(boost::lexical_cast<int>(value)); return Value(boost::lexical_cast<int>(trimmed_value));
case Value::BOOL: case Value::BOOL:
return Value(boost::lexical_cast<bool>(value)); boost::to_lower(trimmed_value);
if ("false" == trimmed_value) {
return Value(false);
} else if ("true" == trimmed_value) {
return Value(true);
}
return Value(boost::lexical_cast<bool>(trimmed_value));
default: default:
assert(false && "invalid value type for settings"); assert(false && "invalid value type for settings");
} }
@ -74,7 +84,9 @@ class SettingsReader : public Settings::Visitor
std::string key = entry.category() + "/" + entry.name(); std::string key = entry.category() + "/" + entry.name();
std::string value = settings.value(QString::fromStdString(key)).toString().toStdString(); std::string value = settings.value(QString::fromStdString(key)).toString().toStdString();
s->set(entry, getValue(entry, value)); const Value v = getValue(entry, value);
PRINTDB("SettingsReader R: %s = '%s' => '%s'", key.c_str() % value.c_str() % v.toString());
s->set(entry, v);
} }
}; };
@ -87,9 +99,11 @@ class SettingsWriter : public Settings::Visitor
QString key = QString::fromStdString(entry.category() + "/" + entry.name()); QString key = QString::fromStdString(entry.category() + "/" + entry.name());
if (entry.is_default()) { if (entry.is_default()) {
settings.remove(key); settings.remove(key);
PRINTDB("SettingsWriter D: %s", key.toStdString().c_str());
} else { } else {
Value value = s->get(entry); Value value = s->get(entry);
settings.setValue(key, QString::fromStdString(value.toString())); settings.setValue(key, QString::fromStdString(value.toString()));
PRINTDB("SettingsWriter W: %s = '%s'", key.toStdString().c_str() % value.toString().c_str());
} }
} }
}; };