30 if ( QFileInfo::exists( path ) )
32 *sGlobalSettingsPath() = path;
38void QgsSettings::init()
40 if ( ! sGlobalSettingsPath()->isEmpty() )
42 mGlobalSettings =
new QSettings( *sGlobalSettingsPath(), QSettings::IniFormat );
43#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
44 mGlobalSettings->setIniCodec(
"UTF-8" );
70 delete pluginTreeElement;
75 mUserSettings =
new QSettings( organization, application, parent );
80 const QString &application, QObject *parent )
82 mUserSettings =
new QSettings( scope, organization, application, parent );
87 const QString &organization,
const QString &application, QObject *parent )
89 mUserSettings =
new QSettings( format, scope, organization, application, parent );
95 mUserSettings =
new QSettings(
fileName, format, parent );
101 mUserSettings =
new QSettings( parent );
107 delete mUserSettings;
108 delete mGlobalSettings;
114 const QString pKey =
prefixedKey( prefix, section );
115 mUserSettings->beginGroup( pKey );
116 if ( mGlobalSettings )
118 mGlobalSettings->beginGroup( pKey );
124 mUserSettings->endGroup();
125 if ( mGlobalSettings )
127 mGlobalSettings->endGroup();
133 return mUserSettings->group();
138 QStringList keys = mUserSettings->allKeys();
139 if ( mGlobalSettings )
141 const QStringList constAllKeys = mGlobalSettings->allKeys();
142 std::copy_if( constAllKeys.constBegin(), constAllKeys.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
150 QStringList keys = mUserSettings->childKeys();
151 if ( mGlobalSettings )
153 const QStringList constChildKeys = mGlobalSettings->childKeys();
154 std::copy_if( constChildKeys.constBegin(), constChildKeys.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
165 QStringList keys = mUserSettings->childGroups();
166 if ( mGlobalSettings )
168 const QStringList constChildGroups = mGlobalSettings->childGroups();
169 std::copy_if( constChildGroups.constBegin(), constChildGroups.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
175 return mUserSettings->childGroups();
178 return mGlobalSettings ? mGlobalSettings->childGroups() : QStringList();
191 return *sGlobalSettingsPath();
199 return mUserSettings->value( pKey );
201 if ( mGlobalSettings )
203 return mGlobalSettings->value( pKey, defaultValue );
211 return mUserSettings->contains( pKey ) ||
212 ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
217 return mUserSettings->fileName();
222 mUserSettings->sync();
228 mUserSettings->remove( pKey );
237 prefix = QStringLiteral(
"core" );
239 case Section::Server:
240 prefix = QStringLiteral(
"server" );
243 prefix = QStringLiteral(
"gui" );
245 case Section::Plugins:
246 prefix = QStringLiteral(
"plugins" );
249 prefix = QStringLiteral(
"misc" );
252 prefix = QStringLiteral(
"auth" );
255 prefix = QStringLiteral(
"app" );
257 case Section::Providers:
258 prefix = QStringLiteral(
"providers" );
260 case Section::Expressions:
261 prefix = QStringLiteral(
"expressions" );
264 prefix = QStringLiteral(
"gps" );
266 case Section::NoSection:
267 return sanitizeKey( key );
269 return prefix +
"/" + sanitizeKey( key );
275 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
276 if ( 0 == size && mGlobalSettings )
278 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
279 mUsingGlobalArray = ( size > 0 );
286 mUsingGlobalArray =
false;
287 mUserSettings->beginWriteArray( prefix, size );
292 mUserSettings->endArray();
293 if ( mGlobalSettings )
295 mGlobalSettings->endArray();
297 mUsingGlobalArray =
false;
302 if ( mGlobalSettings && mUsingGlobalArray )
304 mGlobalSettings->setArrayIndex( i );
308 mUserSettings->setArrayIndex( i );
314 if ( mGlobalSettings && mGlobalSettings->contains( key ) )
317 if ( mUserSettings->contains( key ) )
332 if ( ( currentValue.isValid() ||
value.isValid() ) && ( currentValue !=
value ) )
340 else if ( mGlobalSettings && mGlobalSettings->value(
prefixedKey( key, section ) ) == currentValue )
342 mUserSettings->remove(
prefixedKey( key, section ) );
347QString QgsSettings::sanitizeKey(
const QString &key )
const
349 return QDir::cleanPath( key );
354 mUserSettings->clear();
SettingsOrigin
The setting origin describes where a setting is stored.
@ Global
Global settings are stored in global_settings.ini
@ Local
Local settings are stored in the user profile.
QgsSettingsTreeNode is a tree element for the settings registry to help organizing and introspecting ...
QgsSettingsTreeNode * createChildElement(const QString &key) SIP_THROW(QgsSettingsException)
Creates a normal tree element It will return the existing child element if it exists at the given key...
static QgsSettingsTreeNode * createRootElement()
Creates a tree root element.
QgsSettingsTreeNode * childElement(const QString &key)
Returns the existing child element if it exists at the given key.
This class is a composition of two QSettings instances:
static QgsSettingsTreeNode * treeRoot()
Returns the tree root element for the settings.
static QgsSettingsTreeNode * createPluginTreeElement(const QString &pluginName)
Creates a settings tree element for the given pluginName.
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.
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.
static QgsSettingsTreeNode * sTreePlugins
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...
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.
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.
static void unregisterPluginTreeElement(const QString &pluginName)
Unregisters the tree element for the given plugin.
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.
void setArrayIndex(int i)
Sets the current array index to i.
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
#define BUILTIN_UNREACHABLE
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)