Skip to content
Permalink
Browse files

[settings] add method to get value for a setting associated to an enum

this will make sure the returned value is actually an existing entry of the enum
  • Loading branch information
3nids committed Feb 17, 2018
1 parent 52585cf commit 1198dbad24e30dcb23d05adc7814f8e91607d99e
Showing with 28 additions and 0 deletions.
  1. +2 −0 python/core/qgssettings.sip.in
  2. +26 −0 src/core/qgssettings.h
@@ -9,6 +9,7 @@




class QgsSettings : QObject
{
%Docstring
@@ -220,6 +221,7 @@ An optional Section argument can be used to get a value from a specific Section.
sipIsErr = !sipRes;
%End


bool contains( const QString &key, const QgsSettings::Section section = QgsSettings::NoSection ) const;
%Docstring
Returns true if there exists a setting called key; returns false otherwise.
@@ -18,6 +18,8 @@
#define QGSSETTINGS_H

#include <QSettings>
#include <QMetaEnum>

#include "qgis_core.h"
#include "qgis.h"

@@ -216,6 +218,30 @@ class CORE_EXPORT QgsSettings : public QObject
% End
#endif

#ifndef SIP_RUN

/**
* Return the setting value for a setting defined on an enum.
* This forces the output to be a valid and existing entry of the enum.
* \note The enum needs to be declared with Q_ENUM
*/
template <class T>
T enumSettingValue( const QString &key, const T &defaultValue,
const Section section = NoSection ) const
{
T v = static_cast<T>( value( key, static_cast<int>( defaultValue ), section ).toInt() );
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
if ( metaEnum.isValid() )
{
if ( !metaEnum.valueToKey( static_cast<int>( v ) ) )
{
v = defaultValue;
}
}
return v;
}
#endif

/**
* 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.

0 comments on commit 1198dba

Please sign in to comment.
You can’t perform that action at this time.