QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgssettings.h
Go to the documentation of this file.
1/***************************************************************************
2 qgssettings.h
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#ifndef QGSSETTINGS_H
18#define QGSSETTINGS_H
19
20#include "qgis_core.h"
21#include "qgis_sip.h"
22#include "qgslogger.h"
23#include "qgssettingstreenode.h"
24
25#include <QMetaEnum>
26#include <QSettings>
27#include <QString>
28
29using namespace Qt::StringLiterals;
30
32
67class CORE_EXPORT QgsSettings : public QObject
68{
69 Q_OBJECT
70 public:
71
87
92 explicit QgsSettings( const QString &organization,
93 const QString &application = QString(), QObject *parent = nullptr );
94
109 QgsSettings( QSettings::Scope scope, const QString &organization,
110 const QString &application = QString(), QObject *parent = nullptr );
111
126 QgsSettings( QSettings::Format format, QSettings::Scope scope, const QString &organization,
127 const QString &application = QString(), QObject *parent = nullptr );
128
149 QgsSettings( const QString &fileName, QSettings::Format format, QObject *parent = nullptr );
150
160 explicit QgsSettings( QObject *parent = nullptr );
161 ~QgsSettings() override;
162
169 void beginGroup( const QString &prefix, QgsSettings::Section section = QgsSettings::NoSection );
171 void endGroup();
172
179 QString group() const;
180
182 QStringList allKeys() const;
184 QStringList childKeys() const;
186 QStringList childGroups( Qgis::SettingsOrigin origin = Qgis::SettingsOrigin::Any ) const;
188 QStringList globalChildGroups() const;
190 static QString globalSettingsPath();
192 static bool setGlobalSettingsPath( const QString &path );
194 int beginReadArray( const QString &prefix );
195
201 void beginWriteArray( const QString &prefix, int size = -1 );
203 void endArray();
204
209 void setArrayIndex( int i );
210
216 Qgis::SettingsOrigin origin( const QString &key ) const;
217
222 void setValue( const QString &key, const QVariant &value, QgsSettings::Section section = QgsSettings::NoSection );
223
230#ifndef SIP_RUN
231 QVariant value( const QString &key, const QVariant &defaultValue = QVariant(),
232 Section section = NoSection ) const;
233#else
234 SIP_PYOBJECT value( const QString &key, const QVariant &defaultValue = QVariant(),
235 SIP_PYOBJECT type = 0,
236 QgsSettings::Section section = QgsSettings::NoSection ) const / ReleaseGIL /;
237 % MethodCode
238 typedef PyObject *( *pyqt_from_qvariant_by_type )( QVariant &value, PyObject *type );
239 QVariant value;
240
241 // QSettings has an internal mutex so release the GIL to avoid the possibility of deadlocks.
242 Py_BEGIN_ALLOW_THREADS
243 value = sipCpp->value( *a0, *a1, a3 );
244 Py_END_ALLOW_THREADS
245
246 pyqt_from_qvariant_by_type f = ( pyqt_from_qvariant_by_type ) sipImportSymbol( SIP_PYQT_FROM_QVARIANT_BY_TYPE );
247 sipRes = f( value, a2 );
248
249 sipIsErr = !sipRes;
250 % End
251#endif
252
253
254#ifndef SIP_RUN
255
266 template <class T>
267 T enumValue( const QString &key, const T &defaultValue,
268 const Section section = NoSection )
269 {
270 const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
271 Q_ASSERT( metaEnum.isValid() );
272 if ( !metaEnum.isValid() )
273 {
274 QgsDebugError( u"Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration."_s );
275 }
276
277 T v;
278 bool ok = false;
279
280 if ( metaEnum.isValid() )
281 {
282 // read as string
283 QByteArray ba = value( key, metaEnum.valueToKey( static_cast<const int>( defaultValue ) ), section ).toString().toUtf8();
284 const char *vs = ba.data();
285 v = static_cast<T>( metaEnum.keyToValue( vs, &ok ) );
286 if ( ok )
287 return v;
288 }
289
290 // if failed, try to read as int (old behavior)
291 // this code shall be removed later (probably after QGIS 3.4 LTR for 3.6)
292 // then the method could be marked as const
293 v = static_cast<T>( value( key, static_cast<const int>( defaultValue ), section ).toInt( &ok ) );
294 if ( metaEnum.isValid() )
295 {
296 if ( !ok || !metaEnum.valueToKey( static_cast<int>( v ) ) )
297 {
298 v = defaultValue;
299 }
300 else
301 {
302 // found setting as an integer
303 // convert the setting to the new form (string)
304 setEnumValue( key, v, section );
305 }
306 }
307
308 return v;
309 }
310
318 template <class T>
319 void setEnumValue( const QString &key, const T &value,
320 const Section section = NoSection )
321 {
322 const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
323 Q_ASSERT( metaEnum.isValid() );
324 if ( metaEnum.isValid() )
325 {
326 setValue( key, metaEnum.valueToKey( static_cast<const int>( value ) ), section );
327 }
328 else
329 {
330 QgsDebugError( u"Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration."_s );
331 }
332 }
333
344 template <class T>
345 T flagValue( const QString &key, const T &defaultValue,
346 const Section section = NoSection )
347 {
348 const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
349 Q_ASSERT( metaEnum.isValid() );
350 if ( !metaEnum.isValid() )
351 {
352 QgsDebugError( u"Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration."_s );
353 }
354
355 T v = defaultValue;
356 bool ok = false;
357
358 if ( metaEnum.isValid() )
359 {
360 // read as string
361 QByteArray ba = value( key, metaEnum.valueToKeys( static_cast< const int >( defaultValue ) ) ).toString().toUtf8();
362 const char *vs = ba.data();
363 v = static_cast<T>( metaEnum.keysToValue( vs, &ok ) );
364 }
365 if ( !ok )
366 {
367 // if failed, try to read as int
368 const int intValue = value( key, static_cast<const int>( defaultValue ), section ).toInt( &ok );
369 if ( metaEnum.isValid() )
370 {
371 if ( ok )
372 {
373 // check that the int value does correspond to a flag
374 // see https://stackoverflow.com/a/68495949/1548052
375 const QByteArray keys = metaEnum.valueToKeys( intValue );
376 const int intValueCheck = metaEnum.keysToValue( keys );
377 if ( intValue != intValueCheck )
378 {
379 v = defaultValue;
380 }
381 else
382 {
383 // found property as an integer
384 v = T( intValue );
385 // convert the property to the new form (string)
386 // this code could be removed
387 // then the method could be marked as const
388 setFlagValue( key, v );
389 }
390 }
391 else
392 {
393 v = defaultValue;
394 }
395 }
396 }
397
398 return v;
399 }
400
408 template <class T>
409 void setFlagValue( const QString &key, const T &value,
410 const Section section = NoSection )
411 {
412 const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
413 Q_ASSERT( metaEnum.isValid() );
414 if ( metaEnum.isValid() )
415 {
416 setValue( key, metaEnum.valueToKeys( static_cast< const int >( value ) ), section );
417 }
418 else
419 {
420 QgsDebugError( u"Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration."_s );
421 }
422 }
423#endif
424
429 bool contains( const QString &key, QgsSettings::Section section = QgsSettings::NoSection ) const;
431 QString fileName() const;
432
439 void sync();
441 void remove( const QString &key, QgsSettings::Section section = QgsSettings::NoSection );
443 QString prefixedKey( const QString &key, QgsSettings::Section section ) const;
445 void clear();
446
465 static void holdFlush() SIP_SKIP;
466
481 static void releaseFlush() SIP_SKIP;
482
496 static QgsSettingsProxy get() SIP_SKIP;
497
498 private:
499 void init();
500 QString sanitizeKey( const QString &key ) const;
501 std::unique_ptr<QSettings> mUserSettings;
502 std::unique_ptr<QSettings> mGlobalSettings;
503 bool mUsingGlobalArray = false;
504 Q_DISABLE_COPY( QgsSettings )
505
506};
507
508// as static members cannot be CORE_EXPORTed
510
511#endif // QGSSETTINGS_H
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:4524
@ Any
From any origin.
Definition qgis.h:4525
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
T flagValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on a flag.
void setFlagValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on a flag.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setEnumValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on an enum.
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...
Section
Sections for namespaced settings.
Definition qgssettings.h:74
@ Gps
GPS section, since QGIS 3.22.
Definition qgssettings.h:85
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
#define SIP_SKIP
Definition qgis_sip.h:134
#define QgsDebugError(str)
Definition qgslogger.h:59
QgsSettings * sQgsSettingsThreadSettings