QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 
58 class CORE_EXPORT QgsSettings : public QObject
59 {
60  Q_OBJECT
61  public:
62 
64  enum Section
65  {
68  Gui,
72  App,
75  Misc
76  };
77 
82  explicit QgsSettings( const QString &organization,
83  const QString &application = QString(), QObject *parent = nullptr );
84 
98  QgsSettings( QSettings::Scope scope, const QString &organization,
99  const QString &application = QString(), QObject *parent = nullptr );
100 
113  QgsSettings( QSettings::Format format, QSettings::Scope scope, const QString &organization,
114  const QString &application = QString(), QObject *parent = nullptr );
115 
135  QgsSettings( const QString &fileName, QSettings::Format format, QObject *parent = nullptr );
136 
146  explicit QgsSettings( QObject *parent = nullptr );
147  ~QgsSettings() override;
148 
155  void beginGroup( const QString &prefix, QgsSettings::Section section = QgsSettings::NoSection );
157  void endGroup();
158 
165  QString group() const;
166 
168  QStringList allKeys() const;
170  QStringList childKeys() const;
172  QStringList childGroups() const;
174  QStringList globalChildGroups() const;
176  static QString globalSettingsPath();
178  static bool setGlobalSettingsPath( const QString &path );
180  int beginReadArray( const QString &prefix );
181 
187  void beginWriteArray( const QString &prefix, int size = -1 );
189  void endArray();
190 
195  void setArrayIndex( int i );
196 
201  void setValue( const QString &key, const QVariant &value, QgsSettings::Section section = QgsSettings::NoSection );
202 
209 #ifndef SIP_RUN
210  QVariant value( const QString &key, const QVariant &defaultValue = QVariant(),
211  Section section = NoSection ) const;
212 #else
213  SIP_PYOBJECT value( const QString &key, const QVariant &defaultValue = QVariant(),
214  SIP_PYOBJECT type = 0,
215  QgsSettings::Section section = QgsSettings::NoSection ) const / ReleaseGIL /;
216  % MethodCode
217  typedef PyObject *( *pyqt5_from_qvariant_by_type )( QVariant &value, PyObject *type );
218  QVariant value;
219 
220  // QSettings has an internal mutex so release the GIL to avoid the possibility of deadlocks.
221  Py_BEGIN_ALLOW_THREADS
222  value = sipCpp->value( *a0, *a1, a3 );
223  Py_END_ALLOW_THREADS
224 
225  pyqt5_from_qvariant_by_type f = ( pyqt5_from_qvariant_by_type ) sipImportSymbol( "pyqt5_from_qvariant_by_type" );
226  sipRes = f( value, a2 );
227 
228  sipIsErr = !sipRes;
229  % End
230 #endif
231 
232 #ifndef SIP_RUN
233 
244  template <class T>
245  T enumValue( const QString &key, const T &defaultValue,
246  const Section section = NoSection )
247  {
248  QMetaEnum metaEnum = QMetaEnum::fromType<T>();
249  Q_ASSERT( metaEnum.isValid() );
250  if ( !metaEnum.isValid() )
251  {
252  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
253  }
254 
255  T v;
256  bool ok = false;
257 
258  if ( metaEnum.isValid() )
259  {
260  // read as string
261  QByteArray ba = value( key, metaEnum.valueToKey( static_cast<int>( defaultValue ) ), section ).toString().toUtf8();
262  const char *vs = ba.data();
263  v = static_cast<T>( metaEnum.keyToValue( vs, &ok ) );
264  if ( ok )
265  return v;
266  }
267 
268  // if failed, try to read as int (old behavior)
269  // this code shall be removed later (probably after QGIS 3.4 LTR for 3.6)
270  // then the method could be marked as const
271  v = static_cast<T>( value( key, static_cast<int>( defaultValue ), section ).toInt( &ok ) );
272  if ( metaEnum.isValid() )
273  {
274  if ( !ok || !metaEnum.valueToKey( static_cast<int>( v ) ) )
275  {
276  v = defaultValue;
277  }
278  else
279  {
280  // found setting as an integer
281  // convert the setting to the new form (string)
282  setEnumValue( key, v, section );
283  }
284  }
285 
286  return v;
287  }
288 
296  template <class T>
297  void setEnumValue( const QString &key, const T &value,
298  const Section section = NoSection )
299  {
300  QMetaEnum metaEnum = QMetaEnum::fromType<T>();
301  Q_ASSERT( metaEnum.isValid() );
302  if ( metaEnum.isValid() )
303  {
304  setValue( key, metaEnum.valueToKey( static_cast<int>( value ) ), section );
305  }
306  else
307  {
308  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
309  }
310  }
311 
322  template <class T>
323  T flagValue( const QString &key, const T &defaultValue,
324  const Section section = NoSection )
325  {
326  QMetaEnum metaEnum = QMetaEnum::fromType<T>();
327  Q_ASSERT( metaEnum.isValid() );
328  if ( !metaEnum.isValid() )
329  {
330  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
331  }
332 
333  T v = defaultValue;
334  bool ok = false;
335 
336  if ( metaEnum.isValid() )
337  {
338  // read as string
339  QByteArray ba = value( key, metaEnum.valueToKeys( defaultValue ) ).toString().toUtf8();
340  const char *vs = ba.data();
341  v = static_cast<T>( metaEnum.keysToValue( vs, &ok ) );
342  }
343  if ( !ok )
344  {
345  // if failed, try to read as int (old behavior)
346  // this code shall be removed later (probably after QGIS 3.4 LTR for 3.6)
347  // then the method could be marked as const
348  v = T( value( key, static_cast<int>( defaultValue ), section ).toInt( &ok ) );
349  if ( metaEnum.isValid() )
350  {
351  if ( !ok || metaEnum.valueToKeys( static_cast<int>( v ) ).isEmpty() )
352  {
353  v = defaultValue;
354  }
355  else
356  {
357  // found setting as an integer
358  // convert the setting to the new form (string)
359  setFlagValue( key, v, section );
360  }
361  }
362  }
363 
364  return v;
365  }
366 
374  template <class T>
375  void setFlagValue( const QString &key, const T &value,
376  const Section section = NoSection )
377  {
378  QMetaEnum metaEnum = QMetaEnum::fromType<T>();
379  Q_ASSERT( metaEnum.isValid() );
380  if ( metaEnum.isValid() )
381  {
382  setValue( key, metaEnum.valueToKeys( value ), section );
383  }
384  else
385  {
386  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
387  }
388  }
389 #endif
390 
395  bool contains( const QString &key, QgsSettings::Section section = QgsSettings::NoSection ) const;
397  QString fileName() const;
398 
405  void sync();
407  void remove( const QString &key, QgsSettings::Section section = QgsSettings::NoSection );
409  QString prefixedKey( const QString &key, QgsSettings::Section section ) const;
411  void clear();
412 
413  private:
414  void init();
415  QString sanitizeKey( const QString &key ) const;
416  QSettings *mUserSettings = nullptr;
417  QSettings *mGlobalSettings = nullptr;
418  bool mUsingGlobalArray = false;
419  Q_DISABLE_COPY( QgsSettings )
420 
421 };
422 
423 #endif // QGSSETTINGS_H
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void setFlagValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on a flaf.
Definition: qgssettings.h:375
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:323
Section
Sections for namespaced settings.
Definition: qgssettings.h:64
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:297
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:245