Merge pull request #6364 from 3nids/setting_enum_value

[settings] add method to get value for a setting associated to an enum
This commit is contained in:
Denis Rouzaud 2018-02-17 06:28:35 -09:00 committed by GitHub
commit f31ba78ccd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 0 deletions

View File

@ -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.

View File

@ -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.

View File

@ -165,6 +165,7 @@ SET(TESTS
testqgsrectangle.cpp
testqgsrenderers.cpp
testqgsrulebasedrenderer.cpp
testqgssettings.cpp
testqgsshapeburst.cpp
testqgssimplemarker.cpp
testqgssnappingutils.cpp

View File

@ -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"