Skip to content

Commit 4d6b0e6

Browse files
committedFeb 19, 2018
extend QgsSettings::enumSettingValue to handle flags (with test)
1 parent d468e5b commit 4d6b0e6

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed
 

‎src/core/qgssettings.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,27 @@ class CORE_EXPORT QgsSettings : public QObject
224224
* Return the setting value for a setting based on an enum.
225225
* This forces the output to be a valid and existing entry of the enum.
226226
* Hence if the setting value is incorrect, the given default value is returned.
227-
* \note The enum needs to be declared with Q_ENUM
227+
* If \a flag is true, the value is checked for a flag definition.
228+
* \note The enum needs to be declared with Q_ENUM, and flags with Q_FLAG (not Q_FLAGS).
228229
*/
229230
template <class T>
230231
T enumSettingValue( const QString &key, const T &defaultValue,
231-
const Section section = NoSection ) const
232+
const Section section = NoSection, bool flag = false ) const
232233
{
233-
T v = static_cast<T>( value( key, static_cast<int>( defaultValue ), section ).toInt() );
234+
T v;
235+
if ( !flag )
236+
v = static_cast<T>( value( key, static_cast<int>( defaultValue ), section ).toInt() );
237+
else
238+
v = T( value( key, static_cast<int>( defaultValue ), section ).toInt() );
239+
234240
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
235241
if ( metaEnum.isValid() )
236242
{
237-
if ( !metaEnum.valueToKey( static_cast<int>( v ) ) )
243+
if ( !flag && !metaEnum.valueToKey( static_cast<int>( v ) ) )
244+
{
245+
v = defaultValue;
246+
}
247+
else if ( flag && !metaEnum.valueToKeys( static_cast<int>( v ) ).size() )
238248
{
239249
v = defaultValue;
240250
}

‎tests/src/core/testqgssettings.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgssettings.h"
1919
#include "qgsunittypes.h"
20+
#include "qgsmaplayerproxymodel.h"
2021
#include "qgstest.h"
2122

2223

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

54+
// test a different value than default
5355
settings.setValue( QStringLiteral( "qgis/testing/my_value_for_units" ), QgsUnitTypes::LayoutCentimeters );
5456
QgsUnitTypes::LayoutUnit v3 = settings.enumSettingValue( QStringLiteral( "qgis/testing/my_value_for_units" ), QgsUnitTypes::LayoutMeters );
5557
QCOMPARE( v3, QgsUnitTypes::LayoutCentimeters );
58+
59+
// test for flags
60+
QgsMapLayerProxyModel::Filters pointAndLine = QgsMapLayerProxyModel::Filters( QgsMapLayerProxyModel::PointLayer | QgsMapLayerProxyModel::LineLayer );
61+
QgsMapLayerProxyModel::Filters pointAndPolygon = QgsMapLayerProxyModel::Filters( QgsMapLayerProxyModel::PointLayer | QgsMapLayerProxyModel::PolygonLayer );
62+
settings.setValue( QStringLiteral( "qgis/testing/my_value_for_a_flag" ), 1e8 ); // invalid
63+
QgsMapLayerProxyModel::Filters v4 = settings.enumSettingValue( QStringLiteral( "qgis/testing/my_value_for_a_flag" ), pointAndLine );
64+
QCOMPARE( v4, pointAndLine );
65+
66+
settings.setValue( QStringLiteral( "qgis/testing/my_value_for_a_flag" ), static_cast<int>( pointAndPolygon ) );
67+
QgsMapLayerProxyModel::Filters v5 = settings.enumSettingValue( QStringLiteral( "qgis/testing/my_value_for_a_flag" ), pointAndLine, QgsSettings::NoSection, true );
68+
QCOMPARE( v5, pointAndPolygon );
5669
}
5770

5871

0 commit comments

Comments
 (0)