/
qgssettings.h
197 lines (181 loc) · 9.7 KB
/
qgssettings.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/***************************************************************************
qgssettings.h
--------------------------------------
Date : January 2017
Copyright : (C) 2017 by Alessandro Pasotti
Email : apasotti at boundlessgeo dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSSETTINGS_H
#define QGSSETTINGS_H
#include <QSettings>
#include "qgis_core.h"
/** \ingroup core
* \class QgsSettings
*
* This class is a composition of two QSettings instances:
* - the main QSettings instance is the standard User Settings and
* - the second one (Global Settings) is meant to provide read-only
* pre-configuration and defaults to the first one.
*
* For a given settings key, the function call to value(key, default) will return
* the first existing setting in the order specified below:
* - User Settings
* - Global Settings
* - Default Value
*
* The path to the Global Settings storage can be set before constructing the QgsSettings
* objects, with a static call to:
* static bool setGlobalSettingsPath( QString path );
*
* QgsSettings provides some shortcuts to get/set namespaced settings from/to a specific section:
* - Core
* - Gui
* - Server
* - Plugins
* - Misc
*
* @note added in QGIS 3
*/
class CORE_EXPORT QgsSettings : public QObject
{
Q_OBJECT
public:
//! Sections for namespaced settings
enum Section
{
NoSection,
Core,
Gui,
Server,
Plugins,
Misc
};
/** Construct a QgsSettings object for accessing settings of the application
* called application from the organization called organization, and with parent parent.
*/
explicit 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 organization called organization, and with parent parent.
* If scope is QSettings::UserScope, the QSettings object searches user-specific settings first,
* before it searches system-wide settings as a fallback. If scope is QSettings::SystemScope,
* the QSettings object ignores user-specific settings and provides access to system-wide settings.
*
* The storage format is set to QSettings::NativeFormat (i.e. calling setDefaultFormat() before
* calling this constructor has no effect).
*
* If no application name is given, the QSettings object will only access the organization-wide
* locations.
*/
QgsSettings( QSettings::Scope scope, const QString &organization,
const QString &application = QString(), QObject *parent = nullptr );
/** Construct a QgsSettings object for accessing settings of the application called application
* from the organization called organization, and with parent parent.
* If scope is QSettings::UserScope, the QSettings object searches user-specific settings first,
* before it searches system-wide settings as a fallback. If scope is QSettings::SystemScope,
* the QSettings object ignores user-specific settings and provides access to system-wide settings.
* If format is QSettings::NativeFormat, the native API is used for storing settings. If format
* is QSettings::IniFormat, the INI format is used.
*
* If no application name is given, the QSettings object will only access the organization-wide
* locations.
*/
QgsSettings( QSettings::Format format, QSettings::Scope scope, const QString &organization,
const QString &application = QString(), QObject *parent = nullptr );
/** Construct a QgsSettings object for accessing the settings stored in the file called fileName,
* with parent parent. If the file doesn't already exist, it is created.
*
* If format is QSettings::NativeFormat, the meaning of fileName depends on the platform. On Unix,
* fileName is the name of an INI file. On macOS and iOS, fileName is the name of a .plist file.
* On Windows, fileName is a path in the system registry.
*
* If format is QSettings::IniFormat, fileName is the name of an INI file.
*
* Warning: This function is provided for convenience. It works well for accessing INI or .plist
* files generated by Qt, but might fail on some syntaxes found in such files originated by
* other programs. In particular, be aware of the following limitations:
* - QgsSettings provides no way of reading INI "path" entries, i.e., entries with unescaped slash characters.
* (This is because these entries are ambiguous and cannot be resolved automatically.)
* - In INI files, QSettings uses the @ character as a metacharacter in some contexts, to encode
* Qt-specific data types (e.g., \@Rect), and might therefore misinterpret it when it occurs
* in pure INI files.
*/
QgsSettings( const QString &fileName, QSettings::Format format, QObject *parent = nullptr );
/** Constructs a QgsSettings object for accessing settings of the application and organization
* set previously with a call to QCoreApplication::setOrganizationName(),
* QCoreApplication::setOrganizationDomain(), and QCoreApplication::setApplicationName().
*
* The scope is QSettings::UserScope and the format is defaultFormat() (QSettings::NativeFormat
* by default). Use setDefaultFormat() before calling this constructor to change the default
* format used by this constructor.
*/
explicit QgsSettings( QObject *parent = 0 );
~QgsSettings();
/** Appends prefix to the current group.
* The current group is automatically prepended to all keys specified to QSettings.
* In addition, query functions such as childGroups(), childKeys(), and allKeys()
* are based on the group. By default, no group is set.
*/
void beginGroup( const QString &prefix );
//! Resets the group to what it was before the corresponding beginGroup() call.
void endGroup();
//! Returns a list of all keys, including subkeys, that can be read using the QSettings object.
QStringList allKeys() const;
//! Returns a list of all top-level keys that can be read using the QSettings object.
QStringList childKeys() const;
//! Returns a list of all key top-level groups that contain keys that can be read using the QSettings object.
QStringList childGroups() const;
//! Return the path to the Global Settings QSettings storage file
static QString globalSettingsPath() { return sGlobalSettingsPath; }
//! Set the Global Settings QSettings storage file
static bool setGlobalSettingsPath( QString path );
//! Adds prefix to the current group and starts reading from an array. Returns the size of the array.
int beginReadArray( const QString &prefix );
//! Closes the array that was started using beginReadArray() or beginWriteArray().
void endArray();
//! Sets the current array index to i. Calls to functions such as setValue(), value(),
//! remove(), and contains() will operate on the array entry at that index.
void setArrayIndex( int i );
//! Sets the value of setting key to value. If the key already exists, the previous value is overwritten.
//! An optional Section argument can be used to set a value to a specific Section.
void setValue( const QString &key, const QVariant &value, const Section section = Section::NoSection );
/** Returns the value for setting key. If the setting doesn't exist, it will be
* searched in the Global Settings and if not found, returns defaultValue.
* If no default value is specified, a default QVariant is returned.
* An optional Section argument can be used to get a value from a specific Section.
*/
QVariant value( const QString &key, const QVariant &defaultValue = QVariant(),
const Section section = Section::NoSection ) const;
//! Returns true if there exists a setting called key; returns false otherwise.
//! If a group is set using beginGroup(), key is taken to be relative to that group.
bool contains( const QString &key, const Section section = Section::NoSection ) const;
//! Returns the path where settings written using this QSettings object are stored.
QString fileName() const;
//! Writes any unsaved changes to permanent storage, and reloads any settings that have been
//! changed in the meantime by another application.
//! This function is called automatically from QSettings's destructor and by the event
//! loop at regular intervals, so you normally don't need to call it yourself.
void sync();
//! Removes the setting key and any sub-settings of key.
void remove( const QString &key );
//! Return the sanitized and prefixed key
QString prefixedKey( const QString &key, const Section section ) const;
//! Removes all entries in the user settings
void clear();
private:
static QString sGlobalSettingsPath;
void init();
QString sanitizeKey( QString key ) const;
QSettings *mUserSettings = nullptr;
QSettings *mGlobalSettings = nullptr;
bool mUsingGlobalArray = false;
Q_DISABLE_COPY( QgsSettings )
};
#endif // QGSSETTINGS_H