19#include <QRegularExpression>
25 : mSettings( settings )
27 for (
const auto *setting : std::as_const( mSettings ) )
29 QString otherBaseKey = setting->definitionKey();
30 otherBaseKey = otherBaseKey.left( otherBaseKey.lastIndexOf( QLatin1Char(
'/' ) ) );
31 if ( mDefinitionBaseKey.isEmpty() )
33 mDefinitionBaseKey = otherBaseKey;
37 if ( mDefinitionBaseKey != otherBaseKey )
39 QgsDebugMsg(
"Settings do not share the same base definition key for this group. This will lead to unpredictable results." );
40 if ( fatalErrorIfInvalid )
50 QString key = mDefinitionBaseKey;
52 if ( dynamicKeyPartList.isEmpty() )
54 if ( hasDynamicKey() )
55 QgsDebugMsg( QStringLiteral(
"Settings group '%1' have a dynamic key but the dynamic key part was not provided" ).arg( key ) );
61 if ( !hasDynamicKey() )
63 QgsDebugMsg( QStringLiteral(
"Settings group '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( key ) );
67 for (
int i = 0; i < dynamicKeyPartList.size(); i++ )
69 key.replace( QStringLiteral(
"%" ).append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
78 QString key =
baseKey( dynamicKeyPartList );
80 const thread_local QRegularExpression regularExpression( QStringLiteral(
"^(\\/?(qgis\\/?)?)?$" ) );
81 if ( key.contains( regularExpression ) )
83 QgsDebugMsg( QStringLiteral(
"Preventing mass removal of settings at key %1" ).arg( key ) );
98 for (
const auto *setting : mSettings )
99 setting->remove( dynamicKeyPartList );
102bool QgsSettingsEntryGroup::hasDynamicKey()
const
104 const thread_local QRegularExpression regularExpression( QStringLiteral(
"%\\d+" ) );
105 return mDefinitionBaseKey.contains( regularExpression );
116 QString completeKey = mKey;
117 if ( !mPluginName.isEmpty() )
119 if ( !completeKey.startsWith(
'/' ) )
120 completeKey.prepend(
'/' );
121 completeKey.prepend( mPluginName );
124 if ( dynamicKeyPartList.isEmpty() )
127 QgsDebugMsg( QStringLiteral(
"Settings '%1' have a dynamic key but the dynamic key part was not provided" ).arg( completeKey ) );
135 QgsDebugMsg( QStringLiteral(
"Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( completeKey ) );
139 for (
int i = 0; i < dynamicKeyPartList.size(); i++ )
141 completeKey.replace( QStringLiteral(
"%" ).append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
157 const QRegularExpression regularExpression(
definitionKey().replace( QRegularExpression( QStringLiteral(
"%\\d+" ) ), QStringLiteral(
".*" ) ) );
158 const QRegularExpressionMatch regularExpressionMatch = regularExpression.match(
key );
159 return regularExpressionMatch.hasMatch();
169 const thread_local QRegularExpression regularExpression( QStringLiteral(
"%\\d+" ) );
170 return mKey.contains( regularExpression );
207 if (
exists( dynamicKeyPartList ) )
210 if ( value != currentValue )
222 QStringList dynamicKeyPartList;
223 if ( !dynamicKeyPart.isNull() )
224 dynamicKeyPartList.append( dynamicKeyPart );
225 return dynamicKeyPartList;
247 if ( useDefaultValueOverride )
266 return mDefaultValue;
283 return QgsSettings().
value( formerValuekey( dynamicKeyPartList ), defaultValueOverride );
286QString QgsSettingsEntryBase::formerValuekey(
const QStringList &dynamicKeyPartList )
const
288 return key( dynamicKeyPartList ) + QStringLiteral(
"_formervalue" );
bool exists(const QString &dynamicKeyPart=QString()) const
Returns true if the settings is contained in the underlying QSettings.
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.
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.
virtual Q_DECL_DEPRECATED 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 the dynamicKeyPart argument specifying the dynamic part of the settings k...
bool setVariantValuePrivate(const QVariant &value, const QStringList &dynamicKeyPartList=QStringList()) const
Sets the settings value with a variant value.
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.
void removeAllChildrenSettings(const QString &dynamicKeyPart=QString()) const
Removes all the settings from this group The dynamicKeyPart argument specifies the dynamic part of th...
const QList< const QgsSettingsEntryBase * > settings() const
Returns all the settings.
void removeAllSettingsAtBaseKey(const QStringList &dynamicKeyPartList=QStringList()) const
Removes all the settings at the base key for the given dynamicKeyPartList This means it might remove ...
QgsSettingsEntryGroup(const QList< const QgsSettingsEntryBase * > settings)
Constructor.
QString baseKey(const QStringList &dynamicKeyPartList=QStringList()) const
Returns the base key for the given dynamicKeyPartList.
This class is a composition of two QSettings instances:
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.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH