Skip to content

Commit 3b1b41f

Browse files
committedSep 12, 2018
add qgsEnumMap to list all entries (int, string) of enums
1 parent 827cfbc commit 3b1b41f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
 

‎python/core/auto_generated/qgis.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ Returns a double ``number``, rounded (as close as possible) to the specified num
132132

133133

134134

135+
135136
double qgsPermissiveToDouble( QString string, bool &ok );
136137
%Docstring
137138
Converts a string to a double in a permissive way, e.g., allowing for incorrect

‎src/core/qgis.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <QString>
2323
#include <QRegExp>
2424
#include <QMetaType>
25+
#include <QMap>
26+
#include <QMetaEnum>
2527
#include <QVariant>
2628
#include <QDateTime>
2729
#include <QDate>
@@ -387,6 +389,24 @@ namespace qgis
387389
///@endcond
388390
#endif
389391

392+
/**
393+
* Returns a map of all enum entries.
394+
* The map has the enum values (int) as keys and the enum keys (QString) as values.
395+
* The enum must have been declared using Q_ENUM or Q_FLAG.
396+
*/
397+
template<class T> CORE_EXPORT const QMap<T, QString> qgsEnumMap() SIP_SKIP
398+
{
399+
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
400+
Q_ASSERT( metaEnum.isValid() );
401+
QMap<T, QString> enumMap;
402+
for ( int idx = 0; idx < metaEnum.keyCount(); ++idx )
403+
{
404+
const char *enumKey = metaEnum.key( idx );
405+
enumMap.insert( static_cast<T>( metaEnum.keyToValue( enumKey ) ), QString( enumKey ) );
406+
}
407+
return enumMap;
408+
};
409+
390410
/**
391411
* Converts a string to a double in a permissive way, e.g., allowing for incorrect
392412
* numbers of digits between thousand separators

0 commit comments

Comments
 (0)
Please sign in to comment.