Skip to content
Permalink
Browse files

add enum value to key and reverse helpers (#8879)

* add enum value to key and reverse helpers

* cast cont char* to QString

* use QStringLiteral
  • Loading branch information
3nids committed Jan 17, 2019
1 parent 68994ad commit 5fa5631bbdce5add0c1ad10268805d9d0d9b9936
@@ -0,0 +1,2 @@
# The following has been generated automatically from src/core/qgsmaplayermodel.h
QgsMapLayerModel.ItemDataRole.baseClass = QgsMapLayerModel
@@ -133,6 +133,9 @@ 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
@@ -430,7 +430,36 @@ template<class T> const QMap<T, QString> qgsEnumMap() SIP_SKIP
enumMap.insert( static_cast<T>( metaEnum.keyToValue( enumKey ) ), QString( enumKey ) );
}
return enumMap;
};
}

/**
* Returns the value for the given key of an enum.
* \since QGIS 3.6
*/
template<class T> QString qgsEnumValueToKey( const T &value ) SIP_SKIP
{
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
Q_ASSERT( metaEnum.isValid() );
return QString::fromUtf8( metaEnum.valueToKey( value ) );
}

/**
* Returns the value corresponding to the given \a key of an enum.
* If the key is invalid, it will return the \a defaultValue.
* \since QGIS 3.6
*/
template<class T> T qgsEnumKeyToValue( const QString &key, const T &defaultValue ) SIP_SKIP
{
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
Q_ASSERT( metaEnum.isValid() );
bool ok = false;
T v = static_cast<T>( metaEnum.keyToValue( key.toUtf8().data(), &ok ) );
if ( ok )
return v;
else
return defaultValue;
}


/**
* Converts a string to a double in a permissive way, e.g., allowing for incorrect
@@ -52,6 +52,7 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
EmptyRole, //!< True if index corresponds to the empty (not set) value
AdditionalRole, //!< True if index corresponds to an additional (non map layer) item
};
Q_ENUM( ItemDataRole )

/**
* \brief QgsMapLayerModel creates a model to display layers in widgets.
@@ -20,7 +20,8 @@
#include <memory>

//qgis includes...
#include <qgis.h>
#include "qgis.h"
#include "qgsmaplayermodel.h"

/**
* \ingroup UnitTests
@@ -46,6 +47,8 @@ class TestQgis : public QObject
void testQgsAsConst();
void testQgsRound();
void testQgsVariantEqual();
void testQgsEnumValueToKey();
void testQgsEnumKeyToValue();

private:
QString mReport;
@@ -388,9 +391,18 @@ void TestQgis::testQgsVariantEqual()
// NULL identities
QVERIFY( qgsVariantEqual( QVariant( QVariant::Int ), QVariant( QVariant::Int ) ) );
QVERIFY( qgsVariantEqual( QVariant( QVariant::Double ), QVariant( QVariant::Double ) ) );
}


void TestQgis::testQgsEnumValueToKey()
{
QCOMPARE( qgsEnumValueToKey<QgsMapLayerModel::ItemDataRole>( QgsMapLayerModel::LayerRole ), QStringLiteral( "LayerRole" ) );
}
void TestQgis::testQgsEnumKeyToValue()
{
QCOMPARE( qgsEnumKeyToValue<QgsMapLayerModel::ItemDataRole>( QStringLiteral( "LayerRole" ), QgsMapLayerModel::LayerIdRole ), QgsMapLayerModel::LayerRole );
QCOMPARE( qgsEnumKeyToValue<QgsMapLayerModel::ItemDataRole>( QStringLiteral( "UnknownKey" ), QgsMapLayerModel::LayerIdRole ), QgsMapLayerModel::LayerIdRole );
}



QGSTEST_MAIN( TestQgis )

0 comments on commit 5fa5631

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