QGIS API Documentation  3.0.2-Girona (307d082)
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.h"
25 
57 class CORE_EXPORT QgsSettings : public QObject
58 {
59  Q_OBJECT
60  public:
61 
63  enum Section
64  {
67  Gui,
71  App,
73  Misc
74  };
75 
80  explicit QgsSettings( const QString &organization,
81  const QString &application = QString(), QObject *parent = nullptr );
82 
96  QgsSettings( QSettings::Scope scope, const QString &organization,
97  const QString &application = QString(), QObject *parent = nullptr );
98 
111  QgsSettings( QSettings::Format format, QSettings::Scope scope, const QString &organization,
112  const QString &application = QString(), QObject *parent = nullptr );
113 
133  QgsSettings( const QString &fileName, QSettings::Format format, QObject *parent = nullptr );
134 
144  explicit QgsSettings( QObject *parent = nullptr );
145  ~QgsSettings() override;
146 
153  void beginGroup( const QString &prefix, const QgsSettings::Section section = QgsSettings::NoSection );
155  void endGroup();
157  QStringList allKeys() const;
159  QStringList childKeys() const;
161  QStringList childGroups() const;
163  QStringList globalChildGroups() const;
165  static QString globalSettingsPath() { return sGlobalSettingsPath; }
167  static bool setGlobalSettingsPath( const QString &path );
169  int beginReadArray( const QString &prefix );
170 
176  void beginWriteArray( const QString &prefix, int size = -1 );
178  void endArray();
179 
184  void setArrayIndex( int i );
185 
190  void setValue( const QString &key, const QVariant &value, const QgsSettings::Section section = QgsSettings::NoSection );
191 
198 #ifndef SIP_RUN
199  QVariant value( const QString &key, const QVariant &defaultValue = QVariant(),
200  const Section section = NoSection ) const;
201 #else
202  SIP_PYOBJECT value( const QString &key, const QVariant &defaultValue = QVariant(),
203  SIP_PYOBJECT type = 0,
204  QgsSettings::Section section = QgsSettings::NoSection ) const / ReleaseGIL /;
205  % MethodCode
206  typedef PyObject *( *pyqt5_from_qvariant_by_type )( QVariant &value, PyObject *type );
207  QVariant value;
208 
209  // QSettings has an internal mutex so release the GIL to avoid the possibility of deadlocks.
210  Py_BEGIN_ALLOW_THREADS
211  value = sipCpp->value( *a0, *a1, a3 );
212  Py_END_ALLOW_THREADS
213 
214  pyqt5_from_qvariant_by_type f = ( pyqt5_from_qvariant_by_type ) sipImportSymbol( "pyqt5_from_qvariant_by_type" );
215  sipRes = f( value, a2 );
216 
217  sipIsErr = !sipRes;
218  % End
219 #endif
220 
221 #ifndef SIP_RUN
222 
230  template <class T>
231  T enumValue( const QString &key, const T &defaultValue,
232  const Section section = NoSection, bool flag = false ) const
233  {
234  T v;
235  if ( !flag )
236  v = static_cast<T>( value( key, static_cast<int>( defaultValue ), section ).toInt() );
237  else
238  v = T( value( key, static_cast<int>( defaultValue ), section ).toInt() );
239 
240  QMetaEnum metaEnum = QMetaEnum::fromType<T>();
241  if ( metaEnum.isValid() )
242  {
243  if ( !flag && !metaEnum.valueToKey( static_cast<int>( v ) ) )
244  {
245  v = defaultValue;
246  }
247  else if ( flag && !metaEnum.valueToKeys( static_cast<int>( v ) ).size() )
248  {
249  v = defaultValue;
250  }
251  }
252  return v;
253  }
254 #endif
255 
260  bool contains( const QString &key, const QgsSettings::Section section = QgsSettings::NoSection ) const;
262  QString fileName() const;
263 
270  void sync();
272  void remove( const QString &key, const QgsSettings::Section section = QgsSettings::NoSection );
274  QString prefixedKey( const QString &key, const QgsSettings::Section section ) const;
276  void clear();
277 
278  private:
279 
280  static QString sGlobalSettingsPath;
281  void init();
282  QString sanitizeKey( const QString &key ) const;
283  QSettings *mUserSettings = nullptr;
284  QSettings *mGlobalSettings = nullptr;
285  bool mUsingGlobalArray = false;
286  Q_DISABLE_COPY( QgsSettings )
287 
288 };
289 
290 #endif // QGSSETTINGS_H
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection, bool flag=false) const
Return the setting value for a setting based on an enum.
Definition: qgssettings.h:231
This class is a composition of two QSettings instances:
Definition: qgssettings.h:57
static QString globalSettingsPath()
Return the path to the Global Settings QSettings storage file.
Definition: qgssettings.h:165
Section
Sections for namespaced settings.
Definition: qgssettings.h:63