24#include <QRegularExpression>
27using namespace Qt::StringLiterals;
30 : mParentTreeElement(
parent )
32 , mDefaultValue( defaultValue )
36 mKey = QDir::cleanPath( u
"%1/%2"_s.arg(
parent ?
parent->completeKey() : QString(),
key ) );
40 parent->registerChildSetting(
this,
key );
46 if ( mParentTreeElement )
47 mParentTreeElement->unregisterChildSetting(
this );
52 return QString::fromUtf8( sSettingsTypeMetaEnum.valueToKey(
static_cast<int>(
settingsType() ) ) );
63 return completeKeyPrivate( mKey, dynamicKeyPartList );
66QString QgsSettingsEntryBase::completeKeyPrivate(
const QString &key,
const QStringList &dynamicKeyPartList )
const
68 QString completeKey =
key;
70 if ( dynamicKeyPartList.isEmpty() )
73 QgsDebugError( u
"Settings '%1' have a dynamic key but the dynamic key part was not provided"_s.arg( completeKey ) );
81 QgsDebugError( u
"Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored"_s.arg( completeKey ) );
85 for (
int i = 0; i < dynamicKeyPartList.size(); i++ )
87 completeKey.replace( u
"%"_s.append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
103 const thread_local QRegularExpression digitRx( u
"%\\d+"_s );
104 const QRegularExpression regularExpression(
definitionKey().replace( digitRx, u
".*"_s ) );
105 const QRegularExpressionMatch regularExpressionMatch = regularExpression.match(
key );
106 return regularExpressionMatch.hasMatch();
116 const thread_local QRegularExpression regularExpression( u
"%\\d+"_s );
117 return mKey.contains( regularExpression );
161 if (
exists( dynamicKeyPartList ) )
164 if ( value != currentValue )
166 settings->setValue( formerValuekey( dynamicKeyPartList ), currentValue );
170 settings->setValue(
key( dynamicKeyPartList ), value );
176 QStringList dynamicKeyPartList;
177 if ( !dynamicKeyPart.isNull() )
178 dynamicKeyPartList.append( dynamicKeyPart );
179 return dynamicKeyPartList;
201 if ( useDefaultValueOverride )
219 return mDefaultValue;
243 const QString oldCompleteKey = completeKeyPrivate(
key, dynamicKeyPartList );
245 if ( settings->contains( oldCompleteKey ) )
247 if ( !
exists( dynamicKeyPartList ) )
249 QVariant oldValue = settings->value( oldCompleteKey, mDefaultValue );
254 if ( removeSettingAtKey )
255 settings->remove( oldCompleteKey );
264 const QString completeKey = completeKeyPrivate(
key, dynamicKeyPartList );
276QString QgsSettingsEntryBase::formerValuekey(
const QStringList &dynamicKeyPartList )
const
278 return key( dynamicKeyPartList ) + u
"_formervalue"_s;
@ SaveFormerValue
Save the former value of the settings.
QFlags< SettingsOption > SettingsOptions
SettingsOrigin
The setting origin describes where a setting is stored.
virtual QString typeId() const
Returns the id of the type of settings This can be re-implemented in a custom implementation of a set...
bool copyValueFromKey(const QString &key, bool removeSettingAtKey=false) const
Copies the value from a given key if it exists.
bool exists(const QString &dynamicKeyPart=QString()) const
Returns true if the settings is contained in the underlying QSettings.
Qgis::SettingsOptions options() const
Returns the settings options.
bool hasChanged() const
Returns true if the setting was changed during the current QGIS session.
QVariant defaultValueAsVariant() const
Returns settings default value.
bool keyIsValid(const QString &key) const
Returns true if the provided key match the settings entry.
QString description() const
Returns the settings entry description.
QgsSettingsTreeNode * parent() const
Returns the parent tree element.
void copyValueToKeyIfChanged(const QString &key, const QStringList &dynamicKeyPartList=QStringList()) const
Copies the settings to the given key, if it has changed during the current QGIS session (see hasChang...
virtual ~QgsSettingsEntryBase()
Q_DECL_DEPRECATED int section() const
Returns settings section.
Qgis::SettingsOrigin origin(const QStringList &dynamicKeyPartList) const
Returns the origin of the setting if it exists.
bool hasDynamicKey() const
Returns true if a part of the settings key is built dynamically.
void remove(const QString &dynamicKeyPart=QString()) const
Removes the settings from the underlying QSettings.
bool setVariantValue(const QVariant &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
QString definitionKey() const
Returns settings entry defining key.
QVariant formerValueAsVariant(const QString &dynamicKeyPart) const
Returns the former value of the settings if it has been enabled in the options.
QVariant valueAsVariant(const QString &dynamicKeyPart=QString()) const
Returns settings value with.
void copyValueToKey(const QString &key, const QStringList &dynamicKeyPartList=QStringList()) const
Copies the settings to the given key.
QString key(const QString &dynamicKeyPart=QString()) const
Returns settings entry key.
static QStringList dynamicKeyPartToList(const QString &dynamicKeyPart)
Transforms a dynamic key part string to list.
QVariant valueAsVariantWithDefaultOverride(const QVariant &defaultValueOverride, const QString &dynamicKeyPart=QString()) const
Returns settings value with a defaultValueOverride.
virtual Qgis::SettingsType settingsType() const
Returns the settings entry type.
QgsSettingsEntryBase(const QString &key, const QString §ion, const QVariant &defaultValue=QVariant(), const QString &description=QString(), Qgis::SettingsOptions options=Qgis::SettingsOptions())
Constructor for QgsSettingsEntryBase.
A tree node for the settings tree to help organizing and introspecting the tree.
Qgis::SettingsOrigin origin(const QString &key) const
Returns the origin of the setting if it exists at the given key.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QgsSettingsProxy get()
Returns a proxy for a QgsSettings object.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
#define QgsDebugError(str)