Skip to content
Permalink
Browse files

Merge pull request #6364 from 3nids/setting_enum_value

[settings] add method to get value for a setting associated to an enum
  • Loading branch information
3nids committed Feb 17, 2018
2 parents 1253700 + bda67ad commit f31ba78ccd9c4b5b824bf383a1c8bf2068533ebe
Showing with 86 additions and 0 deletions.
  1. +2 −0 python/core/qgssettings.sip.in
  2. +27 −0 src/core/qgssettings.h
  3. +1 −0 tests/src/core/CMakeLists.txt
  4. +56 −0 tests/src/core/testqgssettings.cpp
@@ -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,31 @@ class CORE_EXPORT QgsSettings : public QObject
% End
#endif

#ifndef SIP_RUN

/**
* Return the setting value for a setting based on an enum.
* This forces the output to be a valid and existing entry of the enum.
* Hence if the setting value is incorrect, the given default value is returned.
* \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.
@@ -165,6 +165,7 @@ SET(TESTS
testqgsrectangle.cpp
testqgsrenderers.cpp
testqgsrulebasedrenderer.cpp
testqgssettings.cpp
testqgsshapeburst.cpp
testqgssimplemarker.cpp
testqgssnappingutils.cpp
@@ -0,0 +1,56 @@
/***************************************************************************
testqgssettings.cpp
--------------------------------------
Date : 17.02.2018
Copyright : (C) 2018 by Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QObject>


#include "qgssettings.h"
#include "qgsunittypes.h"
#include "qgstest.h"


/**
* \ingroup UnitTests
* This is a unit test for the operations on curve geometries
*/
class TestQgsSettings : public QObject
{
Q_OBJECT

private slots:
void enumSettingValue();
};


void TestQgsSettings::enumSettingValue()
{
QgsSettings settings;

// assign to inexisting value
settings.setValue( QStringLiteral( "qgis/testing/my_value_for_units" ), -1 );
// just to be sure it really doesn't exist
QVERIFY( static_cast<int>( QgsUnitTypes::LayoutMeters ) != -1 );

// standard method returns invalid value
int v1 = settings.value( QStringLiteral( "qgis/testing/my_value_for_units" ), QgsUnitTypes::LayoutMeters ).toInt();
QCOMPARE( v1, -1 );

// enum method returns default value if current setting is incorrect
QgsUnitTypes::LayoutUnit v2 = settings.enumSettingValue( QStringLiteral( "qgis/testing/my_value_for_units" ), QgsUnitTypes::LayoutMeters );
QCOMPARE( v2, QgsUnitTypes::LayoutMeters );
}


QGSTEST_MAIN( TestQgsSettings )
#include "testqgssettings.moc"

0 comments on commit f31ba78

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