Skip to content

Commit

Permalink
add enum value to key and reverse helpers (#8879)
Browse files Browse the repository at this point in the history
* add enum value to key and reverse helpers

* cast cont char* to QString

* use QStringLiteral
  • Loading branch information
3nids authored Jan 17, 2019
1 parent 68994ad commit 5fa5631
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
2 changes: 2 additions & 0 deletions python/core/auto_additions/qgsmaplayermodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# The following has been generated automatically from src/core/qgsmaplayermodel.h
QgsMapLayerModel.ItemDataRole.baseClass = QgsMapLayerModel
3 changes: 3 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion src/core/qgis.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsmaplayermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 14 additions & 2 deletions tests/src/core/testqgis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#include <memory>

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

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

private:
QString mReport;
Expand Down Expand Up @@ -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 )
Expand Down

0 comments on commit 5fa5631

Please sign in to comment.