Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a __repr__ method for QgsProperty
  • Loading branch information
nyalldawson committed Apr 7, 2020
1 parent f4bf9e5 commit 8050612
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
33 changes: 33 additions & 0 deletions python/core/auto_generated/qgsproperty.sip.in
Expand Up @@ -491,6 +491,39 @@ be called in non-performance critical code.

operator QVariant() const;


SIP_PYOBJECT __repr__();
%MethodCode
QString typeString;
QString definitionString;
switch ( sipCpp->propertyType() )
{
case QgsProperty::StaticProperty:
typeString = QStringLiteral( "static" );
definitionString = sipCpp->staticValue().toString();
break;

case QgsProperty::FieldBasedProperty:
typeString = QStringLiteral( "field" );
definitionString = sipCpp->field();
break;

case QgsProperty::ExpressionBasedProperty:
typeString = QStringLiteral( "expression" );
definitionString = sipCpp->expressionString();
break;

case QgsProperty::InvalidProperty:
typeString = QStringLiteral( "invalid" );
break;
}

QString str = QStringLiteral( "<QgsProperty: %1%2%3>" ).arg( !sipCpp->isActive() && sipCpp->propertyType() != QgsProperty::InvalidProperty ? QStringLiteral( "INACTIVE " ) : QString(),
typeString,
definitionString.isEmpty() ? QString() : QStringLiteral( " (%1)" ).arg( definitionString ) );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

};


Expand Down
35 changes: 35 additions & 0 deletions src/core/qgsproperty.h
Expand Up @@ -489,6 +489,41 @@ class CORE_EXPORT QgsProperty
return QVariant::fromValue( *this );
}


#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString typeString;
QString definitionString;
switch ( sipCpp->propertyType() )
{
case QgsProperty::StaticProperty:
typeString = QStringLiteral( "static" );
definitionString = sipCpp->staticValue().toString();
break;

case QgsProperty::FieldBasedProperty:
typeString = QStringLiteral( "field" );
definitionString = sipCpp->field();
break;

case QgsProperty::ExpressionBasedProperty:
typeString = QStringLiteral( "expression" );
definitionString = sipCpp->expressionString();
break;

case QgsProperty::InvalidProperty:
typeString = QStringLiteral( "invalid" );
break;
}

QString str = QStringLiteral( "<QgsProperty: %1%2%3>" ).arg( !sipCpp->isActive() && sipCpp->propertyType() != QgsProperty::InvalidProperty ? QStringLiteral( "INACTIVE " ) : QString(),
typeString,
definitionString.isEmpty() ? QString() : QStringLiteral( " (%1)" ).arg( definitionString ) );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

private:

mutable QExplicitlySharedDataPointer<QgsPropertyPrivate> d;
Expand Down
18 changes: 17 additions & 1 deletion tests/src/python/test_python_repr.py
Expand Up @@ -18,7 +18,7 @@
QgsCurvePolygon, QgsEllipse, QgsLineString, QgsMultiCurve, QgsRectangle, QgsExpression, QgsField, QgsError,\
QgsMimeDataUtils, QgsVector, QgsVector3D, QgsVectorLayer, QgsReferencedPointXY, QgsReferencedRectangle,\
QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsProject, QgsClassificationRange, QgsBookmark, \
QgsLayoutMeasurement, QgsLayoutPoint, QgsLayoutSize, QgsUnitTypes, QgsConditionalStyle, QgsTableCell
QgsLayoutMeasurement, QgsLayoutPoint, QgsLayoutSize, QgsUnitTypes, QgsConditionalStyle, QgsTableCell, QgsProperty

start_app()

Expand Down Expand Up @@ -225,6 +225,22 @@ def testQgsTableCell(self):
b.setContent(5)
self.assertEqual(b.__repr__(), "<QgsTableCell: 5>")

def testQgsProperty(self):
p = QgsProperty.fromValue(5)
self.assertEqual(p.__repr__(), '<QgsProperty: static (5)>')
p = QgsProperty.fromField('my_field')
self.assertEqual(p.__repr__(), '<QgsProperty: field (my_field)>')
p = QgsProperty.fromExpression('5*5 || \'a\'')
self.assertEqual(p.__repr__(), '<QgsProperty: expression (5*5 || \'a\')>')
p = QgsProperty.fromValue(5, False)
self.assertEqual(p.__repr__(), '<QgsProperty: INACTIVE static (5)>')
p = QgsProperty.fromField('my_field', False)
self.assertEqual(p.__repr__(), '<QgsProperty: INACTIVE field (my_field)>')
p = QgsProperty.fromExpression('5*5 || \'a\'', False)
self.assertEqual(p.__repr__(), '<QgsProperty: INACTIVE expression (5*5 || \'a\')>')
p = QgsProperty()
self.assertEqual(p.__repr__(), '<QgsProperty: invalid>')


if __name__ == "__main__":
unittest.main()

0 comments on commit 8050612

Please sign in to comment.