From a61a83d6486bb368a1f0527b03494b532737ae07 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Sat, 11 Jul 2020 12:33:57 +0200 Subject: [PATCH] add methods to retrieve from/to value to keys for flags (#37752) * add methods to retrieve from/to value to keys for flags * sipify --- python/core/auto_generated/qgis.sip.in | 2 ++ src/core/qgis.h | 28 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/python/core/auto_generated/qgis.sip.in b/python/core/auto_generated/qgis.sip.in index bd967c06d8a..3ad07728d09 100644 --- a/python/core/auto_generated/qgis.sip.in +++ b/python/core/auto_generated/qgis.sip.in @@ -181,6 +181,8 @@ Returns a double ``number``, rounded (as close as possible) to the specified num + + double qgsPermissiveToDouble( QString string, bool &ok ); %Docstring Converts a string to a double in a permissive way, e.g., allowing for incorrect diff --git a/src/core/qgis.h b/src/core/qgis.h index c8090fa0d77..f9dea8fa220 100644 --- a/src/core/qgis.h +++ b/src/core/qgis.h @@ -527,6 +527,34 @@ template T qgsEnumKeyToValue( const QString &key, const T &defaultValue return defaultValue; } +/** + * Returns the value for the given keys of a flag. + * \since QGIS 3.16 + */ +template QString qgsFlagValueToKeys( const T &value ) SIP_SKIP +{ + QMetaEnum metaEnum = QMetaEnum::fromType(); + Q_ASSERT( metaEnum.isValid() ); + return QString::fromUtf8( metaEnum.valueToKeys( static_cast( value ) ) ); +} + +/** + * Returns the value corresponding to the given \a keys of a flag. + * If the keys are invalid, it will return the \a defaultValue. + * \since QGIS 3.16 + */ +template T qgsFlagKeysToValue( const QString &keys, const T &defaultValue ) SIP_SKIP +{ + QMetaEnum metaEnum = QMetaEnum::fromType(); + Q_ASSERT( metaEnum.isValid() ); + bool ok = false; + T v = static_cast( metaEnum.keysToValue( keys.toUtf8().constData(), &ok ) ); + if ( ok ) + return v; + else + return defaultValue; +} + /** * Converts a string to a double in a permissive way, e.g., allowing for incorrect