QGIS API Documentation 4.1.0-Master (60fea48833c)
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 mUserSettings->remove( pKey );
203}
204
205QString QgsSettings::prefixedKey( const QString &key, const Section section ) const
206{
207 QString prefix;
208 switch ( section )
209 {
210 case Section::Core:
211 prefix = u"core"_s;
212 break;
213 case Section::Server:
214 prefix = u"server"_s;
215 break;
216 case Section::Gui:
217 prefix = u"gui"_s;
218 break;
219 case Section::Plugins:
220 prefix = u"plugins"_s;
221 break;
222 case Section::Misc:
223 prefix = u"misc"_s;
224 break;
225 case Section::Auth:
226 prefix = u"auth"_s;
227 break;
228 case Section::App:
229 prefix = u"app"_s;
230 break;
232 prefix = u"providers"_s;
233 break;
235 prefix = u"expressions"_s;
236 break;
237 case Section::Gps:
238 prefix = u"gps"_s;
239 break;
241 return sanitizeKey( key );
242 }
243 return prefix + "/" + sanitizeKey( key );
244}
245
246int QgsSettings::beginReadArray( const QString &prefix )
247{
248 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
249 if ( 0 == size && mGlobalSettings )
250 {
251 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
252 mUsingGlobalArray = ( size > 0 );
253 }
254 return size;
255}
256
257void QgsSettings::beginWriteArray( const QString &prefix, int size )
258{
259 mUsingGlobalArray = false;
260 mUserSettings->beginWriteArray( prefix, size );
261}
262
264{
265 mUserSettings->endArray();
266 if ( mGlobalSettings && mUsingGlobalArray )
267 {
268 mGlobalSettings->endArray();
269 }
270 mUsingGlobalArray = false;
271}
272
274{
275 if ( mGlobalSettings && mUsingGlobalArray )
276 {
277 mGlobalSettings->setArrayIndex( i );
278 }
279 else
280 {
281 mUserSettings->setArrayIndex( i );
282 }
283}
284
285Qgis::SettingsOrigin QgsSettings::origin( const QString &key ) const
286{
287 if ( mGlobalSettings && mGlobalSettings->contains( key ) )
289
290 if ( mUserSettings->contains( key ) )
292
294}
295
296void QgsSettings::setValue( const QString &key, const QVariant &value, const QgsSettings::Section section )
297{
298 // TODO: add valueChanged signal
299 // Do not store if it hasn't changed from default value
300 // First check if the values are different and if at least one of them is valid.
301 // The valid check is required because different invalid QVariant types
302 // like QVariant(QVariant::String) and QVariant(QVariant::Int))
303 // may be considered different and we don't want to store the value in that case.
304 const QVariant currentValue = QgsSettings::value( prefixedKey( key, section ) );
305 if ( ( currentValue.isValid() || value.isValid() ) && ( currentValue != value ) )
306 {
307 mUserSettings->setValue( prefixedKey( key, section ), value );
308 }
309 // Deliberately an "else if" because we want to remove a value from the user settings
310 // only if the value is different than the one stored in the global settings (because
311 // it would be the default anyway). The first check is necessary because the global settings
312 // might be a nullptr (for example in case of standalone scripts or apps).
313 else if ( mGlobalSettings && mGlobalSettings->value( prefixedKey( key, section ) ) == currentValue )
314 {
315 mUserSettings->remove( prefixedKey( key, section ) );
316 }
317}
318
319// To lower case and clean the path
320QString QgsSettings::sanitizeKey( const QString &key ) const
321{
322 return QDir::cleanPath( key );
323}
324
326{
327 mUserSettings->clear();
328}
329
331{
333 return;
334
336}
337
343
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:4609
@ Global
Global settings are stored in qgis_global_settings.ini.
Definition qgis.h:4611
@ Local
Local settings are stored in the user profile.
Definition qgis.h:4612
@ Any
From any origin.
Definition qgis.h:4610
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:7540
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
QgsSettings * sQgsSettingsThreadSettings
QgsSettings * sQgsSettingsThreadSettings