26 QString QgsSettings::sGlobalSettingsPath = QString();
30 if ( QFileInfo::exists( path ) )
32 sGlobalSettingsPath = path;
38 void QgsSettings::init()
40 if ( ! sGlobalSettingsPath.isEmpty() )
42 mGlobalSettings =
new QSettings( sGlobalSettingsPath, QSettings::IniFormat );
43 mGlobalSettings->setIniCodec(
"UTF-8" );
50 mUserSettings =
new QSettings( organization, application, parent );
55 const QString &application, QObject *parent )
57 mUserSettings =
new QSettings( scope, organization, application, parent );
62 const QString &organization,
const QString &application, QObject *parent )
64 mUserSettings =
new QSettings( format, scope, organization, application, parent );
70 mUserSettings =
new QSettings( fileName, format, parent );
76 mUserSettings =
new QSettings( parent );
83 delete mGlobalSettings;
90 mUserSettings->beginGroup( pKey );
91 if ( mGlobalSettings )
93 mGlobalSettings->beginGroup( pKey );
99 mUserSettings->endGroup();
100 if ( mGlobalSettings )
102 mGlobalSettings->endGroup();
108 return mUserSettings->group();
113 QStringList keys = mUserSettings->allKeys();
114 if ( mGlobalSettings )
116 for (
auto &s : mGlobalSettings->allKeys() )
118 if ( ! keys.contains( s ) )
130 QStringList keys = mUserSettings->childKeys();
131 if ( mGlobalSettings )
133 for (
auto &s : mGlobalSettings->childKeys() )
135 if ( ! keys.contains( s ) )
146 QStringList keys = mUserSettings->childGroups();
147 if ( mGlobalSettings )
149 for (
auto &s : mGlobalSettings->childGroups() )
151 if ( ! keys.contains( s ) )
162 if ( mGlobalSettings )
164 keys = mGlobalSettings->childGroups();
172 if ( !mUserSettings->value( pKey ).isNull() )
174 return mUserSettings->value( pKey );
176 if ( mGlobalSettings )
178 return mGlobalSettings->value( pKey, defaultValue );
186 return mUserSettings->contains( pKey ) ||
187 ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
192 return mUserSettings->fileName();
197 mUserSettings->sync();
203 mUserSettings->remove( pKey );
212 prefix = QStringLiteral(
"core" );
214 case Section::Server :
215 prefix = QStringLiteral(
"server" );
218 prefix = QStringLiteral(
"gui" );
220 case Section::Plugins :
221 prefix = QStringLiteral(
"plugins" );
224 prefix = QStringLiteral(
"misc" );
227 prefix = QStringLiteral(
"auth" );
230 prefix = QStringLiteral(
"app" );
232 case Section::Providers :
233 prefix = QStringLiteral(
"providers" );
235 case Section::NoSection:
237 return sanitizeKey( key );
239 return prefix +
"/" + sanitizeKey( key );
245 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
246 if ( 0 == size && mGlobalSettings )
248 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
249 mUsingGlobalArray = ( size > 0 );
256 mUsingGlobalArray =
false;
257 mUserSettings->beginWriteArray( prefix, size );
262 mUserSettings->endArray();
263 if ( mGlobalSettings )
265 mGlobalSettings->endArray();
267 mUsingGlobalArray =
false;
272 if ( mGlobalSettings && mUsingGlobalArray )
274 mGlobalSettings->setArrayIndex( i );
278 mUserSettings->setArrayIndex( i );
291 if ( ( currentValue.isValid() || value.isValid() ) && ( currentValue != value ) )
293 mUserSettings->setValue(
prefixedKey( key, section ), value );
299 else if ( mGlobalSettings && mGlobalSettings->value(
prefixedKey( key, section ) ) == currentValue )
301 mUserSettings->remove(
prefixedKey( key, section ) );
306 QString QgsSettings::sanitizeKey(
const QString &key )
const 308 return QDir::cleanPath( key );
313 mUserSettings->clear();
void clear()
Removes all entries in the user settings.
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
void setArrayIndex(int i)
Sets the current array index to i.
void endGroup()
Resets the group to what it was before the corresponding beginGroup() call.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QStringList childKeys() const
Returns a list of all top-level keys that can be read using the QSettings object. ...
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
QStringList allKeys() const
Returns a list of all keys, including subkeys, that can be read using the QSettings object...
static bool setGlobalSettingsPath(const QString &path)
Sets the Global Settings QSettings storage file.
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...
Section
Sections for namespaced settings.
QString prefixedKey(const QString &key, QgsSettings::Section section) const
Returns the sanitized and prefixed key.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
QString fileName() const
Returns the path where settings written using this QSettings object are stored.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QString group() const
Returns the current group.
QgsSettings(const QString &organization, const QString &application=QString(), QObject *parent=nullptr)
Construct a QgsSettings object for accessing settings of the application called application from the ...
void endArray()
Closes the array that was started using beginReadArray() or beginWriteArray().
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
int beginReadArray(const QString &prefix)
Adds prefix to the current group and starts reading from an array. Returns the size of the array...
void beginWriteArray(const QString &prefix, int size=-1)
Adds prefix to the current group and starts writing an array of size size.