28#include "moc_qgssettings.cpp"
36 if ( QFileInfo::exists( path ) )
38 *sGlobalSettingsPath() = path;
44void QgsSettings::init()
46 if ( ! sGlobalSettingsPath()->isEmpty() )
48 mGlobalSettings = std::make_unique<QSettings>( *sGlobalSettingsPath(), QSettings::IniFormat );
54 mUserSettings = std::make_unique<QSettings>( organization, application, parent );
59 const QString &application, QObject *parent )
61 mUserSettings = std::make_unique<QSettings>( scope, organization, application, parent );
66 const QString &organization,
const QString &application, QObject *parent )
68 mUserSettings = std::make_unique<QSettings>( format, scope, organization, application, parent );
74 mUserSettings = std::make_unique<QSettings>(
fileName, format, parent );
80 mUserSettings = std::make_unique<QSettings>( parent );
90 const QString pKey =
prefixedKey( prefix, section );
91 mUserSettings->beginGroup( pKey );
92 if ( mGlobalSettings )
94 mGlobalSettings->beginGroup( pKey );
100 mUserSettings->endGroup();
101 if ( mGlobalSettings )
103 mGlobalSettings->endGroup();
109 return mUserSettings->group();
114 QStringList keys = mUserSettings->allKeys();
115 if ( mGlobalSettings )
117 const QStringList constAllKeys = mGlobalSettings->allKeys();
118 std::copy_if( constAllKeys.constBegin(), constAllKeys.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
125 QStringList keys = mUserSettings->childKeys();
126 if ( mGlobalSettings )
128 const QStringList constChildKeys = mGlobalSettings->childKeys();
129 std::copy_if( constChildKeys.constBegin(), constChildKeys.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
140 QStringList keys = mUserSettings->childGroups();
141 if ( mGlobalSettings )
143 const QStringList constChildGroups = mGlobalSettings->childGroups();
144 std::copy_if( constChildGroups.constBegin(), constChildGroups.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
150 return mUserSettings->childGroups();
153 return mGlobalSettings ? mGlobalSettings->childGroups() : QStringList();
166 return *sGlobalSettingsPath();
174 return mUserSettings->value( pKey );
176 if ( mGlobalSettings )
178 return mGlobalSettings->value( pKey, defaultValue );
186 return mUserSettings->contains( pKey ) ||
187 ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
192 return mUserSettings->fileName();
197 mUserSettings->sync();
203 mUserSettings->remove( pKey );
215 prefix = u
"server"_s;
221 prefix = u
"plugins"_s;
233 prefix = u
"providers"_s;
236 prefix = u
"expressions"_s;
242 return sanitizeKey( key );
244 return prefix +
"/" + sanitizeKey( key );
249 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
250 if ( 0 == size && mGlobalSettings )
252 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
253 mUsingGlobalArray = ( size > 0 );
260 mUsingGlobalArray =
false;
261 mUserSettings->beginWriteArray( prefix, size );
266 mUserSettings->endArray();
267 if ( mGlobalSettings && mUsingGlobalArray )
269 mGlobalSettings->endArray();
271 mUsingGlobalArray =
false;
276 if ( mGlobalSettings && mUsingGlobalArray )
278 mGlobalSettings->setArrayIndex( i );
282 mUserSettings->setArrayIndex( i );
288 if ( mGlobalSettings && mGlobalSettings->contains( key ) )
291 if ( mUserSettings->contains( key ) )
306 if ( ( currentValue.isValid() ||
value.isValid() ) && ( currentValue !=
value ) )
314 else if ( mGlobalSettings && mGlobalSettings->value(
prefixedKey( key, section ) ) == currentValue )
316 mUserSettings->remove(
prefixedKey( key, section ) );
321QString QgsSettings::sanitizeKey(
const QString &key )
const
323 return QDir::cleanPath( key );
328 mUserSettings->clear();
SettingsOrigin
The setting origin describes where a setting is stored.
@ Global
Global settings are stored in qgis_global_settings.ini.
@ Local
Local settings are stored in the user profile.
A helper class for access to either a temporary QgsSettings object or the thread local object.
Stores settings for use within QGIS.
QStringList childGroups(Qgis::SettingsOrigin origin=Qgis::SettingsOrigin::Any) const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
Qgis::SettingsOrigin origin(const QString &key) const
Returns the origin of the setting if it exists at the given key.
static bool setGlobalSettingsPath(const QString &path)
Sets the Global Settings QSettings storage file.
void endGroup()
Resets the group to what it was before the corresponding beginGroup() call.
void clear()
Removes all entries in the user settings.
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.
QString prefixedKey(const QString &key, QgsSettings::Section section) const
Returns the sanitized and prefixed key.
void endArray()
Closes the array that was started using beginReadArray() or beginWriteArray().
QString group() const
Returns the current group.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
QStringList childKeys() const
Returns a list of all top-level keys that can be read using the QSettings object.
static QString globalSettingsPath()
Returns the path to the Global Settings QSettings storage file.
QgsSettings(const QString &organization, const QString &application=QString(), QObject *parent=nullptr)
Constructs a QgsSettings object for accessing settings of the application called application from the...
void sync()
Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in t...
QStringList globalChildGroups() const
Returns a list of all key top-level groups (same as childGroups) but only for groups defined in globa...
static void releaseFlush()
Releases a previously made hold on flushing QgsSettings objects and writing new values to the underly...
void beginWriteArray(const QString &prefix, int size=-1)
Adds prefix to the current group and starts writing an array of size size.
Section
Sections for namespaced settings.
@ Gps
GPS section, since QGIS 3.22.
static void holdFlush()
Temporarily places a hold on flushing QgsSettings objects and writing new values to the underlying in...
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.
QString fileName() const
Returns the path where settings written using this QSettings object are stored.
QStringList allKeys() const
Returns a list of all keys, including subkeys, that can be read using the QSettings object.
int beginReadArray(const QString &prefix)
Adds prefix to the current group and starts reading from an array. Returns the size of the array.
static QgsSettingsProxy get()
Returns a proxy for a QgsSettings object.
void setArrayIndex(int i)
Sets the current array index to i.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
#define BUILTIN_UNREACHABLE
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
QgsSettings * sQgsSettingsThreadSettings
QgsSettings * sQgsSettingsThreadSettings