Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
extend QgsSettings::enumSettingValue to handle flags (with test)
  • Loading branch information
3nids committed Feb 19, 2018
1 parent d468e5b commit 4d6b0e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/core/qgssettings.h
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/src/core/testqgssettings.cpp
Expand Up @@ -17,6 +17,7 @@

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


Expand Down Expand Up @@ -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 );
}


Expand Down

0 comments on commit 4d6b0e6

Please sign in to comment.