add methods to retrieve from/to value to keys for flags (#37752)

* add methods to retrieve from/to value to keys for flags

* sipify
This commit is contained in:
Denis Rouzaud 2020-07-11 12:33:57 +02:00 committed by GitHub
parent 06fea4ffe4
commit a61a83d648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

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

View File

@ -527,6 +527,34 @@ template<class T> 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<class T> QString qgsFlagValueToKeys( const T &value ) SIP_SKIP
{
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
Q_ASSERT( metaEnum.isValid() );
return QString::fromUtf8( metaEnum.valueToKeys( static_cast<int>( 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<class T> T qgsFlagKeysToValue( const QString &keys, const T &defaultValue ) SIP_SKIP
{
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
Q_ASSERT( metaEnum.isValid() );
bool ok = false;
T v = static_cast<T>( 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