Skip to content

Commit

Permalink
add qgsEnumMap to list all entries (int, string) of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Sep 12, 2018
1 parent 827cfbc commit 3b1b41f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -132,6 +132,7 @@ 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
20 changes: 20 additions & 0 deletions src/core/qgis.h
Expand Up @@ -22,6 +22,8 @@
#include <QString>
#include <QRegExp>
#include <QMetaType>
#include <QMap>
#include <QMetaEnum>
#include <QVariant>
#include <QDateTime>
#include <QDate>
Expand Down Expand Up @@ -387,6 +389,24 @@ namespace qgis
///@endcond
#endif

/**
* Returns a map of all enum entries.
* The map has the enum values (int) as keys and the enum keys (QString) as values.
* The enum must have been declared using Q_ENUM or Q_FLAG.
*/
template<class T> CORE_EXPORT const QMap<T, QString> qgsEnumMap() SIP_SKIP
{
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
Q_ASSERT( metaEnum.isValid() );
QMap<T, QString> enumMap;
for ( int idx = 0; idx < metaEnum.keyCount(); ++idx )
{
const char *enumKey = metaEnum.key( idx );
enumMap.insert( static_cast<T>( metaEnum.keyToValue( enumKey ) ), QString( enumKey ) );
}
return enumMap;
};

/**
* Converts a string to a double in a permissive way, e.g., allowing for incorrect
* numbers of digits between thousand separators
Expand Down

0 comments on commit 3b1b41f

Please sign in to comment.