24#include <QRegularExpression>
28using namespace Qt::StringLiterals;
36static bool sSettingsInitialized =
false;
38static QSettings &sUserSettings()
40 thread_local std::unique_ptr<QSettings> sSettings;
41 thread_local bool sCreatedBeforeInit =
true;
43 if ( !sSettings || ( sCreatedBeforeInit && sSettingsInitialized ) )
45 sSettings = std::make_unique< QSettings >();
46 sCreatedBeforeInit = !sSettingsInitialized;
55 if ( sSettingsInitialized )
58 QSettings::setDefaultFormat( QSettings::IniFormat );
59 QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, profilePath );
60 sSettingsInitialized =
true;
65 return sUserSettings();
69 : mParentTreeElement(
parent )
71 , mDefaultValue( defaultValue )
75 mKey = QDir::cleanPath( u
"%1/%2"_s.arg(
parent ?
parent->completeKey() : QString(),
key ) );
79 parent->registerChildSetting(
this,
key );
85 if ( mParentTreeElement )
86 mParentTreeElement->unregisterChildSetting(
this );
91 return QString::fromUtf8( sSettingsTypeMetaEnum.valueToKey(
static_cast<int>(
settingsType() ) ) );
102 return completeKeyPrivate( mKey, dynamicKeyPartList );
105QString QgsSettingsEntryBase::completeKeyPrivate(
const QString &key,
const QStringList &dynamicKeyPartList )
const
107 QString completeKey =
key;
109 if ( dynamicKeyPartList.isEmpty() )
112 QgsDebugError( u
"Settings '%1' have a dynamic key but the dynamic key part was not provided"_s.arg( completeKey ) );
120 QgsDebugError( u
"Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored"_s.arg( completeKey ) );
124 for (
int i = 0; i < dynamicKeyPartList.size(); i++ )
126 completeKey.replace( u
"%"_s.append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
142 const thread_local QRegularExpression digitRx( u
"%\\d+"_s );
143 const QRegularExpression regularExpression(
definitionKey().replace( digitRx, u
".*"_s ) );
144 const QRegularExpressionMatch regularExpressionMatch = regularExpression.match(
key );
145 return regularExpressionMatch.hasMatch();
155 const thread_local QRegularExpression regularExpression( u
"%\\d+"_s );
156 return mKey.contains( regularExpression );
161 return sUserSettings().contains(
key( dynamicKeyPart ) );
166 return sUserSettings().contains(
key( dynamicKeyPartList ) );
171 if ( sUserSettings().contains(
key( dynamicKeyPartList ) ) )
179 sUserSettings().remove(
key( dynamicKeyPart ) );
184 sUserSettings().remove(
key( dynamicKeyPartList ) );
200 const QString resolvedKey =
key( dynamicKeyPartList );
204 if (
exists( dynamicKeyPartList ) )
207 if ( value != currentValue )
209 sUserSettings().setValue( formerValuekey( dynamicKeyPartList ), currentValue );
214 sUserSettings().setValue( resolvedKey, value );
220 QStringList dynamicKeyPartList;
221 if ( !dynamicKeyPart.isNull() )
222 dynamicKeyPartList.append( dynamicKeyPart );
223 return dynamicKeyPartList;
233 return sUserSettings().value(
key( dynamicKeyPartList ), mDefaultValue );
245 if ( useDefaultValueOverride )
246 return sUserSettings().value(
key( dynamicKeyPartList ), defaultValueOverride );
248 return sUserSettings().value(
key( dynamicKeyPartList ), mDefaultValue );
253 return sUserSettings().value(
key( dynamicKeyPart ), defaultValueOverride );
258 return sUserSettings().value(
key( dynamicKeyPartList ), defaultValueOverride );
263 return mDefaultValue;
279 QVariant defaultValueOverride =
valueAsVariant( dynamicKeyPartList );
280 return sUserSettings().value( formerValuekey( dynamicKeyPartList ), defaultValueOverride );
285 QSettings &settings = sUserSettings();
287 const QString oldCompleteKey = completeKeyPrivate(
key, dynamicKeyPartList );
289 if ( settings.contains( oldCompleteKey ) )
291 if ( !
exists( dynamicKeyPartList ) )
293 QVariant oldValue = settings.value( oldCompleteKey, mDefaultValue );
298 if ( removeSettingAtKey )
299 settings.remove( oldCompleteKey );
308 const QString completeKey = completeKeyPrivate(
key, dynamicKeyPartList );
309 sUserSettings().setValue( completeKey,
valueAsVariant( dynamicKeyPartList ) );
320QString QgsSettingsEntryBase::formerValuekey(
const QStringList &dynamicKeyPartList )
const
322 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