Skip to content
Permalink
Browse files

add qgsEnumMap to list all entries (int, string) of enums

  • Loading branch information
3nids committed Sep 12, 2018
1 parent 827cfbc commit 3b1b41f3915079bba00a79b77998cb0cf65cf9be
Showing with 21 additions and 0 deletions.
  1. +1 −0 python/core/auto_generated/qgis.sip.in
  2. +20 −0 src/core/qgis.h
@@ -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
@@ -22,6 +22,8 @@
#include <QString>
#include <QRegExp>
#include <QMetaType>
#include <QMap>
#include <QMetaEnum>
#include <QVariant>
#include <QDateTime>
#include <QDate>
@@ -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

0 comments on commit 3b1b41f

Please sign in to comment.
You can’t perform that action at this time.