24#include <QRegularExpression>
28using namespace Qt::StringLiterals;
36static bool sSettingsInitialized =
false;
38static QSettings &sUserSettings()
40 thread_local QSettings *sSettings =
nullptr;
41 thread_local bool sCreatedBeforeInit =
true;
43 if ( !sSettings || ( sCreatedBeforeInit && sSettingsInitialized ) )
46 sSettings =
new QSettings();
47 sCreatedBeforeInit = !sSettingsInitialized;
56 if ( sSettingsInitialized )
59 QSettings::setDefaultFormat( QSettings::IniFormat );
60 QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, profilePath );
61 sSettingsInitialized =
true;
66 return sUserSettings();
70 : mParentTreeElement(
parent )
72 , mDefaultValue( defaultValue )
76 mKey = QDir::cleanPath( u
"%1/%2"_s.arg(
parent ?
parent->completeKey() : QString(),
key ) );
80 parent->registerChildSetting(
this,
key );
86 if ( mParentTreeElement )
87 mParentTreeElement->unregisterChildSetting(
this );
92 return QString::fromUtf8( sSettingsTypeMetaEnum.valueToKey(
static_cast<int>(
settingsType() ) ) );
103 return completeKeyPrivate( mKey, dynamicKeyPartList );
106QString QgsSettingsEntryBase::completeKeyPrivate(
const QString &key,
const QStringList &dynamicKeyPartList )
const
108 QString completeKey =
key;
110 if ( dynamicKeyPartList.isEmpty() )
113 QgsDebugError( u
"Settings '%1' have a dynamic key but the dynamic key part was not provided"_s.arg( completeKey ) );
121 QgsDebugError( u
"Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored"_s.arg( completeKey ) );
125 for (
int i = 0; i < dynamicKeyPartList.size(); i++ )
127 completeKey.replace( u
"%"_s.append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
143 const thread_local QRegularExpression digitRx( u
"%\\d+"_s );
144 const QRegularExpression regularExpression(
definitionKey().replace( digitRx, u
".*"_s ) );
145 const QRegularExpressionMatch regularExpressionMatch = regularExpression.match(
key );
146 return regularExpressionMatch.hasMatch();
156 const thread_local QRegularExpression regularExpression( u
"%\\d+"_s );
157 return mKey.contains( regularExpression );
162 return sUserSettings().contains(
key( dynamicKeyPart ) );
167 return sUserSettings().contains(
key( dynamicKeyPartList ) );
172 if ( sUserSettings().contains(
key( dynamicKeyPartList ) ) )
180 sUserSettings().remove(
key( dynamicKeyPart ) );
185 sUserSettings().remove(
key( dynamicKeyPartList ) );
201 const QString resolvedKey =
key( dynamicKeyPartList );
205 if (
exists( dynamicKeyPartList ) )
208 if ( value != currentValue )
210 sUserSettings().setValue( formerValuekey( dynamicKeyPartList ), currentValue );
215 sUserSettings().setValue( resolvedKey, value );
221 QStringList dynamicKeyPartList;
222 if ( !dynamicKeyPart.isNull() )
223 dynamicKeyPartList.append( dynamicKeyPart );
224 return dynamicKeyPartList;
234 return sUserSettings().value(
key( dynamicKeyPartList ), mDefaultValue );
246 if ( useDefaultValueOverride )
247 return sUserSettings().value(
key( dynamicKeyPartList ), defaultValueOverride );
249 return sUserSettings().value(
key( dynamicKeyPartList ), mDefaultValue );
254 return sUserSettings().value(
key( dynamicKeyPart ), defaultValueOverride );
259 return sUserSettings().value(
key( dynamicKeyPartList ), defaultValueOverride );
264 return mDefaultValue;
280 QVariant defaultValueOverride =
valueAsVariant( dynamicKeyPartList );
281 return sUserSettings().value( formerValuekey( dynamicKeyPartList ), defaultValueOverride );
286 QSettings &settings = sUserSettings();
288 const QString oldCompleteKey = completeKeyPrivate(
key, dynamicKeyPartList );
290 if ( settings.contains( oldCompleteKey ) )
292 if ( !
exists( dynamicKeyPartList ) )
294 QVariant oldValue = settings.value( oldCompleteKey, mDefaultValue );
299 if ( removeSettingAtKey )
300 settings.remove( oldCompleteKey );
309 const QString completeKey = completeKeyPrivate(
key, dynamicKeyPartList );
310 sUserSettings().setValue( completeKey,
valueAsVariant( dynamicKeyPartList ) );
321QString QgsSettingsEntryBase::formerValuekey(
const QStringList &dynamicKeyPartList )
const
323 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.
@ Local
Local settings are stored in the user profile.
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.
static QSettings & userSettings()
Returns a reference to the thread-local QSettings instance used internally by all QgsSettingsEntry op...
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.
static void setupUserSettings(const QString &profilePath)
Configures QSettings to use IniFormat at the given profilePath so that each thread's QSettings instan...
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.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
#define QgsDebugError(str)
#define QGIS_CHECK_MAIN_THREAD_ACCESS