From 1198dbad24e30dcb23d05adc7814f8e91607d99e Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Fri, 16 Feb 2018 21:37:05 -0400 Subject: [PATCH 1/2] [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 --- python/core/qgssettings.sip.in | 2 ++ src/core/qgssettings.h | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/python/core/qgssettings.sip.in b/python/core/qgssettings.sip.in index eacfd8b80b3..6024901d350 100644 --- a/python/core/qgssettings.sip.in +++ b/python/core/qgssettings.sip.in @@ -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. diff --git a/src/core/qgssettings.h b/src/core/qgssettings.h index 6df36f655ef..e4f4e7e4b81 100644 --- a/src/core/qgssettings.h +++ b/src/core/qgssettings.h @@ -18,6 +18,8 @@ #define QGSSETTINGS_H #include +#include + #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 + T enumSettingValue( const QString &key, const T &defaultValue, + const Section section = NoSection ) const + { + T v = static_cast( value( key, static_cast( defaultValue ), section ).toInt() ); + QMetaEnum metaEnum = QMetaEnum::fromType(); + if ( metaEnum.isValid() ) + { + if ( !metaEnum.valueToKey( static_cast( 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. From bda67ada90f52e5fcc800b64d94d3816e32d6106 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Sat, 17 Feb 2018 10:22:04 -0400 Subject: [PATCH 2/2] add test for QgsSettings::enumSettingValue --- src/core/qgssettings.h | 3 +- tests/src/core/CMakeLists.txt | 1 + tests/src/core/testqgssettings.cpp | 56 ++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/src/core/testqgssettings.cpp diff --git a/src/core/qgssettings.h b/src/core/qgssettings.h index e4f4e7e4b81..3e4abe7cfc0 100644 --- a/src/core/qgssettings.h +++ b/src/core/qgssettings.h @@ -221,8 +221,9 @@ class CORE_EXPORT QgsSettings : public QObject #ifndef SIP_RUN /** - * Return the setting value for a setting defined on an enum. + * 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 diff --git a/tests/src/core/CMakeLists.txt b/tests/src/core/CMakeLists.txt index 659529cd975..1268ddaae83 100755 --- a/tests/src/core/CMakeLists.txt +++ b/tests/src/core/CMakeLists.txt @@ -165,6 +165,7 @@ SET(TESTS testqgsrectangle.cpp testqgsrenderers.cpp testqgsrulebasedrenderer.cpp + testqgssettings.cpp testqgsshapeburst.cpp testqgssimplemarker.cpp testqgssnappingutils.cpp diff --git a/tests/src/core/testqgssettings.cpp b/tests/src/core/testqgssettings.cpp new file mode 100644 index 00000000000..c60c3ab0822 --- /dev/null +++ b/tests/src/core/testqgssettings.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 + + +#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( 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"