Fix persistence for global scope settings handles, clean up invalid variants in settings

This commit is contained in:
Bradley Austin Davis 2016-06-13 20:26:27 -07:00
parent 34c8d257d2
commit d52a1fe6f9
4 changed files with 46 additions and 18 deletions

View file

@ -77,15 +77,40 @@ namespace Setting {
virtual ~Handle() { deinit(); } virtual ~Handle() { deinit(); }
// Returns setting value, returns its default value if not found // Returns setting value, returns its default value if not found
T get() { return get(_defaultValue); } T get() const {
return get(_defaultValue);
}
// Returns setting value, returns other if not found // Returns setting value, returns other if not found
T get(const T& other) { maybeInit(); return (_isSet) ? _value : other; } T get(const T& other) const {
T getDefault() const { return _defaultValue; } maybeInit();
return (_isSet) ? _value : other;
}
const T& getDefault() const {
return _defaultValue;
}
void set(const T& value) { maybeInit(); _value = value; _isSet = true; } void reset() {
void reset() { set(_defaultValue); } set(_defaultValue);
}
void remove() { maybeInit(); _isSet = false; }
void set(const T& value) {
maybeInit();
if (_value != value) {
_value = value;
_isSet = true;
save();
}
}
void remove() {
maybeInit();
if (_isSet) {
_isSet = false;
save();
}
}
protected: protected:
virtual void setVariant(const QVariant& variant); virtual void setVariant(const QVariant& variant);

View file

@ -119,9 +119,9 @@ namespace Setting {
} }
void Interface::maybeInit() { void Interface::maybeInit() const {
if (!_isInitialized) { if (!_isInitialized) {
init(); const_cast<Interface*>(this)->init();
} }
} }

View file

@ -39,19 +39,20 @@ namespace Setting {
virtual ~Interface() = default; virtual ~Interface() = default;
void init(); void init();
void maybeInit(); void maybeInit() const;
void deinit(); void deinit();
void save(); void save();
void load(); void load();
bool _isInitialized = false;
bool _isSet = false; bool _isSet = false;
const QString _key; const QString _key;
private:
mutable bool _isInitialized = false;
friend class Manager; friend class Manager;
mutable QWeakPointer<Manager> _manager;
QWeakPointer<Manager> _manager;
}; };
} }

View file

@ -33,8 +33,8 @@ namespace Setting {
void Manager::customDeleter() { } void Manager::customDeleter() { }
void Manager::registerHandle(Setting::Interface* handle) { void Manager::registerHandle(Interface* handle) {
QString key = handle->getKey(); const QString& key = handle->getKey();
withWriteLock([&] { withWriteLock([&] {
if (_handles.contains(key)) { if (_handles.contains(key)) {
qWarning() << "Setting::Manager::registerHandle(): Key registered more than once, overriding: " << key; qWarning() << "Setting::Manager::registerHandle(): Key registered more than once, overriding: " << key;
@ -58,7 +58,9 @@ namespace Setting {
} else { } else {
loadedValue = value(key); loadedValue = value(key);
} }
handle->setVariant(loadedValue); if (loadedValue.isValid()) {
handle->setVariant(loadedValue);
}
}); });
} }
@ -101,7 +103,7 @@ namespace Setting {
if (newValue == savedValue) { if (newValue == savedValue) {
continue; continue;
} }
if (newValue == UNSET_VALUE) { if (newValue == UNSET_VALUE || !newValue.isValid()) {
remove(key); remove(key);
} else { } else {
setValue(key, newValue); setValue(key, newValue);