Skip to content

Commit

Permalink
[settings] add method to get value for a setting associated to an enum
Browse files Browse the repository at this point in the history
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 1198dba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/core/qgssettings.sip.in
Expand Up @@ -9,6 +9,7 @@




class QgsSettings : QObject
{
%Docstring
Expand Down Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions src/core/qgssettings.h
Expand Up @@ -18,6 +18,8 @@
#define QGSSETTINGS_H

#include <QSettings>
#include <QMetaEnum>

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

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1198dba

Please sign in to comment.