QGIS API Documentation 4.1.0-Master (467af3bbe65)
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#include <QString>
28
29#include "moc_qgssettings.cpp"
30
31using namespace Qt::StringLiterals;
32
33Q_GLOBAL_STATIC( QString, sGlobalSettingsPath )
34
36
37bool QgsSettings::setGlobalSettingsPath( const QString &path )
38{
39 if ( QFileInfo::exists( path ) )
40 {
41 *sGlobalSettingsPath() = path;
42 return true;
43 }
44 return false;
45}
46
47void QgsSettings::init()
48{
49 if ( !sGlobalSettingsPath()->isEmpty() )
50 {
51 mGlobalSettings = std::make_unique<QSettings>( *sGlobalSettingsPath(), QSettings::IniFormat );
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, const QString &application, QObject *parent )
62{
63 mUserSettings = std::make_unique<QSettings>( scope, organization, application, parent );
64 init();
65}
66
67QgsSettings::QgsSettings( QSettings::Format format, QSettings::Scope scope, 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
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 ) || ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
187}
188
190{
191 return mUserSettings->fileName();
192}
193
195{
196 mUserSettings->sync();
197}
198
199void QgsSettings::remove( const QString &key, const QgsSettings::Section section )
200{
201 const QString pKey = prefixedKey( key, section );
202 if ( pKey.isEmpty() )
203 {
204 QgsDebugError( u"QSettings::remove called with empty key -- this probably wasn't intentional, but will result in ALL SETTINGS GETTING DELETED!"_s );
205 }
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 = u"core"_s;
216 break;
217 case Section::Server:
218 prefix = u"server"_s;
219 break;
220 case Section::Gui:
221 prefix = u"gui"_s;
222 break;
223 case Section::Plugins:
224 prefix = u"plugins"_s;
225 break;
226 case Section::Misc:
227 prefix = u"misc"_s;
228 break;
229 case Section::Auth:
230 prefix = u"auth"_s;
231 break;
232 case Section::App:
233 prefix = u"app"_s;
234 break;
236 prefix = u"providers"_s;
237 break;
239 prefix = u"expressions"_s;
240 break;
241 case Section::Gps:
242 prefix = u"gps"_s;
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 const QString resolvedKey = prefixedKey( key, section );
320 if ( resolvedKey.isEmpty() )
321 {
322 QgsDebugError( u"QSettings::remove called with empty key -- this probably wasn't intentional, but will result in ALL SETTINGS GETTING DELETED!"_s );
323 }
324 mUserSettings->remove( resolvedKey );
325 }
326}
327
328// To lower case and clean the path
329QString QgsSettings::sanitizeKey( const QString &key ) const
330{
331 return QDir::cleanPath( key );
332}
333
335{
336 mUserSettings->clear();
337}
338
340{
342 return;
343
345}
346
352
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:4708
@ Global
Global settings are stored in qgis_global_settings.ini.
Definition qgis.h:4710
@ Local
Local settings are stored in the user profile.
Definition qgis.h:4711
@ Any
From any origin.
Definition qgis.h:4709
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:68
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:73
@ Gps
GPS section, since QGIS 3.22.
Definition qgssettings.h:84
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:7657
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
#define QgsDebugError(str)
Definition qgslogger.h:59
QgsSettings * sQgsSettingsThreadSettings
QgsSettings * sQgsSettingsThreadSettings