QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 <QSettings>
21 #include <QMetaEnum>
22 
23 #include "qgis_core.h"
24 #include "qgis_sip.h"
25 #include "qgslogger.h"
26 
61 class CORE_EXPORT QgsSettings : public QObject
62 {
63  Q_OBJECT
64  public:
65 
67  enum Section
68  {
71  Gui,
75  App,
79  Gps,
80  };
81 
88  {
89  public:
90  static const inline char *APP_GEOREFERENCER = "app/georeferencer";
91  static const inline char *CORE = "core";
92  static const inline char *CORE_LAYOUT = "core/Layout";
93  static const inline char *GEOMETRYVALIDATION = "geometry_validation";
94  static const inline char *GPS = "gps";
95  static const inline char *GUI_LOCATORFILTERS = "gui/locator_filters";
96  static const inline char *GUI_QGIS = "gui/qgis";
97  static const inline char *LOCALE = "locale";
98  static const inline char *MAP = "Map";
99  static const inline char *PLUGINS = "plugins";
100  static const inline char *PROCESSING_CONFIGURATION = "Processing/Configuration";
101  static const inline char *QGIS = "qgis";
102  static const inline char *QGIS_DIGITIZING = "qgis/digitizing";
103  static const inline char *QGIS_DIGITIZING_SHAPEMAPTOOLS = "qgis/digitizing/shape-map-tools";
104  static const inline char *QGIS_NETWORKANDPROXY = "qgis/networkAndProxy";
105  static const inline char *SVG = "svg";
106  static const inline char *ELEVATION_PROFILE = "elevation-profile";
107  static const inline char *CORE_LAYERTREE = "core/layer-tree";
108  static const inline char *STYLE_MANAGER = "app/style-manager";
109  };
110 
115  explicit QgsSettings( const QString &organization,
116  const QString &application = QString(), QObject *parent = nullptr );
117 
132  QgsSettings( QSettings::Scope scope, const QString &organization,
133  const QString &application = QString(), QObject *parent = nullptr );
134 
149  QgsSettings( QSettings::Format format, QSettings::Scope scope, const QString &organization,
150  const QString &application = QString(), QObject *parent = nullptr );
151 
172  QgsSettings( const QString &fileName, QSettings::Format format, QObject *parent = nullptr );
173 
183  explicit QgsSettings( QObject *parent = nullptr );
184  ~QgsSettings() override;
185 
192  void beginGroup( const QString &prefix, QgsSettings::Section section = QgsSettings::NoSection );
194  void endGroup();
195 
202  QString group() const;
203 
205  QStringList allKeys() const;
207  QStringList childKeys() const;
209  QStringList childGroups() const;
211  QStringList globalChildGroups() const;
213  static QString globalSettingsPath();
215  static bool setGlobalSettingsPath( const QString &path );
217  int beginReadArray( const QString &prefix );
218 
224  void beginWriteArray( const QString &prefix, int size = -1 );
226  void endArray();
227 
232  void setArrayIndex( int i );
233 
238  void setValue( const QString &key, const QVariant &value, QgsSettings::Section section = QgsSettings::NoSection );
239 
246 #ifndef SIP_RUN
247  QVariant value( const QString &key, const QVariant &defaultValue = QVariant(),
248  Section section = NoSection ) const;
249 #else
250  SIP_PYOBJECT value( const QString &key, const QVariant &defaultValue = QVariant(),
251  SIP_PYOBJECT type = 0,
252  QgsSettings::Section section = QgsSettings::NoSection ) const / ReleaseGIL /;
253  % MethodCode
254  typedef PyObject *( *pyqt5_from_qvariant_by_type )( QVariant &value, PyObject *type );
255  QVariant value;
256 
257  // QSettings has an internal mutex so release the GIL to avoid the possibility of deadlocks.
258  Py_BEGIN_ALLOW_THREADS
259  value = sipCpp->value( *a0, *a1, a3 );
260  Py_END_ALLOW_THREADS
261 
262  pyqt5_from_qvariant_by_type f = ( pyqt5_from_qvariant_by_type ) sipImportSymbol( "pyqt5_from_qvariant_by_type" );
263  sipRes = f( value, a2 );
264 
265  sipIsErr = !sipRes;
266  % End
267 #endif
268 
269 #ifndef SIP_RUN
270 
281  template <class T>
282  T enumValue( const QString &key, const T &defaultValue,
283  const Section section = NoSection )
284  {
285  const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
286  Q_ASSERT( metaEnum.isValid() );
287  if ( !metaEnum.isValid() )
288  {
289  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
290  }
291 
292  T v;
293  bool ok = false;
294 
295  if ( metaEnum.isValid() )
296  {
297  // read as string
298  QByteArray ba = value( key, metaEnum.valueToKey( static_cast<const int>( defaultValue ) ), section ).toString().toUtf8();
299  const char *vs = ba.data();
300  v = static_cast<T>( metaEnum.keyToValue( vs, &ok ) );
301  if ( ok )
302  return v;
303  }
304 
305  // if failed, try to read as int (old behavior)
306  // this code shall be removed later (probably after QGIS 3.4 LTR for 3.6)
307  // then the method could be marked as const
308  v = static_cast<T>( value( key, static_cast<const int>( defaultValue ), section ).toInt( &ok ) );
309  if ( metaEnum.isValid() )
310  {
311  if ( !ok || !metaEnum.valueToKey( static_cast<int>( v ) ) )
312  {
313  v = defaultValue;
314  }
315  else
316  {
317  // found setting as an integer
318  // convert the setting to the new form (string)
319  setEnumValue( key, v, section );
320  }
321  }
322 
323  return v;
324  }
325 
333  template <class T>
334  void setEnumValue( const QString &key, const T &value,
335  const Section section = NoSection )
336  {
337  const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
338  Q_ASSERT( metaEnum.isValid() );
339  if ( metaEnum.isValid() )
340  {
341  setValue( key, metaEnum.valueToKey( static_cast<const int>( value ) ), section );
342  }
343  else
344  {
345  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
346  }
347  }
348 
359  template <class T>
360  T flagValue( const QString &key, const T &defaultValue,
361  const Section section = NoSection )
362  {
363  const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
364  Q_ASSERT( metaEnum.isValid() );
365  if ( !metaEnum.isValid() )
366  {
367  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
368  }
369 
370  T v = defaultValue;
371  bool ok = false;
372 
373  if ( metaEnum.isValid() )
374  {
375  // read as string
376  QByteArray ba = value( key, metaEnum.valueToKeys( static_cast< const int >( defaultValue ) ) ).toString().toUtf8();
377  const char *vs = ba.data();
378  v = static_cast<T>( metaEnum.keysToValue( vs, &ok ) );
379  }
380  if ( !ok )
381  {
382  // if failed, try to read as int
383  const int intValue = value( key, static_cast<const int>( defaultValue ), section ).toInt( &ok );
384  if ( metaEnum.isValid() )
385  {
386  if ( ok )
387  {
388  // check that the int value does correspond to a flag
389  // see https://stackoverflow.com/a/68495949/1548052
390  const QByteArray keys = metaEnum.valueToKeys( intValue );
391  const int intValueCheck = metaEnum.keysToValue( keys );
392  if ( intValue != intValueCheck )
393  {
394  v = defaultValue;
395  }
396  else
397  {
398  // found property as an integer
399  v = T( intValue );
400  // convert the property to the new form (string)
401  // this code could be removed
402  // then the method could be marked as const
403  setFlagValue( key, v );
404  }
405  }
406  else
407  {
408  v = defaultValue;
409  }
410  }
411  }
412 
413  return v;
414  }
415 
423  template <class T>
424  void setFlagValue( const QString &key, const T &value,
425  const Section section = NoSection )
426  {
427  const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
428  Q_ASSERT( metaEnum.isValid() );
429  if ( metaEnum.isValid() )
430  {
431  setValue( key, metaEnum.valueToKeys( static_cast< const int >( value ) ), section );
432  }
433  else
434  {
435  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
436  }
437  }
438 #endif
439 
444  bool contains( const QString &key, QgsSettings::Section section = QgsSettings::NoSection ) const;
446  QString fileName() const;
447 
454  void sync();
456  void remove( const QString &key, QgsSettings::Section section = QgsSettings::NoSection );
458  QString prefixedKey( const QString &key, QgsSettings::Section section ) const;
460  void clear();
461 
462  private:
463  void init();
464  QString sanitizeKey( const QString &key ) const;
465  QSettings *mUserSettings = nullptr;
466  QSettings *mGlobalSettings = nullptr;
467  bool mUsingGlobalArray = false;
468  Q_DISABLE_COPY( QgsSettings )
469 
470 };
471 
472 #endif // QGSSETTINGS_H
QgsSettings::Server
@ Server
Definition: qgssettings.h:72
QgsSettings::setFlagValue
void setFlagValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on a flag.
Definition: qgssettings.h:424
QgsSettings::Gps
@ Gps
GPS section, since QGIS 3.22.
Definition: qgssettings.h:79
QgsSettings::App
@ App
Definition: qgssettings.h:75
QgsSettings::Core
@ Core
Definition: qgssettings.h:70
QgsSettings::Providers
@ Providers
Definition: qgssettings.h:76
QGIS
As part of the API refactoring and improvements which landed in QGIS
Definition: porting_processing.dox:1
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsSettings::Expressions
@ Expressions
Definition: qgssettings.h:77
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsSettings::flagValue
T flagValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on a flag.
Definition: qgssettings.h:360
QgsSettings::setEnumValue
void setEnumValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on an enum.
Definition: qgssettings.h:334
qgis_sip.h
QgsSettings::Section
Section
Sections for namespaced settings.
Definition: qgssettings.h:67
QgsSettings::Plugins
@ Plugins
Definition: qgssettings.h:73
QgsSettings::NoSection
@ NoSection
Definition: qgssettings.h:69
QgsSettings::Auth
@ Auth
Definition: qgssettings.h:74
QgsSettings::enumValue
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
Definition: qgssettings.h:282
qgslogger.h
QgsSettings::Misc
@ Misc
Definition: qgssettings.h:78
QgsSettings::Gui
@ Gui
Definition: qgssettings.h:71
QgsSettings::Prefix
Prefixes for the settings keys.
Definition: qgssettings.h:87