QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
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 "qgssettings.h"
18
19#include <cstdlib>
20
21#include "qgssettingsproxy.h"
22#include "qgsvariantutils.h"
23
24#include <QDir>
25#include <QFileInfo>
26#include <QSettings>
27
28#include "moc_qgssettings.cpp"
29
30Q_GLOBAL_STATIC( QString, sGlobalSettingsPath )
31
33
34bool QgsSettings::setGlobalSettingsPath( const QString &path )
35{
36 if ( QFileInfo::exists( path ) )
37 {
38 *sGlobalSettingsPath() = path;
39 return true;
40 }
41 return false;
42}
43
44void QgsSettings::init()
45{
46 if ( ! sGlobalSettingsPath()->isEmpty() )
47 {
48 mGlobalSettings = std::make_unique<QSettings>( *sGlobalSettingsPath(), QSettings::IniFormat );
49#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
50 mGlobalSettings->setIniCodec( "UTF-8" );
51#endif
52 }
53}
54
55QgsSettings::QgsSettings( const QString &organization, const QString &application, QObject *parent )
56{
57 mUserSettings = std::make_unique<QSettings>( organization, application, parent );
58 init();
59}
60
61QgsSettings::QgsSettings( QSettings::Scope scope, const QString &organization,
62 const QString &application, QObject *parent )
63{
64 mUserSettings = std::make_unique<QSettings>( scope, organization, application, parent );
65 init();
66}
67
68QgsSettings::QgsSettings( QSettings::Format format, QSettings::Scope scope,
69 const QString &organization, const QString &application, QObject *parent )
70{
71 mUserSettings = std::make_unique<QSettings>( format, scope, organization, application, parent );
72 init();
73}
74
75QgsSettings::QgsSettings( const QString &fileName, QSettings::Format format, QObject *parent )
76{
77 mUserSettings = std::make_unique<QSettings>( fileName, format, parent );
78 init();
79}
80
81QgsSettings::QgsSettings( QObject *parent )
82{
83 mUserSettings = std::make_unique<QSettings>( parent );
84 init();
85}
86
90
91void QgsSettings::beginGroup( const QString &prefix, const QgsSettings::Section section )
92{
93 const QString pKey = prefixedKey( prefix, section );
94 mUserSettings->beginGroup( pKey );
95 if ( mGlobalSettings )
96 {
97 mGlobalSettings->beginGroup( pKey );
98 }
99}
100
102{
103 mUserSettings->endGroup();
104 if ( mGlobalSettings )
105 {
106 mGlobalSettings->endGroup();
107 }
108}
109
110QString QgsSettings::group() const
111{
112 return mUserSettings->group();
113}
114
115QStringList QgsSettings::allKeys() const
116{
117 QStringList keys = mUserSettings->allKeys();
118 if ( mGlobalSettings )
119 {
120 const QStringList constAllKeys = mGlobalSettings->allKeys();
121 std::copy_if( constAllKeys.constBegin(), constAllKeys.constEnd(), std::back_inserter( keys ), [&keys]( const QString & key ) {return !keys.contains( key );} );
122 }
123 return keys;
124}
125
126QStringList QgsSettings::childKeys() const
127{
128 QStringList keys = mUserSettings->childKeys();
129 if ( mGlobalSettings )
130 {
131 const QStringList constChildKeys = mGlobalSettings->childKeys();
132 std::copy_if( constChildKeys.constBegin(), constChildKeys.constEnd(), std::back_inserter( keys ), [&keys]( const QString & key ) {return !keys.contains( key );} );
133 }
134 return keys;
135}
136
138{
139 switch ( origin )
140 {
142 {
143 QStringList keys = mUserSettings->childGroups();
144 if ( mGlobalSettings )
145 {
146 const QStringList constChildGroups = mGlobalSettings->childGroups();
147 std::copy_if( constChildGroups.constBegin(), constChildGroups.constEnd(), std::back_inserter( keys ), [&keys]( const QString & key ) {return !keys.contains( key );} );
148 }
149 return keys;
150 }
151
153 return mUserSettings->childGroups();
154
156 return mGlobalSettings ? mGlobalSettings->childGroups() : QStringList();
157 }
158
160}
161
166
168{
169 return *sGlobalSettingsPath();
170}
171
172QVariant QgsSettings::value( const QString &key, const QVariant &defaultValue, const QgsSettings::Section section ) const
173{
174 const QString pKey = prefixedKey( key, section );
175 if ( !QgsVariantUtils::isNull( mUserSettings->value( pKey ) ) )
176 {
177 return mUserSettings->value( pKey );
178 }
179 if ( mGlobalSettings )
180 {
181 return mGlobalSettings->value( pKey, defaultValue );
182 }
183 return defaultValue;
184}
185
186bool QgsSettings::contains( const QString &key, const QgsSettings::Section section ) const
187{
188 const QString pKey = prefixedKey( key, section );
189 return mUserSettings->contains( pKey ) ||
190 ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
191}
192
194{
195 return mUserSettings->fileName();
196}
197
199{
200 mUserSettings->sync();
201}
202
203void QgsSettings::remove( const QString &key, const QgsSettings::Section section )
204{
205 const QString pKey = prefixedKey( key, section );
206 mUserSettings->remove( pKey );
207}
208
209QString QgsSettings::prefixedKey( const QString &key, const Section section ) const
210{
211 QString prefix;
212 switch ( section )
213 {
214 case Section::Core:
215 prefix = QStringLiteral( "core" );
216 break;
217 case Section::Server:
218 prefix = QStringLiteral( "server" );
219 break;
220 case Section::Gui:
221 prefix = QStringLiteral( "gui" );
222 break;
223 case Section::Plugins:
224 prefix = QStringLiteral( "plugins" );
225 break;
226 case Section::Misc:
227 prefix = QStringLiteral( "misc" );
228 break;
229 case Section::Auth:
230 prefix = QStringLiteral( "auth" );
231 break;
232 case Section::App:
233 prefix = QStringLiteral( "app" );
234 break;
236 prefix = QStringLiteral( "providers" );
237 break;
239 prefix = QStringLiteral( "expressions" );
240 break;
241 case Section::Gps:
242 prefix = QStringLiteral( "gps" );
243 break;
245 return sanitizeKey( key );
246 }
247 return prefix + "/" + sanitizeKey( key );
248}
249
250int QgsSettings::beginReadArray( const QString &prefix )
251{
252 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
253 if ( 0 == size && mGlobalSettings )
254 {
255 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
256 mUsingGlobalArray = ( size > 0 );
257 }
258 return size;
259}
260
261void QgsSettings::beginWriteArray( const QString &prefix, int size )
262{
263 mUsingGlobalArray = false;
264 mUserSettings->beginWriteArray( prefix, size );
265}
266
268{
269 mUserSettings->endArray();
270 if ( mGlobalSettings && mUsingGlobalArray )
271 {
272 mGlobalSettings->endArray();
273 }
274 mUsingGlobalArray = false;
275}
276
278{
279 if ( mGlobalSettings && mUsingGlobalArray )
280 {
281 mGlobalSettings->setArrayIndex( i );
282 }
283 else
284 {
285 mUserSettings->setArrayIndex( i );
286 }
287}
288
289Qgis::SettingsOrigin QgsSettings::origin( const QString &key ) const
290{
291 if ( mGlobalSettings && mGlobalSettings->contains( key ) )
293
294 if ( mUserSettings->contains( key ) )
296
298}
299
300void QgsSettings::setValue( const QString &key, const QVariant &value, const QgsSettings::Section section )
301{
302 // TODO: add valueChanged signal
303 // Do not store if it hasn't changed from default value
304 // First check if the values are different and if at least one of them is valid.
305 // The valid check is required because different invalid QVariant types
306 // like QVariant(QVariant::String) and QVariant(QVariant::Int))
307 // may be considered different and we don't want to store the value in that case.
308 const QVariant currentValue = QgsSettings::value( prefixedKey( key, section ) );
309 if ( ( currentValue.isValid() || value.isValid() ) && ( currentValue != value ) )
310 {
311 mUserSettings->setValue( prefixedKey( key, section ), value );
312 }
313 // Deliberately an "else if" because we want to remove a value from the user settings
314 // only if the value is different than the one stored in the global settings (because
315 // it would be the default anyway). The first check is necessary because the global settings
316 // might be a nullptr (for example in case of standalone scripts or apps).
317 else if ( mGlobalSettings && mGlobalSettings->value( prefixedKey( key, section ) ) == currentValue )
318 {
319 mUserSettings->remove( prefixedKey( key, section ) );
320 }
321}
322
323// To lower case and clean the path
324QString QgsSettings::sanitizeKey( const QString &key ) const
325{
326 return QDir::cleanPath( key );
327}
328
330{
331 mUserSettings->clear();
332}
333
335{
337 return;
338
340}
341
347
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:4465
@ Global
Global settings are stored in qgis_global_settings.ini.
Definition qgis.h:4467
@ Local
Local settings are stored in the user profile.
Definition qgis.h:4468
@ Any
From any origin.
Definition qgis.h:4466
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.
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.
~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:7208
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
QgsSettings * sQgsSettingsThreadSettings
QgsSettings * sQgsSettingsThreadSettings