QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgssettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssettings.cpp
3 --------------------------------------
4 Date : January 2017
5 Copyright : (C) 2017 by Alessandro Pasotti
6 Email : apasotti at boundlessgeo dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16
17#include <cstdlib>
18
19#include <QFileInfo>
20#include <QSettings>
21#include <QDir>
22
23#include "qgssettings.h"
24#include "moc_qgssettings.cpp"
25#include "qgsvariantutils.h"
26#include "qgssettingsproxy.h"
27
28Q_GLOBAL_STATIC( QString, sGlobalSettingsPath )
29
31
32bool QgsSettings::setGlobalSettingsPath( const QString &path )
33{
34 if ( QFileInfo::exists( path ) )
35 {
36 *sGlobalSettingsPath() = path;
37 return true;
38 }
39 return false;
40}
41
42void QgsSettings::init()
43{
44 if ( ! sGlobalSettingsPath()->isEmpty() )
45 {
46 mGlobalSettings = std::make_unique<QSettings>( *sGlobalSettingsPath(), QSettings::IniFormat );
47#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
48 mGlobalSettings->setIniCodec( "UTF-8" );
49#endif
50 }
51}
52
53QgsSettings::QgsSettings( const QString &organization, const QString &application, QObject *parent )
54{
55 mUserSettings = std::make_unique<QSettings>( organization, application, parent );
56 init();
57}
58
59QgsSettings::QgsSettings( QSettings::Scope scope, const QString &organization,
60 const QString &application, QObject *parent )
61{
62 mUserSettings = std::make_unique<QSettings>( scope, organization, application, parent );
63 init();
64}
65
66QgsSettings::QgsSettings( QSettings::Format format, QSettings::Scope scope,
67 const QString &organization, const QString &application, QObject *parent )
68{
69 mUserSettings = std::make_unique<QSettings>( format, scope, organization, application, parent );
70 init();
71}
72
73QgsSettings::QgsSettings( const QString &fileName, QSettings::Format format, QObject *parent )
74{
75 mUserSettings = std::make_unique<QSettings>( fileName, format, parent );
76 init();
77}
78
79QgsSettings::QgsSettings( QObject *parent )
80{
81 mUserSettings = std::make_unique<QSettings>( parent );
82 init();
83}
84
88
89void QgsSettings::beginGroup( const QString &prefix, const QgsSettings::Section section )
90{
91 const QString pKey = prefixedKey( prefix, section );
92 mUserSettings->beginGroup( pKey );
93 if ( mGlobalSettings )
94 {
95 mGlobalSettings->beginGroup( pKey );
96 }
97}
98
100{
101 mUserSettings->endGroup();
102 if ( mGlobalSettings )
103 {
104 mGlobalSettings->endGroup();
105 }
106}
107
108QString QgsSettings::group() const
109{
110 return mUserSettings->group();
111}
112
113QStringList QgsSettings::allKeys() const
114{
115 QStringList keys = mUserSettings->allKeys();
116 if ( mGlobalSettings )
117 {
118 const QStringList constAllKeys = mGlobalSettings->allKeys();
119 std::copy_if( constAllKeys.constBegin(), constAllKeys.constEnd(), std::back_inserter( keys ), [&keys]( const QString & key ) {return !keys.contains( key );} );
120 }
121 return keys;
122}
123
124QStringList QgsSettings::childKeys() const
125{
126 QStringList keys = mUserSettings->childKeys();
127 if ( mGlobalSettings )
128 {
129 const QStringList constChildKeys = mGlobalSettings->childKeys();
130 std::copy_if( constChildKeys.constBegin(), constChildKeys.constEnd(), std::back_inserter( keys ), [&keys]( const QString & key ) {return !keys.contains( key );} );
131 }
132 return keys;
133}
134
136{
137 switch ( origin )
138 {
140 {
141 QStringList keys = mUserSettings->childGroups();
142 if ( mGlobalSettings )
143 {
144 const QStringList constChildGroups = mGlobalSettings->childGroups();
145 std::copy_if( constChildGroups.constBegin(), constChildGroups.constEnd(), std::back_inserter( keys ), [&keys]( const QString & key ) {return !keys.contains( key );} );
146 }
147 return keys;
148 }
149
151 return mUserSettings->childGroups();
152
154 return mGlobalSettings ? mGlobalSettings->childGroups() : QStringList();
155 }
156
158}
159
164
166{
167 return *sGlobalSettingsPath();
168}
169
170QVariant QgsSettings::value( const QString &key, const QVariant &defaultValue, const QgsSettings::Section section ) const
171{
172 const QString pKey = prefixedKey( key, section );
173 if ( !QgsVariantUtils::isNull( mUserSettings->value( pKey ) ) )
174 {
175 return mUserSettings->value( pKey );
176 }
177 if ( mGlobalSettings )
178 {
179 return mGlobalSettings->value( pKey, defaultValue );
180 }
181 return defaultValue;
182}
183
184bool QgsSettings::contains( const QString &key, const QgsSettings::Section section ) const
185{
186 const QString pKey = prefixedKey( key, section );
187 return mUserSettings->contains( pKey ) ||
188 ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
189}
190
192{
193 return mUserSettings->fileName();
194}
195
197{
198 mUserSettings->sync();
199}
200
201void QgsSettings::remove( const QString &key, const QgsSettings::Section section )
202{
203 const QString pKey = prefixedKey( key, section );
204 mUserSettings->remove( pKey );
205}
206
207QString QgsSettings::prefixedKey( const QString &key, const Section section ) const
208{
209 QString prefix;
210 switch ( section )
211 {
212 case Section::Core:
213 prefix = QStringLiteral( "core" );
214 break;
215 case Section::Server:
216 prefix = QStringLiteral( "server" );
217 break;
218 case Section::Gui:
219 prefix = QStringLiteral( "gui" );
220 break;
221 case Section::Plugins:
222 prefix = QStringLiteral( "plugins" );
223 break;
224 case Section::Misc:
225 prefix = QStringLiteral( "misc" );
226 break;
227 case Section::Auth:
228 prefix = QStringLiteral( "auth" );
229 break;
230 case Section::App:
231 prefix = QStringLiteral( "app" );
232 break;
234 prefix = QStringLiteral( "providers" );
235 break;
237 prefix = QStringLiteral( "expressions" );
238 break;
239 case Section::Gps:
240 prefix = QStringLiteral( "gps" );
241 break;
243 return sanitizeKey( key );
244 }
245 return prefix + "/" + sanitizeKey( key );
246}
247
248int QgsSettings::beginReadArray( const QString &prefix )
249{
250 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
251 if ( 0 == size && mGlobalSettings )
252 {
253 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
254 mUsingGlobalArray = ( size > 0 );
255 }
256 return size;
257}
258
259void QgsSettings::beginWriteArray( const QString &prefix, int size )
260{
261 mUsingGlobalArray = false;
262 mUserSettings->beginWriteArray( prefix, size );
263}
264
266{
267 mUserSettings->endArray();
268 if ( mGlobalSettings && mUsingGlobalArray )
269 {
270 mGlobalSettings->endArray();
271 }
272 mUsingGlobalArray = false;
273}
274
276{
277 if ( mGlobalSettings && mUsingGlobalArray )
278 {
279 mGlobalSettings->setArrayIndex( i );
280 }
281 else
282 {
283 mUserSettings->setArrayIndex( i );
284 }
285}
286
287Qgis::SettingsOrigin QgsSettings::origin( const QString &key ) const
288{
289 if ( mGlobalSettings && mGlobalSettings->contains( key ) )
291
292 if ( mUserSettings->contains( key ) )
294
296}
297
298void QgsSettings::setValue( const QString &key, const QVariant &value, const QgsSettings::Section section )
299{
300 // TODO: add valueChanged signal
301 // Do not store if it hasn't changed from default value
302 // First check if the values are different and if at least one of them is valid.
303 // The valid check is required because different invalid QVariant types
304 // like QVariant(QVariant::String) and QVariant(QVariant::Int))
305 // may be considered different and we don't want to store the value in that case.
306 const QVariant currentValue = QgsSettings::value( prefixedKey( key, section ) );
307 if ( ( currentValue.isValid() || value.isValid() ) && ( currentValue != value ) )
308 {
309 mUserSettings->setValue( prefixedKey( key, section ), value );
310 }
311 // Deliberately an "else if" because we want to remove a value from the user settings
312 // only if the value is different than the one stored in the global settings (because
313 // it would be the default anyway). The first check is necessary because the global settings
314 // might be a nullptr (for example in case of standalone scripts or apps).
315 else if ( mGlobalSettings && mGlobalSettings->value( prefixedKey( key, section ) ) == currentValue )
316 {
317 mUserSettings->remove( prefixedKey( key, section ) );
318 }
319}
320
321// To lower case and clean the path
322QString QgsSettings::sanitizeKey( const QString &key ) const
323{
324 return QDir::cleanPath( key );
325}
326
328{
329 mUserSettings->clear();
330}
331
333{
335 return;
336
338}
339
345
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:4330
@ Global
Global settings are stored in qgis_global_settings.ini
@ Local
Local settings are stored in the user profile.
@ Any
From any origin.
A helper class for access to either a temporary QgsSettings object or the thread local object.
Stores settings for use within QGIS.
Definition qgssettings.h:65
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.
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.
~QgsSettings() override
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.
Definition qgssettings.h:71
@ Gps
GPS section, since QGIS 3.22.
Definition qgssettings.h:82
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
Definition qgis.h:6932
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
thread_local QgsSettings * sQgsSettingsThreadSettings
thread_local QgsSettings * sQgsSettingsThreadSettings