mirror of
https://thingvellir.net/git/overte
synced 2025-03-27 23:52:03 +01:00
Fix persistence for global scope settings handles, clean up invalid variants in settings
This commit is contained in:
parent
34c8d257d2
commit
d52a1fe6f9
4 changed files with 46 additions and 18 deletions
|
@ -77,15 +77,40 @@ namespace Setting {
|
|||
virtual ~Handle() { deinit(); }
|
||||
|
||||
// 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
|
||||
T get(const T& other) { maybeInit(); return (_isSet) ? _value : other; }
|
||||
T getDefault() const { return _defaultValue; }
|
||||
T get(const T& other) const {
|
||||
maybeInit();
|
||||
return (_isSet) ? _value : other;
|
||||
}
|
||||
|
||||
const T& getDefault() const {
|
||||
return _defaultValue;
|
||||
}
|
||||
|
||||
void set(const T& value) { maybeInit(); _value = value; _isSet = true; }
|
||||
void reset() { set(_defaultValue); }
|
||||
|
||||
void remove() { maybeInit(); _isSet = false; }
|
||||
void reset() {
|
||||
set(_defaultValue);
|
||||
}
|
||||
|
||||
void set(const T& value) {
|
||||
maybeInit();
|
||||
if (_value != value) {
|
||||
_value = value;
|
||||
_isSet = true;
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
void remove() {
|
||||
maybeInit();
|
||||
if (_isSet) {
|
||||
_isSet = false;
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void setVariant(const QVariant& variant);
|
||||
|
|
|
@ -119,9 +119,9 @@ namespace Setting {
|
|||
}
|
||||
|
||||
|
||||
void Interface::maybeInit() {
|
||||
void Interface::maybeInit() const {
|
||||
if (!_isInitialized) {
|
||||
init();
|
||||
const_cast<Interface*>(this)->init();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -39,19 +39,20 @@ namespace Setting {
|
|||
virtual ~Interface() = default;
|
||||
|
||||
void init();
|
||||
void maybeInit();
|
||||
void maybeInit() const;
|
||||
void deinit();
|
||||
|
||||
void save();
|
||||
void load();
|
||||
|
||||
bool _isInitialized = false;
|
||||
|
||||
bool _isSet = false;
|
||||
const QString _key;
|
||||
|
||||
private:
|
||||
mutable bool _isInitialized = false;
|
||||
|
||||
friend class Manager;
|
||||
|
||||
QWeakPointer<Manager> _manager;
|
||||
mutable QWeakPointer<Manager> _manager;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -33,8 +33,8 @@ namespace Setting {
|
|||
void Manager::customDeleter() { }
|
||||
|
||||
|
||||
void Manager::registerHandle(Setting::Interface* handle) {
|
||||
QString key = handle->getKey();
|
||||
void Manager::registerHandle(Interface* handle) {
|
||||
const QString& key = handle->getKey();
|
||||
withWriteLock([&] {
|
||||
if (_handles.contains(key)) {
|
||||
qWarning() << "Setting::Manager::registerHandle(): Key registered more than once, overriding: " << key;
|
||||
|
@ -58,7 +58,9 @@ namespace Setting {
|
|||
} else {
|
||||
loadedValue = value(key);
|
||||
}
|
||||
handle->setVariant(loadedValue);
|
||||
if (loadedValue.isValid()) {
|
||||
handle->setVariant(loadedValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -101,7 +103,7 @@ namespace Setting {
|
|||
if (newValue == savedValue) {
|
||||
continue;
|
||||
}
|
||||
if (newValue == UNSET_VALUE) {
|
||||
if (newValue == UNSET_VALUE || !newValue.isValid()) {
|
||||
remove(key);
|
||||
} else {
|
||||
setValue(key, newValue);
|
||||
|
|
Loading…
Reference in a new issue