Skip to content

Commit

Permalink
add methods to retrieve from/to value to keys for flags (#37752)
Browse files Browse the repository at this point in the history
* add methods to retrieve from/to value to keys for flags

* sipify
  • Loading branch information
3nids committed Jul 11, 2020
1 parent 06fea4f commit a61a83d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/core/qgis.h
Expand Up @@ -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
Expand Down

0 comments on commit a61a83d

Please sign in to comment.