Skip to content
Permalink
Browse files

extend QgsSettings::enumSettingValue to handle flags (with test)

  • Loading branch information
3nids committed Feb 19, 2018
1 parent d468e5b commit 4d6b0e6aeac3ba1e7ca9cb056b2c477727d32003
Showing with 27 additions and 4 deletions.
  1. +14 −4 src/core/qgssettings.h
  2. +13 −0 tests/src/core/testqgssettings.cpp
@@ -224,17 +224,27 @@ class CORE_EXPORT QgsSettings : public QObject
* 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
* If \a flag is true, the value is checked for a flag definition.
* \note The enum needs to be declared with Q_ENUM, and flags with Q_FLAG (not Q_FLAGS).
*/
template <class T>
T enumSettingValue( const QString &key, const T &defaultValue,
const Section section = NoSection ) const
const Section section = NoSection, bool flag = false ) const
{
T v = static_cast<T>( value( key, static_cast<int>( defaultValue ), section ).toInt() );
T v;
if ( !flag )
v = static_cast<T>( value( key, static_cast<int>( defaultValue ), section ).toInt() );
else
v = T( value( key, static_cast<int>( defaultValue ), section ).toInt() );

QMetaEnum metaEnum = QMetaEnum::fromType<T>();
if ( metaEnum.isValid() )
{
if ( !metaEnum.valueToKey( static_cast<int>( v ) ) )
if ( !flag && !metaEnum.valueToKey( static_cast<int>( v ) ) )
{
v = defaultValue;
}
else if ( flag && !metaEnum.valueToKeys( static_cast<int>( v ) ).size() )
{
v = defaultValue;
}
@@ -17,6 +17,7 @@

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


@@ -50,9 +51,21 @@ void TestQgsSettings::enumSettingValue()
QgsUnitTypes::LayoutUnit v2 = settings.enumSettingValue( QStringLiteral( "qgis/testing/my_value_for_units" ), QgsUnitTypes::LayoutMeters );
QCOMPARE( v2, QgsUnitTypes::LayoutMeters );

// test a different value than default
settings.setValue( QStringLiteral( "qgis/testing/my_value_for_units" ), QgsUnitTypes::LayoutCentimeters );
QgsUnitTypes::LayoutUnit v3 = settings.enumSettingValue( QStringLiteral( "qgis/testing/my_value_for_units" ), QgsUnitTypes::LayoutMeters );
QCOMPARE( v3, QgsUnitTypes::LayoutCentimeters );

// test for flags
QgsMapLayerProxyModel::Filters pointAndLine = QgsMapLayerProxyModel::Filters( QgsMapLayerProxyModel::PointLayer | QgsMapLayerProxyModel::LineLayer );
QgsMapLayerProxyModel::Filters pointAndPolygon = QgsMapLayerProxyModel::Filters( QgsMapLayerProxyModel::PointLayer | QgsMapLayerProxyModel::PolygonLayer );
settings.setValue( QStringLiteral( "qgis/testing/my_value_for_a_flag" ), 1e8 ); // invalid
QgsMapLayerProxyModel::Filters v4 = settings.enumSettingValue( QStringLiteral( "qgis/testing/my_value_for_a_flag" ), pointAndLine );
QCOMPARE( v4, pointAndLine );

settings.setValue( QStringLiteral( "qgis/testing/my_value_for_a_flag" ), static_cast<int>( pointAndPolygon ) );
QgsMapLayerProxyModel::Filters v5 = settings.enumSettingValue( QStringLiteral( "qgis/testing/my_value_for_a_flag" ), pointAndLine, QgsSettings::NoSection, true );
QCOMPARE( v5, pointAndPolygon );
}


0 comments on commit 4d6b0e6

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