QGIS API Documentation 3.99.0-Master (a8882ad4560)
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 }
50}
51
52QgsSettings::QgsSettings( const QString &organization, const QString &application, QObject *parent )
53{
54 mUserSettings = std::make_unique<QSettings>( organization, application, parent );
55 init();
56}
57
58QgsSettings::QgsSettings( QSettings::Scope scope, const QString &organization,
59 const QString &application, QObject *parent )
60{
61 mUserSettings = std::make_unique<QSettings>( scope, organization, application, parent );
62 init();
63}
64
65QgsSettings::QgsSettings( QSettings::Format format, QSettings::Scope scope,
66 const QString &organization, const QString &application, QObject *parent )
67{
68 mUserSettings = std::make_unique<QSettings>( format, scope, organization, application, parent );
69 init();
70}
71
72QgsSettings::QgsSettings( const QString &fileName, QSettings::Format format, QObject *parent )
73{
74 mUserSettings = std::make_unique<QSettings>( fileName, format, parent );
75 init();
76}
77
78QgsSettings::QgsSettings( QObject *parent )
79{
80 mUserSettings = std::make_unique<QSettings>( parent );
81 init();
82}
83
87
88void QgsSettings::beginGroup( const QString &prefix, const QgsSettings::Section section )
89{
90 const QString pKey = prefixedKey( prefix, section );
91 mUserSettings->beginGroup( pKey );
92 if ( mGlobalSettings )
93 {
94 mGlobalSettings->beginGroup( pKey );
95 }
96}
97
99{
100 mUserSettings->endGroup();
101 if ( mGlobalSettings )
102 {
103 mGlobalSettings->endGroup();
104 }
105}
106
107QString QgsSettings::group() const
108{
109 return mUserSettings->group();
110}
111
112QStringList QgsSettings::allKeys() const
113{
114 QStringList keys = mUserSettings->allKeys();
115 if ( mGlobalSettings )
116 {
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 );} );
119 }
120 return keys;
121}
122
123QStringList QgsSettings::childKeys() const
124{
125 QStringList keys = mUserSettings->childKeys();
126 if ( mGlobalSettings )
127 {
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 );} );
130 }
131 return keys;
132}
133
135{
136 switch ( origin )
137 {
139 {
140 QStringList keys = mUserSettings->childGroups();
141 if ( mGlobalSettings )
142 {
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 );} );
145 }
146 return keys;
147 }
148
150 return mUserSettings->childGroups();
151
153 return mGlobalSettings ? mGlobalSettings->childGroups() : QStringList();
154 }
155
157}
158
163
165{
166 return *sGlobalSettingsPath();
167}
168
169QVariant QgsSettings::value( const QString &key, const QVariant &defaultValue, const QgsSettings::Section section ) const
170{
171 const QString pKey = prefixedKey( key, section );
172 if ( !QgsVariantUtils::isNull( mUserSettings->value( pKey ) ) )
173 {
174 return mUserSettings->value( pKey );
175 }
176 if ( mGlobalSettings )
177 {
178 return mGlobalSettings->value( pKey, defaultValue );
179 }
180 return defaultValue;
181}
182
183bool QgsSettings::contains( const QString &key, const QgsSettings::Section section ) const
184{
185 const QString pKey = prefixedKey( key, section );
186 return mUserSettings->contains( pKey ) ||
187 ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
188}
189
191{
192 return mUserSettings->fileName();
193}
194
196{
197 mUserSettings->sync();
198}
199
200void QgsSettings::remove( const QString &key, const QgsSettings::Section section )
201{
202 const QString pKey = prefixedKey( key, section );
203 mUserSettings->remove( pKey );
204}
205
206QString QgsSettings::prefixedKey( const QString &key, const Section section ) const
207{
208 QString prefix;
209 switch ( section )
210 {
211 case Section::Core:
212 prefix = u"core"_s;
213 break;
214 case Section::Server:
215 prefix = u"server"_s;
216 break;
217 case Section::Gui:
218 prefix = u"gui"_s;
219 break;
220 case Section::Plugins:
221 prefix = u"plugins"_s;
222 break;
223 case Section::Misc:
224 prefix = u"misc"_s;
225 break;
226 case Section::Auth:
227 prefix = u"auth"_s;
228 break;
229 case Section::App:
230 prefix = u"app"_s;
231 break;
233 prefix = u"providers"_s;
234 break;
236 prefix = u"expressions"_s;
237 break;
238 case Section::Gps:
239 prefix = u"gps"_s;
240 break;
242 return sanitizeKey( key );
243 }
244 return prefix + "/" + sanitizeKey( key );
245}
246
247int QgsSettings::beginReadArray( const QString &prefix )
248{
249 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
250 if ( 0 == size && mGlobalSettings )
251 {
252 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
253 mUsingGlobalArray = ( size > 0 );
254 }
255 return size;
256}
257
258void QgsSettings::beginWriteArray( const QString &prefix, int size )
259{
260 mUsingGlobalArray = false;
261 mUserSettings->beginWriteArray( prefix, size );
262}
263
265{
266 mUserSettings->endArray();
267 if ( mGlobalSettings && mUsingGlobalArray )
268 {
269 mGlobalSettings->endArray();
270 }
271 mUsingGlobalArray = false;
272}
273
275{
276 if ( mGlobalSettings && mUsingGlobalArray )
277 {
278 mGlobalSettings->setArrayIndex( i );
279 }
280 else
281 {
282 mUserSettings->setArrayIndex( i );
283 }
284}
285
286Qgis::SettingsOrigin QgsSettings::origin( const QString &key ) const
287{
288 if ( mGlobalSettings && mGlobalSettings->contains( key ) )
290
291 if ( mUserSettings->contains( key ) )
293
295}
296
297void QgsSettings::setValue( const QString &key, const QVariant &value, const QgsSettings::Section section )
298{
299 // TODO: add valueChanged signal
300 // Do not store if it hasn't changed from default value
301 // First check if the values are different and if at least one of them is valid.
302 // The valid check is required because different invalid QVariant types
303 // like QVariant(QVariant::String) and QVariant(QVariant::Int))
304 // may be considered different and we don't want to store the value in that case.
305 const QVariant currentValue = QgsSettings::value( prefixedKey( key, section ) );
306 if ( ( currentValue.isValid() || value.isValid() ) && ( currentValue != value ) )
307 {
308 mUserSettings->setValue( prefixedKey( key, section ), value );
309 }
310 // Deliberately an "else if" because we want to remove a value from the user settings
311 // only if the value is different than the one stored in the global settings (because
312 // it would be the default anyway). The first check is necessary because the global settings
313 // might be a nullptr (for example in case of standalone scripts or apps).
314 else if ( mGlobalSettings && mGlobalSettings->value( prefixedKey( key, section ) ) == currentValue )
315 {
316 mUserSettings->remove( prefixedKey( key, section ) );
317 }
318}
319
320// To lower case and clean the path
321QString QgsSettings::sanitizeKey( const QString &key ) const
322{
323 return QDir::cleanPath( key );
324}
325
327{
328 mUserSettings->clear();
329}
330
332{
334 return;
335
337}
338
344
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:4512
@ Global
Global settings are stored in qgis_global_settings.ini.
Definition qgis.h:4514
@ Local
Local settings are stored in the user profile.
Definition qgis.h:4515
@ Any
From any origin.
Definition qgis.h:4513
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:7477
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
QgsSettings * sQgsSettingsThreadSettings
QgsSettings * sQgsSettingsThreadSettings