1 #ifndef QTMVVM_SETTINGSENTRY_H 2 #define QTMVVM_SETTINGSENTRY_H 6 #include <QtCore/qlist.h> 7 #include <QtCore/qregularexpression.h> 8 #include <QtCore/qhash.h> 9 #include <QtCore/qsharedpointer.h> 11 #include "QtMvvmCore/qtmvvmcore_global.h" 12 #include "QtMvvmCore/isettingsaccessor.h" 33 void set(
const T &value);
70 template <
typename TType>
89 inline TType &
element() {
return _element; }
97 inline void commit() {
if(!_commited) { _commited =
true; _node->commit(_index);} }
104 bool _commited =
false;
115 template <
typename T>
120 using iterator_category = std::random_access_iterator_tag;
122 using difference_type = int;
124 using value_type = T;
126 using pointer = value_type*;
128 using reference = value_type&;
151 inline reference
operator*()
const {
return _node->at(_index); }
153 inline pointer
operator->()
const {
return &_node->at(_index); }
162 inline friend bool operator<(const iterator_base<T> &lhs,
const iterator_base<T> &rhs) { assert(lhs._node == rhs._node);
return lhs._index < rhs._index; }
166 inline friend bool operator<=(const iterator_base<T> &lhs,
const iterator_base<T> &rhs) { assert(lhs._node == rhs._node);
return lhs._index <= rhs._index; }
184 inline reference
operator[](difference_type delta)
const {
return _node->at(_index + delta); }
212 const TType &
at(
int index)
const;
214 TType &
at(
int index);
221 void pop(
int count = 1);
251 void commit(
int index);
254 struct ElementHolder {
255 bool __initialized =
false;
257 inline ElementHolder() : _element{
new TType{}} {}
262 ISettingsAccessor *_accessor =
nullptr;
263 std::function<void(int, TType&)> _setupFn;
272 return _accessor->contains(_key);
284 return _accessor->load(_key, _default).template value<T>();
302 _accessor->remove(_key);
321 addChangeCallback(_accessor, callback);
328 auto mDefault = _default;
332 callback(value.template value<T>());
335 scope, [mKey, mDefault, callback](
const QString &key) {
337 callback(mDefault.template value<T>());
347 Q_ASSERT_X(accessor, Q_FUNC_INFO,
"You must set a valid accessor before initializing the settings!");
348 _key = std::move(key);
349 _accessor = accessor;
350 _default = std::move(defaultValue);
355 template<
typename TType>
358 return _accessor->contains(_sizeKey);
361 template<
typename TType>
367 template<
typename TType>
370 return _accessor->load(_sizeKey, 0).toInt();
373 template<
typename TType>
376 auto &value = _cache[index];
377 if(!value.__initialized) {
378 _setupFn(index, *(value._element));
379 value.__initialized =
true;
381 return *(value._element);
384 template<
typename TType>
387 auto &value = _cache[index];
388 if(!value.__initialized) {
389 _setupFn(index, *(value._element));
390 value.__initialized =
true;
392 return *(value._element);
395 template<
typename TType>
398 auto cIndex = size();
399 _accessor->save(_sizeKey, cIndex + 1);
403 template<
typename TType>
406 auto cIndex = size();
407 return {
this, at(cIndex), cIndex};
410 template<
typename TType>
414 auto nSize = qMax(size() - count, 0);
415 for(
auto i = cSize - 1; i >= nSize; --i)
416 _accessor->remove(_key + QStringLiteral(
"/%1").arg(i));
417 _accessor->save(_sizeKey, nSize);
420 template<
typename TType>
423 _accessor->remove(_key);
426 template<
typename TType>
433 template<
typename TType>
439 template<
typename TType>
445 template<
typename TType>
451 template<
typename TType>
457 template<
typename TType>
463 template<
typename TType>
469 template<
typename TType>
475 template<
typename TType>
478 addChangeCallback(_accessor, callback);
481 template<
typename TType>
488 callback(value.
toInt());
491 scope, [mKey, callback](
const QString &key) {
497 template<
typename TType>
500 Q_ASSERT_X(accessor, Q_FUNC_INFO,
"You must set a valid accessor before initializing the settings!");
501 _key = std::move(key);
502 _sizeKey = _key + QStringLiteral(
"/size");
503 _accessor = accessor;
504 _setupFn = std::move(setupFn);
507 template<
typename TType>
508 void SettingsListNode<TType>::commit(
int index)
510 _accessor->save(_sizeKey, qMax(size(), index + 1));
516 #endif // QTMVVM_SETTINGSENTRY_H pointer operator->() const
Member access operator.
void reset()
Removes the list and all of its elements from the settings.
T get() const
Loads the value from the settings.
Entry container to make deferred creation of a new list element possible.
const_iterator constEnd() const
returns an iterator pointing after the end of the list
void addChangeCallback(const std::function< void(T)> &callback)
Adds a method to be called if the entries value changes.
An interface to provide a generic way to access settings of any origin.
iterator_base< T > & operator--()
Posfix decrement operator.
iterator_base & operator=(const iterator_base< T > &other)=default
Copy assignment operator.
Deferred(Deferred &&other) noexcept=default
Move constructor.
friend bool operator!=(const iterator_base< T > &lhs, const iterator_base< T > &rhs)
Inequality operator.
bool isSet() const
Checks if the entry is stored in the settings.
TType & operator[](int index)
Returns a reference to the element at the given position in the list.
TType & push()
Appends a new element to the list and returns a reference to that entry.
friend difference_type operator-(const iterator_base< T > &lhs, const iterator_base< T > &rhs)
Substraction operator.
QString key() const
Returns the key this entry uses to access the settings.
const_iterator constBegin() const
returns an iterator pointing to the beginning of the list
void commit()
Appends the created element to the list.
TType * operator->()
Member access operator.
friend bool operator>(const iterator_base< T > &lhs, const iterator_base< T > &rhs)
Geater than operator.
void addChangeCallback(const std::function< void(int)> &callback)
Adds a method to be called if the number of elements stored for the list change.
friend iterator_base< T > operator+(difference_type delta, const iterator_base< T > &iter)
Addition operator.
Deferred & operator=(Deferred &&other) noexcept=default
Move assignment operator.
friend bool operator==(const iterator_base< T > &lhs, const iterator_base< T > &rhs)
Equality operator.
bool isSet() const
Checks if the entry that represents the list node is stored in the settings.
int toInt(bool *ok) const const
friend void swap(iterator_base< T > &lhs, iterator_base< T > &rhs) noexcept
Swap operator.
friend bool operator>=(const iterator_base< T > &lhs, const iterator_base< T > &rhs)
Geater equal than operator.
const TType & at(int index) const
Returns a reference to the element at the given position in the list.
A generic wrapper around ISettingsAccessor used by the qsettingsgenerator.
iterator end()
returns an iterator pointing after the end of the list
iterator_base< T > & operator++()
Posfix increment operator.
QString key() const
Returns the key this list node uses to access the settings.
iterator begin()
returns an iterator pointing to the beginning of the list
iterator_base< T > & operator+=(difference_type delta)
Compound assignment operator.
friend iterator_base< T > operator-(const iterator_base< T > &iter, difference_type delta)
Substraction operator.
QVariant fromValue(const T &value)
iterator_base< T > operator--(int)
Suffix decrement operator.
void reset()
Removes the entry and all child entries from the settings.
void set(const T &value)
Stores the passed value in the settings.
Deferred push_deferred()
Creates a new element and returns it, but does not push it yet.
iterator_base()=default
Default constructor.
void pop(int count=1)
Removes count elements from the back of the list from the settings.
TType & element()
Returns a reference to the element beeing edited.
iterator_base< T > & operator-=(difference_type delta)
Compound assignment operator.
void entryRemoved(const QString &key)
Is emitted whenever a settings value was removed, at least via this instance.
int size() const
Returns the current size of the list, as stored in the settings.
iterator_base< T > operator++(int)
Suffix increment operator.
friend iterator_base< T > operator+(const iterator_base< T > &iter, difference_type delta)
Addition operator.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
The primary namespace of the QtMvvm library.
TType & operator*()
Indirection operator.
The iterator class to iterate through the list nodes entries.
reference operator*() const
Indirection operator.
SettingsEntry< T > & operator=(const T &value)
Stores the passed value in the settings.
void entryChanged(const QString &key, const QVariant &value)
Is emitted whenever a settings value was changed, at least via this instance.
reference operator[](difference_type delta) const
Subscript operator.
A helper class used by the qsettingsgenerator to represent list nodes.